How to rename Worksets?

python

#1

Hi - I am trying to batch rename Worksets. Admittedly I don’t have much experience with python and am really guessing at the modifications required to rename in batch vs. by single workset.

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

clr.AddReference(‘RevitNodes’)
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
clr.ImportExtensions(Revit.Elements)

clr.AddReference(‘RevitServices’)
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument

#Converting input from Dynamo to Revit

workset = UnwrapElement(IN[0])
names = IN[1]

#rename workset in a transaction
TransactionManager.Instance.EnsureInTransaction(doc)

WorksetTable.RenameWorkset(doc,workset.Id,name);

TransactionManager.Instance.TransactionTaskDone()

OUT = worksets
";

Complete script;

ReName Worksets.dyn (14.4 KB)

Thanks for any help!!


#2

You have to iterate over your lists of worksets and names using a ForLoop and zip()

import clr

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *

doc = DocumentManager.Instance.CurrentDBDocument
uidoc = DocumentManager.Instance.CurrentUIDocument

worksets = UnwrapElement(IN[0])
names = IN[1]

TransactionManager.Instance.EnsureInTransaction(doc)

for workset, name in zip(worksets,names):
	WorksetTable.RenameWorkset(doc,workset.Id, name)

OUT = worksets

Let me know if this works


#3

Hi @awilliams - thank you for your help!!

I get;
IronPythonEvaluator.EvaluateIronPythonScript operation failed.
unexpected token ‘‘’


#4

Hmm I don’t see any extraneous apostrophes in the code I pasted so I’m thinking maybe just a copy/paste error? Try deleting all the contents of your Python script node and repasting


#5

This would pop up if you mixed tabs and spaces in the same file. I would just go through it and check that you are consistently using one or the other.


#6

I ran the script above and it worked without error so it must be a copy/paste slip up, but thank you for that info! I read the error quickly and thought I saw “’” (an apostrophe). Good to know in case I run into that error on my end at some point


#7

Ha, my bad. If it’s really just an apostrophe then you are totally right. It would not show that message. Any message about illegal characters or unexpected new line, could be caused by what I was referring to.


#8

Could you show a screen shot of your graph and error?


#9

Hi @awilliams, @Konrad_K_Sobon, @salvatoredragotta

I realized I was making a very beginner mistake :flushed: and importing the script as a string.

I am getting an error that the ‘GIven Workset Name is already in use’?

Thank you for your help!!


#10

That’s pretty self explaining…given name is already used. Workset names must be unique.


#11

I tyring to rename this worksets to uppercase but it appears that the code have an error the python show Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed.
Traceback (most recent call last):
File “”, line 21, in
TypeError: expected WorksetId, got inttest.dyn (22.7 KB)