How to find the nesting level of Revit families?

Hi, I’d like to know if there’s a way to obtain the nesting level of a group of families loaded in a .rvt file using Dynamo. Many thanks in advance!

So you want to know how many families are nested within a family?

Hi SeanP, exactly!

I think this should work for you.

#Sean Page, 2021
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

#Preparing input from dynamo to revit
element = UnwrapElement(IN[0])

#Do some action in a Transaction
#TransactionManager.Instance.EnsureInTransaction(doc)
TransactionManager.Instance.ForceCloseTransaction()
famDoc = doc.EditFamily(element)
fams = FilteredElementCollector(famDoc).OfClass(Family).ToElements()
#TransactionManager.Instance.TransactionTaskDone()

OUT = fams

2 Likes

Many thanks!! It worked as a charm. I’d like to know, precisely, how to make it work with multiple families at the same time. Is it possibile to do so?

You could either save the Python script @SeanP provided you into a Custom Node (.dyf), or modify the Python to handle lists; if this is your first experience with utilizing a Python script within Dynamo I’d recommend giving the latter a try. This post gives a good breakdown of modifying Python scripts in Dynamo to handle lists and the section on Python scripts in Dynamo in the Dynamo primer has an example as well!

6 Likes

It would look something like this in Python, or you could wrap it into a custom node as @awilliams noted.

#Sean Page, 2021
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

#Preparing input from dynamo to revit
Families = UnwrapElement(IN[0])

Nested = []
FamilyDocs = []
#Do some action in a Transaction
TransactionManager.Instance.ForceCloseTransaction()

for Fam in Families:
    tempNest = []
    famDoc = doc.EditFamily(Fam)
    for f in FilteredElementCollector(famDoc).OfClass(Family).ToElements():
        tempNest.append(f)
    Nested.append(tempNest)
    #So you can close the Family Documents
    FamilyDocs.append(famDoc)
    

OUT = FamilyDocs,Nested
2 Likes

Many thanks! That’s exactly what I was looking for. Many thanks! :slight_smile:

2 Likes