How to select elements by multiple categories

python

#1

Hello, can you help me to optimize (I need to make the code run faster) the code:
(it selects all elements in the active view by several categories)

import clr
clr.AddReference(‘RevitAPI’)
clr.AddReference(‘RevitServices’)
import Autodesk,RevitServices
from Autodesk.Revit.DB import *
from RevitServices.Persistence import DocumentManager
doc = DocumentManager.Instance.CurrentDBDocument
a=FilteredElementCollector(doc,doc.ActiveView.Id)
b=FilteredElementCollector(doc,doc.ActiveView.Id)
c=FilteredElementCollector(doc,doc.ActiveView.Id)
d=FilteredElementCollector(doc,doc.ActiveView.Id)

rooms = ElementCategoryFilter(BuiltInCategory.OST_Rooms)
walls = ElementCategoryFilter(BuiltInCategory.OST_Walls)
windows= ElementCategoryFilter(BuiltInCategory.OST_Windows)
doors = ElementCategoryFilter(BuiltInCategory.OST_Doors)

rooms=a.WherePasses(rooms).WhereElementIsNotElementType().ToElements()
walls=b.WherePasses(walls).WhereElementIsNotElementType().ToElements()
windows=c.WherePasses(windows).WhereElementIsNotElementType().ToElements()
doors=d.WherePasses(doors).WhereElementIsNotElementType().ToElements()

OUT=rooms, walls, windows, doors

How to choose the hidden and visible elements
#2

Take a look at The Building Coder site, Jeremy talks a little bit about speed and efficiency in this link that may help.

Efficiency Guidelines

  • Filter quick aspects first
  • Filter slow aspects later
  • Do not use .NET or LINQ until after exhausting the built-in filtering techniques

http://thebuildingcoder.typepad.com/blog/2015/12/quick-slow-and-linq-element-filtering.html

and this page

https://forums.autodesk.com/t5/revit-api-forum/quicker-filtering-by-element-name/td-p/5984705

Not sure how much this will help you, but worth taking a look.

Cheers,
Matt


#3

Why not use the ElementMultiCategoryFilter(). Here’s an example:

# Copyright(c) 2017, Konrad K Sobon
# @arch_laboratory, http://archi-lab.net

import clr
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
doc = DocumentManager.Instance.CurrentDBDocument

# Import RevitAPI
clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import *

import System
from System import Array
from System.Collections.Generic import *

import sys
pyt_path = r'C:\Program Files (x86)\IronPython 2.7\Lib'
sys.path.append(pyt_path)

try:
    errorReport = None
    
    cat_list = [BuiltInCategory.OST_Rooms, BuiltInCategory.OST_Walls, BuiltInCategory.OST_Windows, BuiltInCategory.OST_Doors]
    typed_list = List[BuiltInCategory](cat_list)
    filter = ElementMulticategoryFilter(typed_list)
    output = FilteredElementCollector(doc).WherePasses(filter).ToElements()

except:
    # if error occurs anywhere in the process catch it
    import traceback
    errorReport = traceback.format_exc()

# Assign your output to the OUT variable
if None == errorReport:
    OUT = output
else:
    OUT = errorReport

Cheers!


Remove URL parameter from families
#4

@Konrad_K_Sobon, Thank you very much!
to select all items in the active view, i need to replace only one string
output = FilteredElementCollector(**doc,doc.ActiveView.Id**).WherePasses(filter).ToElements()


#7

@til.shviger can you start a new post? This is a new question and it’s unrelated to MultiCategoryFilters.

Cheers!