How to use UnionWith

I was trying to combine two FilteredElementcollector with the method of UnionWith. But it is not working. what may the error in my code?

import clr
import sys
sys.path.append('C:\Program Files (x86)\IronPython 2.7\Lib')
import System
from System import Array
from System.Collections.Generic import *
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager 
from RevitServices.Transactions import TransactionManager 

clr.AddReference("RevitAPI")
clr.AddReference("RevitAPIUI")

import Autodesk 
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI import *
from Autodesk.Revit.DB import Mechanical
from Autodesk.Revit.DB.Mechanical import MechanicalSystemType
doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication 
app = uiapp.Application 
uidoc = uiapp.ActiveUIDocument

#######OK NOW YOU CAN CODE########
vid = uidoc.ActiveView.Id


collector = FilteredElementCollector(doc,vid).OfCategory(BuiltInCategory.OST_DuctCurves).WhereElementIsNotElementType().ToElements()
collectorfitting = FilteredElementCollector(doc,vid).OfCategory(BuiltInCategory.OST_DuctFitting).WhereElementIsNotElementType().ToElements()
union = collector.UnionWith(collectorfitting)

OUT = union


@SHIBUJOSE ,

at my knowlage you have to collect (with append) your elements…

like in this example… you can recreate your code

import sys
import clr
# import RevitNodes
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
# import Revit Services 
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
# import Revit API
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *
# import system.
import System
from System.Collections.Generic import *
# get the current Revit document. 
doc = DocumentManager.Instance.CurrentDBDocument

###  -----------  FilterElementCollector --------------###
#All walls in project
collector = FilteredElementCollector(doc)
wallElements = collector.OfClass(Wall).ToElements()

###  -----------  FilterElementCollector Active View --------------###
#All walls in active view
ActiveView = FilteredElementCollector(doc, doc.ActiveView.Id).OfClass(Wall).ToElements()

###  -----------  FilterElementCollector search for 48" walls --------------###
#collect all walls in Revit then search for SW48
builtInCategory = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Walls).WhereElementIsNotElementType().ToElements()

wallList = []
for w in builtInCategory:
    if w.Name.Equals("SW48"):
        wallList.append(w)

###  -----------  FilterElementCollector lambda expression --------------###
#lambda expression
walls = list(filter(lambda w : w.Name.Equals("SW48"), builtInCategory))

###  -----------  FilterElementCollector using a filter --------------###
#Using a slow filter to get items. 
builtInCat = List[BuiltInCategory]()
builtInCat.Add(BuiltInCategory.OST_Doors)
builtInCat.Add(BuiltInCategory.OST_Walls)

filter = ElementMulticategoryFilter(builtInCat)

elements = FilteredElementCollector(doc).WherePasses(filter).ToElements()

OUT = wallElements

grafik

understood. what is the use of UnionWith?

@SHIBUJOSE ,

i don´t know how to access this comment… at API docs there are still no examples :confused:

1 Like

Can you post the dynamo results showing a collector that gets one duct, a collectorfitting that gets one fitting, and the union?

error is as below
image

collector = FilteredElementCollector(doc,vid).OfCategory(BuiltInCategory.OST_DuctCurves).WhereElementIsNotElementType().ToElements()
collectorfitting = FilteredElementCollector(doc,vid).OfCategory(BuiltInCategory.OST_DuctFitting).WhereElementIsNotElementType().ToElements()
union = FilteredElementCollector.UnionWith(collectorfitting)

some hints here:

https://spiderinnet.typepad.com/blog/2011/06/revit-api-and-vbnet-some-elementquickfilter-cases-in-vbnet.html

It appears that you’re calling the class (FilteredElementCollector), and then it’s UnionWith method, as opposed to calling the UnionWith method on the previously created object (which is of the filtered element collector class).

Try collector.UnionWith(collectorFitting) as an alternative.

I tried that too. Unfortunately that also not working.

collector = FilteredElementCollector(doc,vid).OfCategory(BuiltInCategory.OST_DuctCurves).WhereElementIsNotElementType().ToElements()
collectorfitting = FilteredElementCollector(doc,vid).OfCategory(BuiltInCategory.OST_DuctFitting).WhereElementIsNotElementType().ToElements()
#union = FilteredElementCollector.UnionWith(collectorfitting)
un = collector.UnionWith(collectorfitting)

did you read?
https://spiderinnet.typepad.com/blog/2011/06/revit-api-and-vbnet-some-elementquickfilter-cases-in-vbnet.html

You need to declare FilteredElementCollector first, not write everything together

1 Like