Filter all elements by Level

Hello,

i want to filter all placed elements by level…
…can i also get the elements which are not addressed to a level?

import clr
import sys 

clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument

all_levels = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Levels).WhereElementIsNotElementType().ToElements()
lvl_names = []

for l in all_levels:
	lvl_filter = ElementLevelFilter(l.Id)
	lvl_names.append(l.Name)
	
	elements_lvl = FilteredElementCollector(doc).WherePasses(lvl_filter).WhereElementIsNotElementType().ToElements()

OUT = all_levels,lvl_names, elements_lvl

at moment i get too less object…

KR

Andreas

1 Like

Hi,
here is an example that should work with most elements


import clr
import sys
import System
from System.Collections.Generic import List
#
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
import Autodesk.DesignScript.Geometry as DS

#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
uiapp = DocumentManager.Instance.CurrentUIApplication
uidoc = uiapp.ActiveUIDocument
app = uiapp.Application
sdkNumber = int(app.VersionNumber)

def GetLevelId(item):
	global all_levels
	# try with properties
	if hasattr(item, "LevelId") and item.LevelId != ElementId.InvalidElementId :
		return item.LevelId
	if hasattr(item, "Level") and  is not None:
		return item.Level.Id
	if hasattr(item, "GenLevel") and item.GenLevel. is not None:
		return item.GenLevel.Id
	# try with parameters
	paraLvl = item.get_Parameter(BuiltInParameter.INSTANCE_REFERENCE_LEVEL_PARAM)
	paraLvl_s = item.get_Parameter(BuiltInParameter.INSTANCE_SCHEDULE_ONLY_LEVEL_PARAM)
	if paraLvl is not None:
		return paraLvl.AsElementId()
	if paraLvl_s is not None:
		return paraLvl_s.AsElementId()
	# try with level ProjectElevation
	if isinstance(item.Location, LocationPoint):
		ptZ = item.Location.Point.Z
	else:
		margin = 0.2
		ptZ = item.get_BoundingBox(None).Min.Z + margin
	for lvl in sorted(all_levels , key = lambda x : x.ProjectElevation, reverse=True):
		if ptZ >=  lvl.ProjectElevation:
			return lvl.Id

	return ElementId.InvalidElementId
  
all_levels = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Levels).WhereElementIsNotElementType().ToElements()

outdict = {}
for lvl in all_levels:
	#
	filter = System.Predicate[System.Object](lambda x : x.Category is not None and
											x.Category.CategoryType == CategoryType.Model and 
											x.OwnerViewId == ElementId.InvalidElementId and
											x.get_BoundingBox(None) is not None and 
											GetLevelId(x) == lvl.Id )

	elements_lvl = List[DB.Element](FilteredElementCollector(doc).WhereElementIsNotElementType().ToElements()).FindAll(filter)
	outdict[lvl.Name] = elements_lvl

OUT = outdict
2 Likes