Revit API

Reply
Distinguished Contributor
Jazzster11
Posts: 258
Registered: ‎10-17-2004
Message 1 of 4 (793 Views)
Accepted Solution

Easy Programming Exercise

793 Views, 3 Replies
03-17-2011 03:48 PM

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

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

 

 

Member
Sferra43
Posts: 4
Registered: ‎04-08-2011
Message 2 of 4 (718 Views)

Re: Easy Programming Exercise

04-08-2011 09:53 PM 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);
            }
        }
    }
}

 

 

Distinguished Contributor
Jazzster11
Posts: 258
Registered: ‎10-17-2004
Message 3 of 4 (700 Views)

Re: Easy Programming Exercise

04-13-2011 11:57 AM 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

Active Member
Jeradooka
Posts: 6
Registered: ‎01-04-2012
Message 4 of 4 (337 Views)

Re: Easy Programming Exercise

08-21-2012 04:52 PM 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.

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.