Dynamo for Revit – Window numbering counter-clockwise from a user-picked start point on facade

Post text

Hello,

I’m working in Dynamo for Revit and I want to automatically number windows per floor, counter-clockwise around the building, starting from a user-defined start point.

What I want to achieve:

  • User picks a start point on a facade using “Select Point on Face”

  • That point defines Window 01

  • All windows on the same level are then:

    • sorted counter-clockwise

    • numbered sequentially (01, 02, 03, …)

  • Numbering restarts per level

  • The result is written to a shared instance parameter on Windows

Current approach:

  • Categories → Windows

  • All Elements of Category

  • Element.Location

  • Point.Subtract (window point – picked start point)

  • Python node using atan2(Y, X) to calculate angles

  • List.SortByKey for CCW sorting

  • Sequence → Number.ToString → String.PadLeft

  • Element.SetParameterByName

Questions:

  1. Is this the recommended approach for CCW sorting from a picked point in Dynamo?

  2. Is there a cleaner / more robust way to handle per-level grouping?

  3. Any pitfalls with Select Point on Face I should be aware of?

Thanks in advance!

Have you thought of creating a Room outside the Building and see what Windows the room has

A few things here.

  1. I wouldn’t use Python just to call atan; you are taking a performance hit this way.
  2. I am not sure your results will match your goal for many common building shapes. An L shape where the user wants to start on the end of the leg as one example; a U shape where they want to start almost anywhere, etc.. as the vectors won’t work o it cleanly. Boxes should be fine, but as soon as there is any obtuse angle in the perimeter you’re in for some issues.

My personal preference would be to get the exterior envelope at each level (I like a room at the exterior of the building for this, but there are a few ways to attain that info), build a PolyCurve from each curve loop, break the PolyCurve at the closest point to the picked point, sort the windows by their location point’s parameter on that line.

As Jacob mentioned, the method may vary depending on the geometry of the external curve, which is a valid point. In most standard cases it should work well — I have already tested this approach with curtain walls and regular layouts. Shapes with obtuse or irregular angles may require some additional handling or adjustments, but for typical project conditions this workflow should meet the intended requirements.


numbering in clockwise order.dyn (29.4 KB)

Hi,

I will consider looking into working with the TSP algorithm, the input data would be the locations of the windows (location Point).

Nice…i was think about Alpha Shape by points,just for play around and alternative ways :wink: but guess many ways :wink:

EDIT forget alpha ;)…just test traveling sales man thats the best…:wink: ;O IMO

Example of TSP with networkx for the graph and algorithm, scipy for Euclidean distance, numpy for linear algebra

Pick door, window etc and will collect all of the same BuiltInCategory in the active view

Uses element id to create complete graph where all nodes are connected to each other by an edge, then adds required X & Y coordinates, weights all the edges with distance

Solves TSP, checks for clockwise by picking three points and producing the matrix determinant

Rolls the list to the picked element and numbers from one

Edit: If you need to install networkx check out my post here

from collections import deque

import clr
import networkx as nx
import numpy as np
from networkx.algorithms.approximation import christofides
from scipy.spatial import distance

clr.AddReference("RevitServices")
from RevitServices.Persistence import DocumentManager

clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import FilteredElementCollector, FamilyInstance, LocationCurve


def xy(item):
    if hasattr(item, "Location"):
        loc = item.Location
        if isinstance(loc, LocationCurve):
            point = loc.Curve.Evaluate(0.5, True)
        else:
            point = loc.Point

        return point.X, point.Y


def id(item):
    return item.Id.Value


def weights(edge):
    return *edge, distance.euclidean(G.nodes[edge[0]]["pos"], G.nodes[edge[1]]["pos"])


def to_string(number, offset=1, fill=2):
    return str(number + offset).zfill(fill)


doc = DocumentManager.Instance.CurrentDBDocument

start_elem = UnwrapElement(IN[0])  # type:ignore
clockwise = IN[1]  # type:ignore

elements = (
    FilteredElementCollector(doc, doc.ActiveView.Id)
    .OfCategory(start_elem.Category.BuiltInCategory)
    .OfClass(FamilyInstance)
    .ToElements()
)

# Use id for nodes
ids = list(map(id, elements))

G = nx.complete_graph(ids)

points = dict(zip(ids, map(xy, elements)))
nx.set_node_attributes(G, points, "pos")

edge_weights = map(weights, G.edges())
G.add_weighted_edges_from(edge_weights)

# Solve TSP with Christofides algorithm
# Remove last element to avoid duplicate start/end node
cycle = christofides(G)[:-1]

# Check clockwise with determinant of 3 points in cycle
u, v, w = cycle[0], cycle[len(cycle) // 3], cycle[(len(cycle) * 2) // 3]
det = np.linalg.det([[1, *points[u]], [1, *points[v]], [1, *points[w]]])

# Determinant xor clockwise
if (det < 0) ^ clockwise:
    cycle.reverse()

# Rotate list to start element
start_id = id(start_elem)
start_index = cycle.index(start_id)
cycle = deque(cycle)
cycle.rotate(-start_index)

OUT = cycle, map(to_string, range(len(cycle)))