question

Jackie Ho avatar image
7 Likes"
Jackie Ho asked Phil BoBo commented

Automatically Configure and Run a FlexSim Model

If I have a model that is built and ready, is it possible for FlexSim to import various object and model parameters automatically? And run the simulation automatically?

excelimportcommand lineauto runonmodelopen
5 |100000

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

Jeff Nordgren avatar image
3 Likes"
Jeff Nordgren answered Ben Wilson edited

A FlexSim model has an On Model Open trigger. Here you could put whatever logic you like, including importing data from Excel or a database, setting the runspeed, and starting the model run. This screenshot shows how to add an On Model Open trigger to your model:

If you want the model to open automatically when FlexSim starts, you can launch FlexSim and open the model at the same time by double-clicking the model file.


5 |100000

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

Ben Wilson avatar image
21 Likes"
Ben Wilson answered Phil BoBo commented

Command line options

Alternatively, you can launch FlexSim from a Command Prompt or a batch file, by executing a line like this (this text probably wraps in this web view, but in the command prompt it should all go on a single line):

"C:\Program Files\FlexSim 2016\program\flexsim.exe" 
"C:\Users\username\Documents\Flexsim 16 Projects\test.fsm" /maintenance 
nogui_disablemsg_runscript /scriptpath C:\myscript.txt

This will open FlexSim, open the model at the specified path, set a flag to suppress showing FlexSim's interface, set a flag to suppress any msg() command popups from appearing, and execute the file C:\myscript.txt as FlexScript.

All of those command line features are optional. For instance, remove the nogui flag to launch FlexSim with the normal interface. You can put FlexScript code into myscript.txt to run custom behaviors in the model, such as resetting and running the model, but that is optional. Remove the runscript flag, plus the "/scriptpath C:\myscript.txt" portion from the line if you don't wish to run a FlexScript file on model load.

Using an external script file that runs upon opening the model is an alternative to having the OnModelOpen trigger coded into your model. Instead you can have an external script file that can run upon model open, that can be separately and independently edited without modifying anything inside the model file.

Attached is a zip file containing 3 files that show an example of how you can silently run a FlexSim model without an interface and then export results to a text file.

Extract these 3 files to your computer somewhere (I put them on my Desktop).

Edit runScriptDemo.bat so that it has correct paths to where you have installed FlexSim, where the model is, and where the script file is.

"C:\Program Files\FlexSim 2016\program\flexsim.exe" "C:\Users\username\Desktop\test.fsm" /maintenance nogui_disablemsg_runscript /scriptpath "C:\Users\username\Desktop\script.txt"

The script file (script.txt) has code to run the model:

resetmodel();
runspeed(10000);
go();

The Sink's OnEntry trigger in the test.fsm model has code that exports results as a file and closes FlexSim.

cmdexit();

· 18
5 |100000

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

Bernard L2 avatar image Bernard L2 commented ·

Hi, Ben.

I'm looking for a way to run some automation as well and I was wondering if it is possible to control FlexSim using MS PowerShell, a much newer solution, instead of CMD Prompt, an old and far more limited solution.

I do know it is possible to call the CMD from PowerShell and use the solution above, but it's not a good solution. A more elegant one would be calling FlexSim directly from PowerShell.

I couldn't find any information here at the forum or in the FlexSim user manual.

I would appreciate any suggestions.

Thanks!

1 Like 1 ·
Phil BoBo avatar image Phil BoBo ♦♦ Bernard L2 commented ·

Use the Start-Process cmdlet.

Start-Process -FilePath "C:\Program Files\FlexSim 2018\program\flexsim.exe" -ArgumentList "C:\Users\phil.bobo\Desktop\test.fsm /maintenance runscript /scriptpath C:\Users\phil.bobo\Desktop\script.txt"
3 Likes 3 ·
Alessio Merlo avatar image Alessio Merlo Phil BoBo ♦♦ commented ·

Hi @phil.bobo,

do you know if there is a trouble when the script is saved in a folder which name contains a space?

I executed your code in two different ways and I obtained two different results.

First test works without space and results is good.

Start-Process -FilePath "C:\Program Files\FlexSim 2018\program\flexsim.exe" "/maintenance runscript /scriptpath C:\Users\amerl\Desktop\Folder_Without_Space\FlexScript.txt"

Second test works with space and the script isn't executed.

Start-Process -FilePath "C:\Program Files\FlexSim 2018\program\flexsim.exe" "/maintenance runscript /scriptpath C:\Users\amerl\Desktop\folder with space\FlexScript.txt"

The file FlexScript.txt simply contains this code

msg("Hello","World",1);

Any idea?

Thanks for the support!

0 Likes 0 ·
Show more comments
Wang Z2 avatar image Wang Z2 commented ·

Hi,Ben

I use your way to launch Flexsim

But it will take a long time to run in my model(almost 20 mins)

Although my model is more complicated, I don’t think it takes so long.

my computer have i7 8th CPU,32G Ram,so it shouldn't be the reason for my computer.

0 Likes 0 ·
Ben Wilson avatar image Ben Wilson ♦♦ Wang Z2 commented ·

@Wang Z2,

Loading a model from the command line and starting the simulation via code doesn't automatically make the runspeed go at its maximum, the way background simulations do when using the experimenter or optimizer.

Instead, you need to use a script to set the runspeed to some high value, like:

runspeed(99999999);

Or something similar.

You can also confirm a typical run duration for your model by running it normally in FlexSim at max speed. If it can finish in 4-5 minutes through the regular interface, it should finish in 4-5 minutes when started from the command line.

0 Likes 0 ·
Erik RV2 avatar image Erik RV2 commented ·

Hi everyone!

As somebody tried to import from excel while launching the model from a shell?

To the files shared in this post, I tried adding this line:
excelmultitableimport();

to
'onModelReset' trigger and/or
'script.txt'

without constant success.

Thanks!

0 Likes 0 ·
tannerp avatar image tannerp Erik RV2 commented ·

@erik.rv2,

I suggest posting a new question (this one is answered and fairly outdated) and referencing this question with a link. It sounds like you're looking for guidance on automating an Excel import, which we're happy to help with.

1 Like 1 ·
Ben Wilson avatar image Ben Wilson ♦♦ commented ·

Another command line parameter:

You can pass the command line parameter /excludemodules modulename1,modulename2,etc to exclude specific modules.

https://answers.flexsim.com/answers/95256/view.html

0 Likes 0 ·
Rafael B22 avatar image Rafael B22 commented ·

Ben,

How did you tell FlexSim to save the output into the "results.txt" file?

0 Likes 0 ·
Phil BoBo avatar image Phil BoBo ♦♦ Rafael B22 commented ·

The Sink's OnEntry trigger has code that writes the results.txt file using FlexScript commands:

1666368764445.png

0 Likes 0 ·
1666368764445.png (166.4 KiB)
Rafael B22 avatar image Rafael B22 Phil BoBo ♦♦ commented ·
Thanks a lot!
0 Likes 0 ·
Show more comments

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

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