I choose a simple action to do to try to build my first tool in pyRevit.
So, I try to make what is in topic name : Get Titleblocks Width and Length and write them on Sheets parameters
I try to start by editing this tool : List TitleBlocks on Sheets.pushbutton
and try to print the values of Sheet Width and Sheet Length in the output window…
Then I would try to copy that values to sheet parameters (but that is the next step)
The script is here :
First of all, I had an error because of a “à” in the button_name. Is that impossible with an accentuated caracter ?
IronPython Traceback:
Traceback (most recent call last):
File “XXXXXXX\script.py”, line 43, in
File “XXXXXX\script.py”, line 28, in print_titleblocks
NameError: global name ‘DB’ is not defined
Script Executor Traceback:
IronPython.Runtime.UnboundNameException: global name ‘DB’ is not defined
à IronPython.Runtime.Operations.PythonOps.GetVariable(CodeContext context, String name, Boolean isGlobal, Boolean lightThrow)
à IronPython.Compiler.LookupGlobalInstruction.Run(InterpretedFrame frame)
à Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
à Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)
à System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2)
à Microsoft.Scripting.Interpreter.DynamicInstruction`4.Run(InterpretedFrame frame)
à Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
à Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)
à IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)
à PyRevitLabs.PyRevit.Runtime.IronPythonEngine.Execute(ScriptRuntime& runtime)
this line (and the one below): tblock.Parameter[DB.BuiltInParameter.SHEET_HEIGHT].AsValueString,
should be: tblock.get_Parameter(DB.BuiltInParameter.SHEET_HEIGHT).AsValueString(),
and you are also missing the following import from the top, thats why the exception was thrown: from pyrevit import DB
I first thought that it wasn’t working but it works ! Thank you Tamar !
I have now to grab those SHEET_WIDTH and SHEET_HEIGHT values and set associated sheet parameters with those values combined in a string …
Easy to say, but hard (for me) to do in Python only and without dynamo nodes as support … Let’s continue to explore, and once I find something interesting I will ask or share it there
You have a typo: sheet.get_Parameters(sheet_dimensions_guid).Set('test')
It is not plural. It is get_Parameter(). You should be getting an exception in your output window, always evaluate that before asking for help.
Anyways, you are better off using pyrevit’s own transaction wrapper like this:
with revit.Transaction('My Fancy Transaction'):
# do stuff here
Sorry about that I ask questions when I’m blocked… And I didn’t have any exception before the transaction issue.
My first try wad following this scheme I will try with pyrevit transaction wrapper.
if you have the data “0.42 x 0.30” in separate variables like a="0.42" and b = "0.30" str(float(a)*100) + " x " + str(float(b)*100)
if in a bunch, a = "0.42 x 0.30" you can a.split(" x ") and it will return a list result = ['0.42', '0.30'], you can then grab the first portion like so first = result[0] and last one last = result[-1] then go for the same trick as above with str(float(first)*100) + " x " + str(float(last)*100)
Thank you Jean-Marc, but in this case, 0.30 is an already rounded value of 0.297 …
But if I get the value coming from SHEET_WIDTH AsDouble, the unit is not consistent (imperial ? ) …
I think of a solution with “FormatOptions” but I cannot fin the right way to do this …
EDIT : I also found this but I’m not sure how to use it
if in imperial, without any additionnal module you can just multiply it by x 25.4 x 12, that won’t do any rounding.
there is also a conversion utils in the revit modules and pyrevit ones
here is an example for string formatting: size_string = '{} x {}'.format(width, height)
where width and height are the actual numeric values for the size.
If you want to include the name of the size like A3 you have to create a mapping table for the specific sizes.