Get Levels From Views in Viewports

Hi All

I am trying to create a python script that dimensions the levels and grids of all views on sheets. I have managed to produce the python script below, which gets the crop regions of the views so I can use them as a reference for placing the dimensions. However, I am struggling to understand how to filter the levels/grids within the views using the filtered element collector. I could easily do this using the OOTB node All Elements of Category in View, but I am trying to do everything using Python.
Thanks for your assistance. @john_pierson

# Get Viewports on Sheet
all_viewports = FilteredElementCollector(doc, doc.ActiveView.Id).OfCategory(BuiltInCategory.OST_Viewports).WhereElementIsNotElementType().ToElements()
all_viewports_flat = []
for view in all_viewports:
	if isinstance (view,list):
		for v in view:
# Get Views
all_views = []
for viewport in all_viewports_flat:
	viewId = viewport.ViewId
# Get Crop Regions
all_cropRegions = []
for view in all_views:
	viewShapeManager = view.GetCropRegionShapeManager()
# Flatten Crop Regions
cropLines = []
for line in all_cropRegions:
	for l in line:	
		for ll in l:
# Chop Line List
listLength = len(cropLines)
chopMultiple = listLength / 4
splitLengths = [4]*chopMultiple
Input = iter(cropLines)
choppedLines = [list(islice(Input,elem)) for elem in splitLengths]

# Assign your output to the OUT variable.
OUT = choppedLines

@bayowindapo courtesy of Data-Shapes package, there is a node named All Elements Of Categories in View:

view = UnwrapElement(IN[0])

categories = tolist(IN[1])
catfilters = []

for i in categories:
	catfilters.append(ElementCategoryFilter(System.Enum.ToObject(BuiltInCategory, int(str(i.Id)))))
catfilterlist = List[ElementFilter](catfilters)
filter = LogicalOrFilter(catfilterlist)

elems = FilteredElementCollector(doc,view.Id).WherePasses(filter).ToElements()

OUT = elems
1 Like

Got it!!

all_viewports = FilteredElementCollector(doc, doc.ActiveView.Id).OfCategory(BuiltInCategory.OST_Viewports).WhereElementIsNotElementType().ToElements()

all_views = []
for viewport in all_viewports:
	viewId = viewport.ViewId

levels = []
for view in all_views:
	levels.append(FilteredElementCollector(doc, view.Id).OfCategory(BuiltInCategory.OST_Levels).WhereElementIsNotElementType().ToElements())

Oh wow haha thanks. I literally just figured it out. Much appreciated.