Unload Revit Links

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?

Just put this before you call Unload()

TransactionManager.Instance.ForceCloseTransaction()
5 Likes

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
10 Likes

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

Fixed above code in time limit…

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

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

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

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

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

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.

3 Likes

Thanks, this is pretty helpful.

Great work, T_Pover. :slight_smile:

1 Like

Using the above code I had it working successfully, then i’ve got to a model and ran across this error. Can anyone shed some light?

image

ran across some code that works