How to link a Revit file with Python?

I’m hoping someone can give me some hints on how to link a Revit file with Python, I have a feeling I’m pretty close but it still keeps giving me an internal error on line 38. This is what I’ve got so far.

My best guess is I have to assign the newly created RevitLinkType to a RevitLinkLoadResult, I’ve found some explanation on the Autodesk website, but I don’t know how to translate the code to Python. To be a little more specific, I’m looking for a way to translate this piece of code to Pyhton:

RevitLinkLoadResult result = RevitLinkType.Create(doc, path, options);

https://knowledge.autodesk.com/support/revit-products/troubleshooting/caas/CloudHelp/cloudhelp/2016/ENU/Revit-API/files/GUID-0DFBA9EA-0E7B-49D4-9576-6D0528FC0D3C-htm.html

Any help would be much appreciated, thanks!

  ![link_1|1062x863](upload://do7geaXiiA2uwO8VyyiiISsvVbC.png)

Does anyone has any thoughts on this? Unfortunately I still haven’t managed to get this to work.

@Rob_Verweij1 I can load a file (rvt), but what I want is to reload all linked files before an export. how can I do a reload?


Reloade all rvt-links.dyn (5.2 KB)

1 Like

Hey buddy, can you post the actual code instead of an image? I think this forum has a great formatting for posting code so we no longer need to post images. It will save time everyone when they don’t have to retype it.


import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
# Import ToDSType(bool) extension method
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
# Import geometry conversion extension methods
clr.ImportExtensions(Revit.GeometryConversion)
# Import DocumentManager and TransactionManager
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
from System.Collections.Generic import *
# Import RevitAPI
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *

doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

# Start Transaction
TransactionManager.Instance.EnsureInTransaction(doc)

# Create the Revit Link Type
# creae a for loop
#mp = ModelPathUtils.ConvertUserVisiblePathToModelPath(UnwrapElement(IN[1]))
mp = ModelPathUtils.ConvertUserVisiblePathToModelPath("C:\Dynamo\parkeringspace.rvt")
lnkOp = RevitLinkOptions("")
loadedLnkType = RevitLinkType.Create(doc, mp, lnkOp)
 
# Create the Revit Link Instance
lnkInstance = RevitLinkInstance.Create(doc, loadedLnkType.ElementId)

# End Transaction
TransactionManager.Instance.TransactionTaskDone()

OUT="Worked"
2 Likes

I manage to find a solution for reloading all links.

#python nodes in dynamo 1.0.0
#created by Nicklas Østertgaard  nvo@bimshark.com / nvo@shl.fk

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
# Import ToDSType(bool) extension method
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
# Import geometry conversion extension methods
clr.ImportExtensions(Revit.GeometryConversion)
# Import DocumentManager and TransactionManager
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
from System.Collections.Generic import *
# Import RevitAPI
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *

doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

collector = Autodesk.Revit.DB.FilteredElementCollector(doc)
linkInstances = collector.OfClass(Autodesk.Revit.DB.RevitLinkInstance)

for i in linkInstances:
	RevitLinkType  = doc.GetElement(i.GetTypeId())
	RevitLinkType.Load();

OUT= "Worked"
3 Likes

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