Get list of deleted View template from Python script

Hi @Konrad_K_Sobon

I am using your Python script from the below thread in order to delete all the View templates (either used or unused). It works great. Thanks.

https://forum.dynamobim.com/t/use-dynamo-to-delete-all-view-templates/4434/16?u=david.concept

I am trying to get the list of how many View Template were deleted and use it with a Data shapes UI.TextNote Data node.

Anyway to achive this with your Python Script? Thank you again.

David

Hi @David.Concept,
See if this helps

# Copyright(c) 2016, Konrad K Sobon
# @arch_laboratory, http://archi-lab.net

# Import DocumentManager and TransactionManager
import clr
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

# Import RevitAPI
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *

import sys
pyt_path = r'C:\Program Files (x86)\IronPython 2.7\Lib'
sys.path.append(pyt_path)

#The inputs to this node will be stored as a list in the IN variable.
dataEnteringNode = IN

if IN[0]:
	try:
		errorReport = None
		TransactionManager.Instance.EnsureInTransaction(doc)
		
		allViewTemp = FilteredElementCollector(doc).OfClass(View).ToElements()
		toDelete = []
		viewsDeleted = []
		
		for i in allViewTemp:
			if i.IsTemplate:
				try:
					viewsDeleted.append(i.Name)
					doc.Delete(i.Id)
				except:
					pass

		TransactionManager.Instance.TransactionTaskDone()
	except:
		# if error accurs anywhere in the process catch it
		import traceback
		errorReport = traceback.format_exc()
else:
	errorReport = "Set input to True"
#Assign your output to the OUT variable
if errorReport == None:
	OUT = "View Templates Deleted: "+viewsDeleted.Count.ToString(), viewsDeleted
else:
	OUT = errorReport

Hi @AmolShah. Thank you very much. It works great!

1 Like