Delete view templates not in use

Hi.

I’d like to delete all the view templates not in use before sending the 100% Final Design file to the client.

Looking into the forum https://forum.dynamobim.com/t/use-dynamo-to-delete-all-view-templates/4434 I managed to delete all view templates, but first I’d need to remove from the list the ones in use.

This workflow will delete all view templates but the 3d ones:

The idea is to delete the ones not in use.

1 Like

Hi @franciscusm ,

This should work :

import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import*
clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument

views = FilteredElementCollector(doc).OfClass(View)
appliedtemplates = [v.ViewTemplateId for v in views]
templates = [v.Id for v in views if v.IsTemplate == True]

toDelete = []
for t in templates:
	if t not in appliedtemplates:
		toDelete.append(t)

TransactionManager.Instance.EnsureInTransaction(doc)
for e in toDelete:
	doc.Delete(e)
TransactionManager.Instance.TransactionTaskDone()



OUT = '%d Template(s) deleted' %(len(toDelete))
15 Likes

Nice Mostafa!

@franciscusm Here is another way:

unusedViewTemplates.dyn (5.2 KB)

7 Likes

Thanks both for your replies!

Task done, removed 354 unused view templates. I’ve tried both methods, but as we are using Revit 2014 here I’m stuck to Dynamo 0.8, so the second one was lacking updated nodes. Mostafa’s Python script worked fine with 2014.

I kept both for my records. Thanks.

I realize this is an old topic. I am doing some View Template work and ran across this thread. I have grabbed all the views in the OTB Architectural Imperial template and queried them to see if they are view templates and two items (2 and 20) show up as Null. If you select by ID in Revit, they show up as strange items on Properties: ???

Here’s an image:

Any idea what these two weird views are? Can they be deleted? Thanks

Hi @Paul_Aubin

Use View.Name to know. I think those are rendering views.

Try Filtering null views and use archi-lab delete node.

1 Like

I was able to filter no problem. I was just wondering about deleting these views from Revit. Thanks.

If there are two of them, I think they pertain to ProjectBrowser and SystemBrowser views. There is even a ViewType for them:

http://www.revitapidocs.com/2018.1/bf04dabc-05a3-baf0-3564-f96c0bde3400.htm

I don’t think you can delete them. I don’t think you can do anything with them, but that’s because I never tried.

1 Like

Well that makes sense. I think that I agree, deleting those would be a bad idea… Thanks.

Hi Mostafa,

Sorry to drag up this old topic but I was wondering if you could help with adding an ‘if’ true/false toggle to the code so that it needs a true value to execute the code.

I’m trying to make the purging of view templates compatible with DataShapes so I can issue a warning message to the user before running.

1 Like

how can the 3d view template types be deleted with dynamo? I can delete with dynamo the view templates of floor plan, rcp, elevation types only but the others remain

Hi @ruben.romero I think that the python script of @Mostafa_El_Ayoubi works as expected, it deletes also the 3d view template not applied by Id. Cheers

1 Like

Has to be done in Python. Dynamo cannot pass 3d view templates on the canvas (always ends up being null).

1 Like

This seems to be working in R22 cpy3- 100% python.

import clr                                                  ##Import .net https://forum.dynamobim.com/t/import-os-sys-system/34604/2
import re                                                   ##Import Regex
import System                                               ##Underlying base  system functions (Windows?)
clr.AddReference("RevitAPI")

import Autodesk                                             ##REquired for doc.Delete() Method
import Autodesk.Revit.DB as ARDB

from RevitServices.Persistence import DocumentManager       ##For DOC access

from Autodesk.Revit.DB import Transaction,TransactionGroup  ##For outside and inner transactions

doc = DocumentManager.Instance.CurrentDBDocument

clr.AddReference("System.Core")
from System.Linq import Enumerable

##############################################################
def get_viewtemplates_unused():
    ## Assums all views are in use. Purge views first for more comprehensive cleaning!
    views=[]
    views = ARDB.FilteredElementCollector(doc).OfClass(ARDB.View) ##Get all views in th eproject
    appliedtemplates = [v.ViewTemplateId for v in views]        ##Get all applied view tempaltes
    templates = [v.Id for v in views if v.IsTemplate == True]   ##Gather ALL views that are Templates

    UnusedViewTemplateElements = []                         ##Initialize
    for tid in templates:                                   ##For each of ALL the tempaltes
    	if tid not in appliedtemplates:                     ##If the ID isn't in Applied templates
    		UnusedViewTemplateElements.append(doc.GetElement(tid))  ##Add the element to the group
    return UnusedViewTemplateElements

##############################################################
def view_delete(Views):                                     ##Return deleted views  
    if len(Views)>0:                                        ##Not on sheets is not null - so candidates to delete
        ######################################################In Traqnsaction
        V_Deleted=["Deleted Views:"]                        ##PRefix list with "Deleted"
        for view in Views:                                  ##For each element not on sheets
            transGrp=Transaction(doc, view.Name)            ##GROUP Transaction(s)
            transGrp.Start()                                ##Start Group transaction
            ##################################################
            try:
                V_name=view.Name                            ##Append its name tothe deleted list    
                doc.Delete(view.Id)                         ##<<<DELETE  ##Delete the element VIA the doc handler and its Id
            except:
                V_name="Error deleting: "
                try:
                    V_name = V_name + view.Name               ##Append its name tothe deleted list    
                except:
                    V_name = V_name + "Cannot extract Name"     ##If not view or cannot get name report that
            finally:
                V_Deleted.append(V_name) ##Append its name tothe deleted list    
            ##################################################
            transGrp.Commit()                               ##Commit group transaction for current view : 
    else:                                                    
        V_Deleted=["No views to delete."]                     ##Report nothign to delete
    return V_Deleted                                        ##Return deleted view names or nothing to delete
##############################################################
##############################################################
##############################################################
##def Main():
trans=TransactionGroup(doc, 'Dynamo:DeleteViewTemplatesUnused') ##Transaction group start name
trans.Start()   
##############################################################
Templates_Unused = get_viewtemplates_unused()               ##Get Unused view templates
Message= [tu.Name for tu in Templates_Unused]
Message = view_delete(Templates_Unused)                     ##Return the application of deleted views
trans.Commit()                                              ##Commit the transaction

##############################################################


OUT=Message #Main()