Retrieving Elements Stored in a SelectionFilterElement

Building on this previous post, here is how to retrieve elements that are stored in a SelectionFilterElement. After getting the elements, the code below shows how to select them in the Revit UI.

public void GetSelectionFilterElements()
    Document doc = this.ActiveUIDocument.Document;
    UIDocument uidoc = new UIDocument(doc);

    // Get the SelectionFilterElement named "My Selection Filter" with this LINQ statement
    SelectionFilterElement filter = (from f in new FilteredElementCollector(doc)
        where f.Name == "My Selection Filter" select f).First();

    // Get ids of elements in the filter
    ICollection<ElementId> filterIds = filter.GetElementIds();

    // Report # of elements to user
    TaskDialog.Show("Elements in filter", filterIds.Count.ToString());
    // ToString() must be used here because Count returns an integer and TaskDialog.Show requires a string
    // This hasn't been needed in other examples because when you concatenate a string with an integer
    // the whole thing automatically becomes a string. So ToString() would not be needed if I did:
    // "Number of elements is " + fitlerIds.Count

    // Now select the elements in "My Selection Filter" in the Revit UI
    // Create a SelElemenSet object
    SelElementSet selSet = SelElementSet.Create();
    foreach (ElementId id in filterIds) // loop through the elements in the filter
        Element e = doc.GetElement(id); // get the element for each id
        selSet.Add(e); // add the element to the SelElementSet
    // Use the SelElementSet to set the elements shown as selected in the Revit UI
    uidoc.Selection.Elements = selSet; 
    // Refresh the active view so that the change in selection higlighting is shown
About these ads

11 thoughts on “Retrieving Elements Stored in a SelectionFilterElement

  1. Hello I got the problem to highlight the elements pls help me: my code is like this:

    FilteredElementCollector c = new FilteredElementCollector(doc).OfClass(typeof(Family));
    FilteredElementCollector familyCollector11 = new FilteredElementCollector(doc);
    List GlobalStrings22 = new List();
    Family family2 = null;
    FamilySymbol symbol4 = null;
    SelElementSet selElements = uidoc.Selection.Elements;
    SelElementSet selSet = SelElementSet.Create();
    foreach (Family f in c)
    family2 = f;

    foreach (FamilySymbol s in family2.Symbols)
    symbol4 = s;
    string ss = s.Name.ToString();

    Element ele = s as Element;
    foreach (Material material in s.Materials)
    if (material.Name.ToString() == Class1.GlobalVar22.ToString())
    { ElementId id = s.Id as ElementId;
    Element e = doc.GetElement(id);

    } break;

    uidoc.Selection.Elements = selSet;

      • elements are not highlighting…… if i click the door in list box the i want to highlight all doors..

        • Here is some code that will highlight all instances of the Double-Flush door family. Can you adapt this to your needs?

          public void highlightDoor()
          UIDocument uidoc = this.ActiveUIDocument;
          Document doc = uidoc.Document;
          IList elementList = new FilteredElementCollector(doc).OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_Doors).Cast().Where(e => e.Symbol.Family.Name == “Double-Flush”).ToList();
          SelElementSet selSet = SelElementSet.Create();
          foreach (FamilyInstance e in elementList)
          uidoc.Selection.Elements = selSet;

  2. thank u for reply :)
    IList elementList = new FilteredElementCollector(doc).OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_Doors).Cast().Where(e => e.Symbol.Family.Name
    this statement giving some error like tis
    Error 2 ‘Autodesk.Revit.DB.FilteredElementCollector’ does not contain a definition for ‘Cast’ and no extension method ‘Cast’ accepting a first argument of type ‘Autodesk.Revit.DB.FilteredElementCollector’ could be found (are you missing a using directive or an assembly reference?) C:\Users\p\Documents\Visual Studio 2010\Projects\door\door\Class1.cs 174 43 door

  3. i got the output the code like tis :FilteredElementCollector collector= new FilteredElementCollector(doc).WhereElementIsNotElementType() .WhereElementIsViewIndependent() .OfClass(typeof(FamilyInstance)) ;

    thank you so much :)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s