選択を変更する

選択を変更する

選択した要素を修正するには:

  1. ElementIds の新しいリストを作成します。
  2. ElementIds を入れます。
  3. 新しいリストで SetElementIds()を呼び出します。

次の例では、現在の選択を取得し、壁のみをフィルタして新しい選択として設定することで、選択した要素を変更する方法を示しています。

コード領域 7-1: 選択した要素を変更

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!");
    }
}}