You’re going to need a few different strategies to get all items from Revit classes depending what they are.
Classes with attributes (ForgeTypeId)
These hold various types of information in default properties like SpecTypeId.Weight or UnitTypeId.Celsius which refer to ForgeTypeId values. Python has a handy built in function called vars()
that can extract the available attributes and you will have to filter for .NET methods and python attributes which usually have an underscore in their name and a few standard python functions of the class such as string, int, boolean, etc. We can then use the eval()
function to turn a string representation into an object and voilà instant everything!
Code
import clr
clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import SpecTypeId
properties = sorted(
[
v
for v in vars(SpecTypeId)
if "_" not in v and v not in ["Boolean", "Int", "Reference", "String"]
]
)
def props(p):
obj = eval("SpecTypeId." + p)
return p, obj.TypeId, obj
OUT = map(props, properties)
Built In Enumerations (System.Enum)
Revit has lots of enumerations which are collections of attributes that refer to an integer, this can be specific to a class, usually returning 0, 1, 2 etc. or returning a Revit internal ElementId integer which are negative. The two most used Enums are BuiltInParameter and BuiltInCategory and when we use Enums such as BuiltInParameter.VIEWPORT_SHEET_NAME or BuiltInCategory.OST_Walls these are integers. This is very handy because we can use these to get for example a class of a Parameter or Category, doc.GetElement(<integer>)
or filter using the ParameterFilterRuleFactory or other filters as part of a FilteredElementCollector with conversion by using ElementId(<integer>)
.
Code
import clr
from System import Enum
clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import BuiltInParameter
OUT = sorted(zip(Enum.GetNames(BuiltInParameter), Enum.GetValues(BuiltInParameter)))
Revit Classes (Autodesk.Revit)
Finally we get to gathering Revit classes using the FilteredElementCollector. I will not go into too much details as there is plenty of information online such as Dynamo Forum, Dynamo Python Primer and if you really want to get into the nitty gritty check out Jeremy Tammik’s blog Retrieving Elements Using FilteredElementCollector
. Jeremey’s code is usually in C# however you can convert to python using online tools which should get you 80% of the way there.
Code
import clr
clr.AddReference("RevitServices")
from RevitServices.Persistence import DocumentManager
clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import FilteredElementCollector, Group
doc = DocumentManager.Instance.CurrentDBDocument
groups = FilteredElementCollector(doc).OfClass(Group).ToElements()
def grp_info(g):
view = doc.GetElement(g.OwnerViewId).Title
return g.Name, view, g
OUT = map(grp_info, groups)
Note: If what you are using has a unique name it is quite simple to turn the information into a dictionary so you can retrieve info with a string as key. (You can also do this with System.Enum.GetValue())
Code
import clr
from System import Enum
clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import BuiltInCategory, ElementId
bic_dict = dict(zip(Enum.GetNames(BuiltInCategory), Enum.GetValues(BuiltInCategory)))
elemid = ElementId(bic_dict[IN[0]])
OUT = elemid, type(elemid), bic_dict