Boost Your BIM – making Revit even better

December 15, 2012

Retrieving Elements Stored in a SelectionFilterElement

Filed under: LINQ, Selection — Harry Mattison @ 10:45 am

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.
beforeAfter

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)
        .OfClass(typeof(SelectionFilterElement))
        .Cast<SelectionFilterElement>()
        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
    uidoc.RefreshActiveView();
}
About these ads

11 Comments »

  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);
    familyCollector.OfClass(typeof(FamilySymbol));
    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);
    selSet.Add(e);
    GlobalStrings22.Add(material.Name.ToString());
    }

    } break;
    }
    }

    uidoc.Selection.Elements = selSet;
    uidoc.RefreshActiveView();

    Comment by sachin — April 9, 2013 @ 5:50 am

    • Hi

      What is the problem you are having with this code?

      Comment by harrymattison — April 9, 2013 @ 5:55 am

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

        Comment by sachin — April 9, 2013 @ 7:40 am

        • 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)
          {
          selSet.Add(e);
          }
          uidoc.Selection.Elements = selSet;
          uidoc.RefreshActiveView();
          }

          Comment by harrymattison — April 9, 2013 @ 8:55 am

  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

    Comment by sachin — April 9, 2013 @ 9:08 am

    • Add this to the section of using statements near the top of your file

      using System.Linq;

      Comment by harrymattison — April 9, 2013 @ 9:19 am

      • hi i cant highlight the wall….plz tell me how to highlights walls

        Comment by sachin — April 19, 2013 @ 3:58 am

        • Could you provide a sample of your code that is not having the desired effect?

          Comment by harrymattison — April 19, 2013 @ 6:29 am

  3. i add tat also tat was giving same error

    Comment by sachin — April 10, 2013 @ 1:16 am

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

    thank you so much :)

    Comment by sachin — April 10, 2013 @ 6:09 am

  5. hi i cant highlight the wall….plz tell me how to highlights walls

    Comment by sachin — April 17, 2013 @ 1:16 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

The WordPress Classic Theme Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 967 other followers