FilteredElementCollector -> get all instances except of category X

FilteredElementCollector -> get all instances except of category X

jnoordzij
Advocate Advocate
4,457 Views
5 Replies
Message 1 of 6

FilteredElementCollector -> get all instances except of category X

jnoordzij
Advocate
Advocate

I am filtering for FamilInstances in a doc using FilteredElementCollector. 

I want to get all family instances, except the titleblocks. 

 

public ICollection<Element> CollectFamilyInstances(Document doc)
        {
            FilteredElementCollector collector
                = new FilteredElementCollector(doc);

            ICollection<Element> collection
              = collector.OfClass(typeof(FamilyInstance))
                .ToElements();

            return collection;
        }

How can i apply a filter over this collection so that instances of category OST_TitleBlocks are omitted from this collection?

0 Likes
Accepted solutions (2)
4,458 Views
5 Replies
Replies (5)
Message 2 of 6

jnoordzij
Advocate
Advocate

I do this now but there has to be a cleaner way...

 

public ICollection<Element> CollectFamilyInstances(Document doc)
        {
            FilteredElementCollector collector
                = new FilteredElementCollector(doc);

            ICollection<Element> collection
              = collector.OfClass(typeof(FamilyInstance))
                .ToElements();

            ICollection<Element> collection2 = new List<Element>();

            foreach (Element e in collection)
            {
                if (!e.Category.Equals(BuiltInCategory.OST_TitleBlocks))
                {
                    collection2.Add(e);
                }
            }
            
            return collection2;
        }

 

thanks for any extra advice on this!

0 Likes
Message 3 of 6

jnoordzij
Advocate
Advocate
Accepted solution

Got it to work!

 

public ICollection<Element> CollectFamilyInstances(Document doc)
        {
            FilteredElementCollector collector
                = new FilteredElementCollector(doc);

            ElementCategoryFilter fi = new ElementCategoryFilter(BuiltInCategory.OST_TitleBlocks, true);

            ICollection<Element> collection
              = collector.OfClass(typeof(FamilyInstance)).WherePasses(fi)
                .ToElements();

            return collection;
        }

 

 

Message 4 of 6

Anonymous
Not applicable

Not tested, but maybe using quick filter is what you are looking for (if you want just titleblocks category):

ElementCategoryFilter titleblockfilter = new ElementCategoryFilter(BuiltInCategory.OST_TitleBlocks);
ICollection<Element> collection
              = collector.OfClass(typeof(FamilyInstance))
                .WherePasses(titleblockfilter)
                .ToElements();
0 Likes
Message 5 of 6

naveen.kumar.t
Autodesk Support
Autodesk Support

HI @jnoordzij 

ExclusionFilter Class helps you to filter elements by excluding particular elements.

Exclusion Filter Class

 

FilteredElementCollector Collector = new FilteredElementCollector(doc).OfClass(typeof(FamilyInstance)).WhereElementIsNotElementType();
IList<ElementId> eids = Collector.ToElementIds() as IList<ElementId>;
ICollection<ElementId> exclude_elements = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_TitleBlocks).WhereElementIsNotElementType().ToElementIds() as ICollection<ElementId>; ExclusionFilter EF = new ExclusionFilter(exclude_elements); IList<ElementId> elementsAfterExclusion = Collector.WherePasses(EF).OfClass(typeof(FamilyInstance)).ToElementIds() as IList<ElementId>;

If this helped solve your problem please mark it as solution, so other users can get this solutions as wellSmiley Happy

 

 


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

Message 6 of 6

jnoordzij
Advocate
Advocate
Accepted solution

In conclusion of this thread: 

 

I haven't checked the solution provided by @naveen.kumar.t , since I found a working solution beforehand, but that solution might also work. 

 

My own solution was that the ElementCategoryFilter has an additional boolean option that provides the option to match all elements which are NOT of the given category. So an "invert" boolean. Hence the (invert=) "true" at the end of 

 

ElementCategoryFilter fi = new ElementCategoryFilter(BuiltInCategory.OST_TitleBlocks, true);

 

2019 05 02 - 01.JPG

0 Likes