Get All Elements of Category at Documents list with Python

I a trying to make this python script to make it work with a category list, because it only works with a single list.

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

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager

def ElementsByCategory(bic, doc):
	collector = FilteredElementCollector(doc).OfCategory(bic).WhereElementIsNotElementType()
	return collector.ToElements()

inputdoc = UnwrapElement(IN[2])
if not inputdoc: doc = DocumentManager.Instance.CurrentDBDocument
elif inputdoc.GetType().ToString() == "Autodesk.Revit.DB.RevitLinkInstance": doc = inputdoc.GetLinkDocument()
elif inputdoc.GetType().ToString() == "Autodesk.Revit.DB.Document": doc = inputdoc
else: doc = DocumentManager.Instance.CurrentDBDocument

if isinstance(IN[0], list): OUT = [ElementsByCategory(x, doc) for x in IN[0]]
else: OUT = ElementsByCategory(IN[0], doc)

Try with “for A in B” of Python

@ruben.romero The code works fine for me.

Can you share a screenshot where it fails for you?

I am looking to do it from a document list, not only the current opened document

@ruben.romero This should do the trick:

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

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager

toList = lambda x : x if hasattr(x, '__iter__') else [x]

def ElementsByCategory(bic, doc):
	collector = FilteredElementCollector(doc).OfCategory(bic).WhereElementIsNotElementType()
	return collector.ToElements()

inputdocs = toList(UnwrapElement(IN[1]))
documents = []
if inputdocs == [None]:
	documents.append(DocumentManager.Instance.CurrentDBDocument)
else:
	for inputdoc in inputdocs:
		if inputdoc.GetType().ToString() == "Autodesk.Revit.DB.RevitLinkInstance": documents.append(inputdoc.GetLinkDocument())
		elif inputdoc.GetType().ToString() == "Autodesk.Revit.DB.Document": documents.append(inputdoc)
		else: documents.append(DocumentManager.Instance.CurrentDBDocument)

OUT = []

for document in documents:
	if isinstance(IN[0], list): OUT.append([ElementsByCategory(x, document) for x in IN[0]])
	else: OUT.append(ElementsByCategory(IN[0], document))
1 Like