How to open a revit file with option to close worksets and all Revit links unloaded with revit api python

how to open a revit file with option to close worksets? I mean do not open the revit file until you close the worksets, then open it, it is an option that I see as open revit file with option specify Worksets

This enumeration might help - WorksetConfigurationOption Enumeration

It’s usually used by this method of the WorksetConfiguration class - WorksetConfiguration Constructor (WorksetConfigurationOption)

Which can be called by this method in the OpenOptions class - SetOpenWorksetsConfiguration Method

Which is called by OpenDocumentFile method of the Application class - OpenDocumentFile Method (ModelPath, OpenOptions)

not sure if works closing worksets because revit links are not necessarily in a workset but they are loaded automatically when loading a revit file, I find it very hard to resolve, each time I open a revit file with dynamo I see this in the progress bar, “Updating References” and I do not want to update any references because takes ages to open the file.
image

I tried this myself by following this post Open files without loading linked documents - #7 by john_pierson but I still see this thing of Updating references, I do not see difference:

def UnloadRevitLinks(path):
    """Unloads Revit links from a file by modifying the transmission data."""
    mPath = ModelPathUtils.ConvertUserVisiblePathToModelPath(path)
    tData = TransmissionData.ReadTransmissionData(mPath)
    
    # If transmission data is not found, the file may not have any external references
    if tData is None:
        return path
    
    # Collect all external references in the model and disable Revit links
    externalReferences = tData.GetAllExternalFileReferenceIds()
    for refId in externalReferences:
        extRef = tData.GetLastSavedReferenceData(refId)
        if extRef.ExternalFileReferenceType == ExternalFileReferenceType.RevitLink:
            # Unload the Revit link by setting the desired reference data to "not loaded"
            tData.SetDesiredReferenceData(refId, extRef.GetPath(), extRef.PathType, False)
    
    # Ensure the IsTransmitted property is set
    tData.IsTransmitted = True
    # Save the modified transmission data back to the model
    TransmissionData.WriteTransmissionData(mPath, tData)
    
    return path

def open_document(file_path):
    """Opens a Revit document with Revit links unloaded."""
    # Unload Revit links before opening the document
    UnloadRevitLinks(file_path)
    
    model_path = ModelPathUtils.ConvertUserVisiblePathToModelPath(file_path)
    options = OpenOptions()
    options.DetachFromCentralOption = DetachFromCentralOption.DetachAndPreserveWorksets
    
    try:
        doc = app.OpenDocumentFile(model_path, options)
        if doc is None or doc.IsLinked:
            raise Exception("Failed to open document or document is a linked file.")
        return doc
    except Exception as e:
        error_messages.append("Error opening Revit document '{}': {}".format(Path.GetFileName(file_path), str(e)))
        return None

So the real issue isn’t opening a file with links closed, but opening a file without links?

I believe that links shouldn’t be loaded if they’re in a closed workset, but if your file isn’t workshared that won’t help.

Is this for background processing or live file?

opening a file that is not opened, yes background or whatever as the function def open_document(file_path):

Can you confirm that the UI doesn’t load the links if you open the file with all worksets closed? If so this might be a difference between the API and the UI which the Revit development team might want to look into.

I’m on holiday for another half week or so with no plans on being near an active Revit session, but I can try to take a look at the specifics here when I get back.

1 Like

I just read Updating references in the Revit progress bar, so I do not even know if the function to close worksets is doing nothing I am not able to understand what script is doing, I do not care about the worksets really I just I want to load the revit files fast without Revit links loaded because takes ages to open

If you open the file manually with worksets closed do the links reload?

1 Like

no, they did not load. I put all revit links within a workset. file opened manually very fast, I want to do the same with Dynamo and I sared the functions I tried but maybe it does not work with other revit files as it takes ages to process the opening of the revit files with dynamo still

Ok. Good to know that opening with worksets closed keeps things set.

Now we can make some assumptions and come up with a plan.

The ‘fastest’ a file will ever open is the time it takes in the UI, so time from the point you hit ‘open’ on a workshared file using a stop watch.

Then try the code to open that same file with all worksets closed and see how long that takes to execute. Don’t do anything else (multiple files, etc.). Just open it.

Look at the difference for ‘run’ vs ‘open’ and see how far you’re off.

If it looks off provide a .rvt and .dyn for testing purposes and the community can try to help out.

1 Like