question

Nick avatar image
1 Like"
Nick asked Nick commented

How to get Python to read a Table in FlexSim?

I hope everyone is having a great week so far. I am seeking a solution that allows me to easily import a FlexSim table into Python. My objective is to execute a Python code that reads data from a FlexSim table and stores it in a dataframe for further data processing tasks. Currently, I have a script that exports global tables to a .csv file when prompted, which meets my requirements. However, I believe it would be more efficient to eliminate the intermediate step and create a Python function that directly retrieves data from a FlexSim model. This streamlined approach would save time and eliminate the need to manage "juggling .csv" files during subsequent validation processes in Python. I would like to know if it is possible to achieve this.

FlexSim 23.1.2
pythonflexsim python
· 1
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

Julie Weller avatar image
3 Likes"
Julie Weller answered Nick commented

Hey @Nick!

The answer to your question depends on whether you want to be based in Python or in FlexSim. If you want to call FlexSim from your python model, this git page should be able to explain that process:

https://github.com/flexsim/FlexSimPy

However, if you would like to call python from FlexSim it's pretty easy to call python functions. First make sure your python code is in the same folder as your FlexSim model, then go to toolbox -> User Commands. You can name your function anything you want but make sure your parameter match for example if I want to pass in an array to this function which sums a table in python.

1685052164819.png

Next click the custom code button on the far right (it looks like a little scroll) and at the bottom of the page that pops up click the E button:


screenshot-2023-05-25-152542.pngThen click format for python and yes. Now you can access you file and function by editing the two quoted text "PyMod" and "PyFunc" to be your file name and function respectively for example my file was named Test.py and my function was sumTablePy then I would put in this:

1685052337152.png

Before you use this function make sure to click apply on both the user commands tab and the code tab.

After I do that I can call my new function in the script tab on FlexSim by going to the script tab (on the command bar by tree and backgrounds) and call that with my global list as the table using code that looks something like this

  1. Array data;
  2. Table table = Table("GlobalTable1");
  3. table.cloneTo(Table(data));
  4. return sumTable(data);


I hope that helps!


· 3
5 |100000

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