Purge View Templates with EF-Tools
Before we dive into the code, I want you to know that you can use this tool in EF-Tools. You can even inspect the code by holding ALT and clicking on the button to open the location of the source code.
πBut Now let's look at the code.
Brainstorming
This is a very simple tool.
We need to find all unused view templates and then ask user which ones to delete. I don't think that there is a method to get all unused in Revit API, so we will find all used view templates first, by iterating over all views, and then find the difference with all view templates.
Here are the steps we will go through:
Get All Views and ViewTemplates
Find Used ViewTemplates (iterate through views)
Find all Unused ViewTemplates (set_A - set_B)
Ask User Which ones to Delete (pyrevit.forms)
Purge View Templates
1οΈβ£ Get View and ViewTemplates
First of all, you will need to get your Views and View Templates.
It's also a good idea to sort them accordingly.
all_view_and_vt = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Views).WhereElementIsNotElementType().ToElements()
all_views = [v for v in all_view_and_vt if not v.IsTemplate]
all_vt_ids = [v.Id for v in all_view_and_vt if v.IsTemplate]
2οΈβ£ Get Used ViewTemplates
Now, we can iterate through all views and get ViewTemplateId.
Then, it's important to check that it's not ElementId(-1)
, because it refers to None in Revit API.
used_vt_ids = []
for view in all_views:
vt_id = view.ViewTemplateId
if vt_id != ElementId(-1):
if vt_id not in used_vt_ids:
used_vt_ids.append(vt_id)
3οΈβ£ Get Unused ViewTemplates
We have all view templates, and a list of used view templates.
Now we can turn them into sets and subtract one from another to get the difference, which will be our unused View Templates.
π‘ It's also a good idea to ensure we have unused templates.
unused_vt_ids = set(all_vt_ids) - set(used_vt_ids)
unused_vts = [doc.GetElement(vt_id) for vt_id in unused_vt_ids]
if not unused_vt_ids:
forms.alert('There are no unused ViewTemplates in the project. Please try again.',title=__title__, exitscript=True)
4οΈβ£ Select ViewTemplates to Purge
Now it's time to give some control to our users.
We shouldn't delete all unused view templates; instead, we should ask them to select which ones to delete.
We can use pyrevit.forms.SelectFromList to quickly create a list of our unused View Templates.
π‘Make sure the user has selected something here.
#π Select ViewTemplates to Purge
vt_to_del = forms.SelectFromList.show(unused_vts,
multiselect=True,
name_attr='Name',
button_name='Select Unused ViewTemplates to Purge')
# β
Ensure ViewTemplates were selected
if not vt_to_del:
forms.alert('There were no ViewTemplates Selected. Please try again.',title=__title__, exitscript=True)
5οΈβ£ Purge ViewTemplates
Finally, we can get rid of these unused view templates.
Overall, we just need to use doc.Delete method, but it's also good to add try/except in case these templates are used by other users or something else.
#π₯ Purge ViewTemplates
t = Transaction(doc, 'Purge ViewTemplates')
t.Start() #π
deleted = 0
for vt in vt_to_del:
try:
vt_name = vt.Name
doc.Delete(vt.Id)
output.print_md('π₯Purged ViewTemplate: **{}**'.format(vt_name))
deleted += 1
except Exception as e:
print("βοΈ Couldn't delete ViewTempalte: {} due to {}".format(vt_name, e))
t.Commit() #π
β¨Full Code
πHere is the final code with all the steps together and also a few additional print statements.
y
__title__ = "Purge Unused View Templates"
__doc__ = """Version = 1.0
Date = 24.06.2024
_____________________________________________________________________
Description:
Purge Unused View Templates from your Revit Project.
_____________________________________________________________________
How-To:
- Click the Button
- Select unused View Templates to purge
_____________________________________________________________________
Last update:
- [28.06.2024] - V1.0 RELEASE
_____________________________________________________________________
Author: Erik Frits from LearnRevitAPI.com"""
from Autodesk.Revit.DB import *
from pyrevit import forms, script
doc = __revit__.ActiveUIDocument.Document
uidoc = __revit__.ActiveUIDocument
output = script.get_output()
all_view_and_vt = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Views).WhereElementIsNotElementType().ToElements()
all_views = [v for v in all_view_and_vt if not v.IsTemplate]
all_vt_ids = [v.Id for v in all_view_and_vt if v.IsTemplate]
used_vt_ids = []
for view in all_views:
vt_id = view.ViewTemplateId
if vt_id != ElementId(-1):
if vt_id not in used_vt_ids:
used_vt_ids.append(vt_id)
unused_vt_ids = set(all_vt_ids) - set(used_vt_ids)
unused_vts = [doc.GetElement(vt_id) for vt_id in unused_vt_ids]
if not unused_vt_ids:
forms.alert('There are no unused ViewTemplates in the project. Please try again.',title=__title__, exitscript=True)
vt_to_del = forms.SelectFromList.show(unused_vts,
multiselect=True,
name_attr='Name',
button_name='Select Unused ViewTemplates to Purge')
if not vt_to_del:
forms.alert('There were no ViewTemplates Selected. Please try again.',title=__title__, exitscript=True)
output.print_md('### There were {}/{} Unused ViewTemplates in the project.'.format(len(unused_vt_ids), len(all_vt_ids)))
output.print_md('---')
t = Transaction(doc, 'Purge ViewTemplates')
t.Start()
deleted = 0
for vt in vt_to_del:
try:
vt_name = vt.Name
doc.Delete(vt.Id)
output.print_md('π₯Purged ViewTemplate: **{}**'.format(vt_name))
deleted += 1
except Exception as e:
print("βοΈ Couldn't delete ViewTempalte: {} due to {}".format(vt_name, e))
t.Commit()
output.print_md('---')
output.print_md('*Script Execution has finished. {} ViewTemplates were purged.*'.format(deleted))