i created this script to tell me when i open a project if i have closed/unloaded links. its sorta working. the first thing i run into is i will get the nested links also reported which aren’t in my host model. I’m still trying to get this to work a bit better to get closed/Not Loaded/not found but I figure i could ask how i could not include these nested links.
# -*- coding: utf-8 -*-
# pyRevit | IronPython 2.7
# Hook: doc-opened.py
# Alerts if any Revit links are unloaded, not found, or closed workset.
import clr, os
clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import *
from pyrevit import forms
# Document comes from event args in doc-opened hook
doc = __eventargs__.Document
def is_project_template(document):
"""Detect if file is a project template (.rte)."""
try:
path = document.PathName
return path and path.lower().endswith(".rte")
except:
return False
def get_element_name(elem):
"""Safe way to get element name."""
try:
return Element.Name.GetValue(elem)
except:
try:
return elem.get_Parameter(BuiltInParameter.SYMBOL_NAME_PARAM).AsString()
except:
return "<Unnamed>"
def check_links():
problem_links = []
# --- Check RevitLinkTypes (unloaded or not found) ---
link_types = FilteredElementCollector(doc).OfClass(RevitLinkType)
for ltype in link_types:
name = get_element_name(ltype)
try:
if not ltype.IsLoaded(doc):
problem_links.append(name + " [Unloaded]")
else:
ext_ref = ltype.GetExternalFileReference()
if ext_ref and ext_ref.GetLinkedFileStatus() == LinkedFileStatus.NotFound:
problem_links.append(name + " [Not Found]")
except:
#problem_links.append(name + " [Error Checking Type]")
pass
# --- Check RevitLinkInstances (closed worksets) ---
link_insts = FilteredElementCollector(doc).OfClass(RevitLinkInstance)
for inst in link_insts:
name = get_element_name(inst)
try:
if inst.GetLinkDocument() is None:
problem_links.append(name + " [Closed Workset?]")
except:
#problem_links.append(name + " [Error Checking Instance]")
pass
# --- Show results ---
if problem_links:
msg = "Some Revit links are not active in this project.\n\nReview Manage Links:\n\n" + "\n".join(problem_links)
forms.alert(msg, title="Link Status Alert", warn_icon=True)
# --- Run only for real projects (not families or templates) ---
if not doc.IsFamilyDocument and not is_project_template(doc):
check_links()
