question

chao.g avatar image
1 Like"
chao.g asked Ben Wilson edited

Create and connect network nodes programmatically

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.

connectionsobject creationprogrammatic control
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

1 Answer

Ben Wilson avatar image
3 Likes"
Ben Wilson answered Ben Wilson edited

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.

5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.