Using FlexSim with Python/C api

Using FlexSim with Python/C api

sakamoto_ryosuke
Collaborator Collaborator
102 Views
11 Replies
Message 1 of 12

Using FlexSim with Python/C api

sakamoto_ryosuke
Collaborator
Collaborator

[ FlexSim 20.2.2 ]

Hello! After having discussion with one of your colleague, I have found out that you can use python in FlexSim by Python/C API. However, to confess, I'm not fond in this field. Do you have any kind of manual or note to integrate Python/C to FlexSim? I'd really appreciate if you can also provide some kind of sample project, model,etc. Thank you in advance.

0 Likes
Accepted solutions (1)
103 Views
11 Replies
Replies (11)
Message 2 of 12

philboboADSK
Autodesk
Autodesk
Accepted solution

Download the Module SDK from the Account section of the website. Follow its embedded documentation on how to use it to create a FlexSim Module and compile a module dll.

Once you can compile a module dll, follow the instructions at https://docs.python.org/3/extending/embedding.html for executing Python from C++ code.

When you install Python, it includes a bunch of header files (including Python.h), lib files (including python3.lib), and dlls (including python3.dll). You will use these in your Module DLL project to interact with Python.

33492-1603121043564.png

You are unlikely to find someone who has already done this and can share a sample project/model because executing Python from C is rarely helpful. Most 3rd party code libraries are already written in C or C++, so you can connect directly to them. Usually those libraries have Python wrappers so that you can call them from Python. If you are already in C++, then you don't need to use Python wrapper libraries. You can call them directly from C++ instead of going from C++ to Python back to C.

Is there a particular library that you are trying to use, or is this simply an academic exercise to see what is possible?



Phil BoBo
Sr. Manager, Software Development
0 Likes
Message 3 of 12

sakamoto_ryosuke
Collaborator
Collaborator

@Phil BoBo, thank you for your reply. This is going to take sometime for me to create the environment on my PC. In the customer side, they are using their original AI engine, so we don't know how they are created. I just know that they are using Python and want to access to FlexSim with Python. The sample I would like is to test how we can pass parameter value defined in Python to "Processor's process time" for example as a proof.

0 Likes
Message 4 of 12

philboboADSK
Autodesk
Autodesk

Wanting to access FlexSim from Python is very different than wanting to use/integrate Python in FlexSim. Python is a programming language, and FlexSim is an application.

You can communicate with FlexSim from Python using IPC (inter-process communication) technologies, such as sockets or the FlexSim WebServer.

See https://docs.python.org/3/howto/sockets.html for documentation on using sockets with Python.

See https://archive.flexsim.com/threads/thread3186.html for FlexSim client and server socket examples using FlexScript.

In addition to the FlexScript socket commands, you could also use the DLL Maker or Module SDK to write your own C++ socket code if you want direct access and control over exactly what is happening.

If you just want to call FlexSim from Python and set values, such as a processor's processing time, you can run the FlexSim application from Python and pass it command-line arguments, such as FlexScript code to execute. See https://answers.flexsim.com/answers/21375/view.html and https://docs.python.org/3/library/subprocess.html.

If you want to communicate repeatedly with an already-open FlexSim, then you'll need to use some method of IPC, such as using sockets for a direct connection or using the FlexSim WebServer for communication over HTTP.

See https://docs.python.org/3/library/http.client.html for Python documentation about communicating with a server via HTTP and https://docs.flexsim.com/en/20.2/Reference/DeveloperAdvancedUser/Webserver/#custom for documentation on responding to custom web queries using the FlexSim WebServer.



Phil BoBo
Sr. Manager, Software Development
0 Likes
Message 5 of 12

benjamin_w2
Not applicable

@Ryosuke S,

Please also see the following library and youtube video:

https://pybind11.readthedocs.io/en/stable/

https://www.youtube.com/watch?v=-eIkUnCLMFc

Also consider passing information using a database.

0 Likes
Message 6 of 12

sakamoto_ryosuke
Collaborator
Collaborator

@Phil BoBo, Thank you for your reply. Great! It's nice to know there are several ways. So then, which one do you suggest? The most simplest way to accomplish what I want to do for now. Also, I would like to know what I need to install to my pc, what procedure I need to take to accomplish it. I have gone through the python docs, but are not helpful enough for a beginner. I don't nee any fancy document, but a note describing steps when you tried on your side perhaps? Thank you in advance.

0 Likes
Message 7 of 12

philboboADSK
Autodesk
Autodesk

If you just want to start a FlexSim process with particular inputs, run the model, and get the outputs at the end of the run, I would call FlexSim with command line parameters.

If you want to start FlexSim, run the model partially, pause the model, send information back and forth, resume the model, etc., then I would use sockets.

I don't have a step-by-step document for beginners with exact steps on how to do exactly what you want. To make such a document, you need to actually do what you want and document what you did.

If in attempting to use the provided FlexSim and Python documentation to do what you want, you run into a particular, specific problem, then post a new question detailing what you've done, what you expected to happen, and what actually happened, and we might be able to explain why and suggest ways to overcome that specific issue. But your questions right now are generic, so you are only going to get generic answers. If you want specifics, then start actually doing it so that you can ask specific questions.



Phil BoBo
Sr. Manager, Software Development
0 Likes
Message 8 of 12

sakamoto_ryosuke
Collaborator
Collaborator

@Phil BoBo, @Benjamin W2 Thank you for all your help, we were able to integrate flexsim with python.

0 Likes
Message 9 of 12

jing_69
Not applicable

@Ryosuke S Hello, we have the same question about how to integrate flexsim with Python, and we know that you've solved it. Could you please tell us the solutions or give some notes. Thank you so much!

0 Likes
Message 10 of 12

jason_lightfootVL7B4
Autodesk
Autodesk
0 Likes
Message 11 of 12

sakamoto_ryosuke
Collaborator
Collaborator

@Jing_69 hello! as in @Jason Lightfoot reply, now that in FlexSim2022, we have ui for reinforcement learning, I think you should use this, because what we have done is almost similar to this.

Thanks to the FlexSim development team, that this feature is available now.

0 Likes
Message 12 of 12

jing_69
Not applicable
@Jason Lightfoot @Ryosuke S Thank you so much for your reply!
0 Likes