Get Titleblocks Width and Length and write them on Sheets parameters

Hello,

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 ?

link

Here is the error I get now :

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

1 Like

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

Hi again,

So now I tried to create the transaction and to set a “test” value in a specific Shared Parameter (defined by it’s Guid)

I have an issue about transaction and sub-transaction… Is it possible to help me identify it ?

Here is the script

Thank you

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
1 Like

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.

Thank you again, and have a nice day

It’s working now with this script !

Two questions to enhance it :slight_smile:

1 - Is there a way to get the value coming from those Parameter with two decimals, instead of 1 decimal and then convert them to centimeters ?

image

tblock.get_Parameter(DB.BuiltInParameter.SHEET_WIDTH).AsValueString(),
tblock.get_Parameter(DB.BuiltInParameter.SHEET_HEIGHT).AsValueString(),

For example, for A3 paper size I would like to see “42.0 x 29.7” instead of “0.42 x 0.30”

( I tried to use other methods that AsValueString() but the result is inconsistent or Null )

2- is there a way to Add or Replace the vaue by format for predefined format ?

For example, for A3 paper size I would like to see “A3” or “A3 - 42.0 x 29.7” instead of “42.0 x 29.7”

At which stage do you think that I could do those operations on values ?

Thank you

1 Like
  1. a simple float("0.42")*100
  • 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)
  • there is also a way to use the replace method
1 Like

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

1 Like

In fact, to get the right dimensions, I need to multiply by 30.48 the two parameter values AsDouble… not 25.412

Is this a known value for units conversion ?

It’s maybe not the nicest way to do it, but it works !

Here is the script

Revit stores dimensions internally as imperial units. For conversion I would rather use the built-in UnitUtils class like:

def convert_mm(int_double):
    return DB.UnitUtils.ConvertFromInternalUnits(int_double, DB.DisplayUnitType.DUT_MILLIMETERS)

or similar for centimeters

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.

@danselkaz
:grin:no just a typo thanks to discourse shortcut for bold characters… I typed the asterisk character twice *

25.4 (mm for one inch) x 12 (12 inches in 1 foot)
revit being coded in feet