Last active
May 12, 2024 22:16
-
-
Save MrMagicPenguin/c815b4d01c2ac45564eab5645fd33786 to your computer and use it in GitHub Desktop.
Finds elements based on their Revit Room. This implementation does not rely on the IsPointInRoom() method of the Room Class, so in theory this could be expanded to query against any provided BoundingBoxXYZ.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Load the Python Standard and DesignScript Libraries | |
import sys | |
import clr | |
clr.AddReference('ProtoGeometry') | |
from Autodesk.DesignScript.Geometry import * | |
# import doc manager | |
# Use this to access the hidden RVT DB | |
clr.AddReference("RevitServices") | |
import RevitServices | |
from RevitServices.Persistence import DocumentManager | |
from RevitServices.Transactions import TransactionManager | |
clr.AddReference("RevitAPI") | |
from Autodesk.Revit.DB import * | |
doc = DocumentManager.Instance.CurrentDBDocument | |
uiapp = DocumentManager.Instance.CurrentUIApplication | |
app = uiapp.Application | |
def updateParamByName(elementID, paramName, paramValue): | |
# We receive ElementID instead of Element object from our Collector. | |
# Need to convert to get actual Element reference. | |
element = doc.GetElement(elementID) | |
# GetParameters returns a list. | |
# GetParameter does not work. | |
# Therefore, we take the first in the list to get what we asked for. | |
parameter = element.GetParameters(paramName) | |
parameter[0].Set(str(paramValue)) | |
# The inputs to this node will be stored as a list in the IN variables. | |
dataEnteringNode = IN | |
objects = [] | |
# Define garbage categories, combine into massive logical filter | |
# Begin a transaction | |
TransactionManager.Instance.EnsureInTransaction(doc) | |
try: | |
#* Find all rooms | |
# Create FilteredElementCollector for rooms | |
all_rooms = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Rooms).WhereElementIsNotElementType().ToElements() | |
for room in all_rooms: | |
#* Get the bounding box of our room | |
#* As the room comes from the FEC, we don't need to unwrap it. | |
#* We pass "None" to the BoundingBox function so it ignores any View settings | |
room_BB = room.get_BoundingBox(None) | |
room_name = room.get_Parameter(BuiltInParameter.ROOM_NAME).AsString() | |
if room_BB: | |
# Convert bounding box to Outline for Filter. | |
outln = Outline(room_BB.Min, room_BB.Max) | |
bbFilter = BoundingBoxIsInsideFilter(outln) | |
#? This list can be further clarified with an additional filter to remove other Rooms or garbage objects. | |
#? There is some issue on TBC about ToElements(). Need to investigate. | |
room_objects = FilteredElementCollector(doc).WherePasses(bbFilter).WhereElementIsNotElementType().WhereElementIsViewIndependent() | |
ignored_categories = ["Rooms","Curtain Wall Grids", "Analytical Nodes", "Lines", "<Space Separation>", "<Room Separation>"] | |
if room_objects: | |
for obj in room_objects: | |
if obj.Category and obj.Category.Name not in ignored_categories: | |
print(obj.Id) | |
updateParamByName(obj.Id, "Comments", room.get_Name()) | |
else: | |
print("Room does not contain any objects. Skipping room.") | |
else: | |
print("Room does not have a Bounding Box. Skipping room.") | |
TransactionManager.Instance.TransactionTaskDone() | |
except Exception as e: | |
print("Exception" + str(e)) | |
# Assign your output to the OUT variable. | |
OUT = objects |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment