Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Easy Programming Exercise

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
Jazzster11
1108 Views, 3 Replies

Easy Programming Exercise

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

3 REPLIES 3
Message 2 of 4
Sferra43
in reply to: Jazzster11

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

 

 

Message 3 of 4
Jazzster11
in reply to: Jazzster11

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

 

I'll give it a shot.

 

- Alex

Message 4 of 4
Jeradooka
in reply to: Sferra43

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.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Rail Community