Difference between revisions of "Quantity"

From FreeCAD Documentation
Jump to navigation Jump to search
({{Powerdocnavi{{#translation:}}}} for the proper category)
 
(42 intermediate revisions by 7 users not shown)
Line 1: Line 1:
The Quantity is a combination of a floating point number and an Unit. It is used throughout all of FreeCAD to handle parameters and all other kind of input/output.  
+
<languages/>
 +
<translate>
 +
<!--T:1-->
 +
The quantity is a combination of a floating point number and a unit. It is used throughout all of FreeCAD to handle parameters and all other kind of input/output.  
  
== Basics ==
+
== General == <!--T:2-->
  
In a CAD or CAE system its very important to keep track on the unit of an value. Lot of trouble can arise when mixing up units or calculating results in different unit systems. One famous disaster is the [http://en.wikipedia.org/wiki/Mars_Climate_Orbiter#Cause_of_failure crash of the Mars Climate Orbiter] through out a unit mix-up. Even in the same unit-system the units come in lots of different flavors always tailored to the field of use. Simple examples are e.g. velocity in km/h (cars), m/s (robotics) or mm/minute (milling). A CAD system have to keep reliably track of units. Also it has to calculate with them and check on the right unit for special parameters.  
+
<!--T:3-->
 +
In a CAD or CAE system it is very important to keep track of the unit of a value. Lots of trouble can arise when mixing up units or calculating results in different systems of  units. One famous disaster is the [http://en.wikipedia.org/wiki/Mars_Climate_Orbiter#Cause_of_failure crash of the Mars Climate Orbiter] due to a unit mix-up. Even in the same system of units the units come in lots of different flavours always tailored to the field of use. Simple examples are e.g. velocity in km/h (cars), m/s (robotics) or mm/minute (milling). A CAD system has to keep track of units reliably. Also it has to do calculations with them and check on the right unit for special parameters.  
  
For that reason the FreeCAD Quantity framework was created. It includes all the code and objects to deal with units, unit calculations, user input, conversion in other unit systems and the pretty output of units and values. In the long run no parameter in FreeCAD should be just a number.  
+
<!--T:4-->
 +
For that reason the FreeCAD Quantity framework was created. It includes all the code and objects to deal with units, unit calculations, user input, conversion to other systems of units and the pretty output of units and values. In the long run no parameter in FreeCAD should be just a number.  
  
 +
=== Supported units === <!--T:5-->
  
 +
<!--T:6-->
 +
The FreeCAD input parser supports a bunch of units and systems of units. FreeCAD supports the Greek letter 'µ' for micro but also accepts 'u' as a replacement.
 +
A complete list of all supported units can be [[Expressions#Units|found here]].
 +
 +
<!--T:9-->
 +
The detailed specifications you will find in the code:
 +
* [https://github.com/FreeCAD/FreeCAD/blob/master/src/Base/QuantityLexer.c Quantity lexer] 
 +
* [https://github.com/FreeCAD/FreeCAD/blob/master/src/Base/Quantity.cpp#l167 Quantity definitions]
 +
 +
== Internal representation == <!--T:10-->
 
All physical units can be expressed as a combination of the seven [http://en.wikipedia.org/wiki/International_System_of_Units SI-Units]:
 
All physical units can be expressed as a combination of the seven [http://en.wikipedia.org/wiki/International_System_of_Units SI-Units]:
  
 
+
<!--T:11-->
 
[[File:SI-Derived-Units.jpg|750px]]
 
[[File:SI-Derived-Units.jpg|750px]]
  
  
An easy way to express a Unit is a integer array of size 7 (number of base units) that defines what the unit is.
+
<!--T:12-->
 +
An easy way to express a unit is an integer array of size 7 (number of base units) that defines what the unit is.
 
The signature of the 7 base units are:
 
The signature of the 7 base units are:
 
* LENGTH: [1,0,0,0,0,0,0]
 
* LENGTH: [1,0,0,0,0,0,0]
Line 24: Line 41:
 
* LUMINOUS INTENSITY: [0,0,0,0,0,0,1]
 
* LUMINOUS INTENSITY: [0,0,0,0,0,0,1]
  
From these 7 units, we are then able to express all derived units defined in [http://physics.nist.gov/cuu/pdf/sp811.pdf Guide for the Use of the International System of Units (SI)] and create new ones as needed such as for instance:
+
<!--T:13-->
 +
Using these seven units we are then able to express all derived units defined in [http://physics.nist.gov/cuu/pdf/sp811.pdf 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]
 
* MASS DENSITY: [-3,1,0,0,0,0,0]
 
* AREA: [0,2,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.
+
<!--T:14-->
 +
Since angle is physically dimensionless, but nevertheless 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 ==
+
== Units calculator == <!--T:15-->
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.  
+
Often you are in need of converting values from one system of units to another. For example you have old parameter tables with wired units. In these cases FreeCAD offers a conversion tool called Units-Calculator which helps in translating units.  
  
 +
<!--T:16-->
 
Its description in detail is here:
 
Its description in detail is here:
[[Std_UnitsCalculator]]
+
[[Std_UnitsCalculator|Std_UnitsCalculator]]
 +
 
 +
== InputField == <!--T:17-->
 +
The InputField is a QLineEdit derived Qt widget to handle all kinds of user interaction with quantities and parameters. It features the 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
 +
* selecting values with mouse wheel and arrow keys (tbd)
 +
* selecting with middle mouse button and mouse move (tbd)
 +
* Python integration for usage in Python only dialogs (tbd)
 +
 
 +
<!--T:18-->
 +
The UnitsCalculator uses the InputField already.
 +
 
 +
<!--T:19-->
 +
Main documentation: [[InputField|InputField]]
  
== Python scripting ==
+
<!--T:20-->
 +
Code:
 +
* [https://github.com/FreeCAD/FreeCAD/blob/master/src/Gui/InputField.h InputField.h]
 +
* [https://github.com/FreeCAD/FreeCAD/blob/master/src/Gui/InputField.cpp InputField.cpp]
 +
 
 +
== Python scripting == <!--T:21-->
 
The Unit and Quantity system in FreeCAD is (as nearly everything) fully accessibly via Python.  
 
The Unit and Quantity system in FreeCAD is (as nearly everything) fully accessibly via Python.  
  
=== Unit ===
+
=== Unit === <!--T:22-->
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 class represents the fingerprint of any physical unit. As described in the Basics section a vector of eight numbers is used to represent this fingerprint. The Unit class allows the handling and calculation based on this information.  
  
<source lang="python">
+
</translate>
from Units import Unit
+
{{Code|code=
  
# creating a Unit with certain signature
+
from FreeCAD import Units
Unit(0,1)      # Mass    (kg)
 
Unit(1)        # Length  (mm)
 
Unit(-1,1,-2)  # Pressure (kg/mm*s^2)
 
  
# using predefined constats
+
# creating a unit with certain signature
Unit(FreeCAD.Units.Length)
+
Units.Unit(0,1)      # Mass    (kg)
Unit(FreeCAD.Units.Mass)
+
Units.Unit(1)        # Length   (mm)
Unit(FreeCAD.Units.Pressure)
+
Units.Unit(-1,1,-2)  # Pressure (kg/mm*s^2)
  
# parsing unit out of an string
+
# using predefined constants
Unit('kg/(m*s^2)')    # Pressure
+
Units.Unit(Units.Length)
Unit('Pa')           # the same as combined Unit Pascale
+
Units.Unit(Units.Mass)
Unit('J')            # Joul (Work,Energy) mm^2*kg/(s^2)
+
Units.Unit(Units.Pressure)
  
# you can use units from all supported unit-systems
+
# parsing unit out of a string
Unit('psi')          # Imperial pressure
+
Units.Unit('kg/(m*s^2)')    # Pressure
Unit('lb')            # Mass
+
Units.Unit('Pa')            # the same as combined unit Pascale
Unit('ft^2')          # Area
+
Units.Unit('J')            # Joule (work,energy) mm^2*kg/(s^2)
 +
 
 +
# you can use units from all supported systems of units
 +
Units.Unit('psi')          # imperial pressure
 +
Units.Unit('lb')            # imperial  mass
 +
Units.Unit('ft^2')          # imperial area
  
 
# comparing units
 
# comparing units
Unit(0,1) == Unit(FreeCAD.Units.Mass)
+
Units.Unit(0,1) == Unit(Units.Mass)
  
 
# getting type of unit
 
# getting type of unit
Unit('kg/(m*s^2)').Type == 'Pressure'
+
Units.Unit('kg/(m*s^2)').Type == 'Pressure'
  
 
# calculating
 
# calculating
Unit('kg') * Unit('m^-1*s^-2') == Unit('kg/(m*s^2)')
+
Units.Unit('kg') * Units.Unit('m^-1*s^-2') == Units.Unit('kg/(m*s^2)')
  
</source>
+
}}
 +
<translate>
 +
<!--T:23-->
 +
The unit is mainly used to describe a certain type of unit for a parameter. Therefore a special property type in FreeCAD can pass a unit to check and ensure the right unit.
 +
A unit and a float value is called quantity.
  
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.
+
=== Quantity === <!--T:24-->
A Unit and a float value is called Quantity.
 
  
=== Quantity ===
+
</translate>
<source lang="python">
+
{{Code|code=
from Units import Unit,Quantity
 
  
# to create a quantity you need a value (float) and a Unit
+
from FreeCAD import Units
Quantity(1.0,Unit(0,1))               # Mass     1.0 kg
+
 
Quantity(1.0,Unit(1))                 # Length    1.0 mm
+
# to create a quantity you need a value (float) and a unit
Quantity(1.0,Unit(-1,1,-2))           # Pressure  1.0 kg/mm*s^2
+
Units.Quantity(1.0,Units.Unit(0,1))     # Mass       1.0 kg
Quantity(1.0,FreeCAD.Units.Pressure) # Pressure  1.0 kg/mm*s^2
+
Units.Quantity(1.0,Units.Unit(1))       # Length    1.0 mm
 +
Units.Quantity(1.0,Units.Unit(-1,1,-2)) # Pressure  1.0 kg/mm*s^2
 +
Units.Quantity(1.0,Units.Pressure)     # Pressure  1.0 kg/mm*s^2
  
 
# you can directly give a signature
 
# you can directly give a signature
Quantity(1.0,0,1)     # Mass     1.0 kg
+
Units.Quantity(1.0,0,1)     # Mass       1.0 kg
Quantity(1.0,1)       # Length    1.0 mm
+
Units.Quantity(1.0,1)       # Length    1.0 mm
Quantity(1.0,-1,1,-2) # Pressure  1.0 kg/mm*s^2
+
Units.Quantity(1.0,-1,1,-2) # Pressure  1.0 kg/mm*s^2
 +
 
 +
# parsing quantities out of a string
 +
Units.Quantity('1.0 kg/(m*s^2)') # Pressure
 +
Units.Quantity('1.0 Pa')        # the same as combined Unit Pascale
 +
Units.Quantity('1.0 J')          # Joule (Work,Energy) mm^2*kg/(s^2)
  
# parsing Quantitis out of an string
+
# You can use a point or comma as float delimiter
Quantity('1.0 kg/(m*s^2)')   # Pressure
+
Units.Quantity('1,0 m')
Quantity('1.0 Pa')            # the same as combined Unit Pascale
+
Units.Quantity('1.0 m')
Quantity('1.0 J')            # Joul (Work,Energy) mm^2*kg/(s^2)
 
  
# you can use units from all supported unit-systems
+
# you can use units from all supported systems of units
Quantity('1.0 psi')           # Imperial pressure
+
Units.Quantity('1.0 psi') # imperial pressure
Quantity('1.0 lb')           # Mass
+
Units.Quantity('1.0 lb')   # imperial mass
Quantity('1.0 ft^2')  
+
Units.Quantity('1.0 ft^2') # imperial area
  
 
# the quantity parser can do calculations too
 
# the quantity parser can do calculations too
Quantity('360/5 deg')           # splitting circle  
+
Units.Quantity('360/5 deg')       # splitting circle  
Quantity('1/16 in')             # fractions
+
Units.Quantity('1/16 in')         # fractions
Quantity('5.3*6.3 m^2')         # calculating an area
+
Units.Quantity('5.3*6.3 m^2')     # calculating an area
Quantity('1/(log(2.3)/sin(pi)*3.4)+1.8e-3 m')
+
Units.Quantity('1/(log(2.3)/sin(pi)*3.4)+1.8e-3 m')
Quantity('1fo 3in')             # imperial style
+
Units.Quantity('1ft 3in')         # imperial style
 +
 
 +
# and for sure calculation and comparison
 +
Units.Quantity('1 Pa') * Units.Quantity(2.0) == Units.Quantity('2 Pa')
 +
Units.Quantity('1 m') * Units.Quantity('2 m') == Units.Quantity('2 m^2')
 +
Units.Quantity('1 m') * Units.Quantity('2 ft') + Units.Quantity('2 mm^2')
 +
Units.Quantity('1 m') > Units.Quantity('2 ft')
 +
 
 +
# accessing the components
 +
Units.Quantity('1 m').Value # get the number (always internal system (mm/kg/s))
 +
Units.Quantity('1 m').Unit  # get the unit
 +
Units.Quantity('1 m') == Units.Quantity( Units.Quantity('1 m').Value , Units.Quantity('1 m').Unit)
 +
 
 +
# translating the value into other units than the internal system (mm/kg/s)
 +
Units.Quantity('1 km/h').getValueAs('m/s')                  # translate value
 +
Units.Quantity('1 m').getValueAs(2.45,1)                    # translation value and unit signature
 +
Units.Quantity('1 kPa').getValueAs(Units.Pascal)            # predefined standard units
 +
Units.Quantity('1 MPa').getValueAs(Units.Quantity('N/m^2')) # a quantity
 +
         
 +
}}
 +
<translate>
 +
 
 +
=== User facing values === <!--T:25-->
 +
Normally in scripts you can use Quantity for all kinds of calculations 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 does all the translations to the representation the user likes to see. At the moment there are three schemes implemented:
 +
* 1: Internal    (mm/kg/s)
 +
* 2: MKS          (m/kg/s)
 +
* 3: US customary (in/lb)
 +
There can be easily additional schemas implemented in the future...
 +
 
 +
<!--T:26-->
 +
The Quantity class has two options to use the actual schema translation:
 +
 
 +
</translate>
 +
{{Code|code=
 +
from FreeCAD import Units
 +
 
 +
# Use the translated string:
 +
Units.Quantity('1m').UserString          # '1000 mm' in 1; '1 m' in 2; and '1.09361 yr' in 3
 +
}}
 +
<translate>
 +
<!--T:27-->
 +
This does the job if you only need a string. But sometimes you need more control, e.g. if you want to have a dialog button which dials up and down. Then you need more information about the translation output. Therefore the getUserPreferred() method of quantity is used:
 +
 
 +
</translate>
 +
{{Code|code=
 +
Units.Quantity('22 m').getUserPreferred() # gets a tuple:('22 m', 1000.0, 'm')
 +
Units.Quantity('2  m').getUserPreferred() # Tuple: ('2000 mm', 1.0, 'mm')
 +
}}
 +
<translate>
 +
<!--T:28-->
 +
Here you get more information using a tuple (three items). You get the string as before, plus the factor of the value and the raw string with only the unit chosen by the translation schema. With this information you can implement a much richer user interaction.
 +
 
 +
<!--T:29-->
 +
The code of the schema translation can be found here:
 +
* [https://github.com/FreeCAD/FreeCAD/blob/master/src/Base/UnitsSchemaInternal.cpp Internal]
 +
* [https://github.com/FreeCAD/FreeCAD/blob/master/src/Base/UnitsSchemaMKS.cpp MKS]
 +
* [https://github.com/FreeCAD/FreeCAD/blob/master/src/Base/UnitsSchemaImperial.cpp Imperial]
 +
 
 +
=== Precision === <!--T:39-->
  
 +
<!--T:40-->
 +
The precision of quantities is within FreeCAD dialogs the number of decimals specified [[Preferences_Editor#Units|in the preferences]]. To use this settings for your script (for example in dialogs), you can get it with this code:
 +
</translate>
 +
{{Code|code=
 +
import FreeCAD
  
</source>
+
params = App.ParamGet("User parameter:BaseApp/Preferences/Units")
 +
params.GetInt('Decimals') # returns an int
 +
}}
 +
<translate>
  
 +
== Appendix == <!--T:30-->
  
== Appendix ==  
+
=== Parser supported units === <!--T:31-->
 +
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 ). Therefore the units parser in FreeCAD supports lot of SI and Imperial combined units. These units are defined in src/Base/QuantityParser.l file and can be further expanded in the future.
  
=== Parser supported Units ===
+
</translate>
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.
+
{{Code|code=
  
 +
from FreeCAD import Units
  
  "nm"   = Quantity(1.0e-6    ,Unit(1));           // nano meter
+
  "nm" = Units.Quantity(1.0e-6    ,Units.Unit(1));         // nano meter
  "ym"   = Quantity(1.0e-3    ,Unit(1));           // micro meter
+
  "µm" = Units.Quantity(1.0e-3    ,Units.Unit(1));         // micro meter
  "mm"   = Quantity(1.0      ,Unit(1));           // milli meter
+
  "mm" = Units.Quantity(1.0      ,Units.Unit(1));         // milli meter
  "cm"   = Quantity(10.0      ,Unit(1));           // centi meter
+
  "cm" = Units.Quantity(10.0      ,Units.Unit(1));         // centi meter
  "dm"   = Quantity(100.0    ,Unit(1));           // deci meter
+
  "dm" = Units.Quantity(100.0    ,Units.Unit(1));         // deci meter
  "m"   = Quantity(1.0e3    ,Unit(1));           // meter
+
  "m"   = Units.Quantity(1.0e3    ,Units.Unit(1));         // meter
  "km"   = Quantity(1.0e6    ,Unit(1));           // kilo meter
+
  "km" = Units.Quantity(1.0e6    ,Units.Unit(1));         // kilo meter
  "l"   = Quantity(1000000.0 ,Unit(3));           // Liter      dm^3
+
  "l"   = Units.Quantity(1000000.0 ,Units.Unit(3));         // liter dm^3
 
                                                    
 
                                                    
  "yg"   = Quantity(1.0e-9    ,Unit(0,1));         // milli gram
+
  "µg" = Units.Quantity(1.0e-9    ,Units.Unit(0,1));       // micro gram
  "mg"   = Quantity(1.0e-6    ,Unit(0,1));         // milli gram
+
  "mg" = Units.Quantity(1.0e-6    ,Units.Unit(0,1));       // milli gram
  "g"   = Quantity(1.0e-3    ,Unit(0,1));         // gram
+
  "g"   = Units.Quantity(1.0e-3    ,Units.Unit(0,1));       // gram
  "kg"   = Quantity(1.0      ,Unit(0,1));         // kilo gram
+
  "kg" = Units.Quantity(1.0      ,Units.Unit(0,1));       // kilo gram
  "t"   = Quantity(1000.0    ,Unit(0,1));         // ton
+
  "t"   = Units.Quantity(1000.0    ,Units.Unit(0,1));       // ton
 
                                                    
 
                                                    
  "s"   = Quantity(1.0      ,Unit(0,0,1));       // second                         (internal standard time)
+
  "s"   = Units.Quantity(1.0      ,Units.Unit(0,0,1));     // second (internal standard time)
  "min" = Quantity(60.0      ,Unit(0,0,1));       // minute
+
  "min" = Units.Quantity(60.0      ,Units.Unit(0,0,1));     // minute
  "h"   = Quantity(3600.0    ,Unit(0,0,1));       // hour   
+
  "h"   = Units.Quantity(3600.0    ,Units.Unit(0,0,1));     // hour   
 
                                                    
 
                                                    
  "A"   = Quantity(1.0      ,Unit(0,0,0,1));     // Ampere         (internal standard electric current)
+
  "A"   = Units.Quantity(1.0      ,Units.Unit(0,0,0,1));   // Ampere (internal standard electric current)
  "mA"   = Quantity(0.001    ,Unit(0,0,0,1));     // milli Ampere         
+
  "mA" = Units.Quantity(0.001    ,Units.Unit(0,0,0,1));   // milli Ampere         
  "kA"   = Quantity(1000.0    ,Unit(0,0,0,1));     // kilo Ampere         
+
  "kA" = Units.Quantity(1000.0    ,Units.Unit(0,0,0,1));   // kilo Ampere         
  "MA"   = Quantity(1.0e6    ,Unit(0,0,0,1));     // Mega Ampere         
+
  "MA" = Units.Quantity(1.0e6    ,Units.Unit(0,0,0,1));   // Mega Ampere         
 
                                                    
 
                                                    
  "K"   = Quantity(1.0      ,Unit(0,0,0,0,1));   // Kelvin (internal standard thermodynamic temperature)
+
  "K"   = Units.Quantity(1.0      ,Units.Unit(0,0,0,0,1)); // Kelvin (internal standard thermodynamic temperature)
  "mK"   = Quantity(0.001    ,Unit(0,0,0,0,1));   // Kelvin         
+
  "mK" = Units.Quantity(0.001    ,Units.Unit(0,0,0,0,1)); // Kelvin         
  "yK"   = Quantity(0.000001  ,Unit(0,0,0,0,1));   // Kelvin         
+
  "µK" = Units.Quantity(0.000001  ,Units.Unit(0,0,0,0,1)); // Kelvin         
  
  "mol" = Quantity(1.0      ,Unit(0,0,0,0,0,1));   // Mole     (internal standard amount of substance)         
+
  "mol" = Units.Quantity(1.0      ,Units.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)         
+
  "cd" = Units.Quantity(1.0      ,Units.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)
+
  "deg" = Units.Quantity(1.0         ,Units.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         
+
  "rad" = Units.Quantity(180/M_PI   ,Units.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         
+
  "gon" = Units.Quantity(360.0/400.0 ,Units.Unit(0,0,0,0,0,0,0,1)); // gon         
  
  "in"   = Quantity(25.4         ,Unit(1));      // inch
+
  "in" = Units.Quantity(25.4       ,Units.Unit(1));      // inch
  "\""   = Quantity(25.4         ,Unit(1));      // inch
+
  "\"" = Units.Quantity(25.4       ,Units.Unit(1));      // inch
  "fo"   = Quantity(304.8         ,Unit(1));      // foot
+
  "fo" = Units.Quantity(304.8       ,Units.Unit(1));      // foot
  "'"   = Quantity(304.8         ,Unit(1));      // foot
+
  "'"   = Units.Quantity(304.8       ,Units.Unit(1));      // foot
  "th"   = Quantity(0.0254       ,Unit(1));      // thou
+
  "th" = Units.Quantity(0.0254     ,Units.Unit(1));      // thou
  "yr"   = Quantity(914.4         ,Unit(1));      // yard
+
  "yd" = Units.Quantity(914.4       ,Units.Unit(1));      // yard
  
 +
"lb"  = Units.Quantity(0.45359237  ,Units.Unit(0,1));    // pound
 +
"oz"  = Units.Quantity(0.0283495231 ,Units.Unit(0,1));    // ounce
 +
"st"  = Units.Quantity(6.35029318  ,Units.Unit(0,1));    // Stone
 +
"cwt" = Units.Quantity(50.80234544  ,Units.Unit(0,1));    // hundredweights
 +
}}
  
"lb"  = Quantity(0.45359237    ,Unit(0,1));    // pound
+
{{Powerdocnavi{{#translation:}}}}
"oz"  = Quantity(0.0283495231  ,Unit(0,1));    // ounce
+
[[Category:Developer{{#translation:}}]]
"st"  = Quantity(6.35029318    ,Unit(0,1));    // Stone
 
"cwt"  = Quantity(50.80234544  ,Unit(0,1));    // hundredweights
 
  
 
{{clear}}
 
{{clear}}
{{languages | {{fr|Quantity/fr}} {{it|Quantity/it}} }}
 

Latest revision as of 23:56, 11 May 2020

Other languages:
Deutsch • ‎English • ‎français • ‎italiano • ‎čeština

The quantity is a combination of a floating point number and a unit. It is used throughout all of FreeCAD to handle parameters and all other kind of input/output.

General

In a CAD or CAE system it is very important to keep track of the unit of a value. Lots of trouble can arise when mixing up units or calculating results in different systems of units. One famous disaster is the crash of the Mars Climate Orbiter due to a unit mix-up. Even in the same system of units the units come in lots of different flavours always tailored to the field of use. Simple examples are e.g. velocity in km/h (cars), m/s (robotics) or mm/minute (milling). A CAD system has to keep track of units reliably. Also it has to do calculations with them and check on the right unit for special parameters.

For that reason the FreeCAD Quantity framework was created. It includes all the code and objects to deal with units, unit calculations, user input, conversion to other systems of units and the pretty output of units and values. In the long run no parameter in FreeCAD should be just a number.

Supported units

The FreeCAD input parser supports a bunch of units and systems of units. FreeCAD supports the Greek letter 'µ' for micro but also accepts 'u' as a replacement. A complete list of all supported units can be found here.

The detailed specifications you will find in the code:

Internal representation

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

SI-Derived-Units.jpg


An easy way to express a unit is an 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]

Using these seven 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 nevertheless 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 converting values from one system of units to another. For example you have old parameter tables with wired units. In these 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 kinds of user interaction with quantities and parameters. It features the 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
  • selecting values with mouse wheel and arrow keys (tbd)
  • selecting with middle mouse button and mouse move (tbd)
  • Python integration for usage in Python only dialogs (tbd)

The UnitsCalculator uses the InputField already.

Main documentation: InputField

Code:

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 described in the Basics section a vector of eight numbers is used to represent this fingerprint. The Unit class allows the handling and calculation based on this information.

from FreeCAD import Units

# creating a unit with certain signature
Units.Unit(0,1)      # Mass     (kg)
Units.Unit(1)        # Length   (mm)
Units.Unit(-1,1,-2)  # Pressure (kg/mm*s^2)

# using predefined constants
Units.Unit(Units.Length)
Units.Unit(Units.Mass)
Units.Unit(Units.Pressure)

# parsing unit out of a string
Units.Unit('kg/(m*s^2)')    # Pressure
Units.Unit('Pa')            # the same as combined unit Pascale
Units.Unit('J')             # Joule (work,energy) mm^2*kg/(s^2)

# you can use units from all supported systems of units
Units.Unit('psi')           # imperial pressure
Units.Unit('lb')            # imperial  mass
Units.Unit('ft^2')          # imperial area

# comparing units
Units.Unit(0,1) == Unit(Units.Mass)

# getting type of unit
Units.Unit('kg/(m*s^2)').Type == 'Pressure'

# calculating
Units.Unit('kg') * Units.Unit('m^-1*s^-2') == Units.Unit('kg/(m*s^2)')

The unit is mainly used to describe a certain type of unit for a parameter. Therefore a special property type in FreeCAD can pass a unit to check and ensure the right unit. A unit and a float value is called quantity.

Quantity

from FreeCAD import Units

# to create a quantity you need a value (float) and a unit
Units.Quantity(1.0,Units.Unit(0,1))     # Mass       1.0 kg
Units.Quantity(1.0,Units.Unit(1))       # Length    1.0 mm
Units.Quantity(1.0,Units.Unit(-1,1,-2)) # Pressure  1.0 kg/mm*s^2
Units.Quantity(1.0,Units.Pressure)      # Pressure  1.0 kg/mm*s^2

# you can directly give a signature
Units.Quantity(1.0,0,1)     # Mass       1.0 kg
Units.Quantity(1.0,1)       # Length    1.0 mm
Units.Quantity(1.0,-1,1,-2) # Pressure  1.0 kg/mm*s^2

# parsing quantities out of a string
Units.Quantity('1.0 kg/(m*s^2)') # Pressure
Units.Quantity('1.0 Pa')         # the same as combined Unit Pascale
Units.Quantity('1.0 J')          # Joule (Work,Energy) mm^2*kg/(s^2)

# You can use a point or comma as float delimiter
Units.Quantity('1,0 m')
Units.Quantity('1.0 m')

# you can use units from all supported systems of units
Units.Quantity('1.0 psi')  # imperial pressure
Units.Quantity('1.0 lb')   # imperial mass
Units.Quantity('1.0 ft^2') # imperial area

# the quantity parser can do calculations too
Units.Quantity('360/5 deg')        # splitting circle 
Units.Quantity('1/16 in')          # fractions
Units.Quantity('5.3*6.3 m^2')      # calculating an area
Units.Quantity('1/(log(2.3)/sin(pi)*3.4)+1.8e-3 m')
Units.Quantity('1ft 3in')          # imperial style

# and for sure calculation and comparison
Units.Quantity('1 Pa') * Units.Quantity(2.0) == Units.Quantity('2 Pa')
Units.Quantity('1 m') * Units.Quantity('2 m') == Units.Quantity('2 m^2')
Units.Quantity('1 m') * Units.Quantity('2 ft') + Units.Quantity('2 mm^2')
Units.Quantity('1 m') > Units.Quantity('2 ft')

# accessing the components
Units.Quantity('1 m').Value # get the number (always internal system (mm/kg/s))
Units.Quantity('1 m').Unit  # get the unit
Units.Quantity('1 m') == Units.Quantity( Units.Quantity('1 m').Value , Units.Quantity('1 m').Unit)

# translating the value into other units than the internal system (mm/kg/s)
Units.Quantity('1 km/h').getValueAs('m/s')                  # translate value
Units.Quantity('1 m').getValueAs(2.45,1)                    # translation value and unit signature
Units.Quantity('1 kPa').getValueAs(Units.Pascal)            # predefined standard units 
Units.Quantity('1 MPa').getValueAs(Units.Quantity('N/m^2')) # a quantity

User facing values

Normally in scripts you can use Quantity for all kinds of calculations 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 does all the translations to the representation the user likes to see. At the moment there are three schemes implemented:

  • 1: Internal (mm/kg/s)
  • 2: MKS (m/kg/s)
  • 3: US customary (in/lb)

There can be easily additional schemas implemented in the future...

The Quantity class has two options to use the actual schema translation:

from FreeCAD import Units

# Use the translated string:
Units.Quantity('1m').UserString           # '1000 mm' in 1; '1 m' in 2; and '1.09361 yr' in 3

This does the job if you only need a string. But sometimes you need more control, e.g. if you want to have a dialog button which dials up and down. Then you need more information about the translation output. Therefore the getUserPreferred() method of quantity is used:

Units.Quantity('22 m').getUserPreferred() # gets a tuple:('22 m', 1000.0, 'm')
Units.Quantity('2  m').getUserPreferred() # Tuple: ('2000 mm', 1.0, 'mm')

Here you get more information using a tuple (three items). You get the string as before, plus the factor of the value 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 of the schema translation can be found here:

Precision

The precision of quantities is within FreeCAD dialogs the number of decimals specified in the preferences. To use this settings for your script (for example in dialogs), you can get it with this code:

import FreeCAD

params = App.ParamGet("User parameter:BaseApp/Preferences/Units")
params.GetInt('Decimals') # returns an int

Appendix

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 ). Therefore the units parser in FreeCAD supports lot of SI and Imperial combined units. These units are defined in src/Base/QuantityParser.l file and can be further expanded in the future.

from FreeCAD import Units

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

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

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

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

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

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