Multiple Collectors

Multiple Collectors

floretti
Advocate Advocate
1,113 Views
3 Replies
Message 1 of 4

Multiple Collectors

floretti
Advocate
Advocate

Apologies if this duplicates any previous discussions but I've done quite a few searches and couldn't find the answer.

 

I noticed if I create multiple collectors in the same script they don't work properly and most likely end up empty. I've tried to use Dispose() before creating the second collector to see if it can sort of "reset" the collector but I always get the error below. What is it that I'm missing?

Exception : Autodesk.Revit.Exceptions.InvalidObjectException: The managed object is not valid.

 

Below is a simple example where I collect all shared parameters in a project first so I can use the GUIDs to collect data from them in families.

collector = FilteredElementCollector(doc)

sharedPars = collector.OfClass(SharedParameterElement)
# Find GUID of desired shared parameters

families = collector.OfClass(FamilyInstance).WhereElementIsViewIndependent()
# Collect data from families based on parameter GUID.

 

0 Likes
Accepted solutions (1)
1,114 Views
3 Replies
Replies (3)
Message 2 of 4

jeremy_tammik
Alumni
Alumni
Accepted solution

Creating multiple collectors is no problem at all.

  

Applying several different filters to one single collector does exactly what it should:

  

Every single filter is applied to the collector results.

  

If the filters are mutually exclusive, you end up with an empty result.

  

For a more detailed explanation, please read here:

  

https://thebuildingcoder.typepad.com/blog/2019/11/design-automation-api-stacks-collectors-and-links....

  

Also reiterated here:

   

https://thebuildingcoder.typepad.com/blog/2021/01/sheet-view-xform-coords-img-export-and-title-block...

   

In your sample below, simply create two separate collectors for shared parameters and family instances.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 4

floretti
Advocate
Advocate

Hi Jeremy, first of all I appreciate the reply and support.

 

I definitely understand what I did wrong now, I can't reuse the same collector variable as the filters just pile up, which obviously causes the collector to be empty, so simple.

 

It does worry me that after so much research I couldn't find the answer anywhere.

0 Likes
Message 4 of 4

jeremy_tammik
Alumni
Alumni

Thank you for your appreciation. Happy to hear that the problem is solved and the solution clear and simple,.

 

I'll spell it out in the blog again and hope that will be easier to find in case anyone runs into this again in the future.

 

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes