create On Draw Trigger in 1000+ NetworkNode using Script

create On Draw Trigger in 1000+ NetworkNode using Script

AmeenShabeerJ4UQ4
Contributor Contributor
53 Views
4 Replies
Message 1 of 5

create On Draw Trigger in 1000+ NetworkNode using Script

AmeenShabeerJ4UQ4
Contributor
Contributor

[ FlexSim 18.2.2 ]

Hi,

How can i create On Draw Trigger in 1000+ NetworkNode using Script window ?

i want to place following code in all the NetworkNodes !!

Object current = ownerobject(c);
treenode view = param(1);

if (PathVisibility == 1)
{
Object NN0 = current;
Object NN1 = current.outObjects[1];

double width = current.width;
drawtomodelscale(current);

double dx1 = NN1.location.x - NN0.location.x;
double dy1 = NN1.location.y - NN0.location.y;

double Angle1 = Math.degrees(Math.atan2(dy1 ,dx1));
double Distance1 = Math.sqrt(dx1 * dx1 + dy1 * dy1);

fglRotate(Angle1, 0, 1, 0);
drawrectangle(0,width/2,0,Distance1,width,0,0,0, 255,255,0, 1, 0,0,0);
}

0 Likes
Accepted solutions (1)
54 Views
4 Replies
Replies (4)
Message 2 of 5

roi_sn
Not applicable

Hi @Ameen MS,

After copying this code in the OnDraw trigger of one node, you can select all of the other nodes (in red), then click again in the one in which you pasted the code (highlighting it in yellow) and then go to View >> Edit Selected Objects and click in All Variables so as to paste all of the logic in the highlighted node to the others.

0 Likes
Message 3 of 5

AmeenShabeerJ4UQ4
Contributor
Contributor

Hi Roi Sánche

The NetworkNodes are dynamically created. So i am looking for a script to create triggers.

0 Likes
Message 4 of 5

philboboADSK
Autodesk
Autodesk
Accepted solution

You can use assertvariable() to assert the On Draw trigger on your object.

I would put that code in a user command and set the On Draw trigger of your network nodes to call the user command. That way, if you want to edit that code, you can edit it in one place without having to re-copy it to all of your nodes.

See the attached sample model. programmatically-set-ondrawtrigger.fsm

string drawCodeText = 
"/**Custom Code*/\n"+
"Object current = ownerobject(c);\n"+
"treenode view = param(1);\n"+
"return NetNode_OnDraw(current, view);";

treenode prevNN = 0;
for (int i = 1; i <= 5; i++) {
	Object NN = createinstance(library().find("?NetworkNode"), model());
	NN.width = 1;
	NN.location = Vec3(i * 5, 0, 0);
	treenode ondrawtrigger = assertvariable(NN, "ondrawtrigger", DATATYPE_STRING);
	ondrawtrigger.value = drawCodeText;
	switch_flexscript(ondrawtrigger, 1);
	buildnodeflexscript(ondrawtrigger);
	rebindobjectattributes(NN);
	if (prevNN) {
		contextdragconnection(prevNN, NN, "A");
	}
	prevNN = NN;
}


Phil BoBo
Sr. Manager, Software Development
Message 5 of 5

philboboADSK
Autodesk
Autodesk

Also, here's a more robust version of your code that will work in all 3 dimensions and with any arbitrary number of network node output connections:

if (PathVisibility == 1)
{
  double width = current.width?;
  drawtomodelscale(current);
  
  for (int i = 1; i <= current.outObjects.length; i++) {
    Object otherNN = current.outObjects;
    
    double dx = otherNN.location.x - current.location.x;
    double dy = otherNN.location.y - current.location.y;
    double dz = otherNN.location.z - current.location.z;
    
    double Angle = Math.degrees(Math.atan2(dy,dx));
    double Distance = Math.sqrt(dx * dx + dy * dy);
    double Angle2 = Math.degrees(Math.atan2(dz,Distance));
    double Distance2 = Math.sqrt(dx * dx + dy * dy + dz * dz);
    
    fglRotate(Angle, 0, 1, 0);
    fglRotate(Angle2, 0, 0, 1);
    drawrectangle(0,width/2,-0.01,Distance2/2,width,0,0,0, 255,255,0, 1, 0,0,0);
    fglRotate(Angle2, 0, 0, -1);
    fglRotate(Angle, 0, -1, 0);
  }
}


Phil BoBo
Sr. Manager, Software Development