ATTENTION

This 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

Go Back   FlexSim Community Forum > FlexSim Software > Tips and Tricks
Downloads

Tips and Tricks Share helpful modeling ideas

  #1  
Old 05-15-2008
Naresh Yamani's Avatar
Naresh Yamani Naresh Yamani is offline
Flexsim User
 
Join Date: Aug 2007
Posts: 11
Downloads: 20
Uploads: 0
Thanks: 7
Thanked 7 Times in 4 Posts
Rep Power: 157
Naresh Yamani is on a distinguished road
Default 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  
Old 05-16-2008
Alex Christensen Alex Christensen is offline
Flexsim Technical Support
 
Join Date: Nov 2007
Location: Provo, UT
Posts: 96
Downloads: 41
Uploads: 8
Thanks: 29
Thanked 141 Times in 56 Posts
Rep Power: 298
Alex Christensen is a splendid one to beholdAlex Christensen is a splendid one to beholdAlex Christensen is a splendid one to beholdAlex Christensen is a splendid one to beholdAlex Christensen is a splendid one to beholdAlex Christensen is a splendid one to beholdAlex Christensen is a splendid one to behold
Default

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  
Old 05-16-2008
Naresh Yamani's Avatar
Naresh Yamani Naresh Yamani is offline
Flexsim User
 
Join Date: Aug 2007
Posts: 11
Downloads: 20
Uploads: 0
Thanks: 7
Thanked 7 Times in 4 Posts
Rep Power: 157
Naresh Yamani is on a distinguished road
Default

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


All times are GMT -6.
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, vBulletin Solutions Inc.
Copyright 1993-2018 FlexSim Software Products, Inc.