Can't collect elements from multiple views using FilteredElementCollector

Hi all,

I was trying to collect elements from specific views, but when i use “FilteredElementCollector” to pass multiple view Ids i encounter an error stating
“Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed.
Traceback (most recent call last):
File “”, line 51, in
Exception: viewId is not valid for element iteration, because it has no way of representing drawn elements. Many view templates will fail this check.
Parameter name: viewId”

Can some one please help me with this? @solamour , @Gui_Talarico

import clr

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

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
ViewPlan=[]
ViewElements=[]
RefSec=[]
ViewLocation=[]
EleList=[]
#Do some action in a Transaction
TransactionManager.Instance.EnsureInTransaction(doc)
Viewcol = FilteredElementCollector(doc).OfClass(View).ToElements()
for i in Viewcol:
	if i.GetType().Name=="ViewPlan":
		ViewPlan.append(i.Id)
for x in ViewPlan:
	EleCol = FilteredElementCollector(doc,x).ToElements()
	for j in EleList:
		if j.GetType().Name=="Element" :
			ViewElements.append(j)
	for k in ViewElements:
		if k.LookupParameter("View Name")!=None:
			if k.LookupParameter("View Name").IsReadOnly:
				RefSec.append(k)
				ViewLocation.append(x)			
TransactionManager.Instance.TransactionTaskDone()

OUT = ViewLocation

Best Regards
Buvanesh

1 Like

What kind of Elements are you trying to collect? all Elements? or familyinstances or certain categories? a more detailed explanation of your goal/problem would help. I assume you want to find viewer elements like section cuts and callouts, you can use OST_Viewers built-in-category with the OfCategory filter in your FilteredElementCollector

A few ideas:

  • Line 37 in OfClass() use ViewPlan instead of View so you do not have to check it with the if statement two lines below

  • Maybe you do not want to check Revit View templates to find elements so as you iterate over Viewcol (they are considered Views generally, but do not include any elements) check if i.IsTemplate == False, then append it to ViewPlan (basically if a viewplan is not a template, append it to ViewPlan - BTW, change the name of this list, because ViewPlan is a Revit API class, so you won’t face other issues).

  • In EleCol, specify what category or what class of elements you want to find, use OfClass() or OfCategory() like you did for Viewcol, You may want to use OST_Viewers as mentioned above.

1 Like

Thanks a lot @habdirad. It worked like a charm. I was exactly missing to check for templates.
And can you please suggest me any method to get the BuiltInCategory names of the elements easily, that would help me a lot.
Thanks again: :innocent:

Glad it worked. In Python, you can use BuiltInCategory like this:
ViewRefs = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Viewers).ToElements()

and you can find the list of built-in categories here: https://www.revitapidocs.com/2016/ba1c5b30-242f-5fdc-8ea9-ec3b61e6e722.htm

2 Likes