question

Pert P avatar image
1 Like"
Pert P asked Jason Lightfoot edited

How to use Flexscript (Python) to create the data into global table

I have coding in python program and I want to use Flexscript to code python to generate data into global table (No coding in C++ script on logic global table). How to do like that? , It can?

FlexSim 22.1.2
global tableflexscriptpython
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

1 Answer

Kavika F avatar image
4 Likes"
Kavika F answered MPeyman commented

Hey @Pert P, it is possible to generate data from python and then import it into FlexSim and put it into a Global Table. Here's a quick guide on hooking up the Python.


Start by making sure that you have Python installed on your machine. When you start a new FlexSim project, go into File > Global Preferences > Code and ensure the selected Python version matches the Python version you'll be coding in.

1656359945697.png

Next, under the Toolbox section, hit the green plus and add a new Modeling Logic > User Command. It will open a new window that looks like the image below. The documentation has more information on what kind of user commands you can make here.

1656360032899.png

You'll then change some things in the window like the Name, Parameters (maybe you want a number for the rows and a number for the columns? Or you can leave it empty so it accepts no parameters), Return Type (in our case we want a 2D array to be returned, so our return type will be var), and descriptions if you want them. Once you're done, next to the Code section, click the Script icon to open a black code space.

1656360456329.png

In the code space, at the bottom of the window, next to the letter S, click the E to toggle External Code. Select Format for Python click Yes.

1656360547270.png

Two lines will be generated: one that's commented "external python" and the other "function name". Replace the first text with the name of your python file (without the .py) and the other with the function name. Click Apply on the code block and Apply on the User Commands window. This is what it should look like now.

1656360254964.png

Now open your File Explorer and navigate to where your project is saved (if you haven't saved it yet, save it now!). Create a new text file, then rename the whole file (including the extension) to what you put in the FlexSim code window (for me it will be TestScript.py). You should now have the project and a python file in the folder.

1656361460608.png

Open the python file in a code or text editor of your choice. Write the function, making sure you match the number of parameters you specified (if any).

1656361556725.png

Save the python file. Go back to your model and hit "Reset". Whenever you make changes to the python file, you need to reset the model so it reloads the changes you make to the script. (The above code will create a 2D list of integers).

Create a Global Table from the toolbox and name it whatever you'd like. Change the number of rows and columns to match how many you're planning to use. (For my example I have 10 rows and 5 columns).

1656361828886.png

Open a FlexScript window and add some code to call the function you just created and put the data into the table.

Array data = generateData(Table("MyTable").numRows, Table("MyTable").numCols);
for (int i = 1; i < Table("MyTable").numRows+1; i++) {
  Array row = data[i];
  for (int j = 1; j < Table("MyTable").numCols+1; j++) {
    int value = row[j];
    Table("MyTable")[i][j] = value;
  }
}

Check the table you just made and it should now be filled with the values generated by the Python code.

1656362171335.png

Hope this helps.

example project.fsmTestScript.py


1656360032899.png (68.5 KiB)
1656360254964.png (46.0 KiB)
1656361460608.png (8.1 KiB)
1656361556725.png (22.7 KiB)
1656361828886.png (29.8 KiB)
1656362171335.png (42.1 KiB)
1656359977428.png (87.7 KiB)
1656360468844.png (30.7 KiB)
1656360562595.png (17.6 KiB)
example-project.fsm (25.2 KiB)
testscript.py (387 B)
· 9
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Pert P avatar image Pert P commented ·
Thank You so much. moreover I can write a logic in python file such as there are generate Random number for making a sequence then in in python algorithm there is a logic to rearrange sequence maybe minimize distance problem and when finish algorithm then import into Flexsim and show the data into table. Can I do like that?
0 Likes 0 ·
Kavika F avatar image Kavika F ♦ Pert P commented ·

Yes you can do that, I just used simple list comprehension as an example but you can return whatever data you'd like. You just have to be sure that on the FlexScript side of things that you store the value with the right data type (usually a var if not one of the default values, you can check different return types here). Your script could look like this:

import random

def rand_sequence():
    my_list = [random.random() for _ in range(10)]
    # Insert rearrange logic here
    return my_list
    
    """ Since this returns a 1D list, you could loop over your function
        and get different lists each time. Or you could change it so it
        returns a 2D list like my previous example and you only make 1
        call to the function.
    """ 
0 Likes 0 ·
MPeyman avatar image MPeyman commented ·

Hi dear @Kavika F, I wanted to do the same, so I wanted my felxsim input generated from python so when I connect python with flexsim the global table created by python and then I can run flexsim through python. I wanted to follow the instructure here but in my flexsim I dont have "modeling logic" "user command". the version of my flexsim is 23.0.8. peyman-flex-model_original.fsm

0 Likes 0 ·
Joerg Vogel avatar image Joerg Vogel MPeyman commented ·

@MPeyman, are you sure, that you work with a licensed version rather than "Express".

0 Likes 0 ·
MPeyman avatar image MPeyman Joerg Vogel commented ·
@Joerg Vogel , At the moment I am working with the limited version, so that is the issue?
0 Likes 0 ·
Show more comments
MPeyman avatar image MPeyman commented ·

Hi Dear @Kavika F , I followed your structure, in my model I created the python file that the function read data from folder and file the it create a data, how can I get those number in the global table inside the FlexSim?python-script.png

flexsim-script.pngpython-output.png

0 Likes 0 ·
python-script.png (80.2 KiB)
python-output.png (18.3 KiB)
Jason Lightfoot avatar image Jason Lightfoot ♦♦ MPeyman commented ·

To me it looks like your user command should be defined like this (if the python file is called 'readFile.py'):

1705416005212.png

1705415911714.png


And then you can try writing to the table with this line:

getData("FolderName","testData.txt").as(Array).as(Table).cloneTo(Table("GlobalTable1"))
0 Likes 0 ·
1705415911714.png (5.6 KiB)
1705416005212.png (9.3 KiB)