Unload Revit Links


#1

Trying to write a small python script that will Unload Revit Links.
Here’s the code snippet:

revitlinktypes=list()
linkcollector=FilteredElementCollector(doc).OfClass(RevitLinkInstance)
revitlinktypes=[]
for rli in linkcollector:
	rlt=doc.GetElement(rli.GetTypeId())
	revitlinktypes.append(rlt)
	rlt.Unload(None)

Here’s the error:

Does this mean that because I am writing this in Dynamo, and Dynamo as the API client has “Started a Transaction” - I cannot invoke the RevitLinkType.Unload() method?


#2

Just put this before you call Unload()

TransactionManager.Instance.ForceCloseTransaction()

#3

Thanks Konrad. Works fine now.

For reference to anyone searching this:

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

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

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)


dataEnteringNode = IN

doc = DocumentManager.Instance.CurrentDBDocument

revitlinktypes=list()
linkcollector=FilteredElementCollector(doc).OfClass(RevitLinkInstance)
revitlinktypes=[]
TransactionManager.Instance.ForceCloseTransaction()
for rli in linkcollector:
	rlt=doc.GetElement(rli.GetTypeId())
	revitlinktypes.append(rlt)
	rlt.Unload(None)
	

OUT= 0

Close Document Python Embedded Script
#4

You can actually put that outside the loop. That will prevent it from being called for every link.


#5

Fixed above code in time limit…


#6

Can the unload revit links script above modify to unload a specific link model?


#7

Yes - all you would need to do is identify which linked files need to be unloaded and only have it loop thru those.


#8

Thanks, how would i modify this to work for multiple link document that open in the background?


#9

Do you want it to unload them before you open your project?


#10

Yes, if that is possible. I just don’t want it a permanent effect to the original revit file.


#11

I have been working on a new node this week that might be of use. With this node you can change the file path to a linked file, but it also allows you to set the linked file to ‘Loaded’ or ‘Unloaded’. The best part is that you don’t even need to open the host model for it. All you need is the file path to the host model.

The next time you open the host model the linked file will be changed to loaded or unloaded.
It’s in the MEPover package.


#12

Thanks, this is pretty helpful.


#13

Great work, T_Pover. :slight_smile: