In Dynamo RevitAPI 2021, i want to make a python node to call the BuiltInCategory Enumeration and get the ElementId of it. I want to filter the list containing elements by its Category properties ElementId compared to the BuiltInCategory ElementId. I tried using python dir() method on the BuiltInCategory but return nothing about properties or method that returned ElementId. How to get the Element Id?
Note: To Collect the Elements, i explicitly not use FilteredElementCollector and not using it’s WherePasses method. Just using the uidoc.Selection.GetElementIds() to get selected element in active view.
Here is the code:
#standard import boilerplate for Revit API
"""--- CODE STARTS HERE ---"""
builtincateg = BuiltInCategory.OST_DetailComponents #example builtincateg DetailComponent
elems = [ ] #example containing Elements collected from revit selection
filtered = [ ]
for i in elems:
idata = UnwrapElement(i)
icateg = idata.Category.Id
# why i use ElementId instead of Name because for BuiltInCategory returned "DetailComponents"
# the i.Category.Name returned "Detail Items" and not "DetailComponents"
# Cannot check matching
if icateg.Id == builtincateg.Id:
#this check if ElementId same but error because builtincateg don't have ElementId properties
filtered.append(i)
else:
pass
OUT = filtered
"""--- CODE END HERE ---"""
def GetId(item):
if hasattr(item, "Id"): return item.Id.IntegerValue
else: return None
items = UnwrapElement(IN[0])
if isinstance(IN[0], list): OUT = [GetId(x) for x in items]
else: OUT = GetId(items)
Those custom node do the job to get 1. BuiltInCategory, 2. BuiltInCategory ElementId, 3. BuiltInCategory Revit Category. And using the import System, i can use System.Enums.GetValues(BuiltInCategories) to get all list of RevitAPI BuiltInCategories in this RevitAPI BuiltInCategory Enumeration
Here are the finished script
#standard import boilerplate for Revit API
"""--- CODE STARTS HERE ---"""
#1.List of All BuiltInCategory Enumeration in RevitAPI
#no need for this script because already specified the builtincategory
import System.Enum
biclist = System.Enum.GetValues(BuiltInCategory)
#init builtin object as variable
builtincateg = BuiltInCategory.OST_DetailComponents #example builtincateg DetailComponent
#2. Get Revit ElementId of the BuiltInCategory object
builtincategId = ElementId(builtincateg)
#3. Get the Revit Category (object class same as Element.Category)
biccategRevit = Revit.Elements.Category.ById(ElementId(builtincateg).IntegerValue)
elems = [ ] #example containing Elements collected from revit selection
filtered = [ ]
for i in elems:
idata = UnwrapElement(i)
icateg = idata.Category.Id
if icateg.Id == builtincategId:
filtered.append(i)
else:
pass
OUT = filtered
"""--- CODE END HERE ---"""