Search code examples
c#revit-apirevit

How to select multiple elements in a Revit drawing


  • I'm trying to create a "Multi-Select" method in Revit (for 2016/2017) where the users can select specific parameters of instances contained within a drawing (like Nominal Diameter, Pipe Type and so on), and it will select all the instances within the drawing based on their selections. First, screen shot:

enter image description here

Let's predicate this with the fact that this window is created dynamically based on contents of the drawing. Nothing gets put in this window unless there is an element in the drawing that contains/meets one or more of these parameters.

So, ideally, when I Click the DO IT! button, I would like it to select all of the elements in the drawing that have meet any of these parameters. I can filter through this window and find all of my selections - now I just don't know what do with the selections.

I've looked through the Revit.chm and source and found the Selection namespace and class. There are functions like:

PickObject(ObjectType objectType);

that seem like they would be what I want, but I don't know if it's REALLY what I need. Furthermore, if that IS, in fact, what I'm looking for, I don't know the syntax of how to use it.

A little code:

I have a method that collects all of the users' selections:

private List<CheckBox> GetUserFilterPrefs()
{
    //CYCLES THROUGH ALL THE PANELS AND BOXES IN THE WINDOW

    return lstCheckBox;
}

Now I want to create my EventHandler for btnDoIt_Click... I started it, but I'm walking in the dark on this part.

private void btnDoIt_Click(object sender, RoutedEventArgs e)
{
    int itr = 0;

    GetUserFilterPrefs();
    List<Reference> lstRefs = new List<Reference>();

    foreach (CheckBox cb in lstCheckBox)
    {
        if (lstElts[itr].Name == cb.Name)
        {
             //HOW DO I SELECT ALL ITEMS LIKE THE GIVEN ELEMENT
             //THAT ARE RELATED TO THE CHECKBOX SELECTION??
        }

         itr +=1;
    } 

I'll obviously keep looking around; but if anyone knows a way, or can point me in the right direction, it would be massively helpful!

THANKS!!!


Solution

  • The PickObject function that you've found is one that asks the user to select an object in the model. Based on your description, that is not what you are looking for.

    The function you need is:

    SetElementIds(ICollection<ElementId> elementIds)
    

    It is also part of the Selection class. This will highlight the desired elements in the model. To clear the selection in the model, pass an empty List as your argument. Passing null will cause an exception to be thrown.

    To focus on the elements, you need the function:

    UIDocument.ShowElements
    

    There are a number of overloads for this function. Note that none if the elements are in the currently open view, Revit will attempt to find the best view for you, a task that it generally performs very poorly if there are many views in the model.