question

Bernard L2 avatar image
0 Likes"
Bernard L2 asked Matthew Gillespie edited

Automatically connect multiple Objects to networknodes based on proximity?

Hello,

Using Flexsim, I was wondering if it is possible to automatically connect a number of selected Fixed Resources to the Network Nodes based on the proximity of nodes in the network with respect to Object locations in the selection set. Looking for solutions here in the forum, I found that topic for FC from 2016 from @Matthew Gillespie. https://answers.flexsim.com/questions/31942/how-to-automatically-connect-multiple-locations-to.html?childToView=81333#answer-81333.

It doesn't work in FlexSim 19.2 I'm current using but I guess it should be possible to develop something similar to FlexSim 19 onwards. Following my forum search, I found this other topic which works for 1 Object: https://answers.flexsim.com/questions/47653/automatically-connect-to-the-closest-network-node.html.

All that needs to be done is to create a loop that track all Fixed resource or task Executers, tried take group subnodes, but it didn't work.

Thanks,

Bernard

FlexSim 19.2.0
scriptconnectionsnetworknodes
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

·
Matthew Gillespie avatar image
3 Likes"
Matthew Gillespie answered Matthew Gillespie edited

You just need to take the code from the single object example and wrap it in a for loop. Here's an example that finds all the Fixed Resource objects in the model and connects them to the nearest node:

treenode nodes = getvarnode(model().find("DefaultNetworkNavigator"), "nodemembers");

for (int i = 2; i <= model().subnodes.length; i++) {
	Object obj = model().subnodes[i];
	if(!isclasstype(obj, "FixedResource"))
		continue;
	
	Vec3 objPos = obj.location.project(obj.up, model());

	//find closest node
	treenode closestNode = 0;
	double closestDist = 1000000;
	for( int i = 1; i <= nodes.subnodes.length; i++) {
		Object curNode = ownerobject(nodes.subnodes[i].value);
		Vec3 pos = curNode.location.project(curNode.up, model());
		Vec3 distVec = objPos - pos;
		double curDist = distVec.magnitude;
		if(curDist < closestDist)	{
			closestDist = curDist;
			closestNode = curNode;
		}
	}
		
	//if node found and not already connected
	if(closestNode && getvarnode(obj, "networknodes").subnodes.length == 0)
		contextdragconnection(closestNode, obj, "A");
}

If instead you wanted to connect all the members of a group you could change the first few lines of the script like this:

treenode nodes = getvarnode(model().find("DefaultNetworkNavigator"), "nodemembers");
Group group = Group("Group1");

for (int i = 1; i <= group.length; i++) {
	Object obj = group[i];	
	Vec3 objPos = obj.location.project(obj.up, model());

	//find closest node
	...
}
5 |100000

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

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

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