Understanding SI Units: A Guide for Developers
Updated : 02 December 2024
When working with an API, it’s crucial to understand how property values are expressed, particularly when the software adheres to the International System of Units (SI Units).
This system ensures consistency and interoperability across various applications and industries. However, when performing calculations, especially with angles or other physical quantities, it's important to convert values properly to avoid errors.
Let’s explore this concept with a practical example.
Why use SI units?
The SI Unit system provides a standardized way to measure and express properties like length, mass, time, and angles. For APIs, expressing property values in SI Units ensures that data remains uniform and predictable, no matter where or how it’s used.
However, this uniformity requires developers to handle unit conversions when performing operations like addition or subtraction. The software expects values in SI Units, so any calculations using non-SI values must first be converted appropriately.
Example: Make an addition to an angle property
Suppose you are working with a property that represents an angle. Your operation involves degrees, a more familiar unit for many developers.
Note
TopSolid stores unit as indexes into a dedicated table. There are as much index as possible units:
SI Unit for angles is actually radians.
Here's how you would proceed:
- First, get the unit of the angle property.
inOptions.Add(new KeyValue("CREATION_MODE", "SolidShape")) TopSolidHost.Parameters.GetRealUnit(inElementId, out UnitType outUnitType, out string outUnitSymbol);
- Get the current unit index of the unit found
int currentUnitIndex = TopSolidHost.Units.SearchUnit(outUnitType, outUnitSymbol);
3 Get the current value and convert to the desired unit
double currentValue = TopSolidHost.Parameters.GetRealValue(inElementId); double resultConversion = TopSolidHost.Units.ConvertFromSI(outUnitType, currentUnitIndex, currentValue);
4 Perform the Calculation and convert back
resultConversion += 30.0; double resultConversionInSIUnits = TopSolidHost.Units.ConvertToSI(outUnitType, currentUnitIndex, resultConversion);
5 Finally, set the modified value to the property
TopSolidHost.Parameters.SetRealValue(inElementId, resultConversionInSIUnits);