Saturday, October 19, 2013

Macro that Exports All Sheets to DWG

***UPDATED***
Code has been updated to fix a bug with Sheet Placeholders causing an error.

Jay Zallan asked me if I could help him create a macro that would quickly export all sheets to DWG files for his consultants that only use CAD. Here is the result, a macro that will export all the sheets to DWG into a dated subfolder where the model is located. The DWGs have bound views and shared coordinates.

Macro has also been added to https://bitbucket.org/BoostYourBIM/revit_api_public/

Code:
        public void ExportSheetsToDWG()
        {
            // setup uidoc and doc for accessing the Revit UI (uidoc) and the Model (doc)
            UIDocument uidoc = this.ActiveUIDocument;
            Document doc = uidoc.Document;
           
            // get all the elements in the model database
            FilteredElementCollector collector = new FilteredElementCollector(doc);
            // filter out all elements except Views
            ICollection<Element> collection = collector.OfClass(typeof(ViewSheet)).ToElements();
           
            // create a transaction
            using(Transaction t = new Transaction(doc, "Export Sheets"))
            {
                // start the transaction
                t.Start();
               
                // create a list to hold the sheets
                List<ElementId> sheetsToDWG = new List<ElementId>();
               
                // create DWG export options
                DWGExportOptions dwgOptions = new DWGExportOptions();
                dwgOptions.MergedViews = true;
                dwgOptions.SharedCoords = true;
               
                // add a counter to count the sheets exported
                int x = 0;
            
                // loop through each view in the model
                foreach (Element e in collection)
                {                   
                    try
                    {   
                        ViewSheet viewsheet = e as ViewSheet;
                       
                        // only add sheets to list
                        if (viewsheet.IsPlaceholder == false)
                        {
                            sheetsToDWG.Add(e.Id);
                            x += 1;
                        }
                    }
                    catch
                    {
                    }
                }

                string path = "";
                string file = "";
               
                // get the current date and time
                DateTime dtnow = DateTime.Now;
                string dt = string.Format("{0:yyyyMMdd HHmm}", dtnow);
               
                if (doc.PathName != "")
                {
                    // use model path + date and time
                    path = Path.GetDirectoryName(doc.PathName) + "\\" + dt;                   
                }
                else
                {
                    // model has not been saved
                    // use C:\DWG_Export + date and time
                    path = "C:\\DWG_Export\\" + dt;
                    file = "NONAME";
                }
               
                // create folder
                Directory.CreateDirectory(path);
               
                // export
                doc.Export(path, file, sheetsToDWG, dwgOptions);
               
                TaskDialog.Show("Export Sheets to DWG", x + " sheets exported to:\n" + path);
            }
        }

Thursday, October 17, 2013

Open Source Macros for Revit

Last week several people on Twitter started a discussion of having a way to collect and share Revit macros for the Revit community. Luke (What Revit Wants) suggested that we use an open source repository to collect and manage the macros. Eventually, Harry (BoostYourBim) setup a repository on BitBucket (https://bitbucket.org/BoostYourBIM/revit_api_public/overview).

I have since added a few of my macros and plan to add all the existing ones. I will continue to contribute the macros I create for myself as well for LARUG. Follow Harry's instructions below to get the macros added to your Revit folder from BitBucket. Then whenever there are changes just pull them down to your computer and you will have all the macros in Revit ready to use.

Source:
What Revit Wants: Public Macro Repository for Revit
BoostYourBIM: A public Git repository for sharing Revit Macros at BitBucket

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, October 07, 2013

Macros for Deleting Views and Sheets

These 3 macros were created to solve a task that I do frequently, clean models from other companies. As an MEP firm, we receive several models per project from the architects, structural, and others. We also use Revit Server to enable our teams to work from any office on any project. So in order to reduce time and space, we clean out the models we receive by deleting views, legends, schedules, and sheets. Then we purge and the models range from 30-60% smaller in size. Depending on the source of the model we may or may not delete the sheets and typically for arch models we keep the floor and ceiling plans for linking.

Caution: DO NOT run these in your model unless you want to delete views and sheets


Delete All Sheets And Views
The first macro will delete all views and all sheets except for the currently open view or sheet. This is the ultimate cleaner for models that you only want the model and nothing else.

Code:

public void DeleteAllSheetsAndViews()
{
    // setup uidoc and doc for accessing the Revit UI (uidoc) and the Model (doc)
    UIDocument uidoc = this.ActiveUIDocument;
    Document doc = uidoc.Document;
    
    // get all the elements in the model database
    FilteredElementCollector collector = new FilteredElementCollector(doc);
    // filter out all elements except Views
    ICollection<Element> collection = collector.OfClass(typeof(View)).ToElements();
    
    // create a transaction
    using(Transaction t = new Transaction(doc, "Delete All Sheets and Views"))
    {
        // start the transaction
        t.Start();
        
        // add a counter to count the views/sheets deleted
        int x = 0;
        
        // loop through each view in the model
        foreach (Element e in collection)
        {                    
            try 
            {    
                View view = e as View;
                
                // all views/sheets are deleted and increment counter by 1
                doc.Delete(e);
                x += 1;
            }
            catch(Exception ex)
            {
                // uncomment below to show error messages
                //View view = e as View;
                //TaskDialog.Show("Error", e.Name + "\n" + "\n" + ex.Message);
                //TaskDialog.Show("Error", ex.Message);
            }
        }
        // finalize the transaction
        t.Commit();
        // show message with number of views/sheets deleted
        TaskDialog.Show("DeleteAllSheetsViews", "Views & Sheets Deleted: " + x.ToString());
    }
}


Delete Sheets And Views (except floor & ceiling plans)
The second macro will delete all sheets and all views except for floor and ceiling plans. This is typically the macro I run on arch models towards the end of a project when we no longer to look through their sheets.

Code:

public void DeleteSheetsAndViews()
{
    // setup uidoc and doc for accessing the Revit UI (uidoc) and the Model (doc)
    UIDocument uidoc = this.ActiveUIDocument;
    Document doc = uidoc.Document;
    
    // get all the elements in the model database
    FilteredElementCollector collector = new FilteredElementCollector(doc);
    // filter out all elements except Views
    ICollection<Element> collection = collector.OfClass(typeof(View)).ToElements();
    
    // create a transaction
    using(Transaction t = new Transaction(doc, "Delete Sheets and Views"))
    {
        // start the transaction
        t.Start();
        
        // add a counter to count the views/sheets deleted
        int x = 0;
        
        // loop through each view in the model
        foreach (Element e in collection)
        {                    
            try 
            {    
                View view = e as View;
                
                // determine what type of view it is
                switch(view.ViewType)
                {
                    // if view is a floor plan, don't delete
                    case ViewType.FloorPlan:
                        break;
                    // if view is a ceiling plan, don't delete
                    case ViewType.CeilingPlan:
                        break;
                    // all other views/sheets can be deleted and increment counter by 1
                    default:
                        doc.Delete(e);
                        x += 1;
                        break;
                }
            }
            catch(Exception ex)
            {
                // uncomment below to show error messages
                //View view = e as View;
                //TaskDialog.Show("Error", e.Name + "\n" + "\n" + ex.Message);
                //TaskDialog.Show("Error", ex.Message);
            }
        }
        // finalize the transaction
        t.Commit();
        // show message with number of views/sheets deleted
        TaskDialog.Show("DeleteSheetsViews", "Views & Sheets Deleted: " + x.ToString());
    }
}


Delete Views (except floor and ceiling plans)
The last macro will delete all views except for floor and ceiling plans. It will also not delete the sheets. I use this macro on arch models from the beginning of a project through mid CDs. It allows people on the project to review the arch plans sheets by opening their model.

Code:

public void DeleteViews()
{
    // setup uidoc and doc for accessing the Revit UI (uidoc) and the Model (doc)
    UIDocument uidoc = this.ActiveUIDocument;
    Document doc = uidoc.Document;
    
    // get all the elements in the model database
    FilteredElementCollector collector = new FilteredElementCollector(doc);
    // filter out all elements except Views
    ICollection<Element> collection = collector.OfClass(typeof(View)).ToElements();
    
    // create a transaction
    using(Transaction t = new Transaction(doc, "Delete Views"))
    {
        // start the transaction
        t.Start();
        
        // add a counter to count the views deleted
        int x = 0;
        
        // loop through each view in the model
        foreach (Element e in collection)
        {                    
            try 
            {    
                View view = e as View;
                
                // determine what type of view it is
                switch(view.ViewType)
                {
                    // if view is a floor plan, don't delete
                    case ViewType.FloorPlan:
                        break;
                    // if view is a ceiling plan, don't delete
                    case ViewType.CeilingPlan:
                        break;
                    // if view is a sheet, don't delete
                    case ViewType.DrawingSheet:
                        break;
                    // all other views can be deleted and increment counter by 1
                    default:
                        doc.Delete(e);
                        x += 1;
                        break;
                }
            }
            catch(Exception ex)
            {
                // uncomment below to show error messages
                //View view = e as View;
                //TaskDialog.Show("Error", e.Name + "\n" + "\n" + ex.Message);
                //TaskDialog.Show("Error", ex.Message);
            }
        }
        // finalize the transaction
        t.Commit();
        
        // show message with number of views/sheets deleted
        TaskDialog.Show("DeleteViews", "Views Deleted: " + x.ToString());
    }

}
 

If you have any questions about any of these macros, please leave a comment and I will try to assist.