UnitsOfMeasure object and example

By Augusto Goncalves

UnitsOfMeasure is an object that helps conversion of values from and to Inventor’s database units. Inventor internally always uses cm (centimeters) for length, radians for angle, kg (kilograms) for mass and seconds for time. But the user can choose to work in another units, which can be set by this object. The various API methods, for example, creation of sketch entities, always take values that are in the internal database units. When a SketchPoint object is created whose coordinates are say (5,5), the values are always in cm even though the user may have his default units set to inches. Another example is the creation of parameters using the API, the methods always take the values of the parameters in the internal database units.

Consider the scenario of a program that accepts new parameter values from an end-user using a form and passes this value to a method that updates the parameter value. The end-user assumes that the values that he enters are in the default units that he has set up and it would not be intuitive to ask the user to convert the values to the internal database units before passing it on to the API method that updates the parameter value.

The UnitsOfMeasure object is very useful, in this case, to allow conversion from one unit system to the other. This object supports the following methods to help convert values and strings from one unit type to another:

1. GetStringFromType(UnitsType as UnitsTypeEnum) as String
This method returns the string representation of the unit type for the UnitTypeEnum parameter that is passed in. For example, when kCentimeterLengthUnits is supplied, "centimeter" is returned. The items in the list box on the right are displayed by making use of this method.

2. GetTypeFromString(UnitsString As String) As UnitsTypeEnum
This method returns the reverse of the above method, i.e. it returns the UnitTypeEnum for the string representation that is passed in. For example, when centimeter or "cm" is passed in, kCentimeterLengthUnits is returned.

3. GetStringFromValue(Value As Double, UnitsSpecifier) As String
This method returns the value along with the units for the value and the unit type in which you want the returned value to be specified in. The unit type of the Value parameter is the internal units that Inventor uses and are converted to the desired unit type as specified by the UnitsSpecifier parameter, which can be a string or the UnitsTypeEnum. For example, if parameters 2.0 and "in" (or "inches" or kInchLengthUnits) are passed in, the string that would be returned is "0.787 in".

4. GetValueFromExpression(Expression As String, UnitsSpecifier) As Double
When an expression is passed in, this method returns the value in the internal units, the UnitsSpecifier is used to indicate the unit type of any number that is part of the string and which doesn’t have a string next to it to indicate its unit type. For example, if the string " 2 + 3 cm" and kInchLengthUnits is passed in, the unit type of 2 is inches and so the returned value would be 8.08 (the equivalent of 2in + 3cm in cm units).

The UnitsOfMeasure object can be queried from the Document object

‘get the units of measure object

oUOM = oDoc.UnitsOfMeasure


Comments

7 responses to “UnitsOfMeasure object and example”

  1. Hello Augusto,
    I see this is a pretty old post, and there may be new approaches.
    I my case I need the parameter Values as “mm”, “deg”, “bool” or “text” values (without the dimension – but that is not the challenge :-) ). So following this topic I could parse the expression to internal units. That’s fine so far. But then I needed to convert Lenght-Pars to “mm” and Ang-Pars to “deg” an leave the others as they are. Is there a simple way to “categorize” the paramaters without the need to compare the returned “GetTypeFromString”-Enum to all possible UnitsTypeEnum? So I would need some select case construction or a linq-query, wouldn’t I?
    Thank you for taking time on this.
    BR,
    Daniel

  2. Daniel,
    I believe the code below does what you described. There are a few if statements but it’s not what I would consider very complex.
    Public Sub UOMSample()
    Dim partDoc As PartDocument
    Set partDoc = ThisApplication.ActiveDocument
    Dim uom As UnitsOfMeasure
    Set uom = partDoc.UnitsOfMeasure
    Dim pi As Double
    pi = Atn(1) * 4
    Dim param As Parameter
    For Each param In partDoc.ComponentDefinition.Parameters
    If Not (param.Units = “Text” Or param.Units = “Boolean”) Then
    If uom.CompatibleUnits(param.expression, param.Units, “1”, “mm”) Then
    Debug.Print “Length param ” & param.name & ” = ” & param.Value * 10
    ElseIf uom.CompatibleUnits(param.expression, param.Units, “1”, “deg”) Then
    Debug.Print “Angle param ” & param.name & ” = ” & param.Value * (180 / pi)
    Else
    Debug.Print “Other param ” & param.name & ” = ” & param.expression
    End If
    Else
    Debug.Print “Other param ” & param.name & ” = ” & param.Value
    End If
    Next
    End Sub

  3. Hello Brian,
    thank you for the quick answer. I built something similar, just liked to ask if there was a “classification” per Type available. There is some textual reference in the API-Help… (it doens’t state there was a property for this, it’s just in textdescription….)
    Here is my solution in form of an extension method for the parameter object in case someone could use it:
    Public Function ConvertToKeytechValue(p As Parameter) As String
    Dim uom = DirectCast(p.Parent, Document).UnitsOfMeasure
    Dim ut As UnitsTypeEnum
    Try
    ut = uom.GetTypeFromString(p.Units)
    Catch ex As Exception
    Return p.Value
    End Try
    Dim utName As String = [Enum].GetName(GetType(UnitsTypeEnum), ut)
    Dim pval As Double
    Try
    pval = uom.GetValueFromExpression(p.Expression, ut)
    Catch ex As Exception
    Return p.Value
    End Try
    Select Case True
    Case utName.Contains(“Angle”)
    Return pval * (180.0 / Math.PI)
    Case utName.Contains(“Length”)
    Return pval * 10
    End Select
    Return “”
    End Function
    BR,
    Daniel

  4. I wonder if there is some code-syntax formatting/highlighting available on this blog… :)

  5. Daniel,
    Before Brian jump in, for more complex questions, the API Forum is the best route: https://forums.autodesk.com/t5/inventor-customization/bd-p/120
    Regards,
    Augusto Goncalves

  6. As Augusto said, the forum is a better place for questions. But for this question I went ahead and created a blog post that goes into more detail. Hopefully this will help.
    http://modthemachine.typepad.com/my_weblog/2017/04/units-and-parameters-in-inventor.html
    -Brian

  7. Typepad-HTML-E-Mail
    Hello Brian, Augusto
    Thanks for the effort.  Sure I know,  this is not a discussion platform.  I use the Forums normally for questions,  I just meant to ask here,  since it seemed to be related directly to this Blog entry.
    Thank you both again!
    BR,
    Daniel
    Gesendet mit TypeApp

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading