Hi dynamo users. I stucked with idea of loop that can sort the points by distance but not from just one point:
->Starting with starting point and point collection,-> then find closest point to the starting then take the found one and repeat the search of closest one. Then sort it in found order.
I tried with loop while node after I created custom node that giving the closest point and rest of collection.
Ty for help!.
See if this works for you. Doesn’t require the looping.
4 Likes
Ty for reply, I tried it, but its still one problem. We are picking only first control point, next ones should be found, thats why im was trying with loop. It will be used for reconnecting bad quality dwg polylines when is alot of trash aroud. I dont know before starting the script with points will be next in the order. Thats the issue not sorting.
The LoopWhile node is hard to use because you’re limited to a single input. However, that input can be a list or a list of lists. Once you realize that, its use becomes much easier:
It’s more fun if you discover the approach yourself, so I’ll just post the above as an example for now.
1 Like
After a few infinite loop crashes I got this:
The sorting node works fine as alone but with the same data connected to loop I’m getting only one item, wich is should be the last one. What is the difference when is connected? And I dont get how You Dimitar getting same preparing data result as I’m when using one List.Create node less.
The closest sorting node is attatched.
closest sorting 27.dyf (8.7 KB)
Thank you for help!
In your case you’re using the the list syntax in your code block - {0}
generates a list with a zero. When you feed a list into a node that expects a singleton, the interpreter detects that and outputs a list as well.
Looks like you’re almost there! Your node is replacing the closest point with the previous one, where as my example was appending it to the end of the list.
_tempSortPt.dyf (8.4 KB)
1 Like
Hello guys,
I’ve tried do something similar but considering multiple lists of points.
When i run the script without the LoopWhile I get the correct Sort but once i put the node LoopWhile the Script simplt doesn’t respond…
I’m not authorized to attach files as new member but i can send you an e-mail with the script and custom node that I’ve created to see if one of you can help me out.
You could try using this Design Script option instead
closestPointsSequence.dyn (8.7 KB)
def ClosestPointsSequence (stPt, pnts:var[]..[])
{
return = [Imperative]
{
cnt1 = List.Count (pnts);
pnt1 = pnts;
pnt2 = [stPt];
while (List.Count(pnt2)<cnt1)
{
pnt3 = List.SetDifference(pnt1,pnt2);
pnt4 = List.LastItem(pnt2);
dis1 = pnt4.DistanceTo(pnt3);
pnt5 = List.SortByKey(pnt3,dis1)["sorted list"];
pnt1 = List.RestOfItems(pnt5);
pnt2 = List.AddItemToEnd(List.FirstItem(pnt5),pnt2);
}
return pnt2;
}
};
3 Likes
Hello Vikram,
Your suggestion works very well, but only when you have just one list because when you have list of list the Design Script doesn’t work…
I’m going to try adapt the script for a list of lists…
Thank you one more time
@Vikram_Subbaiah, just to clarify:
In my case i will have multiple list (different lists of points) that i need to sort by the closest way… I can’t do it one by one … i want to choose different origins and one destiny (for example…) and generate the closest path for every origin… in the end i will generate conduits for every path.
Use Replication Guides like this …
ClosestPointsSequence (sp<1>, pnts<1>)
closestPointsSequence.dyn (10.8 KB)
You want the shortest path connecting all points (in multiple lists of points)?
shortestPathConnectingPoints.dyn (14.7 KB)
3 Likes
This is exactly what I was looking for…however…
Using Revit 2022/ Dynamo 2.12, the Design Script for sorting points by proximity fails - returns the first point and the rest are nulls.
Working in Revit 2020 (whatever Dynamo version that is) it works completely fine.
With my limited knowledge of scripting, the logic seems fine, so I’m assuming this is due to a change in Design Script interpretation, but I could not figure it out. Any hints on what has changed?
Shouldn’t be any changes; are you sure the data sets are the same in both files?
Yep, data sets are the same. I even downloaded the example from Dimitar above (first one) and got the same erroneous result.
The only other thing I can think of is perhaps a package error somewhere in my 2022 build?
Can you post a screenshot of the failing node? May be as simple as adding “Autodesk.” or “DSCore.” in front of a method which isn’t showing as a failure because of the in-line code.
This is what I’m getting. Points are created fine, but when calling the design script, first point only is collected.
Hmmm… I’ll try and take a look at that tomorrow. 
Change “sorted list” on line 14 to “sortedList”
2 Likes
That did it. Thanks a heap Vikram!
Any hints as to why the original worked in prior versions, but the change is needed in Dynamo 2.12?
tbh I’m not really sure what that part of the line is doing?