How to get the Category Name - API Name?

Hello,

i can get the name of a category. But how can i get the API Name like OST_Rooms, OST_Slabs, OST_StructuralColumn, … a.s.o. ?

import clr

clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *

clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import *

clr.AddReference('System')
from System.Collections.Generic import List

clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
clr.ImportExtensions(Revit.Elements)

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

doc = DocumentManager.Instance.CurrentDBDocument
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

items = FilteredElementCollector(doc, doc.ActiveView.Id).WhereElementIsNotElementType().ToElements()

# 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
        dict_elements[cat_name].append(i)
    except:
        pass
        # You might get 1-2 elements without Category!

# Show Results

result =  [k for k in dict_elements.items()]  

OUT = result
element = UnwrapElement(IN[0])

APICategory = [i.get_Parameter(BuiltInParameter.ELEM_CATEGORY_PARAM).AsValueString() for i in element]

OUT = APICategory

I highly recommend installing the Revit Lookup tool to help with things like this. It’s way easier to just look for the stuff you’re after than go searching on the internet.

image

2 Likes

See my post here How get OST (Built-In Name) of element - #23 by Mike.Buttery

2 Likes

How I would do it now

import clr
import System

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

OUT = System.Enum.GetName(BuiltInCategory, IN[0].Id)

EDIT IN[0] is a Dynamo Category object

5 Likes

This should cover almost anything with a category

import clr
import System

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

inputs = UnwrapElement(IN)

ids = [
    i.Id
    if isinstance(i.Id, int)  # Dynamo Category
    else i.Id.IntegerValue
    if isinstance(i, Category)  # Revit Category
    else i.Category.Id.IntegerValue  # Revit/Dynamo Element
    for i in inputs
]

OUT = [System.Enum.GetName(BuiltInCategory, id) for id in ids]
4 Likes

@Nick_Boyts ,

i got a empty list when i use BuiltInParameter

KR

Andreas

i understand i need category itself …

i have to combine it with python-dicctionary

i can run it in this way…

KR

Andreas

1 Like

The property is only available in 2023+. I believe it still requires you to check for the matching Id, but it’s a bit of a shortcut.

You could just do this in your code rather than getting the category separately.

1 Like