Changing the Selection

Changing the Selection

To modify the selected elements:

  1. Create a new List of ElementIds.
  2. Put ElementIds in it.
  3. Call SetElementIds() with the new list.

The following example illustrates how to change the selected Elements by getting the current selection and filtering out just walls to set as the new selection.

Code Region 7-1: Changing selected elements

private void ChangeSelection(UIDocument uidoc)
{
    // Get selected elements from current document.
    ICollection<ElementId> selectedIds = uidoc.Selection.GetElementIds();
  
    // Display current number of selected elements
    TaskDialog.Show("Revit", "Number of selected elements: " + selectedIds.Count.ToString());

    // Go through the selected items and filter out walls only.
    ICollection<ElementId> selectedWallIds = new List<ElementId>();

    foreach (ElementId id in selectedIds)
    {
        Element elements = uidoc.Document.GetElement(id);
        if (elements is Wall)
        {
            selectedWallIds.Add(id);
        }
    }

    // Set the created element set as current select element set.
    uidoc.Selection.SetElementIds(selectedWallIds);

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