question

qoie200011 avatar image
0 Likes"
qoie200011 asked Julie Weller commented

connect to Python DLL in Flexsim

Hello,

Because I want to implement an routing algorithm written in Python, and the Flexsim version I am using is 21.2.3 which can only use Flexscript or read DLL. Then I found the information from the flexsim website that flexsim can connect to C/C++ DLL. If I can convert Python file with my functions to DLL. I wonder is there any difference between connecting to Python DLL or C/C++ DLL in Flexsim or it can only connect to C/C++ DLL?

Any help would be appreciated!

1688621160660.png

FlexSim 21.2.4
dll
1688621160660.png (91.7 KiB)
· 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.

Julie Weller avatar image Julie Weller commented ·

Hi @qoie200011, was Jordan Johnson's answer helpful? If so, please click the "Accept" button at the bottom of their answer. Or if you still have questions, add a comment and we'll continue the conversation.

If we haven't heard back from you within 3 business days we'll auto-accept an answer, but you can always comment back to reopen your question.

0 Likes 0 ·

1 Answer

Jordan Johnson avatar image
1 Like"
Jordan Johnson answered Jordan Johnson edited

FlexSim 2021 will only connect to a C++ dll and FlexScript.

It is theoretically possible to create a C++ dll that calls python functions. In that case, FlexSim could call a function in a C++ dll, which then calls a function in a python module (a .py or .pyd file). However, doing so requires a lot of C++ know-how.

If you want to do that, the best advice I can give is to look at how we created the PyConnector dll:

https://github.com/flexsim/FlexSimPy/tree/v23.2/PyConnector

It would be easier to create an http server with python. You can write FlexScript code to send an HTTP request to your python server. Then, as a response to the request, you could run your routing algorithm, and return some JSON. When you get the response in FlexSim, you can use JSON to parse the result:

https://docs.flexsim.com/en/23.2/Reference/CodingInFlexSim/FlexScriptAPIReference/Data/JSON.html

As an example, here is an attached python script:

routing_functions.py

If you run the python script, you can then run the following FlexScript to get a response from the server:

Http.Request request;
request.host = "localhost";
request.port = 8080;
request.path = "func_foo";
request.data = JSON.stringify([1, 2, 3, 4, 5, 6, 7]);

var response = request.sendAndWait();
var fooValue = JSON.parse(response.value);

request.path = "func_bar";
request.data = JSON.stringify(5.3);
response = request.sendAndWait();
var barValue = JSON.parse(response.value);

return [fooValue, barValue];

Note that there is some delay in creating and sending the HTTP request. If speed becomes an issue, you'd probably be able to use FlexSim's socket commands with your own protocol to get a speed improvement.


5 |100000

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