Create and connect network nodes programmatically

Create and connect network nodes programmatically

chao_gao
Not applicable
29 Views
1 Reply
Message 1 of 2

Create and connect network nodes programmatically

chao_gao
Not applicable

In my model I had to manually place many network nodes and then connect them manually - again, really time consuming. I am sure there is a way to accomplish this automatically by writing some simple code.

I have to build out my model further, so being able to automatically create and connect network nodes and other objects will help save me quite a bit time.

Accepted solutions (1)
30 Views
1 Reply
Reply (1)
Message 2 of 2

Ben_WilsonADSK
Community Manager
Community Manager
Accepted solution

The following code snippet is an example to show creation and connection of objects. It places 6 network nodes in random locations in the model and connects them in order. You can test this code by starting a new model, then pasting the code below into a script window. Run it to see what it does:

treenode prevnode = NULL;
for (int i=1; i<=6; i++) {
	treenode nnode = createinstance(node("/travelnetworks/NetworkNode",library()),model());
	setloc(nnode, duniform(-10, 10), duniform(-10, 10), 0);
	if (objectexists(prevnode))
		contextdragconnection(prevnode, nnode, "A");
	prevnode = nnode;
}

You will most likely have location data for your network nodes in a table, or organized in some other way. You can use the commands demonstrated in the code above (createinstance, setloc, contextdragconnection) to create the logic that will traverse your data, in whatever form it might be in, and create and connect your network nodes properly.