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…
seth09
February 12, 2018, 8:42pm
6
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.
seth09
February 14, 2018, 2:32pm
8
Thanks, how would i modify this to work for multiple link document that open in the background?
CVest
February 14, 2018, 2:54pm
9
Do you want it to unload them before you open your project?
seth09
February 14, 2018, 2:56pm
10
Yes, if that is possible. I just don’t want it a permanent effect to the original revit file.
T_Pover
February 14, 2018, 7:21pm
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.
3 Likes
seth09
February 14, 2018, 10:10pm
12
Thanks, this is pretty helpful.
JB86
August 27, 2019, 2:45am
14
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?
JB86
August 27, 2019, 3:53am
15
ran across some code that works
I did some changes and it is working now!
I set the line:
newfilepath = newpath + "\\" + filenames
To:
newfilepath = newpath + "\\" + str(filenames)
And removed two flatten nodes, leaving as this:
[Script%202]
However, it seems that the files are not being closed, even with the line
newdoc.Close(True)
As I can see them in the worksharing monitor and the memory is still in use.
The current script is now:
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geomet…