PythonScript to filter only ducts of selection

hello everybody.

I’m having a problem with my PythonScript.
It works with 4 conditions

Total collection of Ducts.
Partial collection of Ducts.
Total collection of Generic Models
Partial collection of Generic Models

everything works fine, except the partial collection of ducts
it returns “empty list”

I’ve already tried about 10 different approaches and got no results.
Does anyone have any ideas to help?

import clr

# Add references to interact with Revit
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import FilteredElementCollector, BuiltInCategory

# Add references to use Dynamo with Revit
clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager

# Inputs
Furo = IN[0]  # Determines whether to collect from Generic Model or Ducts
Total = IN[1]  # Determines if the collection is total or partial
Selecao = IN[2]  # Selected elements

# List to store the selected elements
selected_elements = []

# Access the document and the current view
doc = DocumentManager.Instance.CurrentDBDocument
active_view = doc.ActiveView

# Check the value of Furo and Total
if Furo:  # Ducts
    if Total:  # Total collection
        ducts = FilteredElementCollector(doc, active_view.Id).OfCategory(BuiltInCategory.OST_DuctCurves).WhereElementIsViewIndependent().ToElements()
        selected_elements.extend(ducts)
    else:  # Partial collection
        unwrapped_elements = [UnwrapElement(elem) for elem in Selecao]
        for element in unwrapped_elements:
			if hasattr(element, "Category") and element.Category == doc.Settings.Categories.get_Item(BuiltInCategory.OST_DuctCurves):
			    selected_elements.append(element)

else:  # Generic Model
    if Total:  # Total collection
        generic_models = FilteredElementCollector(doc, active_view.Id).OfCategory(BuiltInCategory.OST_GenericModel).WhereElementIsViewIndependent().ToElements()
        for element in generic_models:
            if element.Name.startswith("Furo"):
                selected_elements.append(element)
    else:  #
        unwrapped_elements = [UnwrapElement(elem) for elem in Selecao]
        for element in unwrapped_elements:
            if hasattr(element, "Name") and element.Name.startswith("Furo"):
                selected_elements.append(element)

OUT = selected_elements

Hi,

instead

if hasattr(element, "Category") and element.Category == doc.Settings.Categories.get_Item(BuiltInCategory.OST_DuctCurves):

try

if hasattr(element, "Category") and element.Category.Id == ElementId(BuiltInCategory.OST_DuctCurves):

1 Like

Great, this solution was accurate and resolved it.
Thanks @c.poupin

1 Like