question

Bernard L2 avatar image
0 Likes"
Bernard L2 asked Jason Lightfoot 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
4 Likes"
Matthew Gillespie answered Jason Lightfoot 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
	...
}
· 2
5 |100000

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

Stella avatar image Stella commented ·

Hello, I don't see the responses here for this question from 2020. Curious what the answer was.


Also, Is there a way to connect a queue to the travel network automatically by chance?


I made my travel nodes invisible and cannot make it visible again. It would be great if there is a way to connect my queue closest to the nearest node.

0 Likes 0 ·
Jason Lightfoot avatar image Jason Lightfoot ♦♦ Stella commented ·

The post should be visible to you now.

You should be able to toggle the network display by holding 'x' and clicking a network node in the tree view.

Here's an updated version of the connection script that also clears previous connections and uses the object's centre point to determine distance. It also finds all fixed resources in the model, not just those in the root layer.

Array fixedResources=Table.query("SELECT ARRAY_AGG(Object) FROM Objects() WHERE 'FixedResource' IN Classes ")[1][1];
Array netnodes=Table.query("SELECT ARRAY_AGG(Object) FROM Objects() WHERE Class='NetworkNode'")[1][1];
Vec3 midFactors=Vec3(0.5,0.5,0);
Object node;
Object nearest;
while (fixedResources.length){
    Object fr=fixedResources.pop();
    variables(fr).subnodes["networknodes"].subnodes.clear(); //remove previous connections
    double mindist=GLOBAL_UNREACHABLE;
    Vec3 absLoc=fr.getLocation(midFactors).project(fr.up,model());
    for (int n=netnodes.length;n>0;n--){
        node=netnodes[n];
        Vec3 diff=absLoc-node.location.project(node.up,model());
        if (diff.magnitude<mindist) {
            mindist=diff.magnitude;
            nearest=node;
        }
    }
    contextdragconnection(fr,nearest,"A");
}

ConnectNearestNodes.fsm

0 Likes 0 ·