Warning script

Hi all, i want to make our companyworksets by using Dynamo. I’ve got help from AI gemini 2.0 Pro but there is a line not correct and giving a warning while not execute what i want.

NameError : name ‘Documentmanager’ is not
defined [’ File “”, line 22, in \n’]

I am getting the warning on this line: doc = DocumentManager.Instance.CurrentDBDocument

Importeer de benodigde Revit API libraries

import clr
clr.AddReference(‘RevitAPI’)
import Autodesk
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI import *

doc = DocumentManager.Instance.CurrentDBDocument

workset_names = [
“50_SPA”,
“52_HWI”,
“52_VWI”,
“53_TWI”,
“55_KLI”,
“56_CVI”,
“57_MVI”,
“LF_BWK”,
“LF_CNS”,
“LF_ADV”,
“LF_ELT”,
“LF_XXX”
]

t = Transaction(doc, “Maak Worksets”)
t.Start()

existing_worksets = FilteredWorksetCollector(doc).OfKind(WorksetKind.UserWorkset)
existing_workset_names = [ws.Name for ws in existing_worksets]

for name in workset_names:
try:
# Controleer of de workset al bestaat
if name not in existing_workset_names:
Workset.Create(doc, name)
print(“Workset ‘{}’ aangemaakt.”.format(name))
else:
print(“Workset ‘{}’ bestaat al.”.format(name))

except Exception as e:
    print("Fout bij het aanmaken van workset '{}': {}".format(name, e))

t.Commit()

Output

OUT = “Worksets aangemaakt/gecontroleerd.”

Maybe someone sees what i did wrong?

Thx in advance,

Regards Tim

import clr
clr.AddReference('RevitAPI')
clr.AddReference('RevitServices')

import Autodesk
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI import *

import RevitServices
from RevitServices.Persistence import DocumentManager

# Get the active Revit document
doc = DocumentManager.Instance.CurrentDBDocument

# List of workset names to create
workset_names = [
    "50_SPA",
    "52_HWI",
    "52_VWI",
    "53_TWI",
    "55_KLI",
    "56_CVI",
    "57_MVI",
    "LF_BWK",
    "LF_CNS",
    "LF_ADV",
    "LF_ELT",
    "LF_XXX"
]

# Start the transaction
t = Transaction(doc, "Maak Worksets")
t.Start()

# Collect existing user worksets
existing_worksets = FilteredWorksetCollector(doc).OfKind(WorksetKind.UserWorkset)
existing_workset_names = [ws.Name for ws in existing_worksets]

# Collect output messages
messages = []

for name in workset_names:
    try:
        # Check if the workset already exists
        if name not in existing_workset_names:
            Workset.Create(doc, name)
            messages.append("Workset '{}' aangemaakt.".format(name))
        else:
            messages.append("Workset '{}' bestaat al.".format(name))
    except Exception as e:
        messages.append("Fout bij '{}' : {}".format(name, str(e)))

# Commit the transaction
t.Commit()

# Return the messages to the Dynamo OUT port
OUT = messages

2 Likes

Thank you! So clr.AddReference(‘RevitServices’) was the thing that was missing?

Yes you were missing that.
I changed the quotes too.
Also you were missing an except (always need one with a try)

1 Like