要素を削除する

要素を削除する

Revit プラットフォーム API には、プロジェクト内の 1 つまたは複数の要素を削除するための Delete()メソッドがあります。

表 23: メンバーを削除

メンバー

説明

Delete(ElementId)

要素 ID を使用して、プロジェクトから要素を削除します

Delete(ICollection<ElementId>)

ID を使用して、プロジェクトから複数の要素を削除します。

例にあるように、最初のメソッドは ID に基づいて単一の要素を削除します。

コード領域: ElementId に基づいて要素を削除

private void DeleteElement(Autodesk.Revit.DB.Document document, Element element)
{
        // Delete an element via its id
        Autodesk.Revit.DB.ElementId elementId = element.Id;
        ICollection<Autodesk.Revit.DB.ElementId> deletedIdSet = document.Delete(elementId);

        if (0 == deletedIdSet.Count)
        {
                throw new Exception("Deleting the selected element in Revit failed.");
        }

        String prompt = "The selected element has been removed and ";
        prompt += deletedIdSet.Count - 1;
        prompt += " more dependent elements have also been removed.";

        // Give the user some information
        TaskDialog.Show("Revit", prompt);
}
注: 上記の例にあるように、要素を削除すると、その要素に関連付けられている子要素も削除されます。

API には複数の要素を削除する方法も用意されています。

コード領域: ID に基づいて複数の要素を削除

// Delete all the selected elements via the set of element ids.
ICollection<Autodesk.Revit.DB.ElementId> idSelection = null ;
UIDocument uidoc = new UIDocument(document);
foreach (Autodesk.Revit.DB.Element elem in uidoc.Selection.Elements)
{
        Autodesk.Revit.DB.ElementId id = elem.Id;
        idSelection.Add(id);
}

ICollection<Autodesk.Revit.DB.ElementId> deletedIdSet = document.Delete(idSelection);

if (0 == deletedIdSet.Count)
{
        throw new Exception("Deleting the selected elements in Revit failed.");
}

TaskDialog.Show("Revit","The selected element has been removed.");
注: 要素を削除した後は、削除された要素への参照は無効となり、アクセスすると例外が発生します。