Thursday, October 10, 2013

Macros for Deleting all MEP Spaces and Rooms

Another task I do frequently is deleting all the spaces in my MEP model so that I can recreate them all based on the architect's rooms. Why delete them all? Sometimes its just easier to delete them all than to search out which rooms have been changed, deleted, or added. This macro prevents me from needing to delete the spaces from a schedule, since Revit doesn't delete allow deleting spaces from a plan view, only schedules.

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());
        
    }
                
}

1 comment:

  1. Thank you for this useful code. I've already put it into use.
    For 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!

    ReplyDelete