Quantity/en

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:
 * Quantity lexer
 * Quantity definitions

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



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:
 * InputField.h
 * 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 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.

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.

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: 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 options to use the actual schema translation:

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:

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:
 * Internal
 * MKS
 * Imperial

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:

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.