Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

FilteredElementCollector -> get all instances except of category X

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
jnoordzij
3547 Views, 5 Replies

FilteredElementCollector -> get all instances except of category X

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?

5 REPLIES 5
Message 2 of 6
jnoordzij
in reply to: jnoordzij

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!

Message 3 of 6
jnoordzij
in reply to: jnoordzij

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
in reply to: jnoordzij

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();
Message 5 of 6
naveen.kumar.t
in reply to: jnoordzij

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
in reply to: naveen.kumar.t

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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Customer Advisory Groups


Rail Community