[c#]代码库
ElementSet collection = m_doc.Selection.Elements;
// check user selection
if (collection.Size < 1)
{
MessageBox.Show("Please select an object to delete.", "DeleteObject");
return;
}
bool error = true;
try
{
error = true;
// delete selection
IEnumerator e = collection.GetEnumerator();
bool MoreValue = e.MoveNext();
while (MoreValue)
{
Element component = e.Current as Element;
m_doc.Document.Delete(component);
MoreValue = e.MoveNext();
}
error = false;
}
catch
{
// if revit threw an exception, try to catch it
foreach (Element c in collection)
{
m_doc.Selection.Elements.Insert(c);
}
MessageBox.Show("Element(s) can't be deleted.", "DeleteObject");
return;
}
finally
{
// if revit threw an exception, display error and return failed
if (error)
{
MessageBox.Show("Deletion failed.");
}
}