Units, Quantities, Schemas and Translations

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
berniev
Posts: 247
Joined: Wed Apr 13, 2022 10:45 pm
Location: Oz

Units, Quantities, Schemas and Translations

Post by berniev »

FYI I have extracted from thousands of lines of code and classes etc the raw data behind units, quantities, schemas and translations. Doing this has made them much more understandable.

Units look like:

Code: Select all

//                                                                                                                               Angle
//                                                                                                               LuminousIntensity   |
//                                                                                                           AmountOfSubstance   |   |
//                                                                                                ThermodynamicTemperature   |   |   |
//                                                                                                     ElectricCurrent   |   |   |   |
//                                                                                                           Time    |   |   |   |   |
//                                                                                                        Mass   |   |   |   |   |   |
//                                                                                                  Length   |   |   |   |   |   |   |
//                                                                                                       |   |   |   |   |   |   |   |
//                                                            Name                                       |   |   |   |   |   |   |   |
/**
 * Null type
 */
constexpr UnitSpec uNull                                   { "Null"                                  ,{  0,  0,  0,  0,  0,  0,  0,  0 } };

/**
 * Base SI units
 */
constexpr UnitSpec uLength                                 { "Length"                                ,{  1,  0,  0,  0,  0,  0,  0,  0 } };
constexpr UnitSpec uMass                                   { "Mass"                                  ,{  0,  1,  0,  0,  0,  0,  0,  0 } };
constexpr UnitSpec uTimeSpan                               { "TimeSpan"                              ,{  0,  0,  1,  0,  0,  0,  0,  0 } };
constexpr UnitSpec uElectricCurrent                        { "ElectricCurrent"                       ,{  0,  0,  0,  1,  0,  0,  0,  0 } };
constexpr UnitSpec uTemperature                            { "Temperature"                           ,{  0,  0,  0,  0,  1,  0,  0,  0 } };
constexpr UnitSpec uAmountOfSubstance                      { "AmountOfSubstance"                     ,{  0,  0,  0,  0,  0,  1,  0,  0 } };
constexpr UnitSpec uLuminousIntensity                      { "LuminousIntensity"                     ,{  0,  0,  0,  0,  0,  0,  1,  0 } };
constexpr UnitSpec uAngle                                  { "Angle"                                 ,{  0,  0,  0,  0,  0,  0,  0,  1 } };

/**
 * Other base units are built on combinations of base SI units
 */
constexpr UnitSpec uAcceleration                           { "Acceleration"                          ,{  1,  0, -2,  0,  0,  0,  0,  0 } };
constexpr UnitSpec uArea                                   { "Area"                                  ,{  2,  0,  0,  0,  0,  0,  0,  0 } };
etc.

Quantities:

Code: Select all

//                                             Name                   Factor              Unit
constexpr QuantitySpec qNanoMetre           { "NanoMetre"           , 1.0e-6            , uLength                };
constexpr QuantitySpec qMicroMetre          { "MicroMetre"          , 1.0e-3            , uLength                };
constexpr QuantitySpec qMilliMetre          { "MilliMetre"          , 1.0               , uLength                };
constexpr QuantitySpec qCentiMetre          { "CentiMetre"          , 10.0              , uLength                };
constexpr QuantitySpec qDeciMetre           { "DeciMetre"           , 100.0             , uLength                };
constexpr QuantitySpec qMetre               { "Metre"               , 1.0e3             , uLength                };
constexpr QuantitySpec qKiloMetre           { "KiloMetre"           , 1.0e6             , uLength                };
constexpr QuantitySpec qInch                { "Inch"                , 25.4              , uLength                };
constexpr QuantitySpec qFoot                { "Foot"                , 304.8             , uLength                };
constexpr QuantitySpec qThou                { "Thou"                , 0.0254            , uLength                };
constexpr QuantitySpec qYard                { "Yard"                , 914.4             , uLength                };
etc

Schema translations:

Code: Select all

//                                                                      Scaled Factor
//                                                           Scaled UnitStr         |
//                                                   Scaled LessThan      |         |
//  UnitSpec                              UnitString     Factor    |      |         |
//--------------------- SI1 -------------------------------------------

/**
 *
 */
constexpr UnitTranslations SI1 {{
    { uAngle,                              "°",           1,     {}},
    { uArea,                               "km^2",        1e12,  {{
                                                                 { 100,  "mm^2",    1    },
                                                                 { 1e6,  "cm^2",    100  },
                                                                 { 1e12, "m^2",     1e6  }}}},
    { uDensity,                            "kg/mm^3",     1,     {{
                                                                 { 1e-4, "kg/m^3",  1e-9 },
                                                                 { 1,    "kg/cm^3", 1e-3 }}}},
    { uForce,                              "MN",          1e9,   {{
                                                                 { 1e3,  "mN",      1.0 },
                                                                 { 1e6,  "N",       1e3 },
                                                                 { 1e9,  "kN",      1e6 }}}},
etc

and schemas:

Code: Select all

//                                    Description
//                                Is default    |
//                  Has multiple angles    |    |
//            Has multiple lengths    |    |    |
//             BaseLengthStr     |    |    |    |
//  Name                   |     |    |    |    |                                        Translations
{{ "SI1",                 "mm",  "",  "", "Y", "Standard (mm/kg/s/degree)"            }, SI1                 },
{{ "SI2",                 "mm",  "",  "",  "", "MKS (m/kg/s/degree)"                  }, SI2                 },
{{ "Centimeters",         "cm",  "",  "",  "", "Building Euro (cm/m²/m³)"             }, Centimetre          },
{{ "MmMin",               "mm",  "",  "",  "", "Metric small parts & CNC(mm, mm/min)" }, MmMin               },
{{ "FemMilliMeterNewton", "mm",  "",  "",  "", "FEM (mm, N, s)"                       }, FemMilliMeterNewton },
{{ "Imperial1",           "in",  "",  "",  "", "US customary (in/lb)"                 }, Imperial1           },
{{ "ImperialDecimal",     "in",  "",  "",  "", "Imperial decimal (in/lb)"             }, ImperialDecimal     },
{{ "ImperialBuilding",    "ft", "Y",  "",  "", "Building US (ft-in/sqft/cft)"         }, ImperialBuilding    },
{{ "ImperialCivil",       "ft",  "", "Y",  "", "Imperial for Civil Eng (ft, ft/sec)"  }, ImperialCivil       }
}};
All these things are really just simple data.
berniev
Posts: 247
Joined: Wed Apr 13, 2022 10:45 pm
Location: Oz

Re: Units, Quantities, Schemas and Translations

Post by berniev »

Unit types

Code: Select all

YieldStrength,                       
YoungsModulus,                       
UltimateTensileStrength,             
ShearModulus,                        
Stress,                              
AngleOfFriction,                     
Magnetization,                       
VolumetricThermalExpansionCoefficient
are actually aliases of other unit types i.e. they share the exact same structure except for the name (which goes on to be called the type).

I guess this is by design, but it feels wrong?
Post Reply