I'm learning the API so I figured I would take a crack at it.
I don't know your experience level with programming in general or even what language your using but the code below worked for me. If it's over your head, I also attached the finished product. To use the addin, place the addin in your Revit 2011 Addin folder (see below) and the Deleter.dll at C:\ Then next time your in Revit you will have this command available under the 'Addins' ribbon tab then under 'External Commands'
Addin Locations:
XP - C:\Documents and Settings\All Users\Application Data\Autodesk\Revit\Addins\2011
Vista/7 - C:\ProgramData\Autodesk\Revit\Addins\2011
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using System.Collections;
namespace Deleter
{
[Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Automatic)]
public class SheetsAndViews : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
// Warn the user about deleting all views and sheets
TaskDialogResult result = TaskDialog.Show("Delete All?", "This command will delete all sheets and views in the current project. \r\n\r\nAre you sure you want to continue?",
TaskDialogCommonButtons.Yes | TaskDialogCommonButtons.No, TaskDialogResult.No);
//End the command if no was clicked
if(result == TaskDialogResult.No)
{
return Result.Succeeded;
}
// Get the active UIDoc
UIDocument uiDoc = commandData.Application.ActiveUIDocument;
try
{
DeleteSheetsAndViews(uiDoc);
TaskDialog.Show("Success", "All views deleted successfully.");
}
catch (Exception Ex)
{
TaskDialog.Show("Error", "An error occurred.\r\n\r\n" + Ex.Message);
return Result.Failed;
}
return Result.Succeeded;
}
private void DeleteSheetsAndViews(UIDocument uiDoc)
{
// Used to store the ElementIds to delete
ArrayList list = new ArrayList();
// Get all of the Views in the current drawing
FilteredElementCollector collector = new FilteredElementCollector(uiDoc.Document);
FilteredElementIterator iterator = collector.OfClass(typeof(View)).GetElementIterator();
iterator.Reset();
// Iterate all of the views and collect their ElementIds
while (iterator.MoveNext())
{
View v = iterator.Current as View;
if (v.ViewName != uiDoc.Document.ActiveView.ViewName)
{
list.Add(v.Id);
}
}
// Delete all of the collected ids
foreach (ElementId id in list)
{
uiDoc.Document.Delete(id);
}
}
}
}