Remove View Template from View

I am wanting to remove a view template from existing views. I couldn’t find a node that did this so attempted to write some python coding to perform this task. I am getting an error saying that the integer value that I am trying to write the -1 to is read only.

Is there an easy way of doing this using nodes or what am I missing in my python coding?

import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager

#The inputs to this node will be stored as a list in the IN variables.
views=UnwrapElement(IN[0])
elementlist=list()

for i in views:
	i.ViewTemplateId.IntegerValue = -1
	elementlist.append(i.ViewTemplateId.IntegerValue)

#Assign your output to the OUT variable.
OUT = elementlist

You’ll need to create an actual ElementId from that -1.
Have a look at this script, it ought to help you solve your problem:

Sorry Andreas my python coding skills aren’t up to your level.
I’m trying to set the ViewTemplate.IntegerValue parameter. I’m getting an error with the below do I need to “Set” the value of use an “=”?

import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

#The inputs to this node will be stored as a list in the IN variables.
views=UnwrapElement(IN[0])
elementlist=list()

for view in views:
#	try:
		view.ViewTemplateId.IntegerValue.Set(ElementId(-1))
		elementlist.append(i.ViewTemplateId)
#	except:
#		elementlist.append("fail")

#Assign your output to the OUT variable.
OUT = elementlist

Try this:
view.ViewTemplateId = ElementId(-1)

It might be better if you use the static property for that: ElementId.InvalidElementId

1 Like

Cool - didn’t know that existed, @Dimitar_Venkov

There are custom nodes already in archi-lab_Grimshaw package for handing this:

Check if view has a view template:

Apply a view template:

and finally remove a view template:

Good luck!

3 Likes

Thanks Konrad, it always helps when you have the latest package installed (gee I wish Dynamo let you know when packages were updated!)

1 Like

Thanks for your help @Andreas_Dieckmann I have used Konrad’s node but for my education I have tried your suggestion but am still getting an error. Any ideas

import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

#The inputs to this node will be stored as a list in the IN variables.
views=UnwrapElement(IN[0])
elementlist=list()

for view in views:
#	try:
		view.ViewTemplateId = ElementId(-1)
		elementlist.append(i.ViewTemplateId)
#	except:
#		elementlist.append("fail")

#Assign your output to the OUT variable.
OUT = elementlist

@Dimitar_Venkov, thank you for your input sorry to be a pain but again for my own education what is the coding that uses the static property, I tired the below but received error messages

import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

#The inputs to this node will be stored as a list in the IN variables.
views=UnwrapElement(IN[0])
elementlist=list()

for view in views:
#	try:
		view.ViewTemplateId.ElementId.InvalidElementId
		elementlist.append(i.ViewTemplateId)
#	except:
#		elementlist.append("fail")

#Assign your output to the OUT variable.
OUT = elementlist

It would help if you could share the error message… :slight_smile:

Couple of comments:

UnwrapElement(IN[0])

If you input a list of views into that input then the method that you are calling will create a list object and it will cause an error when you try to iterate over it few lines down the line. It basically tries to unwrap a list rather than each view in a list. Instead call UnwrapElement() on each element in the IN[0]

Now, once you get past that you are missing a transaction start and end.

Then you are also calling it all wrong. To assign a new view template you must call it like that:

view.ViewTemplateId = ElementId.InvalidElementId

Try that.

@Konrad_K_Sobon thanks for your help as always.

Based your feedback I also found some useful info at https://github.com/DynamoDS/Dynamo/wiki/Python-0.6.3-to-0.7.x-Migration that helped clarify the wrapping of elements.

successful code below.

import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

#The inputs to this node will be stored as a list in the IN variables.
views=IN[0]
elementlist=list()

# Start Transaction
doc = DocumentManager.Instance.CurrentDBDocument
TransactionManager.Instance.EnsureInTransaction(doc)

for view in views:
	viewunwrap=UnwrapElement(view)
	viewunwrap.ViewTemplateId = ElementId.InvalidElementId
	elementlist.append(viewunwrap.ViewTemplateId)

# End Transaction
TransactionManager.Instance.TransactionTaskDone()

#Assign your output to the OUT variable.
OUT = elementlist
3 Likes

Actually I think this was fixed a couple of years a go in this PR, UnwrapElement() sould be able to handle unwrapping of elements in both flat and nested list. See this issue on github:

Thanks @Einar_Raknes, so based on your advice I tried the following code and it worked perfectly

 import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

#The inputs to this node will be stored as a list in the IN variables.
views=UnwrapElement(IN[0])
elementlist=list()

# Start Transaction
doc = DocumentManager.Instance.CurrentDBDocument
TransactionManager.Instance.EnsureInTransaction(doc)

for view in views:
#	try:
		view.ViewTemplateId = ElementId.InvalidElementId
		elementlist.append(view.ViewTemplateId)
#	except:
#		elementlist.append("fail")

# End Transaction
TransactionManager.Instance.TransactionTaskDone()

#Assign your output to the OUT variable.
OUT = elementlist
1 Like