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.

9 comments:

  1. Hi,

    I want to keep one sheet ,is it possible with this code.
    Sheet name some thing like "sheet301-3D"

    Regards
    Vk

    ReplyDelete
    Replies
    1. You could use an if statement to skip the sheet from deletion. Just check for the view name or view sheet number.

      Delete
  2. Thanks for your reply, If you don't mind can give me one small example for this.

    ReplyDelete
  3. Hi Troy,
    (You probably hear this all the time, but) I'm REALLY new to macros and I was trying to build your application "Delete Views except Floor and Ceiling Plans" macro, but it keeps giving me errors. I am using Revit 2015. The first build resulted in having to add two (})brackets at the end of the code. Once I tried to build it again, those errors went away and 3 additional errors resulted. Two errors in line 60 and one in line 29.

    The first error states "Argument 1:cannot convert from 'Autodesk.Revit.DB.Element' to 'System.Collections.Generic.ICollection (CS1503)."

    The Second error states: "The best overloaded method match for'Autodesk.Revit.DBDocument.Delete)System.Collections.Generic.ICollection' has some invalid arguments(CS1502)"

    Finally, the third error states: "MacroName.ThisApplication' does not contain a definition for 'InternalStartup' and no extension method 'InternalStartup' accept in a first argument of type 'MacroName.ThisApplicaiton' could be found (are you missing a using directive or a)"

    Could you please help me fix this? I think this macro would be an invaluable time saver if I can get it to work.

    Thank you for all your awesome work!!!

    ReplyDelete
  4. I'm new in the Revit API. When entering this code, I keep getting the following errors when I build solution:

    "Argument 1: cannot convert from 'Autodesk.Revit.DB.Element' to 'System.Collections.Generic.ICollection' (CS1503)"

    The best overloaded method match for 'Autodesk.Revit.DB.Document.Delete(System.Collections.Generic.ICollection)' has some invalid arguments (CS1502)

    Any thoughts? Thanks!

    ReplyDelete
  5. Anonymous7:22 PM

    as I do or where in stick my macros to run ????

    ReplyDelete
  6. Anonymous8:57 AM

    Hi there Can you please help with this error. I am using a Revit Architecture 2014 version.

    Thanks.

    'CLEANUPVIEWS.ThisDocument' does not contain a definition for 'activeUIDocument' and no extension method 'activeUIDocument' accepting a first argument of type 'CLEANUPVIEWS.ThisDocument' could be found (are you missing a using directive or an assembly reference?) (CS1061) - C:\Users\5\AppData\Local\Temp\{423CB890-4CF4-4645-BB8B-D5D143515556}\Revit\DocHookups15412\284433664\CLEANUPVIEWS\Source\CLEANUPVIEWS\ThisDocument.cs:42,26

    ReplyDelete
  7. Anonymous8:58 AM

    Hi I keep getting this error can you please help me out.
    thanks.

    'CLEANUPVIEWS.ThisDocument' does not contain a definition for 'activeUIDocument' and no extension method 'activeUIDocument' accepting a first argument of type 'CLEANUPVIEWS.ThisDocument' could be found (are you missing a using directive or an assembly reference?) (CS1061)

    ReplyDelete