Manage links (Reload from) with transactions?

Hi,

I try to ‘overwrite’ (reload from) a linked dummy template under manage links using the api. The hurdle is that I want to do it with transactions and not manually open the file itself. (I need to do it for multiple files and links…)

Here’s my code:

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
from System.Collections.Generic import *
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)

filepath = IN[0] # the new filepath to link
remote_rvt_file = IN[1]  # the rvt file to change

TransactionManager.Instance.EnsureInTransaction(doc)
open_file = app.OpenDocumentFile(remote_rvt_file)
TransactionManager.Instance.TransactionTaskDone()	
	
trans = Transaction(open_file, "changeWorkset")
trans.Start()

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

for i in linkInstances:
    RevitLinkType = open_file.GetElement(i.GetTypeId())
    filepath2 = ModelPathUtils.ConvertUserVisiblePathToModelPath(UnwrapElement(filepath))
    RevitLinkType.LoadFrom(filepath2, None)

trans.Commit()
# open_file.Save()
open_file.Close(True)

I’m not sure why I get the following error for the code above. Does anyone have a suggestion what I’m doing wrong? Please feel free to spill any transaction knowledge, tips, tricks, etc… :blush:

  Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed. 
Traceback (most recent call last):
  File "<string>", line 46, in <module>
Exception: Unable to close all open transaction phases!

? :slightly_smiling_face:

Try to use Dynamos Transaction manager, and ForceCloseTransaction() like this:

# Import DocumentManager and TransactionManager
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument

TransactionManager.Instance.ForceCloseTransaction()

TransactionManager.Instance.EnsureInTransaction(doc)

TransactionManager.Instance.TransactionTaskDone()

Please explain fully what you are doing and what you are after it doing because it seems you are mixing things up.

Firstly you are opening a revit file and getting its linked files but you are doing this inside a separate document transaction. I do not see a transaction being required for this task as the saving will sort this out.

I see this happening is follows:

  • For each input location you open the file within a for statement, then within this element of the statement you will set it as the document and get the linked files.
  • Then within a another for statement within the revit file form statement you need to then start getting the link file info like its file name then add the path and file name together and set it as its new location.
  • Then to end the main for statement you will use the close command to close the file.

This should send you back on the right track, may still be issues with it.

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
clr.AddReference("RevitNodes")

import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
clr.AddReference("RevitServices")

import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
from System.Collections.Generic import *

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

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


filepath = IN[0] # the new filepath to link

#Checks if the remote files are a list
#and if not makes it a list
if isinstance(IN[1],list):
	remote_rvt_file=IN[1]
else:
	remote_rvt_file=[IN[1]]

for rvf in remote_rvt_file:
	open_file = app.OpenDocumentFile(rvf)

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

	WorksetConfig=WorksetConfiguration()
	
	for i in linkInstances:
	
		RevitLinkType = open_file.GetElement(i.GetTypeId())
		
		#Need to add file name at end of NewFilePath
		NewFilePath=filepath + 
		
		filepath2 = ModelPathUtils.ConvertUserVisiblePathToModelPath(NewFilePath)
		
		
		RevitLinkType.LoadFrom(filepath2, WorksetConfig)

	open_file.Close(True)

	OUT="Completed"