How to open/close workset in a linked revit programmatically

How to open/close workset in a linked revit programmatically

Anonymous
Not applicable
2,292 Views
7 Replies
Message 1 of 8

How to open/close workset in a linked revit programmatically

Anonymous
Not applicable

I am trying to open/close worksets in a linked revit. I can't change the workset.Isopen because it is readonly.

Anybody has an idea,

Thanks

 

 

2,293 Views
7 Replies
Replies (7)
Message 2 of 8

PerryLackowski
Advocate
Advocate

Here's a complete solution for PyRevit as of 2020.1. The API does not have an easy way to set a workset open or closed - instead you need to reload the link with a new workset configuration. This means creating a workset configuration that defaults to closing all worksets, then loading it up with all the worksets you want to remain open. Then you pass this into the LoadFrom method to reload the link.

'''
Goes to Manage > Manage Links > Manage Worksets and opens the Shared Levels and Grids workset for each LOADED link.

TESTED REVIT API: 2020

Author: Robert Perry Lackowski

'''
from pyrevit import DB
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB import FilteredElementCollector
from Autodesk.Revit.DB import Transaction
from Autodesk.Revit.DB import WorksharingUtils
from Autodesk.Revit.DB import WorksetConfiguration
from Autodesk.Revit.DB import RevitLinkType
from Autodesk.Revit.UI import TaskDialog

from pyrevit import script
output = script.get_output()

import traceback

doc = __revit__.ActiveUIDocument.Document
app = __revit__.ActiveUIDocument.Application.Application

# Set the name of the workset you would like to hide. Decide if you want to hide it or unhide it.
hide_this_workset = "Shared Levels and Grids"
hide_workset = False

for link_doc in app.Documents:
    
    link_doc_title = link_doc.Title
    print link_doc_title

    # Exclude the project file itself
    if not link_doc.IsLinked:
        print "this one isn't linked, skip the rest of the for loop \n\n "
        continue
    
    # Get a list of the RevitLinkType elements in the active document.
    revit_link_types = FilteredElementCollector(doc).OfClass(DB.RevitLinkType).ToElements()
 
    # This finds the RevitLinkType element that matches the link_doc.
    rlt = next((rlt for rlt in revit_link_types if rlt.LookupParameter("Type Name").AsString().rstrip(".rvt") == link_doc_title), None)
    if not rlt:
        print "No matching RevitLinkType for the link_doc."
    else:
        temp = "Updating workset visibility on this file: " + str(link_doc.Title)
        print temp

        # generate a list of the worksets in the link_doc
        workset_table = link_doc.GetWorksetTable()
        model_path = link_doc.GetWorksharingCentralModelPath()
        lst_preview = WorksharingUtils.GetUserWorksetInfo(model_path)

        # Populate list of worksets to enable
        open_this_list_of_worksets = []
        
        for objWorksetPreview in lst_preview:
            wkset = workset_table.GetWorkset(objWorksetPreview.Id)
            
            if hide_workset:
                #gather a list of all the open worksets, EXCLUDING the workset we want to hide
                if wkset.IsOpen and (wkset.Name != hide_this_workset):
                    open_this_list_of_worksets.append(wkset.Id)
            else:
                #gather a list of all the open worksets, INCLUDING the one we hid.
                if wkset.IsOpen or wkset.Name == hide_this_workset:
                    open_this_list_of_worksets.append(wkset.Id)
        
        # Set the WorksetConfiguration file to close all worksets, then open the ones from the list
        workset_config = WorksetConfiguration(WorksetConfigurationOption.CloseAllWorksets)
        workset_config.Open(open_this_list_of_worksets)
        
        #reload the link with the updated workset configuration
        rlt.LoadFrom(model_path, workset_config)
        
        #delete the workset configuration after use
        workset_config.Dispose()

print "Script complete."

 

Message 3 of 8

MYS-JonathanT
Participant
Participant

Hi @PerryLackowski 

I was trying this solution but it does not work since most of out projects are on Bim360. 

the WorksharingUtils.GetUserWorksetInfo does not take a cloud path and throws an Exception : 

Autodesk.Revit.Exceptions.ArgumentException: The given path path is a cloud path which is not supported in this method. (see here - https://www.revitapidocs.com/2021.1/15ec1e3e-61d5-b6a1-3604-8b866a988270.htm)

I  wondering if you have any idea how to get this to work if the Links are cloud path ?

Thanks , Jonathan . 

 

Message 4 of 8

tamas.deri
Advocate
Advocate

 

Have you tried with the model path accessed below?

ModelPath mpath = link_instance.GetLinkDocument().GetCloudModelPath()

 

https://www.revitapidocs.com/2022/087a7c14-1a6e-7022-c47b-923e90f4c5be.htm

0 Likes
Message 5 of 8

alfredior
Observer
Observer

Still no solution for this? I am trying to open worksets from model in ACC?

0 Likes
Message 6 of 8

richard.laroche
Contributor
Contributor

Welcome to the club.
I too am trying to open a cloud model and manage the opened/closed worksets.
So I see that WorksharingUtils.GetUserWorksetInfo does not work with CloudPath indeed.
So the only option is to open the document and tinker with the worksets after.
But... there seems to be no way to open or close a workset within an opened document (which seems really odd)?!??

Anyone has a lead on that?
I tried workset.IsOpen = True but the property is read only.
Various AI tools systematically assume that WorksetTable.SetOpen method exist (but it doesn't).
So I'm stuck with the dilemma of managing all my cloud models using 
WorksetConfigurationOption.OpenAllWorksets or  WorksetConfigurationOption.CloseAllWorksets
Help...

Message 7 of 8

PerryLackowski
Advocate
Advocate

The pyrevit code I posted above was the only way I have found to manipulate the worksets. But yes, it does require setting the configuration before reloading the link.

If that's not working with cloud model paths we may need to call in the big guns. @jeremytammik 

0 Likes
Message 8 of 8

alfredior
Observer
Observer

Same problem. Looks like the only way of doing this is to open ALL the models from the beginning. No other way until now.

Thanks for replying

0 Likes