Match Linked Revit Model View Template Category Visibility

Wanted to share this very simple graph that take a select Revit Link Instance and then will loop all View Templates in the current Document and the Link Document, get any of them that match Names, then update the Category Visibility to match the Link.

#Sean Page, 2021
import clr

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

clr.AddReference('System')
from System.Collections.Generic import List

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

doc = DocumentManager.Instance.CurrentDBDocument
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

#Get a Link Instance
link = UnwrapElement(IN[0])

#Get View elements from Link and Local Models
LinkedViews = FilteredElementCollector(link.GetLinkDocument()).OfCategory(BuiltInCategory.OST_Views).ToElements()
LocalViews = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Views).ToElements()

#Get all Categories in the current model
Cats = doc.Settings.Categories

#Create Lists to hold View Templates
LinkedViewTemplates = []
LocalViewTemplates = []
#Create a container for any Errors
errors = []
#Create a container for all View Template Matches
matches = []

#Check all views and get only View Templates
for LinkView in LinkedViews:
	if LinkView.IsTemplate:
		LinkedViewTemplates.append(LinkView)

for LocalView in LocalViews:
	if LocalView.IsTemplate:
		LocalViewTemplates.append(LocalView)

#Do some action in a Transaction
TransactionManager.Instance.EnsureInTransaction(doc)

#Loop each Local View Template
for LocalVT in LocalViewTemplates:
	#Then Loop each Linked View Template
	for LinkVT in LinkedViewTemplates:
		#If the Template Names match then that the one we want
		if LocalVT.Name == LinkVT.Name:
			try:
				#Look the Categories available
				for Cat in Cats:
					#Check each category for the Link View and see if it can be turned On/Off
					if LinkVT.CanCategoryBeHidden(Cat.Id):
						#Check each category for the Local View and see if it can be turned On/Off
						if LocalVT.CanCategoryBeHidden(Cat.Id):
							#If the Category can be Hidden in both, set the Local View Visibilty to match the Linked View
							LocalVT.SetCategoryHidden(Cat.Id,LinkVT.GetCategoryHidden(Cat.Id))
				matches.append(LocalVT)
			except:
				errors.append(LocalVT.Name)
				continue				

TransactionManager.Instance.TransactionTaskDone()
if errors.Count > 0:
	OUT = matches,errors
else:
	OUT = matches
8 Likes

does it take in consideration the revit link display settings?

1 Like

No, unfortunately this is not currently available via the Revit API and will only affect the Main Model and Annotation categories. However, if you have them set to Host View of course they will also change.

weird API, someone would have to pay Autodesk consultation to make it happen…

It’s because they’re effectively rewriting code in order to unlock parts of the code for API access. I expect some of these things will eventually be solved (as was the case with view titles, floor/ceiling sketch etc.) but many of them will likely never be accessible in Revits lifetime. A really strange one for me is placement of keynote tags not being available.

You get used to it eventually.

3 Likes