Boost Your BIM – making Revit even better

December 11, 2012

Save a Set of Elements with SelectionFilterElement

Filed under: Selection — Harry Mattison @ 10:25 am

Here’s another great question from a reader! Is it possible to save a selection, with a name, and call upon it later?

Yes, it is.

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

    // Prompt user to pick one or more elements
    IList<Reference> refList = uidoc.Selection.PickObjects(ObjectType.Element, "Pick elements to add to selection filter");

    // Create a transaction
    using (Transaction t = new Transaction(doc,"Create & Add To Selection Filter"))
        t.Start(); // Start the transaction

        // Create a SelectionFilterElement
        SelectionFilterElement selFilter = SelectionFilterElement.Create(doc,"My Selection Filter");

        foreach (Reference r in refList) // Iterate through list of selected elements
            // Add the ElementId of each selected element to the selection filter

        // Commit the transaction

After the filter is created it can be seen in the Filters dialog. It cannot be applied to a view but it can be used in other ways which is a a good topic for a future posts.


P.S. If you try to run this macro twice, it will fail the 2nd time because the filter names must be unique. The IsNameUnique method could be used to check for this case.

About these ads


  1. I think I am going to be the code police for this one :-)
    I totally understand and agree with that snippets published here are not intended to be (and should not be) up to production level. Typically, a lot of validations and error handling would have to be added, which is not at all necessity to show in the snippets presented herein. However, with just this particular code around object picking, I think it should be mentioned that if the end-user escapes the picking mode (by ether hitting the Esc key or the cancel button in Revit), the Revit API will throw an exception as a way of notifying the API programmer. For that reason the OperationCancelledException should probably be always handled in code that invokes picking.

    And while I am on the subject, the transaction would typically not be started (or even instantiated) until after the picking is completed, mainly for the reason I have just mentioned above. (Naturally, the creation of the selection filter would have to be postponed too.)

    Comment by Arnošt Löbel — December 11, 2012 @ 12:28 pm

    • Hi Policeman Lobel,

      Isn’t it enough that I am using the ugly “transaction using block” code that you suggested :)
      Seriously, you are right that the selection does not belong in the transaction, so I have moved it accordingly. But I disagree that I should include code to handle the OperationCancelledException every time I show an example with PickObject or PickObjects. I will write a separate post on the subject and hope that suffices.

      Comment by harrymattison — December 11, 2012 @ 12:41 pm

  2. The transaction in this sample is fine the way it is, at least for demonstration purposes. The “using” block ensures the transaction will be rolled black and closed in case of an exception. I too do agree with you that you should not bother with exception handling in the code snippets you provide in this blog. (By the way, the snippets are great! A seriously good job!) However, the OperationCancelledException deserves an exception from that rule, in my opinion. It is so because escaping the picking mode is so very likely that handling of that particular exception should always be part of object picking operation. I do not think you need to change the snippet though; I just thought it should be noted at least as a comment.

    Comment by Arnošt Löbel — December 12, 2012 @ 5:40 pm

RSS feed for comments on this post. TrackBack URI

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

The WordPress Classic Theme Blog at


Get every new post delivered to your Inbox.

Join 972 other followers