question

Sri_vikas K avatar image
0 Likes"
Sri_vikas K asked Logan Gold commented

Calling the python script located in my PC using custom code to execute it.

Hi cammunity,

I am attempting to call a Python script stored in my local storage from FlexSim using the script below. However, I am encountering errors related to calling external commands and handling messages. I would appreciate any help or suggestions on how to resolve these issues and successfully execute the Python script from FlexSim.


// Define the path to Python and the Python script

string pythonExePath = "Storage Path"; // Path to Python executable

string scriptPath = "Storage Path"; // Path to your Python script


// Prepare the command to run

string command = pythonExePath + " \"" + scriptPath + "\""; // Make sure to handle spaces in paths


// Run the command using callExternal()

int result = callExternal(command);


// Check the result to see if the script was called successfully

if (result == 0)

{

message("SMS sent successfully!");

}

else

{

message("Failed to send SMS. Error code: " + result);

}

Execute python Script.fsm

Thanks in advance for your assistance!

FlexSim 23.1.4
python connection with flexsim
· 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.

Logan Gold avatar image Logan Gold ♦♦ commented ·

Hi @Sri_vikas K, was Felix Möhlmann'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

Felix Möhlmann avatar image
0 Likes"
Felix Möhlmann answered Felix Möhlmann commented

"callExternal" and "message" are not valid commands.

To run python code, it's easiest to set up a user command that links to the python function you want to execute. The parameters of the user command will be passed to python and the return value of the python function is returned as the result of the user command.

https://docs.flexsim.com/en/23.1/Reference/DeveloperAdvancedUser/ConnectingToExternalCode/ConnectingToExternalCode.html#python

See also the post below.

https://answers.flexsim.com/questions/158244/how-i-can-conect-a-python-file-with-a-flexsim-mode.html

· 4
5 |100000

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

Sri_vikas K avatar image Sri_vikas K commented ·

Thanks @Felix Möhlmann

I tried and the result is not very bad, but it seems that I am missing something.

1732790164684.png

I am calling it from processflow using the code below.
1732790243726.png

0 Likes 0 ·
1732790164684.png (155.7 KiB)
1732790243726.png (48.2 KiB)
Sri_vikas K avatar image Sri_vikas K Felix Möhlmann commented ·

@Felix Möhlmann ,

Executed it, but there is no response received from flexsim, I am calling it from process flow, Is this the proper way to call the user command?

1732799813474.png

0 Likes 0 ·
1732799813474.png (19.7 KiB)
Show more comments