Magnitude of value with units

konstantin_shiriazdanov
konstantin_shiriazdanov Member Posts: 1,221 ✭✭✭✭✭
How to extract unitless magnitude of value with units if the units are unknown? Or what is internal structure of ValueWithUnits type, is it of map type?

Comments

  • paul_chastell
    paul_chastell Onshape Employees Posts: 133 image
    edited July 2017
    I would suggest you divide by the unit you are interested in, and ignore the internal representation. If you have a length and what to know how many meters it is then you can do "var lengthInMeters = length / meter". It doesn't matter what unit 'length' has. Or "length / inch" to get the length in inches. In both cases the result is a unitless value.

    E.g. this custom feature measures the distance between two points, in inches, and logs it to the console

    <div>annotation { "Feature Type Name" : "Distance in Inches" }
    </div><div>export const inchDistance = defineFeature(function(context is Context, id is Id, definition is map)
    </div><div>&nbsp; &nbsp; precondition
    </div><div>&nbsp; &nbsp; {
    </div><div>&nbsp; &nbsp; &nbsp; &nbsp; annotation { "Name" : "Points", "Filter" : EntityType.VERTEX, "MaxNumberOfPicks" : 2 }
    </div><div>&nbsp; &nbsp; &nbsp; &nbsp; definition.points is Query; &nbsp; &nbsp; &nbsp; &nbsp;
    </div><div>&nbsp; &nbsp; }
    </div><div>&nbsp; &nbsp; {
    </div><div>&nbsp; &nbsp; &nbsp; &nbsp; if (size(evaluateQuery(context, definition.points)) == 2)
    </div><div>&nbsp; &nbsp; &nbsp; &nbsp; {
    </div><div>&nbsp; &nbsp; &nbsp; &nbsp;     var result = evDistance(context, {
    </div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "side0" : qNthElement(definition.points, 0),
    </div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "side1" : qNthElement(definition.points, 1)
    </div><div>&nbsp; &nbsp; &nbsp; &nbsp;     });
    </div><div>&nbsp; &nbsp; &nbsp; &nbsp;     println("Distance in inches is: " ~ (<b>result.distance / inch</b>));
    </div><div>&nbsp; &nbsp; &nbsp; &nbsp; }
    </div><div>&nbsp; &nbsp; });
    
    </div>
    Paul Chastell
    TVP, Onshape Labs
  • ilya_baran
    ilya_baran Onshape Employees, Developers, HDM Posts: 1,278 image
    Paul is absolutely right.  But if you're curious about what the representation is, you can look at units.fs in std or do println("" ~ inch);
    Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc
  • konstantin_shiriazdanov
    konstantin_shiriazdanov Member Posts: 1,221 ✭✭✭✭✭
    ok, thank you Ilya and Paul, I found out that ValueWithUnits is internally a map and "value" field contains magnitude