Here I will quickly cover some basics of Enums. Quite often when working with the Revit API we are confronted with Enums. They are literally everywhere and for good reason – they are super handy.
What are they?
Enums are a set of named numerical values, so a string and a number pair. The numbers are usually integers and you’ll find in the Revit API this is always the case (from what I’ve noticed anyway). A very simple example of Enums you’ll see on pretty much most blog sites is the DaysOfWeek example. I chose this particular example as I found it more thorough than most of the others I’ve seen and you can get a feel for how they are structured even though it is written in c#. I won’t go into much more detail about them as they are well covered on the web.
How to access them…
Now, if you have struggled to understand how to access them in Python in Dynamo, then you are not alone. I will be honest and admit I found Enums infuriating coming from a c# background into Python and couldn’t figure out how to list all the values. Like most people, I went bounding over to the Python docs page and it gives you all these awesome methods to use with Enums, but you’ll instantly get stuck because Dynamo uses IronPython 2.7 and this does not come with the “enum” module (the enum module was new to Python 3.4 – although it is back-ported to earlier versions).
Well, there is a way around this, and it is using the .net Enum instead – credit to Konrad Sobon as I peeked at his nodes to see how he was doing it and kicked myself after realising how simple it actually was. Cheating… maybe, learning from what others have done… definitely. See the example below…
import clr #We need to import the Revit API to access the RevitAPI's enums... clr.AddReference("RevitAPI") from Autodesk.Revit.DB import * #We need to import System to use .net Enum... import System #from System.Collections.Generic import * #Simply one line of code will Enumerate all the Values of FamilyPlacementType Enum... OUT = System.Enum.GetValues(FamilyPlacementType)
This gives you all the values for the FamilyPlacementType Enum as a list, now you can pick and choose which you want to use. Easy stuff!
Alternatively, if you need just the one you can do the following (Intellisense helps out lot’s here allowing you too see what Enum values are available)…
import clr #We need to import the Revit API to access the RevitAPI's enums... clr.AddReference("RevitAPI") from Autodesk.Revit.DB import * #We need to import System to use .net Enum... import System #from System.Collections.Generic import * #Choose just one Enum Value... OUT = FamilyPlacementType.OneLevelBased
Where to find Enums in the Revit API…
In the Revit API, each Namespace has a bunch of Enums you can use throughout that Namespace. If you want to have a look at what is available, then go to either the Revit API Docs, click on the Revit version you are using and select the appropriate Namespace (most commonly used Namespace is Autodesk.Revit.DB – but check out the others).
With the Namespace selected, you’ll see the following….
I have highlighted the Enums above, but you can spot these easily by looking for the boxes with a white fill instead of a black fill.
Alternatively, you could use the RevitAPI.chm file (my preferred source) which comes with the Revit SDK (I recommend downloading the SDK for those who want to learn more than just Dynamo/Python, there are great example scripts in there as well as the offline help file). Similar to the above, click on the Namespace you want to have a look at and scroll down until you find Enumerations and you’ll find a complete list of Enums…
If you know the Enum you need then you could search for it in the search bar in either the Web based version or the .chm file.
Worked Examples with Enums…
(TODO- will add some soon, promise! :))
Leave a Reply