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?
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?
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.
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.
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.
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.
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.
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.
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).
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).
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.
Hope this helps.
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. """
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
@MPeyman, are you sure, that you work with a licensed version rather than "Express".
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?
To me it looks like your user command should be defined like this (if the python file is called 'readFile.py'):
And then you can try writing to the table with this line:
getData("FolderName","testData.txt").as(Array).as(Table).cloneTo(Table("GlobalTable1"))
12 People are following this question.
FlexSim can help you understand and improve any system or process. Transform your existing data into accurate predictions.
FlexSim is a fully 3D simulation software environment. FlexSim can be used to simulate any process in any industry.
FlexSim®, FlexSim Healthcare™, Problem Solved.®, the FlexSim logo, the FlexSim X-mark, and the FlexSim Healthcare logo with stylized Caduceus mark are trademarks of FlexSim Software Products, Inc. All rights reserved.
Privacy | Do not sell or share my personal information | Cookie preferences | Report noncompliance | Terms of use | Legal | © Autodesk Inc. All rights reserved