I am attempting to write a python script that would highlight elements or write to a list any disconnects involving a specific type of element (thread o lets or stub ons for example). I have a copy of the python script so far below and have attached a screenshot of my dynamo.
disconnects.py (6.2 KB)
Can you share the python code here in a post using the preformatted text option (</> in the above toolbar)? It would also be helpful to include the node preview bubbles in your screenshot so we can see exactly what elements you’re dealing with.
import clr
import sys
import System
# --- DesignScript (boilerplate) ---
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
# --- Revit API ---
clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *
import Autodesk.Revit.DB as DB
# --- RevitServices (Dynamo bridge) ---
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
# --- RevitNodes (for UnwrapElement) ---
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
from System.Collections.Generic import List
doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
uidoc = uiapp.ActiveUIDocument
toList = lambda x: x if hasattr(x, "__iter__") and not isinstance(x, (str, System.String)) else [x]
# -------------------------------------------------------
# INPUT (OPTIONAL FILTER)
# -------------------------------------------------------
# IN[0] = elements (e.g. from Categories → MEP Fittings → All Elements of Category)
# If provided, only elements that BOTH
# - appear in warnings AND
# - are in this list
# will be considered.
try:
raw_in = IN[0]
elems_in = toList(UnwrapElement(raw_in))
except:
elems_in = []
selected_ids = set()
for e in elems_in:
try:
selected_ids.add(e.Id.IntegerValue)
except:
pass
# -------------------------------------------------------
# MAIN: SCAN WARNINGS FOR STUB-ON / THREAD-O-LET
# -------------------------------------------------------
warnings = doc.GetWarnings() or []
problem_ids = set()
problem_elems = []
for w in warnings:
# Get the element IDs involved in this warning
try:
failing_ids = w.GetFailingElements()
except:
continue
if not failing_ids:
continue
for eid in failing_ids:
# If user gave a selection, only consider elements in that selection
if selected_ids and (eid.IntegerValue not in selected_ids):
continue
try:
elem = doc.GetElement(eid)
except:
elem = None
if elem is None:
continue
# Build a line similar to the Warnings "Messages" list:
# "Category : Name : id 123456"
try:
cat_name = elem.Category.Name if elem.Category else "<no category>"
except:
cat_name = "<no category>"
try:
elem_name = elem.Name or ""
except:
elem_name = ""
line = u"{0} : {1} : id {2}".format(
cat_name,
elem_name,
eid.IntegerValue
)
# Normalize and search for Stub-On / Thread-O-Let keywords
s = line.lower()
s = s.replace("thread o let", "thread-o-let")
s = s.replace("thread o-let", "thread-o-let")
s = s.replace("threadolet", "thread-o-let")
s = s.replace("stub on", "stub-on")
if ("stub-on" in s) or ("thread-o-let" in s):
if eid.IntegerValue not in problem_ids:
problem_ids.add(eid.IntegerValue)
problem_elems.append(elem)
# -------------------------------------------------------
# HIGHLIGHT IN REVIT
# -------------------------------------------------------
if problem_elems:
ids = List[ElementId]()
for e in problem_elems:
try:
ids.Add(e.Id)
except:
pass
uidoc.Selection.SetElementIds(ids)
# -------------------------------------------------------
# OUTPUT (FOR WATCH)
# -------------------------------------------------------
OUT = problem_elems