Get all BuiltInCategory - ERROR

Very good days.
I have the following problem.
I am trying to get all the categories of the currently open file and then I want to get its items.
It happens that I can’t get past the line to obtain the categories by ID and I always get the error as you can see…
I’m doing wrong…
Thanks for your help.


Python script.dyn (3.4 KB)

@AM3D.BIM.STUDIO ,

try this:

# 📦 imports
import sys
import clr

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

doc = DocumentManager.Instance.CurrentDBDocument
sets = doc.Settings.Categories

# 🎯 gets all categories in the document
allCats = doc.Settings.Categories 

OUT = allCats

i used R2024, but IronPython3 … CPython is still not working.

you mean in current View? 2D and 3D and views ?

KR

Andreas

@AM3D.BIM.STUDIO ,

i can share this it runs on PyRevit pretty well!


items = FilteredElementCollector(doc, doc.ActiveView.Id).WhereElementIsNotElementType().ToElements()
print('\n There are {} Elements'.format(len(items)))

# Prepare dict with an empty List as default value
from collections import defaultdict
dict_elements = defaultdict(list)

for i in items:
    try:
        cat_name = i.Category.Name
        str_built_cat = str(i.Category.Name)
        dict_elements[str_built_cat].append(i)
    except:
        print('Element ({}) does not have BuiltInCategory!'.format(i.Id))
        # You might get 1-2 elements without Category!


# Show Results
print('\n List number of element in each category')
for k,v in dict_elements.items():
    dist = 30 - len(k) # Calculate ammount of dashes to print nicely
    print('{}:{} {} Elements'.format(k, '-'*dist, len(v)))

you can modify it to dynamo

KR

Andreas

Thanks for your comment…
Sorry I forgot to mention that I am working within REVIT 2020, I thought it was the same for all versions and now researching I found the surprise that it is different in REVIT 2020
any idea what it could be like in the 2020 version

This will work with R2020

import sys

import clr
import System

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

clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import BuiltInCategory

doc = DocumentManager.Instance.CurrentDBDocument

all_cats = doc.Settings.Categories
builtin_cats = [cat for cat in all_cats if cat.IsBuiltInCategoryValid]
all_bics = System.Enum.GetValues(BuiltInCategory)

OUT = all_cats, builtin_cats, all_bics
3 Likes

for all version of Revit

import sys
import clr
import System

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

clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import BuiltInCategory

doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
uidoc = uiapp.ActiveUIDocument
app = uiapp.Application
sdkNumber = int(app.VersionNumber)

all_cats = doc.Settings.Categories
if sdkNumber > 2022:
    builtin_cats = [cat.BuiltInCategory if cat.IsBuiltInCategoryValid else None  for cat in all_cats ]
else:
    builtin_cats = [System.Enum.ToObject(BuiltInCategory, cat.Id.IntegerValue) if cat.IsBuiltInCategoryValid else None  for cat in all_cats ]

OUT = builtin_cats
3 Likes