Difference between revisions of "Quantity"

From FreeCAD Documentation
Jump to navigation Jump to search
Line 90: Line 90:
  "st"      yylval = Quantity(6.35029318    ,Unit(0,1));  return UNIT; // Stone
  "st"      yylval = Quantity(6.35029318    ,Unit(0,1));  return UNIT; // Stone
  "cwt"    yylval = Quantity(50.80234544    ,Unit(0,1));  return UNIT; // hundredweights
  "cwt"    yylval = Quantity(50.80234544    ,Unit(0,1));  return UNIT; // hundredweights
{{languages | {{it|Quantity/it}} {{fr|Quantity/fr}} }}

Revision as of 16:13, 10 November 2013

The Quantity is a combination of a floating point number and an Unit.

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 crash of the Mars Climate Orbiter through out a unit mixup. 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.

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.


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]
  • 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 phisicaly 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


Python scripting


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"      yylval = Quantity(1.0e-6    ,Unit(1));      return UNIT; // nano meter
"ym"      yylval = Quantity(1.0e-3    ,Unit(1));      return UNIT; // micro meter
"mm"      yylval = Quantity(1.0       ,Unit(1));      return UNIT; // milli meter
"cm"      yylval = Quantity(10.0      ,Unit(1));      return UNIT; // centi meter
"dm"      yylval = Quantity(100.0     ,Unit(1));      return UNIT; // deci meter
"m"       yylval = Quantity(1.0e3     ,Unit(1));      return UNIT; // meter
"km"      yylval = Quantity(1.0e6     ,Unit(1));      return UNIT; // kilo meter
"l"       yylval = Quantity(1000000.0 ,Unit(3));      return UNIT; // Liter      dm^3
"yg"      yylval = Quantity(1.0e-9    ,Unit(0,1));    return UNIT; // milli gram
"mg"      yylval = Quantity(1.0e-6    ,Unit(0,1));    return UNIT; // milli gram
"g"       yylval = Quantity(1.0e-3    ,Unit(0,1));    return UNIT; // gram
"kg"      yylval = Quantity(1.0       ,Unit(0,1));    return UNIT; // kilo gram
"t"       yylval = Quantity(1000.0    ,Unit(0,1));    return UNIT; // ton
"s"       yylval = Quantity(1.0       ,Unit(0,0,1));  return UNIT; // second                          (internal standard time)
"min"     yylval = Quantity(60.0      ,Unit(0,0,1));  return UNIT; // minute
"h"       yylval = Quantity(3600.0    ,Unit(0,0,1));  return UNIT; // hour  
"A"       yylval = Quantity(1.0       ,Unit(0,0,0,1));    return UNIT; // Ampere          (internal standard electric current)
"mA"      yylval = Quantity(0.001     ,Unit(0,0,0,1));    return UNIT; // milli Ampere         
"kA"      yylval = Quantity(1000.0    ,Unit(0,0,0,1));    return UNIT; // kilo Ampere         
"MA"      yylval = Quantity(1.0e6     ,Unit(0,0,0,1));    return UNIT; // Mega Ampere         
"K"       yylval = Quantity(1.0       ,Unit(0,0,0,0,1));  return UNIT; // Kelvin (internal standard thermodynamic temperature)
"mK"      yylval = Quantity(0.001     ,Unit(0,0,0,0,1));  return UNIT; // Kelvin         
"yK"      yylval = Quantity(0.000001  ,Unit(0,0,0,0,1));  return UNIT; // Kelvin         
"mol"     yylval = Quantity(1.0       ,Unit(0,0,0,0,0,1));    return UNIT; // Mole     (internal standard amount of substance)        
"cd"      yylval = Quantity(1.0       ,Unit(0,0,0,0,0,0,1));  return UNIT; // Candela   (internal standard luminous intensity)        
"deg"     yylval = Quantity(1.0           ,Unit(0,0,0,0,0,0,0,1));    return UNIT; // degree         (internal standard angle)
"rad"     yylval = Quantity(180/M_PI      ,Unit(0,0,0,0,0,0,0,1));    return UNIT; // radian         
"gon"     yylval = Quantity(360.0/400.0   ,Unit(0,0,0,0,0,0,0,1));    return UNIT; // gon         
"in"      yylval = Quantity(25.4      ,Unit(1));      return UNIT; // inch
"\""      yylval = Quantity(25.4      ,Unit(1));      return UNIT; // inch
"fo"      yylval = Quantity(304.8     ,Unit(1));      return UNIT; // foot
"'"       yylval = Quantity(304.8     ,Unit(1));      return UNIT; // foot
"th"      yylval = Quantity(0.0254    ,Unit(1));      return UNIT; // thou
"yr"      yylval = Quantity(914.4     ,Unit(1));      return UNIT; // yard

"lb"      yylval = Quantity(0.45359237     ,Unit(0,1));   return UNIT; // pound
"oz"      yylval = Quantity(0.45359237     ,Unit(0,1));   return UNIT; // ounce
"st"      yylval = Quantity(6.35029318     ,Unit(0,1));   return UNIT; // Stone
"cwt"     yylval = Quantity(50.80234544    ,Unit(0,1));   return UNIT; // hundredweights
Available translations of this page: Flag-it.jpg Flag-fr.jpg