Matt Johnson
Matt Johnson

Reputation: 55

python for revit - collect views in active view

I'm trying to use a FilteredElementCollector inside my pyRevit script to collect all views (sections, elevations, plan callouts etc.) in the active view.

from pyrevit.framework import clr
from pyrevit import revit, DB

clr.AddReference('RevitAPI')
clr.AddReference('RevitAPIUI')

from Autodesk.Revit.DB import *
from pyrevit import forms

doc = __revit__.ActiveUIDocument.Document

view = doc.ActiveView
AllStuff = FilteredElementCollector(doc,doc.ActiveView.Id).WhereElementIsNotElementType().ToElements()

AllViews = []

try:
    for x in AllStuff:
        if "View" in x.Category.Name:
            AllViews.append(x)

This will return some, but not all of the views. For example, some sections are included but others are not and I can't tell why.

If I add ".OfCategory(BuiltInCategory.OST_Views)" I get nothing at all. Do I need to break it down into several more specific categories? Thanks for any help.

Upvotes: 0

Views: 1366

Answers (1)

Cyril Waechter
Cyril Waechter

Reputation: 535

There is no view in FilteredElementCollector(doc, doc.ActiveView.Id), you can see it by doing :

for el in FilteredElementCollector(doc, doc.ActiveView.Id):
    print(el)

There is an Element which is not of category OST_Views and is not a view even if it has the same name as your view. To see this you can use RevitLookUp. Lookup

I found a way to retrieve the actual view (I don't know any other way at the moment) by looking at VIEW_FIXED_SKETCH_PLANE BuiltInParameter which refer to the SketchPlane whiche reference the actual view as Element.OwnerViewId. Then you can make sure that the element is of class View :

for el in FilteredElementCollector(doc,doc.ActiveView.Id):
    sketch_parameter = el.get_Parameter(BuiltInParameter.VIEW_FIXED_SKETCH_PLANE)
    # If parameter do not exist skip the element
    if not sketch_parameter:
        continue
    view_id = doc.GetElement(sketch_parameter.AsElementId()).OwnerViewId
    view = doc.GetElement(view_id)
    if isinstance(view, View):
        print(view)

Upvotes: 1

Related Questions