ATTENTIONThis FlexSim Community Forum is read-only. Please post any new questions, ideas, or discussions to our new community (we call it Answers) at https://answers.flexsim.com/. Our new Question & Answer site brings a modern, mobile-friendly interface and more focus on getting answers quickly. There are a few differences between how our new Q&A community works vs. a classic, threaded-conversation-style forum like the one below, so be sure to read our Answers Best Practices. |
flexsim.com |
|
Downloads |
Tips and Tricks Share helpful modeling ideas |
#1
|
||||
|
||||
Dynamic script generation: Good Example with a User Command
User Command:
Name : listfind Parameters : (str TableName, str ListCommandName, num colnr) Example : listfind("mytable", "listmaximum", 1) Description: The basic idea behind this example is how we could generate dynamic scripts and execute them using commands executestring() or executefsnode(). This will give a real flexibility and run it dynamically. I have described this by creating a user command named “listfind”. You can use this user command for the purpose of finding: maximum (“listmaximum”), minimum (“listminimum”), index of lowest value (“listmin”), index of highest value (“listmax”), sum(“listsum”), subtraction (“listsub”), multiplication (“listmul”), mean (“listmean”) from a given Table Name, List Command and column number as parameters. This example gives an idea for creating user commands and generating dynamic scripts to execute. Code: string TableName=parstr(1); string LCommandName=parstr(2); int ColNum=parval(3); int Trows=gettablerows(TableName); int NumPrecision=getnodenum(node("/Tools/precision",model())); string Dstring; for(int RowNum=1;RowNum<=Trows;RowNum++) { Dstring=concat(Dstring,concat(",",numtostring(gettablenum (TableName,RowNum,ColNum),0,NumPrecision))); } string Lresult=concat("double", " ", "Lval", "=", LCommandName, "(", numtostring(Trows,0,0), Dstring, ")", ";", "return", " ", "Lval", ";"); return(executestring(Lresult,NULL,NULL,NULL,0)); ----------- Regards Naresh |
The Following 3 Users Say Thank You to Naresh Yamani For This Useful Post: | ||
Jörg Vogel (08-19-2016) |
#2
|
|||
|
|||
Thanks, Naresh. This is really clever! I've always been interested in writing programs that write programs for you. Most people don't realize the possibilities of dynamically writing code.
I like this technique a lot. However, I must say that this code will run relatively slowly because it must write, build, and interpret the code during execution, instead of just interpreting. Writing code this way, though, can do some really cool things. Thanks for the example. |
#3
|
||||
|
||||
Thanks for your comments Alex, Yes. This is relatively slow since it has to write the code during execution. However I can see a lot of benefits from it and I like to write the code in this way most of the times. I can also see many other possibilities.
I don’t know how many people so far using the list commands, since we have to supply the data to it. And most of the times the data is not static or maybe from a table. I believe that this is another way of using list commands more effectively. -Naresh |
Thread | Thread Starter | Forum | Replies | Last Post |
How to display dynamic text next to an object | Cliff King | Tips and Tricks | 4 | 11-11-2010 01:45 PM |
problem with automatic generation of objects by using a user library and c++ | Martin Saler | Q&A | 17 | 06-05-2009 09:56 AM |
releaseitem command | AlanZhang | Q&A | 3 | 10-04-2007 10:11 AM |