Delete All Spaces
Code
public void DeleteAllSpaces()
{
// setup uidoc and doc for accessing the Revit UI (uidoc) and the Model (doc)
UIDocument uidoc = this.ActiveUIDocument;
Document doc = uidoc.Document;
// get all elements in the model
FilteredElementCollector collector = new FilteredElementCollector(doc);
// filter out everything but spaces
ICollection<Element> collection = collector.OfClass(typeof(SpatialElement)).OfCategory(BuiltInCategory.OST_MEPSpaces).ToElements();
// create and start a new transaction
using(Transaction t = new Transaction(doc, "Delete All Spaces"))
{
t.Start();
// add a counter to count the spaces deleted
int x = 0;
// loop through each space in the model
foreach (Element e in collection)
{
try
{
// delete the space and increment counter by 1
doc.Delete(e);
x += 1;
}
catch(Exception ex)
{
}
}
// finalize the transaction
t.Commit();
// show a dialog with the number of spaces deleted
TaskDialog.Show("DeleteSpaces", "Spaces Deleted: " + x.ToString());
}
}
Delete All Rooms
After showing this macro at LARUG, people asked if I could do the same for deleting all rooms. So here is a modified version.
Code
public void DeleteAllRooms()
{
// setup uidoc and doc for accessing the Revit UI (uidoc) and the Model (doc)
UIDocument uidoc = this.ActiveUIDocument;
Document doc = uidoc.Document;
// get all elements in the model
FilteredElementCollector collector = new FilteredElementCollector(doc);
// filter out everything but rooms
ICollection<Element> collection = collector.OfClass(typeof(SpatialElement)).OfCategory(BuiltInCategory.OST_Rooms).ToElements();
// create and start a new transaction
using(Transaction t = new Transaction(doc, "Delete All Rooms"))
{
t.Start();
// add a counter to count the rooms deleted
int x = 0;
// loop through each room in the model
foreach (Element e in collection)
{
try
{
// delete the room and increment counter by 1
doc.Delete(e);
x += 1;
}
catch(Exception ex)
{
}
}
// finalize the transaction
t.Commit();
// show a dialog with the number of rooms deleted
TaskDialog.Show("DeleteRooms", "Rooms Deleted: " + x.ToString());
}
}
Thank you for this useful code. I've already put it into use.
ReplyDeleteFor Revit 2014, I found that there were some minor changes needed:
Element(s) needed to be replaced with ElementId(s), and the catch variable caused trouble when called out as ex (catch(Exception ex){} failed, but catch(Exception){} worked.
Also, great class at Minn-U!