Showing posts with label mep. Show all posts
Showing posts with label mep. Show all posts

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

Monday, September 30, 2013

Creating Macros (LARUG Sept 2013)

At the LARUG Sept 2013 meeting (video here: http://losangelesrevitusersgroup.blogspot.com/2013/09/larug-september-2013-macro-creation-in.html) I presented on Revit Macros. As promised I would do a follow up of the process, links to tools, and the macros themselves. In this post I will give you everything to download and I will follow up with separate posts about each macro over the next few days.

Here are the downloads and links:

Revit Software Development kits
Revit Development Blogs
Revit Macro/Programming Tools
Macros (Individual posts to follow)
* Disclaimer: I am not an expert in programming, so the macros below may or may not work and I hold no responsibility to any problems they may cause your projects.
* Disclaimer #2: Fill free to use and edit these macros, but please give credit if you republish them.

* I included install instructions in the Revit Lookup zip files and the LARUG zip file. If you have questions on how to install these, leave a comment below.