Pyrevit Macro to detach from central

Hi ! i’d like to create a pyrevit button that when i open a central file ,it detaches the model from central and create a new local model . I have to do it everytime manually and i lack the knowledge to do it ,can somebody help me? thanks very much in advance ! :slight_smile:

Hi @wearevirtual
The code will look like this

# OpenAndActivateDocument and detach
from pyrevit import revit, DB, script, forms, HOST_APP
doc = revit.doc
output= script.get_output()
output.close_others()
app = HOST_APP.uiapp
try:
    filepath = forms.pick_file(file_ext='rvt')
    open_options = DB.OpenOptions()
    open_options.DetachFromCentralOption = DB.DetachFromCentralOption.DetachAndPreserveWorksets
    worksetConfiguration = DB.WorksetConfiguration(DB.WorksetConfigurationOption.OpenAllWorksets)
    open_options.SetOpenWorksetsConfiguration(worksetConfiguration)
    model_path = DB.ModelPathUtils.ConvertUserVisiblePathToModelPath(filepath)
    app.OpenAndActivateDocument(model_path, open_options, False)
except Exception as e:
    print("Error" + e)

for the button creation see Notion – The all-in-one workspace for your notes, tasks, wikis, and databases.

1 Like

Thank you very much! it works :slight_smile: you have no idea how helpful this is !! :smiley:

Hi ! so I created a button that prompts a window where i can choose the file again but unfortunately, it stays central

‘create a new local’
Do you want to open detached and Dave it in place as a new central file, or just saving the file as is?
Please explain the end purpose. The best you can.
What comes in,.what comes out and what for?

There are two different pieces of code
The two methods.
OpenDocumentFile
OpenAndActivateDoculentFile

One is at application level
The other at UI application level

@Jean-Marc Thank you for your quick reply.
What I want to do:
When I open a central file, I have to open it a second time and check the option ‘detach from central file’ to be able to have a version of the project that I can work on and modify.


I would like to have a button that checks this option automatically and opens the file as a detached editable version, with the possibility of saving this version as the central version.

that should get you closer

# OpenAndActivateDocument and detach
from pyrevit import revit, DB, script, forms, HOST_APP

doc = revit.doc
output = script.get_output()
output.close_others()

app = HOST_APP.uiapp

open_options = DB.OpenOptions()
open_options.DetachFromCentralOption = DB.DetachFromCentralOption.DetachAndPreserveWorksets

try:
    file = forms.pick_file()
    if file:
        if file.endswith(".rvt"):
            model_path = DB.ModelPathUtils.ConvertUserVisiblePathToModelPath(
                file)
            document = app.OpenAndActivateDocument(
                model_path, open_options, False)
            output.print_md('# Open and detach from central')
            output.print_md(
                "- Document **{}** processed".format(file.split("\\")[-1]))
        else:
            pass
except Exception as e:
    print(e)
1 Like

Thank you very much, I’m going to test it tonight. Have a nice day :slight_smile:

1 Like

It works. Thank you very much for you help and time !

1 Like