Get all BuiltInCategory

Hi all,

I am trying to convert some of my IronPython 2.7 scrips to cPython3 to be used with the newer versions of Dynamo. 2.7 gives us direct access to the Enums through “System.Enum.GetValues(BuiltInCategory)” whereas cPython3 returns the element id.

What I was doing.

def builtInCategories():
	builtInCategories = {}
	for bic in System.Enum.GetValues(BuiltInCategory):
		builtInCategories[bic.ToString()] = bic
	return builtInCategories

This worked very well for my purposes.

I have been trying for some time now to convert this and have failed. I have managed to get the name of the BuiltInCategory but I cannot figure out how to get the Enum itself.

for bic in System.Enum.GetValues(BuiltInCategory):    
    name = System.Enum.GetName(BuiltInCategory, bic)

How can I get the enum itself to store in the dictionary? I have tried doc.GetElement() but it returns null

Thank you for any help you can provide.
Steven

Use Reflection :wink:
Just don’t make a dict() out of the names and values, because then it just converts back to the Int Ids :sweat_smile:

3 Likes

you can try to use a .Net Dictionary instead a python Dictionary (to avoid conversion)

import sys
import clr
import System
#import net library
from System import Array
from System.Collections.Generic import List, IList, Dictionary
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

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


net_dict = Dictionary[System.String, BuiltInCategory]()
for i, j in zip(System.Enum.GetNames(BuiltInCategory), System.Enum.GetValues(BuiltInCategory)):
	net_dict.Add(i,j)
OUT = net_dict
2 Likes

Thank you @c.poupin that worked seamlessly into my other code.

@Ewan_Opie thank you for your suggestion as well. Using indexing you can get very similar results that could be more user-friendly for others.