Collect Elements in View using Python

I want to feed a node a single view element and get a list of elements visible in that view, specifically Revit links. This is easy for the active view, because there is the built in node “All Elements in Active View”. But I can’t set the active view programmatically and would prefer to iterate the views with a collector and return the elements that are visible.

This is the snippet of code I can’t get to work. I nkow that the viewElem.Id is right, but I’m guess something about my category statement is wrong:

listed = list()
listed.append(FilteredElementCollector(doc,viewElem.Id).WhereElementIsNotElementType().OfCategory(BuiltInCategory(OST_RvtLinks)).ToElementIds())

 

Full code:

import clr
clr.AddReference(‘ProtoGeometry’)
from Autodesk.DesignScript.Geometry import *

Import DocumentManager

clr.AddReference(“RevitServices”)
import RevitServices
from RevitServices.Persistence import DocumentManager

Import RevitAPI

clr.AddReference(“RevitAPI”)
import Autodesk

Import ToDSType(bool) extension method

clr.AddReference(“RevitNodes”)
import Revit
from Autodesk.Revit.DB import *
clr.ImportExtensions(Revit.Elements)

import System

viewElem = IN[0]

doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application

listed = list()
listed.append(FilteredElementCollector(doc,viewElem.Id).WhereElementIsNotElementType().OfCategory(BuiltInCategory(OST_RvtLinks)).ToElementIds())

OUT = listed

 

 

<span style=“color: #f92672; font-weight: bold;”> </span>

1 Like

Hi Luke. I’m not sure that this will work with the built in category filter. You could try using an “.OfClass(Autodesk.Revit.DB.RevitLinkInstance)” filter instead.

Thanks Dimitar. I actually found what I was looking for in the “Elements in View(s)” package. <span style=“color: #75715e;”>Marc Tavenier</span> method was:

for view in views:
collector = FilteredElementCollector(doc, view.Id)
elementlist.append(collector.ToElements())

In this case, I think the define collector, then ToElements steps were necessary? I couldn’t seem to get the same to work in one expression.

Hello Luke,

I want to do the exact same thing as you, unfortunately I have no background in coding. Can you please write out the final code for me which worked for you? Or maybe you could upload your dynamo file in which this works for you.

Thanks.