How to link a Revit file with Python?

Update and Solution: After a couple of days of struggling with this, I realized that the internal error has to do with forward slashes in the filename. The Revit link gets loaded using forward slashes, but apparently will not actually be created as a RevitLinkType. Instead it’s necessary to use backslashes. The error was resolved by replacing each forward slash with two backslashes.

I’m attempting to do something similar to what you’ve demonstrated here, except instead of adding the Revit links to the current document, I’m opening another document. Also, my desire is to add new links, and not reload existing links from a new location. I’ve seen this other post, but it is also related to reloading links.

The result is the same as the OP was experiencing - an internal error in the following line:
linkloadresult = RevitLinkType.Create(mDoc, linkpath, linkopt)

Is there some other procedure for linking Revit files in documents other than the current document?

Below is the complete python code:

# Common Language Runtime Modules
import clr
clr.AddReference('RevitAPI')
clr.AddReference('RevitServices')

# Revit and Dynamo Modules
from Autodesk.Revit.DB import *
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application

# input assigned the IN variable
ProjRootPath = IN[0]  # Root path to templates
ProjNum = IN[1] 

# disciplines to be included in the project
if isinstance(IN[2],list):
    DiscInProj=IN[2]
else:
    DiscInProj=[IN[2]]

# instantiate error log
error_log = None

# save as options - as shown by Thomas Perkov
woption = WorksharingSaveAsOptions() 
woption.SaveAsCentral = True

option = SaveAsOptions()
option.OverwriteExistingFile = True
option.Compact = True
option.SetWorksharingOptions(woption)

elementlist = list()
FileDisc = DiscInProj
rvtlink_list = list()

for EachDisc in DiscInProj:
    savename = "P:/" + ProjNum + "/" + EachDisc + "/" + ProjNum + "_" + EachDisc + ".rvt"
    mDoc = app.OpenDocumentFile(savename)
    
    WorksetConfig = WorksetConfiguration()

    TransactionManager.Instance.EnsureInTransaction(mDoc)    
    for rvt in FileDisc:
        if rvt != EachDisc:
            #Create Worksets for other discipline Revit links
            wksetName = "z_LINK " + rvt
            Workset.Create(mDoc,wksetName)

            #add Revit Links for other disciplines
            rvtlink = "P:/" + ProjNum + "/" + rvt + "/" + ProjNum + "_" + rvt + ".rvt"
            #from Clockwork Document.AddLink Module
            linkpath = ModelPathUtils.ConvertUserVisiblePathToModelPath(rvtlink)
            linkopt = RevitLinkOptions("")
            linkloadresult = RevitLinkType.Create(mDoc, linkpath, linkopt)
            rvtlink_list.append(EachDisc + "-" + RevitLinkInstance.Create(mDoc, linkloadresult.ElementId))

    # End Transaction
    TransactionManager.Instance.TransactionTaskDone()
    TransactionManager.Instance.ForceCloseTransaction()
    mDoc.SaveAs(savename, option)
    mDoc.Close(False)

# output assigned the OUT variable
if error_log:
    out_list = error_log
OUT = (rvtlink_list)
1 Like