A friend who is still running Revit 2014 on their Windows NT box noticed that yesterday’s post works only in 2015 because it uses the newfangled ElementType.FamilyName Property.
So that you can continue living happily in the Stone Age, here is a different way to find the Viewport Types based on the presence of the Viewport Type-specific parameters.
Also, while testing this Revit kept giving me a nonsensical “Deleting all open views in a project is not allowed” error, even though I was never trying to delete all open views in the project. It seems that Revit 2014 & 2015 really really really love Viewport 1 and don’t want it to be deleted. So I added some code to leave it alone.
public void deleteUnusedViewportTypesFor2014()
{
Document doc = this.ActiveUIDocument.Document;
// get all viewport types
// this approach to find the Viewport Types can be used in 2014
// whereas the ElementType.FamilyName property exists in 2015 only
IList<ElementType> viewportTypes = new FilteredElementCollector(doc).OfClass(typeof(ElementType)).Cast<ElementType>()
.Where(q =>
q.get_Parameter("Title") != null &&
q.get_Parameter("Show Title") != null &&
q.get_Parameter("Show Extension Line") != null &&
q.get_Parameter("Line Weight") != null &&
q.get_Parameter("Color") != null &&
q.get_Parameter("Line Pattern") != null
).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);
}
// create list of Viewport Type Element Ids sorted by the integer value of the element ids
List<ElementId> toDelete = viewportTypeIds.OrderBy(q => q.IntegerValue).ToList();
// Remove the first element in this list so that "Viewport 1" will not be deleted.
// There seems to be a bug in Revit that prevents deleting "Viewport 1".
// Even if this viewport is not being used, attempting to delete it results in an error "Deleting all open views in a project is not allowed."
toDelete.RemoveAt(0);
// 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();
toDelete.Remove(typeId);
}
// do the deletion
using (Transaction t = new Transaction(doc,"Delete Unused Viewport Types"))
{
t.Start();
doc.Delete(toDelete);
t.Commit();
}
}
Like this:
Like Loading...