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