How to get the view template of Linked Revit file Views?

How to get the view template of Linked Revit file Views?

Warning: Element.GetParameterValueByName operation failed. 
Could not obtain element from the current document!  The id may not be valid.

image
although I achieved it in other way but disappointed about results, is it really necessary for this stupid warning?

I used python node but it does not show correctly the values of some views

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

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

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)

doc =  DocumentManager.Instance.CurrentDBDocument
app =  DocumentManager.Instance.CurrentUIApplication.Application

elements = UnwrapElement(IN[0])
parameter = IN[1]

values = []
if hasattr(elements, "__iter__"):
	output = []
	for elem in elements:
		if hasattr(elem, "__iter__"):
			vals = []
			for e in elem:
				for p in elem.Parameters:
					if p.Definition.Name == parameter:		
						parm = p.AsValueString()
						if (parm is None):
							parm = p.AsString()
				vals.append(parm)
			values.append(vals)
		else:
			for p in elem.Parameters:
				if p.Definition.Name == parameter:		
					parm = p.AsValueString()
					if (parm is None):
							parm = p.AsString()
			values.append(parm)
	output.append(values)
else:
	parm = 	elements.Parameter[parameter].AsValueString()
	output = parm

OUT = output

You need to ask for the View template Id then get the element with that Id from the linked document, not the current one. I’m not quite sure what the Python code above aims to achieve, but this script and Python should get the job done. The nodes I’ve used from Crumple just get the linked document from a linked instance, and get the views from a document.

# Made by Gavin Crump
# Free for use
# BIM Guru, www.bimguru.com.au

# Boilerplate text
import clr

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

# Define unwrap list
def uwlist(input):
    result = input if isinstance(input, list) else [input]
    return UnwrapElement(result)

# This should be the document which contains the view (linked)
doc = IN[0]

# Preparing input from dynamo to revit
uw_list  = uwlist(IN[1])
vts = []

# Try to get the view template from the document
for view in uw_list:
	vtid = view.ViewTemplateId
	try:
		# Note that 'doc' here is linked, not current
		vt = doc.GetElement(vtid)
		vts.append(vt)
	except:
		vts.append(None)

# Preparing output to Dynamo
OUT = vts

get view templates.dyn (10.3 KB)

Feel free to mark solution :wink:

5 Likes

If you want to collect the view templates of several documents, the View ByType node is intended to operate with links.

2 Likes

that is great, but I do not want to see all view templates existing in the documents because they may not be used in views, so I just want to know the view templates used on filtered views in the documents or linked revit files. @GavinCrump responded awesome to the question, I managed to use only the OOTB nodes but I do not know why the python I used gives nulls when the view has a view template applied, I thought maybe is because that view template does not exist in the current project file

1 Like

The two likely reasons for view templates usually coming back tend to be no template applied, or a 3D view template.