LearnRevitAPI

Jul 2, 2024

Purge View Templates

I'm continuing with the Purge Tools for Revit series, and this time we will look at how to purge unused View Templates.

This tool will also be included in EF-Tools, but I will also explain all the steps in the code.

💡Let's Learn: How To use Purge View Templates with pyRevit

You can use it in EF-Tools

Before we dive into the code, I want you to know that you can use this tool in EF-Tools.

Inspect the code by holding ALT and clicking on the button to open the location of the source code.

👀Now let's look at the code.

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.

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.

Get Unused ViewTemplates

We have all view templates, and we have 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.

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.

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.

Full Code

👇Here is the final code with all the steps together.

💡You can copy code snippets in this blog post.

P.S. Want to Purge View Filters too?

I've forgotten to send out the newsletter about the first purge tool - Purge View Filters. 

You can check this video here if you want to learn about it too.

Enjoyed Newsletter?

Leave a Testimonial Here.

⌨️ Happy Coding!
Erik Frits

Join Newsletter

📩 You will be added to Revit API Newsletter

Join Us!

which is already read by 6700+ people!