Deleting Unused Viewport Types

If there are unused Viewport Types that you can’t get rid of with Purge Unused, here is an API approach to try. The ElementType class has many subclasses but not one for ViewportType so the FamilyName property is used instead.

public void deleteUnusedViewportTypes()
{
    Document doc = this.ActiveUIDocument.Document;
    
    // get all viewport types
    IList<ElementType> viewportTypes = new FilteredElementCollector(doc).OfClass(typeof(ElementType)).Cast<ElementType>().Where(q => q.FamilyName == "Viewport").ToList();

    // create list of ElementIds from list of ElementType objects
    IList<ElementId> viewportTypeIds = new List<ElementId>();
    foreach (Element viewportType in viewportTypes)
    {
        viewportTypeIds.Add(viewportType.Id);
    }
    
    // remove from this list all viewport types that are in use
    foreach (Element viewport in new FilteredElementCollector(doc).OfClass(typeof(Viewport)))
    {
        ElementId typeId = viewport.GetTypeId();
        viewportTypeIds.Remove(typeId);
    }
    
    // do the deletion
    using (Transaction t = new Transaction(doc,"Delete Unused Viewport Types"))
    {
        t.Start();
        doc.Delete(viewportTypeIds);
        t.Commit();
    }
}

EDIT: Here is a new version with some additional diagnostic information and the opportunity for the user to abort after seeing the list of viewport types to be deleted.

public void deleteUnusedViewportTypes()
{
    Document doc = this.ActiveUIDocument.Document;
    
    // get all viewport types
    IList<ElementType> viewportTypes = new FilteredElementCollector(doc).OfClass(typeof(ElementType)).Cast<ElementType>().Where(q => q.FamilyName == "Viewport").ToList();

    // create list of ElementIds from list of ElementType objects
    IList<ElementId> viewportTypeIds = new List<ElementId>();
    // do not try to delete Viewport 1, as it is a special viewport type that Revit does not want deleted
    foreach (Element viewportType in viewportTypes.Where(q => q.Name != "Viewport 1"))
    {
        viewportTypeIds.Add(viewportType.Id);
    }
    
    // remove from this list all viewport types that are in use
    foreach (Element viewport in new FilteredElementCollector(doc).OfClass(typeof(Viewport)))
    {
        ElementId typeId = viewport.GetTypeId();
        viewportTypeIds.Remove(typeId);
    }
    
    // generate list of names of viewports to be deleted
    string viewports = "";
    foreach (ElementId id in viewportTypeIds)
    {
        viewports += doc.GetElement(id).Name + Environment.NewLine;
    }
    
    // show Task Dialog with opportunity for user to cancel the operation
    TaskDialog td = new TaskDialog("Viewports");
    td.MainInstruction = "The following viewports will be deleted";
    td.MainContent = viewports;
    td.CommonButtons = TaskDialogCommonButtons.Ok | TaskDialogCommonButtons.Cancel;
    if (td.Show() != TaskDialogResult.Ok)
        return;
    
    string idsNotDeleted = "";
    // do the deletion
    using (Transaction t = new Transaction(doc,"Delete Unused Viewport Types"))
    {
        t.Start();
        
        foreach (ElementId id in viewportTypeIds)
        {
            try
            {
                doc.Delete(id);
            }
            catch // deletion failed - get the name of the element that could not be deleted
            {
                Element eNotDeleted = doc.GetElement(id);
                idsNotDeleted += eNotDeleted.Name + " " + id.IntegerValue.ToString();
            }
        }
        
        t.Commit();
    }
    
    if (idsNotDeleted != "")
    {
        TaskDialog tdError = new TaskDialog("Error");
        tdError.MainInstruction = "The following elements were not able to be deleted";
        tdError.MainContent = idsNotDeleted;
        tdError.Show();
    }
}
Advertisements

10 thoughts on “Deleting Unused Viewport Types

  1. I ran the macro and it deleted the unused viewport types (except the one called “Viewport 1” for some reason), but it also deleted all of my keynote legends (which were placed on sheets and not empty) and most schedules (also placed on sheets and not empty). Do you know why this might’ve happened?

    • Hi,

      I’ve updated the post with some new code that might help make clear what is happening for you. I did some testing though and did not see and keynote schedules or schedules get deleted.

      Hope that helps
      Harry

      • Thank you! Makes a lot more sense now. And I think in my earlier comment I may have been running the macro while in a sheet view; do you think that may have something to do with the deleted keynote legends?

        • Hi Chris – Glad that helped. It would be strange if the active view changed the list of viewports returned by: new FilteredElementCollector(doc).OfClass(typeof(Viewport))

    • Hi Chris,

      Did you figure out the reason for the the deleted schedules? I meet the same problem now. When I deleted a viewport type, it deleted some schedules too.

      Thanks,
      Yueying

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s