Formatting unit strings

What methods can I use to create a properly formatted unit string according to the project units?

If the value you are formatting resides in a Revit parameter, the answer is simple and a complete solution immediately provided by the Revit API. On one hand, the Parameter class provides the following public methods to read and write its parameter value:

  • AsDouble: Provides access to the double precision number within the parameter.
  • AsElementId: Provides access to the element id stored within the parameter.
  • AsInteger: Provides access to the integer number within the parameter.
  • AsString: Provides access to the string contents of the parameter.
  • Set: Defines a new value for the parameter.

On the other hand, you can ask the parameter to return its current value as a preformatted string using the current unit settings:

  • AsValueString: Get the parameter value as a string with unit.

It also provides access to set the value making use of such a formatted string with units:

  • SetValueString: Set the parameter value according to the input string.

If the value you wish to format does not reside in a parameter, you do not have access to these methods.
In that case, you might be able to temporarily hijack some existing parameter on an existing element and use that for your formatting task.

Unfortunately, you cannot simply create an own parameter for temporary use as a formatting helper, because the constructor is for internal use only. This is due to the fact that a parameter cannot live on its own; it is linked to a definition and needs to be hooked up properly in the Revit database system to work.

If you wish to avoid the hijacking, you could create an own shared parameter for an element category that occurs only once in the project and use that. How to do this is discussed the post on

adding a shared parameter to a DWG file
,
and the

Revit API introduction labs

provide samples for creating and analysing parameters in Revit.

Finally, for full control over the formatting details, you can query Revit for the current unit settings using the ProjectUnit class and use that information to do the formatting yourself as you see fit.
Querying the unit settings is demonstrated by the ProjectUnit SDK sample.


Comments

5 responses to “Formatting unit strings”

  1. Hello Jeremy. Sorry for my English. Thank you for your blog.
    Hm… I am create parameter “Length” type (project unit setup for length “Feet and fractional lines” rounding ‘to the nearest 1/32″ ), and I set random double value…
    When I am trying use AsValueString() to retrieve string, data returned correct, but only not rounded correctly… like (123’ 3 47/256″)
    am I something missing? How to round length or other parameter type data? hope for your respond…

  2. Dear Ruslan,
    AsValueString performs the same rounding as Revit does for the user interface. What is ‘correct’ is a very debatable topic. If you are not happy with the Revit formatting, you should not use it. Instead, use AsDouble to retrieve the raw database value in the standard database unit and round it and format it any way you please.
    Cheers, Jeremy.

  3. Hi Jeremy.
    I have a strange behavior of AsValueString method. All sources and you also wrote, that:”AsValueString performs the same rounding as Revit does for the user interface”.
    In Revit UI I have several properties:
    Fitting Outside Diameter = 206.4 mm
    Center to End Label = 115.9 mm
    etc.
    But when I try to get values of these parameters through AsValueString it returns respectively:
    Fitting Outside Diameter = 206 mm
    Center to End Label = 116 mm
    Do you have any idea what could I miss or do wrong?
    (I’m working with Revit Architecture 2014).
    Thank you,
    Olga

  4. Dear Olga,
    Nope, sorry, I have never heard of such an issue before.
    Cheers, Jeremy.

  5. What a pity!
    But anyway – thank you for your reply.
    I will look for other way of getting parameters.
    Regards,
    Olga

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading