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 > Q&A
Downloads

Q&A Using Flexsim and building models

  #1  
Old 09-26-2012
Vadim Fooks's Avatar
Vadim Fooks Vadim Fooks is offline
Flexsim User
 
Join Date: Feb 2012
Posts: 67
Downloads: 26
Uploads: 0
Thanks: 50
Thanked 24 Times in 13 Posts
Rep Power: 118
Vadim Fooks will become famous soon enough
Default Is it possible to create a #define in the object (not model) scope

Ladies and Gentlemen,
I am trying to bring some order to a massive and ugly piece of code in the OnMessage trigger which presently looks like this:

Code:
#define MSGCODE1 1
...
#define MSGCODEn <n>
int msgCode = msgparam(1);
if (msgCode == MSGCODE1)
{
   ...
   insertproxytask(..., TASKTYPE_CALLSUBTASK, current, NULL, MSGCODE2, ...);
   ...
   dispatchcoordinatedtasksequence(...);
}
else
if (msgCode == MSGCODE2)
{
   ...
   insertproxytask(..., TASKTYPE_CALLSUBTASK, current, NULL, MSGCODE3, ...);
   ...
   return cts;
}
else
if (...)
...
The code of the trigger is getting too long to be maintainable.

I intend to refactor this code by removing every case into a separate nodefunction and only keep the skeleton in the trigger itself.
The problem, however, is that I want to keep the #defined values only within the scope of the object handling the messages.
Under different circumstances I would have defined these values globally, under "global macros", but the goal of this project is to create a completely self-contained reusable object, so it has to encapsulate any constants it happen to require.
I would also prefer to avoid #defining the message codes twice, once in the nodefunction and once in the OnMessage trigger.

Herein lies my question, is there a way to work around this problem, something that would mimic the behavior of the header file in C++ which can be implemented once and included in several places?
Thank you!
Vadim
  #2  
Old 09-26-2012
Vadim Fooks's Avatar
Vadim Fooks Vadim Fooks is offline
Flexsim User
 
Join Date: Feb 2012
Posts: 67
Downloads: 26
Uploads: 0
Thanks: 50
Thanked 24 Times in 13 Posts
Rep Power: 118
Vadim Fooks will become famous soon enough
Default

At the first glance this looks impossible to do using flexscript and #defines
:-(, not elegantly, anyway. I guess it is possible to create a new node at runtime, copy the #defines from a text node, copy the code from another text node, compile flexscript and call a nodefunction from a nodefunction, but it sounds a bit Rube-Goldberg-esque.
Without knowing how buildnodeflexscript() is implemented, I cannot say if my musings below make any sense or not, but I think it should not be difficult implement something like this:

NOTE THAT THE FUNCTIONALITY DESCRIBED BELOW DOES NOT EXIST!
I am just hypothesising what it may look like.

includenodeflexscript(node codenode)
that during execution of the command buildnodeflexscript() will check that it is not called recursively and copy the content of the codenode into the input of the buildnodeflexscript().
The trick is in ensuring that the node calling this function gets rebuilt if the included node changes, but does not get rebuilt every time the function is called, but if that can be addressed, the rest seems simple.
For example, let's say in my object, I create a text node "/MyObject>includes/constants" containing the following:
Code:
#define ONE 1
#define TWO 2
Then I can create a nodefunction "/MyObject>methods/HandleMsgOne" that will look like so:
Code:
includenodeflexscript(node(">includes/constants", current);
...
treenode cts = createcoordinatedtasksequence(...);
...
insertproxytask(cts,..., TASKTYPE_CALLSUBTASK, current, NULL, TWO,...);
...
dispatchcoordinatedtasksequence(cts);
and then in the MyObject's OnMessage trigger I can write:

Code:
includenodeflexscript(node(">includes/constants", current);
int msgCode = msgparam(1);
if (msgCode == ONE)
{
    nodefunction(node(">methods/HandleMsgOne", current),...);
}
else
if (msgCode == TWO)
{
...
}

IMHO, it would allow to make the code so much tidier, more object-oriented, and easier to reuse.

Last edited by Vadim Fooks; 09-26-2012 at 08:16 PM.
  #3  
Old 09-27-2012
Steven Hamoen's Avatar
Steven Hamoen Steven Hamoen is offline
Talumis, Flexsim Distributor, The Netherlands
 
Join Date: Aug 2007
Location: Soest, NL
Posts: 854
Downloads: 43
Uploads: 0
Thanks: 391
Thanked 661 Times in 379 Posts
Rep Power: 684
Steven Hamoen has a reputation beyond reputeSteven Hamoen has a reputation beyond reputeSteven Hamoen has a reputation beyond reputeSteven Hamoen has a reputation beyond reputeSteven Hamoen has a reputation beyond reputeSteven Hamoen has a reputation beyond reputeSteven Hamoen has a reputation beyond reputeSteven Hamoen has a reputation beyond reputeSteven Hamoen has a reputation beyond reputeSteven Hamoen has a reputation beyond reputeSteven Hamoen has a reputation beyond repute
Default

Have you taken a look at the modellibraries node option in the helpfile topic Creating Custom Libraries?

That way you could per object create a link to the #defines you need.

How to create these nodes depends on how you want to distribute your objects. If you create a userlibrary to load your objects it's quite easy to to use the loadinstall node and newmodel node to install these nodes.

Other option is of course to call a dll function from your message trigger and that way write all the complex code nicely in visual studio.
The Following 2 Users Say Thank You to Steven Hamoen For This Useful Post:
Vadim Fooks (09-27-2012)


Thread Thread Starter Forum Replies Last Post
Object GUI from Model GUI shashanktrivedi Q&A 1 06-26-2011 04:28 AM
create an object from UserLibrary1 Nischith Kashyap Q&A 1 04-07-2011 01:03 AM
Simulati report for model with Fluid object David Chan Q&A 1 07-26-2008 03:42 AM
is there a command opposite to "destroyobject" which create a object? KelvinHo Q&A 3 07-03-2008 08:09 AM
Controlling the camera view to be focused on a moving object in your model. Regan Blackett Tips and Tricks 0 08-03-2007 10:12 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.