Quantity/fr

Quantity est une combinaison d'un nombre à virgule flottante et d'une unité. Quantity est utilisé dans l'ensemble de FreeCAD pour gérer les paramètres et toutes sortes d'autres entrées/sorties.

Généralité
Dans un système CAO ou CAE il est très important de garder une trace de l'unité d'une valeur. Beaucoup de problèmes peuvent survenir lors du mélange des unités ou des résultats de calculs dans des systèmes d'unités différentes. Une catastrophe célèbre est la perte de la sonde Mars Climate Orbiter à cause d'une confusion d'interprétation d'unité. Même dans le même système d'unité, les unités sont de type différents, et toujours adaptés à son domaine d'utilisation. Un petit exemple, la vitesse interprétée en km/h (voitures), en m/s pour la (robotique) ou calcul de l'avance de l'outil en mm/minute (fraisage). Un système de CAO dois rester fiable avec toutes les unités. Donc, il faut compter sur elle et utiliser la bonne unité pour les paramètres spéciaux.

C'est pour cela que la FreeCAD Quantity framework a été créé. Il comprend le code et les objets à traiter avec les unités, calculs d'unités, entrées utilisateurs, conversion dans d'autres systèmes d'unités et affichage des unités et des valeurs. À long terme, aucun paramètre ne devrait être dans FreeCAD, juste un nombre.

Unités supportées
L'analyseur de FreeCAD prend en charge un tas d'unités et d'unités-systme. Nous utilisons la lettre grecque pour micro " µ " mais peut être substituée par la lettre " u ".


 * Length
 * "nm"
 * "µm"
 * "mm"
 * "cm"
 * "dm"
 * "m"
 * "km"
 * "in"
 * "ft"
 * "thou"
 * "mil"
 * "yd"
 * "mi"

Todo: all the rest...

The detailed specification you find in the code:
 * Quantity lexer: http://sourceforge.net/p/free-cad/code/ci/master/tree/src/Base/QuantityParser.l
 * Quantity definitions: http://sourceforge.net/p/free-cad/code/ci/master/tree/src/Base/Quantity.cpp#l167

Internal representation
All physical units can be expressed as a combination of the seven SI-Units:



An easy way to express a Unit is a integer array of size 7 (number of base units) that defines what the unit is. The signature of the 7 base units are:
 * LENGTH: [1,0,0,0,0,0,0]
 * MASS: [0,1,0,0,0,0,0]
 * TIME: [0,0,1,0,0,0,0]
 * ELECTRIC CURRENT: [0,0,0,1,0,0,0]
 * THERMODYNAMIC TEMPERATURE: [0,0,0,0,1,0,0]
 * AMOUNT OF SUBSTANCE: [0,0,0,0,0,1,0]
 * LUMINOUS INTENSITY: [0,0,0,0,0,0,1]

From these 7 units, we are then able to express all derived units defined in Guide for the Use of the International System of Units (SI) and create new ones as needed such as for instance:
 * MASS DENSITY: [-3,1,0,0,0,0,0]
 * AREA: [0,2,0,0,0,0,0]

Since angle is physically dimensionless, but never the less important to a CAD system we add one more virtual unit for Angle. This makes a vector of 8 in the FreeCAD unit signature.

Units calculator
Often you are in need of calculating units from one system to another. For example you have old parameter tables with wired units. In that cases FreeCAD offers a conversion tool called Units-Calculator which helps in translating units.

Its description in detail is here: Std_UnitsCalculator

InputField
The InputField is a QLineEdit derived Qt widget to handle all kind of user interaction with Quantities and parameters. It features following properties:
 * parsing arbitrary value/unit input
 * checking on the right unit (if given) and give the user feedback
 * special context menu for operations on Quantities/Values
 * history management (save the last used values)
 * save often needed values as shortcut in context menu
 * dialing values with mouse wheel and arrow keys (tbd)
 * dialing with middle mouse button and mouse move (tbd)
 * python integration for usage in python only dialogs (tbd)

The UnitsCalculator uses the InputField already.

Main docu: InputField

Code:
 * http://sourceforge.net/p/free-cad/code/ci/master/tree/src/Gui/InputField.h
 * http://sourceforge.net/p/free-cad/code/ci/master/tree/src/Gui/InputField.cpp

Python scripting
The Unit and Quantity system in FreeCAD is (as nearly everything) fully accessibly via Python.

Unit
The Unit class represents the Fingerprint of any physical unit. As descriped in the Basics section a vector of 8 numbers is used to represent this fingerprint. The Unit class allows the handling and calculation with this information.

The Unit is mainly used to descripe a certain unit-type for a parameter. There fore a special Property Type in FreeCAD can transport a Unit to check and ensure the right Unit. A Unit and a float value is called Quantity.

User facing values
Normally in script you can use Quantity for all kind of calculation and checking, but there comes the time you have to output information to the user. You could use getValueAs to force a certain unit, but normally the user sets his preferred unit-schema in the preferences. This unit-schema do all the translations to the representation the user likes to see. At the moment there are 3 schema implemented: There can be easily additional schemas implemented in the future...
 * 1: Internal    (mm/kg/s)
 * 2: MKS         (m/kg/s)
 * 3: US customary (in/lb)

The quantity class has two possibilities to use the actual schema translation:

This does the job if you only need a string. But somethimes you need more control, e.g. if you want to have a dialog button which dial up and down. Then you need more information about the translation output. There fore the getUserPrefered method of quantity is used:

Here you get two more informations as a tuple of size 3. You get the string as before, plus the factor the number is translated and the raw string with only the unit chosen by the translation schema. With this information you can implement a much richer user interaction.

The code for the schema translation you can see here:
 * http://sourceforge.net/p/free-cad/code/ci/master/tree/src/Base/UnitsSchemaInternal.cpp
 * http://sourceforge.net/p/free-cad/code/ci/master/tree/src/Base/UnitsSchemaMKS.cpp
 * http://sourceforge.net/p/free-cad/code/ci/master/tree/src/Base/UnitsSchemaImperial1.cpp

Parser supported Units
Although all physical units can be described with the seven SI units, most of the units used in technical areas are common combined units (like Pa = N/m^2 Pascal ). There fore the units parser in FreeCAD support lot of SI and Imperial combined units. This units are defined in src/Base/QuantityParser.l file and can be further advanced in the future.

"nm"  = Quantity(1.0e-6    ,Unit(1));           // nano meter "µm"  = Quantity(1.0e-3    ,Unit(1));           // micro meter "mm"  = Quantity(1.0       ,Unit(1));           // milli meter "cm"  = Quantity(10.0      ,Unit(1));           // centi meter "dm"  = Quantity(100.0     ,Unit(1));           // deci meter "m"   = Quantity(1.0e3     ,Unit(1));           // meter "km"  = Quantity(1.0e6     ,Unit(1));           // kilo meter "l"   = Quantity(1000000.0 ,Unit(3));           // Liter      dm^3 "µg"  = Quantity(1.0e-9    ,Unit(0,1));         // micro gram "mg"  = Quantity(1.0e-6    ,Unit(0,1));         // milli gram "g"   = Quantity(1.0e-3    ,Unit(0,1));         // gram "kg"  = Quantity(1.0       ,Unit(0,1));         // kilo gram "t"   = Quantity(1000.0    ,Unit(0,1));         // ton "s"   = Quantity(1.0       ,Unit(0,0,1));       // second                          (internal standard time) "min" = Quantity(60.0      ,Unit(0,0,1));       // minute "h"   = Quantity(3600.0    ,Unit(0,0,1));       // hour "A"   = Quantity(1.0       ,Unit(0,0,0,1));     // Ampere          (internal standard electric current) "mA"  = Quantity(0.001     ,Unit(0,0,0,1));     // milli Ampere "kA"  = Quantity(1000.0    ,Unit(0,0,0,1));     // kilo Ampere "MA"  = Quantity(1.0e6     ,Unit(0,0,0,1));     // Mega Ampere "K"   = Quantity(1.0       ,Unit(0,0,0,0,1));   // Kelvin (internal standard thermodynamic temperature) "mK"  = Quantity(0.001     ,Unit(0,0,0,0,1));   // Kelvin "µK"  = Quantity(0.000001  ,Unit(0,0,0,0,1));   // Kelvin

"mol" = Quantity(1.0       ,Unit(0,0,0,0,0,1));   // Mole     (internal standard amount of substance)

"cd"  = Quantity(1.0       ,Unit(0,0,0,0,0,0,1)); // Candela   (internal standard luminous intensity)

"deg" = Quantity(1.0           ,Unit(0,0,0,0,0,0,0,1));  // degree         (internal standard angle) "rad" = Quantity(180/M_PI      ,Unit(0,0,0,0,0,0,0,1));  // radian "gon" = Quantity(360.0/400.0   ,Unit(0,0,0,0,0,0,0,1));  // gon

"in"  = Quantity(25.4          ,Unit(1));       // inch "\""  = Quantity(25.4          ,Unit(1));       // inch "fo"   = Quantity(304.8         ,Unit(1));       // foot "'"    = Quantity(304.8         ,Unit(1));       // foot "th"   = Quantity(0.0254        ,Unit(1));       // thou "yd"   = Quantity(914.4         ,Unit(1));       // yard

"lb"  = Quantity(0.45359237    ,Unit(0,1));    // pound "oz"  = Quantity(0.0283495231  ,Unit(0,1));    // ounce "st"  = Quantity(6.35029318    ,Unit(0,1));    // Stone "cwt" = Quantity(50.80234544   ,Unit(0,1));    // hundredweights