Renumber Room by Distance to the next Room

There have been plenty of posts about renumbering rooms by level, or using a spline drawn through, and many other options.

I was taking a different approach. What I want to do is select a “Base Room” and from that room find the next closest room and the next closest room etc… What I have so far is the ability to sort rooms by distance to the base room, which gets me close, but what i want dynamo to do is cycle through a list of rooms and always find the next closest room to the previous room, starting with a base room.

Any thoughts on how to cycle through the list of rooms and find the next closest room in a list of 100 rooms?

I am using quite a few of custom packages, Data-Shapes, Rhythm & DanEDU, Revit 2018.3 & Dynamo 2.1


Renumber Rooms by Distance to Selection.dyn (80.4 KB)

So I don’t know how your data is structured and be warned that I am using a while loop so make sure you save everything first but I got a python script to go through a list of rooms sorting using what I think is the right logic:

# Enable Python support and load DesignScript library
import clr
from Autodesk.DesignScript.Geometry import *

# The inputs to this node will be stored as a list in the IN variables.
t0 = IN[0] #Starting room
t1 = IN[1][0] #Starting room's geometry
t2 = IN[2] #Rest of the rooms
t3 = IN[3] #Rest of the rooms' geometries
t4 = [t0]

while len(t2) >0:
	t6 = []
	for i in t3:
		t6.append(Geometry.DistanceTo(t1, i))
	t7 = min(t6)
	t8 = t6.index(t7)
	t1 = t3.pop(t8)

# Place your code below this line

# Assign your output to the OUT variable.
OUT = t4

It will start with the room you choose, and cycle through all of the rooms, finding the smallest distance. Then it will use this new room and cycle through to the smallest distance to it and on until it finishes all of the rooms. The output is the ordered list of rooms.

See this graph for how it is laid out. Note that I did make the geometry outside of the python script because it was easier but it may slow down the processing time.



Thanks for the quick reply! I’ll check this out today. I was always assuming this would be a slow process as it has to loop through the entire list as it pairs down to find the next closest room. I imagine this as a script only run only a few times throughout the project life cycle.

For some reason it wont filter my selected room out of the list?

I have attached my script in progress. The only difference is i’m using Data Shapes to filter my list of rooms per a selected level. But that shouldn’t make a difference as I now have a list of rooms and a room and i’m trying to filter out the one that is the same…

Renumber Rooms by Selections Distance and Distance.dyn (68.7 KB)


Thanks so much for the help! it did exactly what i wanted it to do. I modified it a bit so it used Room Location Point rather than geometry, it gave me more controlled results.

Renumber Rooms by Selections Distance and Distance.dyn (82.1 KB)

1 Like

Not sure if i should make a new post since its building on top of this original script.

The current way i have the script set up is to select a starting room, then select the other rooms. I want to automate this by filtering rooms by level, get the first item (room), and iterate the distances from that room to the rest on that level. In part doing this for all levels, with the click of a button, rather than being precise about which room I start with. I tested the room location with the script and worked before now, when i try it, i get an error with line 8 (I did not make any changes to the original python script).

Perhaps it is an issue passing points rather than geometry, although i’m not sure why it worked when yesterday when manually “Select.Elements” node was used. Unfortunately I am not much of a python scripter.


Resolved, had to flatten the list of points…thought it was already flattened.


@kennyb6 , thanks again so much! your source code was extremely helpful

Here is my final solution.

Since my plan is so large we are dividing it into 4 quadrants. I created a custom node for sorting rooms in quadrants after each level is sorted (This is not necessary if you don’t want quadrants and want entire levels, you’ll have to extract the distance part of the custom node to list your rooms in order per first room, then every closest room after)

Renumber Rooms by Level Grid Distance.dyn (257.9 KB)

1 Like

Hi Kenny. Is it possible to get this to handle multiple start points for groups of items?