Edit and save a linked file causes fatal error

Hi there ! :slight_smile:

I’m trying to modify a linked file but I got the famous critical error we all love that closes Revit. The curious thing is that it doesn’t occurs on each run.
image
What I’m trying to do in human language:

  1. From the current project, get the first link and unload it.
  2. Open this unloaded link and start a new transaction in it
  3. Modify the only one door in this link
  4. Commit transaction
    5.Synchronize link with central
    6.Close it (with or without saving, I tried both)
    7.Reload the link

I tried to comment the last 3 steps without sucess. I think the error is here.
The code:

import clr

clr.AddReference('ProtoGeometry')

from Autodesk.DesignScript.Geometry import *

clr.AddReference('RevitNodes')

import Revit

clr.ImportExtensions(Revit.Elements)

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 *

clr.ImportExtensions(Revit.GeometryConversion)

doc = DocumentManager.Instance.CurrentDBDocument

uiapp = DocumentManager.Instance.CurrentUIApplication

app = uiapp.Application

#Get link path

linkcollector=FilteredElementCollector(doc).OfClass(RevitLinkInstance).ToElements()

link_path=linkcollector[0].GetLinkDocument().PathName

#Unload link

doc.GetElement(linkcollector[0].GetTypeId()).Unload(None)

#Saving options

OpenOptions = OpenOptions()

OpenOptions.DetachFromCentralOption = DetachFromCentralOption.DoNotDetach

tOptions=TransactWithCentralOptions()

rOptions=RelinquishOptions(True)

rOptions.CheckedOutElements=True

rOptions.FamilyWorksets=True

rOptions.StandardWorksets=True

rOptions.UserWorksets=True

rOptions.ViewWorksets=True

sOptions=SynchronizeWithCentralOptions()

sOptions.SetRelinquishOptions(rOptions)


modelpath=ModelPathUtils.ConvertUserVisiblePathToModelPath(link_path)

link_doc=app.OpenDocumentFile(modelpath,OpenOptions)

#link_doc = DocumentManager.Instance.CurrentDBDocument

t=Transaction(link_doc,"LOC_Door")

t.Start()

doors=FilteredElementCollector(link_doc).OfCategory(BuiltInCategory.OST_Doors).WhereElementIsNotElementType().ToElements()

for d in doors:

    d.LookupParameter("BY_LOC_Niveau").Set("R+04")

    

t.Commit()

link_doc.SynchronizeWithCentralOptions(tOptions,sOptions)

link_doc.Close(1)

doc.GetElement(linkcollector[0].GetTypeId()).Load()

OUT = ""

Can anyone help me with this ?
Dyn: 2.0.3
Revit: 19.2.1.1

I tried 2 loops of 100 steps in which I :

  1. just open and save and reload the link, without changing the model (no error)

  2. do the same loop but this time I increment a parameter in each loop from R+0 to R+99 and …

image
100 save later …
image
it works and saves but still crashes at the last step (same fatal error).

I don’t get how transaction works in links, my way of doing may be wrong, any help will be appreciated ! :slight_smile: