Doc-worksharing-enabled hook

I’m trying to create a hook to automaticaly rename and create new worksets after worksharing is enabled.
I got the python script running through RPS without the hook, I got it working as a button, I got the hook working with a simple alert. but I can’t seem to get the python script running with the hook. Is it because the hook is run before the worksets window is opened? Or is it because Revit is still in a transaction?

mind sharing a bit of the code?

might be the way you grab the document. Showing the way you handle transaction for example
also

doc = __eventargs__.Document is what I use in a doc opened hook

and doc = __revit__.ActiveUIDocument.Document in a command hook

might be a clue as well

https://www.revitapidocs.com/2022/51879d12-03fd-0360-d6e1-ce1dcbccbe2d.htm

Documentation says it is allowed to make modifications during this event. Luckily

I’ll share the code when I get into the office

from pyrevit import forms

from pyrevit import EXEC_PARAMS

forms.alert("Het model is nu een Central Model")

import clr

clr.AddReferenceByPartialName('PresentationCore')

clr.AddReferenceByPartialName("PresentationFramework")

clr.AddReferenceByPartialName('System')

clr.AddReferenceByPartialName('System.Windows.Forms')

clr.AddReferenceByPartialName('RevitAPI')

from Autodesk.Revit import DB

from Autodesk.Revit import UI

# Define list/unwrap list functions

def tolist(input):

    result = input if isinstance(input, list) else [input]

    return result

def uwlist(input):

    result = input if isinstance(input, list) else [input]

    return UnwrapElement(result)

# Current doc/app/ui

uidoc = __revit__.ActiveUIDocument

av = __revit__.ActiveUIDocument.ActiveView

doc = __revit__.ActiveUIDocument.Document

# Collect all worksets

collector = DB.FilteredWorksetCollector(doc)

worksets  = collector.OfKind(DB.WorksetKind.UserWorkset).ToWorksets()

# Ensure iterable

if not hasattr(worksets, "__iter__"):

    worksets = [worksets]

# Get Workset names

old_names = []

[old_names.append(w.Name) for w in worksets]

rename_names = ["KOW_bouwkundig", "Levels and Shared Grids"]

new_names = ["KOW_constructie", "KOW_situatie"]

# Preparing input from dynamo to revit

wkst_ele = worksets

wkst_nam = rename_names

new_worksets,new_ids,new_bools = [],[],[]

# Do some action in a Transaction

t = DB.Transaction(doc, 'Configure Worksets')

t.Start()

#rename worksets "Workset1" and "Shared Levels and Grids"

wksts,bools = [],[]

for w,n in zip(wkst_ele, wkst_nam):

    DB.WorksetTable.RenameWorkset(doc,w.Id,n)

    wksts.append(w)

    bools.append(w.Name == n)

#create new worksets with the given names

for n in new_names:

    try:

        w_r = DB.Workset.Create(doc, n)

        new_worksets.append(w_r)

        new_ids.append(w_r.Id.IntegerValue)

        new_bools.append(True)

    except:

        new_worksets.append(None)

        new_ids.append(None)

        new_bools.append(False)

t.Commit()

tried and adapted the code for french.
It worked as expected as a button as mentionned but not as an hook
the error message makes sense:

Traceback (most recent call last):
    File "C:\pyRevit\B1_ToolBar.extension\hooks\doc-worksharing-enabled.py", line 45, in <module>
    File "C:\pyRevit\dev\dev\pyrevitlib\pyrevit\revit\db\transaction.py", line 60, in __enter__
    Exception: Cannot modify the document for either a read-only external command is being executed, or changes to the document are temporarily disabled.

because when you first enable the worksharing, a series of transaction are enabled in the background in Revit: It creates the worksets, design the file as central and opens up the worksets management windows. While all these operations are running in Revit, you cannot use your own transaction to do stuff.

you might want to try with another hook or stick with a button imho

1 Like