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.
Hi,
ReplyDeleteI want to keep one sheet ,is it possible with this code.
Sheet name some thing like "sheet301-3D"
Regards
Vk
You could use an if statement to skip the sheet from deletion. Just check for the view name or view sheet number.
DeleteThanks for your code
ReplyDeleteThanks for your reply, If you don't mind can give me one small example for this.
ReplyDeleteHi Troy,
ReplyDelete(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!!!
I'm new in the Revit API. When entering this code, I keep getting the following errors when I build solution:
ReplyDelete"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!
as I do or where in stick my macros to run ????
ReplyDeleteHi there Can you please help with this error. I am using a Revit Architecture 2014 version.
ReplyDeleteThanks.
'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
Hi I keep getting this error can you please help me out.
ReplyDeletethanks.
'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)