Changing the Selection

Changing the Selection

To modify the Selection.Elements:

  1. Create a new SelElementSet.
  2. Put Elements in it.
  3. Set the Selection.Elements to the new SelElementSet instance.

The following example illustrates how to change the selected Elements.

Code Region 7-1: Changing selected elements

private void ChangeSelection(Document document)
{
        // Get selected elements form current document.
        UIDocument uidoc = new UIDocument(document);
        Autodesk.Revit.UI.Selection.SelElementSet collection = uidoc.Selection.Elements;
 
        // Display current number of selected elements
        TaskDialog.Show("Revit","Number of selected elements: " + collection.Size.ToString());
        
        //Create a new SelElementSet
        SelElementSet newSelectedElementSet = SelElementSet.Create();
 
        // Add wall into the created element set.
        foreach (Autodesk.Revit.DB.Element elements in collection)
        {
                if (elements is Wall)
                {
                        newSelectedElementSet.Add(elements);
                }
        }

        // Set the created element set as current select element set.
        uidoc.Selection.Elements = newSelectedElementSet;

        // Give the user some information.
        if (0 != newSelectedElementSet.Size)
        {
                TaskDialog.Show("Revit",uidoc.Selection.Elements.Size.ToString() + 
                        " Walls are selected!");
        }
        else
        {
                TaskDialog.Show("Revit","No Walls have been selected!");
        }
}