SetOpenWorksetsConfiguration

Hi everyone,

I making a script to set wich worksets open when a document is opend, rest needs to stay closed.
it seemsed logical to sue the methode in the title:
GetOpenWorksetsConfiguration Method (revitapidocs.com)

right now it seems i can select and set the values i want.
But i can’t close the document through the revit api and when i try to save the values and use them for the second part of the script (when it opens back up) nothing really happens.

Any insights on this?
Could it be possible to code this into a Hook: doc-opening.* or would this be ‘to late’ to run the code for setting worksets?

my current code:

# -*- coding: utf-8 -*-

# imports

# ===================================================

import datetime

starttime = datetime.datetime.today()

from Autodesk.Revit.DB import FilteredWorksetCollector, WorksetKind,WorksetId, WorksetConfiguration, WorksetConfigurationOption

from System.Collections.Generic import List

from pyrevit import forms, revit, DB

# doc

# ===================================================

doc = revit.doc

uidoc = revit.uidoc

# definitions

# ===================================================

# Main

# ===================================================

# Get all user-created worksets in the document

doc = revit.doc

worksets = FilteredWorksetCollector(doc).OfKind(WorksetKind.UserWorkset)

# Get workset names and ids

workset_names = [ws.Name for ws in worksets]

workset_ids = [ws.Id for ws in worksets]

# Let user select worksets to open

selected_names = forms.SelectFromList.show(workset_names, title='Select Worksets to Open', multiselect=True)

if not selected_names:

forms.alert('No worksets were selected',exitscript= True)

# Get ids of selected worksets

selected_ids = [workset_ids[workset_names.index(name)] for name in selected_names]

# Create WorksetConfiguration to open only selected worksets

workset_config = WorksetConfiguration(WorksetConfigurationOption.CloseAllWorksets)

workset_config.Open(List[WorksetId](selected_ids))

# Save and close the document

doc.Save()

doc.Close()

# OpenOptions to open document with specified worksets

open_opt = DB.OpenOptions()

open_opt.SetOpenWorksetsConfiguration(workset_config)

# Open the document with the new WorksetConfiguration

model_path = DB.ModelPathUtils.ConvertUserVisiblePathToModelPath(doc.PathName)

revit.uidoc.Application.OpenDocumentFile(model_path, open_opt)

Opening a closing files is a tricky topic.
you basically cannot use doc.Close() to close the active document.
You have to trick it using a placeholder to you open to be able to close the previously active doc
You will find plenty info looking for this specific topic googling it

I Came across something similar but with a different approach which could help

1 Like