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.

  1. Array data = generateData(Table("MyTable").numRows, Table("MyTable").numCols);
  2. for (int i = 1; i < Table("MyTable").numRows+1; i++) {
  3. Array row = data[i];
  4. for (int j = 1; j < Table("MyTable").numCols+1; j++) {
  5. int value = row[j];
  6. Table("MyTable")[i][j] = value;
  7. }
  8. }

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.