Get Elements In View(s)

Hello Dynamo Community,

I’m encountering an issue while trying to filter families within a view using Dynamo. Here’s the problem I’m facing:

Problem Statement:

I’m working on a project where I need to retrieve families that are visible in a specific view. However, the current script I’m using also retrieves subfamilies that are created within the main/global family. What I actually need is to retrieve only the master family that contains all the subfamilies within it, excluding the subfamilies themselves.

Current Approach:

I’ve attempted to filter the families within the view using Dynamo scripting. However, the script I’m using seems to be returning not only the master families but also the subfamilies created within them.


1 Like

Look at “FamilyInstances.SubComponents” from archilab or “Element.SubComponents” from Clockwork. Use that to get the subcomponents of all the elements and use those results to filter out all matching elements in the main list.

2 Likes

an example with API

import clr
import sys
import System

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

#import transactionManager and DocumentManager (RevitServices is specific to Dynamo)
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

clr.AddReference("System.Core")
clr.ImportExtensions(System.Linq)

toList = lambda x : x if hasattr(x, '__iter__') else [x]

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

out = []

for v in views:
    elems = FilteredElementCollector(doc, v.Id).OfClass(DB.FamilyInstance).Where(lambda x : x.SuperComponent is None).ToList()
    out.append(elems)
    
OUT = out
3 Likes

Hi @c.poupin ,

Thanks for your input! I’ve tested the code, and it indeed works well. However, I found that adding some additional filters could enhance the results.

1 Like