Easy Programming Exercise

Easy Programming Exercise

Anonymous
Not applicable
1,399 Views
3 Replies
Message 1 of 4

Easy Programming Exercise

Anonymous
Not applicable

Well, easy for someone other than me.  I am completely new to the Revit API and would like to create a script that deletes all of the views and sheets from a file.  Any suggestions?

 

Thanks.

 

- Alex

0 Likes
Accepted solutions (1)
1,400 Views
3 Replies
Replies (3)
Message 2 of 4

Anonymous
Not applicable
Accepted solution

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

 

 

0 Likes
Message 3 of 4

Anonymous
Not applicable

Thank you so much; I had pretty much given up on figuring this out.

 

I'll give it a shot.

 

- Alex

0 Likes
Message 4 of 4

Anonymous
Not applicable

Sferra, I like the warning box at the beginning. The formatting on it is straight forward, and simple enough that users will actually read it. Thanks for sharing.

0 Likes