Code from Dynamo to PyRevit

Hello pyRevit community :slight_smile:

I have problems understanding how i have to modify my python code from Dynamo to use it in pyRevit.

For example, i can´t use the following FEC in pyRevit because the Class does not exist:

families = DB.FilteredElementCollector(doc).OfClass(Family).ToElements()

It would work with DB.FamilySymbols, but i would like to have Families. Why is Family and DB.Family not working for me in pyRevit?

As another example here is a code i successfully used in pyRevit.
To make it work i had to add the prefix “DB.” to many objects like DB.FEC, DB.Element, DB.Class…

Why is this necessary? And are there other things i have to consider when using my “dynamo code” in PyRevit?

Would be great if someone could enlighten me a little bit :slight_smile:

Kind regards

# -*- coding: utf-8 -*-

from Autodesk.Revit import DB

import time
st = time.time()

doc = __revit__.ActiveUIDocument.Document

def SetWDParameter():

	famTypes = DB.FilteredElementCollector(doc).OfClass(DB.FamilySymbol).ToElements()

	for famType in famTypes:
		if famType.Family.Name == "Wandöffnung rechteckig" and DB.Element.Name.GetValue(famType) == "WD":
			WD_famType = famType
		
	provider = DB.ParameterValueProvider(DB.ElementId(DB.BuiltInParameter.ELEM_FAMILY_PARAM))
	evaluator = DB.FilterNumericEquals()

	filter = DB.ElementParameterFilter(DB.FilterElementIdRule(provider, evaluator, WD_famType.Id))	
	elements = DB.FilteredElementCollector(doc).OfCategoryId(WD_famType.Category.Id).WherePasses(filter).ToElements()

	for element in elements:
		elevation_from_level = element.get_Parameter(DB.BuiltInParameter.INSTANCE_ELEVATION_PARAM).AsDouble()
		hight = element.LookupParameter("Höhe Aussparung").AsDouble()
		OK_Wanddurchbruch = element.LookupParameter("OK Wanddurchbruch")
		level = doc.GetElement(element.LevelId)
		level_elevation = level.LookupParameter("Elevation").AsDouble()
		elevation = level_elevation + elevation_from_level + hight
		if OK_Wanddurchbruch is not None and OK_Wanddurchbruch != elevation:
			set = OK_Wanddurchbruch.Set(elevation)

IronPython of CPython?
Seems to work fine here under IronPython…

1 Like

This doesn’t work because you didn’t import “Family”. If you want to access the Family type you need to access the Family type through the DB namespace by using DB.Family. You said it didn’t work even when you used DB.Family, but it works on my end, so you may not have done it properly. I tried both of these and it worked and gave me all the families.

from Autodesk.Revit.DB import Family, FilteredElementCollector

doc = __revit__.ActiveUIDocument.Document
print(FilteredElementCollector(doc).OfClass(Family).ToElements())
from Autodesk.Revit import DB

doc = __revit__.ActiveUIDocument.Document
print(DB.FilteredElementCollector(doc).OfClass(DB.Family).ToElements())

With your original code, if you change the Family to DB.Family it should work.

# this
families = DB.FilteredElementCollector(doc).OfClass(DB.Family).ToElements()
# instead of this
families = DB.FilteredElementCollector(doc).OfClass(Family).ToElements()

You need to prefix everything with DB because you only imported the DB namespace from the Revit Api. Thus, you need to access all the classes through the DB namespace by prefixing everything with “DB.” If you don’t want to prefix you need to import everything individually, like so.

# import like this
from Autodesk.Revit.DB import (FamilySymbol, FilteredElementCollector, ParameterValueProvider, Element, 
                               ElementParameterFilter, FilterElementIdRule, BuiltInParameter)
# instead of like this
from Autodesk.Revit import DB

Edit: You could also import like this if you don’t want to prefix, but it is not recommended because it imports everything in the DB namespace.

from Autodesk.Revit.DB import *
3 Likes

alternatively within the pyrevit framework

from pyrevit import DB, revit

doc = revit.doc
print(DB.FilteredElementCollector(doc).OfClass(DB.Family).ToElements())
3 Likes

Thank you so much for the help, things are getting clearer now :slight_smile:

I also found out i can use the DB namespace code in Dynamo if I import it like this:

from Autodesk.Revit import DB

That will help me.
I gave my best at reproducing my problem with OfClass(DB.Family) but i can´t. Very strange, this took me a long time today, but now it magically works as you say.

1 Like