How to use the same unit of measure in Revit and pyRevit?

Hi everyone, I am moving my first steps in pyRevit. I am using a metric template within my file. I am getting flow value from the ducts selected in Revit using this script:

for el in selection:
if el.LookupParameter(‘Flow’):

The values I obtain are different from the ones I can read in the properties palette, which are displayed in m^3/h. I tryed to solve the problem by using variables to convert the values directly inside pyRevit, but it is tricky because I have to pay attention every time I deal with numeric values. Another way I tryed is to obtain value .AsValueString(), and but I don’t think this is the best method, because in this case I obtain strings that report also the unit of measure. My question is: is there a way to use directly the same unit of measure automatically, to avoid undesired issues in my calculations?
Thank you in advice for your attention

AsValueString is probably the best method IMHO. converting in python, you might end up having rounding issues depending on the settings in revit.

1 Like

Thank you for your fast reply. Can I please ask how do you “clean” your string after you obtain it? I tryed list slicing, but I don’t think It is a good method because it depends on the number of charatcters I have to remove , that can be different every time I deal with different kind of parameters.

yourlist  = ['84468 m²', '6468 m²', '64878 m²']

if you are trying to remove a space and get first item of your sliced list

x.split(' ')[0] for x in yourlist

or removing the same characters set over and over

x.split(' m²') for x in yourlist

@Jean-Marc , do you have an excerpt of a script that shows how the “AsValueString” should be used?
I am trying to get an elevation of a level AsValueString but I keep getting errors.
Thank you!

and I forgot that one

    yourlist  = ['84468 m²', '6468 m²', '64878 m²']
    for x in yourlist:
        print (x[:-3])

or a slightly different flavor

print(list((x[:3]) for x in yourlist))
1 Like

something like this

Vals = []
for i in UnwrapElements([yourListOfRevitElements]]):
	Vals.append(i.LookupParameter("Type Id").AsValueString())

but there are a lot of examples all over the place:

or better

where you will have code samples in C# most of the time but also in python for the most common methods and properties

1 Like

This isn’t a ‘smart’ fix but it’s worked for me in similar situations… Units default to imperial when using python (API or Dynamo python nodes) so I just convert the units to sqm by multiplying the double by the conversion factor. For example to get the sqm value:

area = el.LookupParameter(‘Flow’).AsDouble()
area = area * 10.76391041671

This table is handy for it:

1 Like

Adding to the conversation, there is also the UnitFormatUtils as part of Revit API that allows all sorts of formatting conversions

See here :point_right: pyRevit/ at 92cb346eece706db810468984b3fa8564f14b16c · eirannejad/pyRevit · GitHub


Thank you very much, it worked!
Here an example based on a walls selection:

pp = param.DisplayUnitType
number = UnitUtils.ConvertFromInternalUnits(param.AsDouble(), pp)
print (number)

Thank you again :slight_smile:

1 Like

Thank you so much @Jean-Marc ! very helpful!! :+1: