Quantity/it: Difference between revisions
Renatorivo (talk | contribs) (Created page with "== Generale ==") |
No edit summary |
||
(79 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
<languages/> |
|||
Quantity è la combinazione di un numero in virgola mobile e di una unità di misura. |
Quantity è la combinazione di un numero in virgola mobile e di una unità di misura. |
||
== |
== Aspetti generali == |
||
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. |
|||
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. |
|||
In un sistema CAD o CAE è molto importante mantenere la traccia dell'unità di misura di un valore. Quando si mescolano le unità o si calcolano i risultati in diverse unità di misura possono sorgere un sacco di problemi. |
|||
=== Supported units === |
|||
Un famoso disastro, causato da un disguido sulle unità di misura, è il [http://en.wikipedia.org/wiki/Mars_Climate_Orbiter#Cause_of_failure crash of the Mars Climate Orbiter]. Anche all'interno di uno stesso Sistema di misura le unità possono essere disponibili in diversi formati, secondo il settore di utilizzo. Ad esempio, la velocità in km/h per le automobili, in m/s in robotica o in mm/min per la fresatura. Un sistema CAD deve conservare una traccia affidabile delle unità. Inoltre deve utilizzarle per eseguire i calcoli e deve verificare che per i parametri speciali sia adottata la giusta unità. |
|||
Per questo motivo è stata creata la sezione Quantity di FreeCAD. Essa include tutto il codice e gli oggetti che servono per trattare le unità, i calcoli delle unità, i dati inseriti dall'utente, la conversione tra i Sistemi e per fornire un corretto output delle unità e dei valori. In futuro, in FreeCAD, nessun parametro dovrebbe essere solo più un numero. |
|||
The FreeCAD input parser supports a bunch of units and units-systmes. We do use the Greek letter for micro but also accept 'u' as a replacement. |
|||
=== Unità supportate === |
|||
* Length |
|||
** "nm" |
|||
** "µm" |
|||
** "mm" |
|||
** "cm" |
|||
** "dm" |
|||
** "m" |
|||
** "km" |
|||
** "in" |
|||
** "ft" |
|||
** "thou" |
|||
** "mil" |
|||
** "yd" |
|||
** "mi" |
|||
Il parser dei dati in ingresso di FreeCAD supporta una serie di unità e di Sistemi di unità. FreeCAD supporta la lettera greca 'µ' per micro ma accetta anche 'u' in sostituzione. |
|||
Todo: all the rest... |
|||
Un elenco completo di tutte le unità supportate [[Expressions/it#Unità|si trova quì]]. |
|||
Le specifiche dettagliate si trovano nel codice: |
|||
* [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] |
|||
== Rappresentazione interna == |
|||
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 |
|||
Tutte le unità fisiche possono essere espresse come combinazione delle sette [http://en.wikipedia.org/wiki/International_System_of_Units Unità SI] di base: |
|||
== Internal representation == |
|||
All physical units can be expressed as a combination of the seven [http://en.wikipedia.org/wiki/International_System_of_Units SI-Units]: |
|||
[[File:SI-Derived-Units.jpg|750px]] |
[[File:SI-Derived-Units.jpg|750px]] |
||
Un modo semplice per esprimere una Unità è quello di utilizzare un gruppo di 7 interi, il numero delle unità di base, che definisce di quale unità si tratta. |
|||
An easy way to express a Unit is a integer array of size 7 (number of base units) that defines what the unit is. |
|||
Le firme delle 7 unità di base sono: |
|||
The signature of the 7 base units are: |
|||
* |
* LUNGHEZZA: [1,0,0,0,0,0,0] |
||
* |
* MASSA: [0,1,0,0,0,0,0] |
||
* |
* TEMPO: [0,0,1,0,0,0,0] |
||
* |
* CORRENTE ELETTRICA: [0,0,0,1,0,0,0] |
||
* |
* TEMPERATURA TERMODINAMICA: [0,0,0,0,1,0,0] |
||
* |
* QUANTITÀ DI SOSTANZA: [0,0,0,0,0,1,0] |
||
* |
* INTENSITÀ LUMINOSA: [0,0,0,0,0,0,1] |
||
Usando queste 7 unità, siamo poi in grado di esprimere tutte le unità derivate definite nella [http://physics.nist.gov/cuu/pdf/sp811.pdf Guida per l'utilizzo del Sistema Internazionale di Unità (SI)] e di crearne di nuove, secondo le esigenze, come ad esempio: |
|||
* 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] |
||
Dato che l'angolo è fisicamente adimensionale, ma in un sistema CAD non è meno importante, si è aggiunta una unità virtuale in più per Angle. Questo crea un vettore di 8 dati nella firma delle unità di FreeCAD. |
|||
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. |
|||
== Il convertitore delle unità == |
|||
== Units calculator == |
|||
Sovente è necessario convertire le unità da un sistema a un altro. Per esempio, quando i parametri sono contenuti in vecchie tabelle e espressi in determinate unità. In questi casi FreeCAD offre uno strumento di conversione denominato Units-Calculator che aiuta a tradurre le unità. |
|||
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. |
|||
La sua desrizione si trova nella pagina: [[Std_UnitsCalculator/it|Std_UnitsCalculator]] |
|||
Its description in detail is here: |
|||
[[Std_UnitsCalculator]] |
|||
== InputField == |
== InputField == |
||
Inputfield è un oggetto QLineEdit derivato da un widget Qt e serve per gestire tutti i tipi di interazioni dell'utente con le quantità e con i parametri. Offre le seguenti proprietà: |
|||
The InputField is a QLineEdit derived Qt widget to handle all kind of user interaction with Quantities and parameters. It features following properties: |
|||
* analisi dei valori e delle unità arbitrarie inserite |
|||
* parsing arbitrary value/unit input |
|||
* verifica della corrispondenza dell'unità (se fornita) e restituzione di un riscontro |
|||
* checking on the right unit (if given) and give the user feedback |
|||
* speciale menu contestuale per le operazioni sulle Quantità e sui Valori |
|||
* special context menu for operations on Quantities/Values |
|||
* gestione della cronologia (salva gli ultimi valori usati) |
|||
* history management (save the last used values) |
|||
* salvataggio dei valori usati frequentemente nella scorciatoia del menu contestuale |
|||
* save often needed values as shortcut in context menu |
|||
* |
* selezione dei valori con la rotellina del mouse e con i tasti freccia (tbd) |
||
* selezione con il tasto centrale più il movimento del mouse (tbd) |
|||
* dialing with middle mouse button and mouse move (tbd) |
|||
* integrazione Python per l'utilizzo nella console Python (tbd) |
|||
* python integration for usage in python only dialogs (tbd) |
|||
Lo strumento UnitsCalculator usa già InputField. |
|||
Documentazione di riferimento: [[InputField/it|InputField]] |
|||
Codice: |
|||
Code: |
|||
* |
* [https://github.com/FreeCAD/FreeCAD/blob/master/src/Gui/InputField.h InputField.h] |
||
* http://sourceforge.net/p/free-cad/code/ci/master/tree/src/Gui/InputField.cpp |
* [https://github.com/FreeCAD/FreeCAD/blob/master/src/Gui/InputField.cpp InputField.cpp] http://sourceforge.net/p/free-cad/code/ci/master/tree/src/Gui/InputField.cpp |
||
== |
== Script Python == |
||
Come quasi tutto in FreeCAD, anche i sistemi Unit e Quantity sono totalmente accessibili tramite Python. |
|||
=== Unit === |
=== Unit === |
||
La classe Unit rappresenta la firma digitale di qualsiasi unità fisica. Come descritto nella sezione di base, per rappresentare questa firma digitale viene usato un vettore di otto numeri. La classe Unit permette di gestire tutte queste informazioni e di usarle per i calcoli. |
|||
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. |
|||
{{Code|code= |
|||
<syntaxhighlight> |
|||
from |
from FreeCAD import Units |
||
# creating a |
# creating a unit with certain signature |
||
Unit(0,1) # Mass (kg) |
Units.Unit(0,1) # Mass (kg) |
||
Unit(1) # Length (mm) |
Units.Unit(1) # Length (mm) |
||
Unit(-1,1,-2) # Pressure (kg/mm*s^2) |
Units.Unit(-1,1,-2) # Pressure (kg/mm*s^2) |
||
# using predefined |
# using predefined constants |
||
Unit( |
Units.Unit(Units.Length) |
||
Unit( |
Units.Unit(Units.Mass) |
||
Unit( |
Units.Unit(Units.Pressure) |
||
# parsing unit out of |
# parsing unit out of a string |
||
Unit('kg/(m*s^2)') # Pressure |
Units.Unit('kg/(m*s^2)') # Pressure |
||
Unit('Pa') # the same as combined |
Units.Unit('Pa') # the same as combined unit Pascale |
||
Unit('J') # |
Units.Unit('J') # Joule (work,energy) mm^2*kg/(s^2) |
||
# you can use units from all supported |
# you can use units from all supported systems of units |
||
Unit('psi') # |
Units.Unit('psi') # imperial pressure |
||
Unit('lb') # |
Units.Unit('lb') # imperial mass |
||
Unit('ft^2') # |
Units.Unit('ft^2') # imperial area |
||
# comparing units |
# comparing units |
||
Unit(0,1) == Unit( |
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)') |
||
}} |
|||
</syntaxhighlight> |
|||
Unit è utilizzata principalmente per descrivere un parametro in un determinato tipo di unità. In FreeCAD si può quindi importare una particolare proprietà Type abbinata a una Unit per controllare e garantire che sia utilizzata l'unità corretta. |
|||
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. |
|||
Una Unit più un valore in virgola mobile sono definiti una quantità. |
|||
A Unit and a float value is called Quantity. |
|||
=== Quantity === |
=== Quantity === |
||
{{Code|code= |
|||
<syntaxhighlight> |
|||
from |
from FreeCAD import Units |
||
# to create a quantity you need a value (float) and a |
# to create a quantity you need a value (float) and a unit |
||
Quantity(1.0,Unit(0,1)) |
Units.Quantity(1.0,Units.Unit(0,1)) # Mass 1.0 kg |
||
Quantity(1.0,Unit(1)) |
Units.Quantity(1.0,Units.Unit(1)) # Length 1.0 mm |
||
Quantity(1.0,Unit(-1,1,-2)) |
Units.Quantity(1.0,Units.Unit(-1,1,-2)) # Pressure 1.0 kg/mm*s^2 |
||
Quantity(1.0, |
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) |
Units.Quantity(1.0,0,1) # Mass 1.0 kg |
||
Quantity(1.0,1) |
Units.Quantity(1.0,1) # Length 1.0 mm |
||
Quantity(1.0,-1,1,-2) |
Units.Quantity(1.0,-1,1,-2) # Pressure 1.0 kg/mm*s^2 |
||
# parsing |
# parsing quantities out of a string |
||
Quantity('1.0 kg/(m*s^2)') |
Units.Quantity('1.0 kg/(m*s^2)') # Pressure |
||
Quantity('1.0 Pa') |
Units.Quantity('1.0 Pa') # the same as combined Unit Pascale |
||
Quantity('1.0 J') |
Units.Quantity('1.0 J') # Joule (Work,Energy) mm^2*kg/(s^2) |
||
# You can |
# You can use a point or comma as float delimiter |
||
Quantity('1,0 m') |
Units.Quantity('1,0 m') |
||
Quantity('1.0 m') |
Units.Quantity('1.0 m') |
||
# you can use units from all supported |
# you can use units from all supported systems of units |
||
Quantity('1.0 psi') |
Units.Quantity('1.0 psi') # imperial pressure |
||
Quantity('1.0 lb') |
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') |
Units.Quantity('360/5 deg') # splitting circle |
||
Quantity('1/16 in') |
Units.Quantity('1/16 in') # fractions |
||
Quantity('5.3*6.3 m^2') |
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('1ft 3in') |
Units.Quantity('1ft 3in') # imperial style |
||
# and for sure calculation and comparison |
# and for sure calculation and comparison |
||
Quantity('1 Pa')* Quantity(2.0) == Quantity('2 Pa') |
Units.Quantity('1 Pa') * Units.Quantity(2.0) == Units.Quantity('2 Pa') |
||
Quantity('1 m')* Quantity('2 m') == Quantity('2 m^2') |
Units.Quantity('1 m') * Units.Quantity('2 m') == Units.Quantity('2 m^2') |
||
Quantity('1 m')* Quantity('2 ft') + Quantity('2 mm^2') |
Units.Quantity('1 m') * Units.Quantity('2 ft') + Units.Quantity('2 mm^2') |
||
Quantity('1 m') > Quantity('2 ft') |
Units.Quantity('1 m') > Units.Quantity('2 ft') |
||
# accessing the components |
# accessing the components |
||
Quantity('1 m').Value |
Units.Quantity('1 m').Value # get the number (always internal system (mm/kg/s)) |
||
Quantity('1 m').Unit |
Units.Quantity('1 m').Unit # get the unit |
||
Quantity('1 m') == Quantity( Quantity('1 m').Value , Quantity('1 m').Unit |
Units.Quantity('1 m') == Units.Quantity( Units.Quantity('1 m').Value , Units.Quantity('1 m').Unit) |
||
# translating the value into other units |
# translating the value into other units than the internal system (mm/kg/s) |
||
Quantity('1 km/h').getValueAs('m/s') # translate value |
Units.Quantity('1 km/h').getValueAs('m/s') # translate value |
||
Quantity('1 m').getValueAs(2.45,1) # translation value and unit signature |
Units.Quantity('1 m').getValueAs(2.45,1) # translation value and unit signature |
||
Quantity('1 kPa').getValueAs( |
Units.Quantity('1 kPa').getValueAs(Units.Pascal) # predefined standard units |
||
Quantity('1 MPa').getValueAs(Quantity('N/m^2')) |
Units.Quantity('1 MPa').getValueAs(Units.Quantity('N/m^2')) # a quantity |
||
}} |
|||
</syntaxhighlight> |
|||
=== |
=== I valori nell'interfaccia utente === |
||
Normalmente negli script si può utilizzare Quantity per ogni tipo di calcolo e di verifica, ma può capitare che si debba attendere un po' per ottenere l'uscita dei risultati. Si può utilizzare getValueAs() per forzare una certa unità, ma di solito si definisce il proprio sistema di unità nelle preferenze. Il sistema di unità converte tutta la rappresentazione nel sistema impostato. Al momento sono implementati questi 3 sistemi di unità: |
|||
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: |
|||
* 1: Internal (mm/kg/s) |
* 1: Internal (mm/kg/s) |
||
* 2: MKS (m/kg/s) |
* 2: MKS (m/kg/s) |
||
* 3: US customary (in/lb) |
* 3: US customary (in/lb) |
||
In futuro potranno essere facilmente implementati altri sistemi ... |
|||
There can be easily additional schemas implemented in the future... |
|||
Attualmente, ci sono due modi di utilizzare la classe Quantity per realizzare la conversione del sistema: |
|||
The quantity class has two possibilities to use the actual schema translation: |
|||
{{Code|code= |
|||
<syntaxhighlight> |
|||
from FreeCAD import Units |
|||
from Units import Unit,Quantity |
|||
# Use the translated string: |
# Use the translated string: |
||
Quantity('1m').UserString |
Units.Quantity('1m').UserString # '1000 mm' in 1; '1 m' in 2; and '1.09361 yr' in 3 |
||
}} |
|||
Questo metodo va bene quando serve solo una stringa. A volte però, si ha bisogno di un controllo maggiore, ad esempio quando si vuole avere una finestra di dialogo con un pulsante su e giù. In questo caso servono ulteriori informazioni sull'uscita della conversione, allora viene usato il metodo getUserPreferred() di quantity: |
|||
{{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') |
|||
}} |
|||
Qui si ottengono due informazioni in più sotto forma di una tupla di 3 elementi. Si ottiene, come prima, la stringa, inoltre il fattore numero viene tradotto e la stringa cruda con solo l'unità scelta dal sistema di conversione. Con queste informazioni è possibile implementare una interazione con l'utente molto più ricca. |
|||
Il codice di definizione del sistema di conversione è visibile in: |
|||
</syntaxhighlight> |
|||
* [https://github.com/FreeCAD/FreeCAD/blob/master/src/Base/UnitsSchemaInternal.cpp Internal] |
|||
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: |
|||
* [https://github.com/FreeCAD/FreeCAD/blob/master/src/Base/UnitsSchemaMKS.cpp MKS] |
|||
* [https://github.com/FreeCAD/FreeCAD/blob/master/src/Base/UnitsSchemaImperial1.cpp Imperial] |
|||
=== Precisione === |
|||
<syntaxhighlight> |
|||
La precisione delle quantità è il numero di decimali specificato nelle finestre di dialogo di FreeCAD [[Preferences_Editor/it#Unità|nelle preferenze]]. Per utilizzare queste impostazioni negli script (ad esempio nelle finestre di dialogo), si può usare questo codice: |
|||
Quantity('22 m').getUserPrefered() # gets a tubple:('22 m', 1000.0, 'm') |
|||
{{Code|code= |
|||
Quantity('2 m').getUserPrefered() # Tuple: ('2000 mm', 1.0, 'mm') |
|||
import FreeCAD |
|||
params = App.ParamGet("User parameter:BaseApp/Preferences/Units") |
|||
</syntaxhighlight> |
|||
params.GetInt('Decimals') # returns an int |
|||
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. |
|||
}} |
|||
== Appendice == |
|||
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 |
|||
=== Unità supportate dal Parser === |
|||
== Appendix == |
|||
Anche se tutte le unità fisiche possono essere descritte con le sette unità SI, la maggior parte delle unità utilizzate nelle aree tecniche sono delle combinazioni di unità comuni (come Pa = N/m^2 Pascal ). Pertanto il parser delle unità di FreeCAD supporta molte combinazioni del SI e del Sistema Imperiale. Queste unità sono definite nel file src/Base/QuantityParser.l e in futuro potranno essere incrementate. |
|||
=== 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. |
|||
{{Code|code= |
|||
from FreeCAD import Units |
|||
"nm" = Quantity(1.0e-6 ,Unit(1)); // nano meter |
|||
"µm" = Quantity(1.0e-3 ,Unit(1)); // micro meter |
|||
" |
"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" |
"µg" = Units.Quantity(1.0e-9 ,Units.Unit(0,1)); // micro gram |
||
"mg" |
"mg" = Units.Quantity(1.0e-6 ,Units.Unit(0,1)); // milli gram |
||
"g" |
"g" = Units.Quantity(1.0e-3 ,Units.Unit(0,1)); // gram |
||
"kg" |
"kg" = Units.Quantity(1.0 ,Units.Unit(0,1)); // kilo gram |
||
"t" |
"t" = Units.Quantity(1000.0 ,Units.Unit(0,1)); // ton |
||
"s" |
"s" = Units.Quantity(1.0 ,Units.Unit(0,0,1)); // second (internal standard time) |
||
"min" |
"min" = Units.Quantity(60.0 ,Units.Unit(0,0,1)); // minute |
||
"h" |
"h" = Units.Quantity(3600.0 ,Units.Unit(0,0,1)); // hour |
||
"A" |
"A" = Units.Quantity(1.0 ,Units.Unit(0,0,0,1)); // Ampere (internal standard electric current) |
||
"mA" |
"mA" = Units.Quantity(0.001 ,Units.Unit(0,0,0,1)); // milli Ampere |
||
"kA" |
"kA" = Units.Quantity(1000.0 ,Units.Unit(0,0,0,1)); // kilo Ampere |
||
"MA" |
"MA" = Units.Quantity(1.0e6 ,Units.Unit(0,0,0,1)); // Mega Ampere |
||
"K" |
"K" = Units.Quantity(1.0 ,Units.Unit(0,0,0,0,1)); // Kelvin (internal standard thermodynamic temperature) |
||
"mK" |
"mK" = Units.Quantity(0.001 ,Units.Unit(0,0,0,0,1)); // Kelvin |
||
"µK" |
"µK" = Units.Quantity(0.000001 ,Units.Unit(0,0,0,0,1)); // Kelvin |
||
"mol" |
"mol" = Units.Quantity(1.0 ,Units.Unit(0,0,0,0,0,1)); // Mole (internal standard amount of substance) |
||
"cd" |
"cd" = Units.Quantity(1.0 ,Units.Unit(0,0,0,0,0,0,1)); // Candela (internal standard luminous intensity) |
||
"deg" |
"deg" = Units.Quantity(1.0 ,Units.Unit(0,0,0,0,0,0,0,1)); // degree (internal standard angle) |
||
"rad" |
"rad" = Units.Quantity(180/M_PI ,Units.Unit(0,0,0,0,0,0,0,1)); // radian |
||
"gon" |
"gon" = Units.Quantity(360.0/400.0 ,Units.Unit(0,0,0,0,0,0,0,1)); // gon |
||
"in" |
"in" = Units.Quantity(25.4 ,Units.Unit(1)); // inch |
||
"\"" |
"\"" = Units.Quantity(25.4 ,Units.Unit(1)); // inch |
||
"fo" |
"fo" = Units.Quantity(304.8 ,Units.Unit(1)); // foot |
||
"'" |
"'" = Units.Quantity(304.8 ,Units.Unit(1)); // foot |
||
"th" |
"th" = Units.Quantity(0.0254 ,Units.Unit(1)); // thou |
||
"yd" |
"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 |
|||
}} |
|||
{{Powerdocnavi{{#translation:}}}} |
|||
"lb" = Quantity(0.45359237 ,Unit(0,1)); // pound |
|||
[[Category:Developer Documentation{{#translation:}}]] |
|||
"oz" = Quantity(0.0283495231 ,Unit(0,1)); // ounce |
|||
[[Category:Python Code{{#translation:}}]] |
|||
"st" = Quantity(6.35029318 ,Unit(0,1)); // Stone |
|||
"cwt" = Quantity(50.80234544 ,Unit(0,1)); // hundredweights |
|||
{{clear}} |
{{clear}} |
||
<languages/> |
Latest revision as of 20:33, 7 June 2022
Quantity è la combinazione di un numero in virgola mobile e di una unità di misura.
Aspetti generali
In un sistema CAD o CAE è molto importante mantenere la traccia dell'unità di misura di un valore. Quando si mescolano le unità o si calcolano i risultati in diverse unità di misura possono sorgere un sacco di problemi. Un famoso disastro, causato da un disguido sulle unità di misura, è il crash of the Mars Climate Orbiter. Anche all'interno di uno stesso Sistema di misura le unità possono essere disponibili in diversi formati, secondo il settore di utilizzo. Ad esempio, la velocità in km/h per le automobili, in m/s in robotica o in mm/min per la fresatura. Un sistema CAD deve conservare una traccia affidabile delle unità. Inoltre deve utilizzarle per eseguire i calcoli e deve verificare che per i parametri speciali sia adottata la giusta unità.
Per questo motivo è stata creata la sezione Quantity di FreeCAD. Essa include tutto il codice e gli oggetti che servono per trattare le unità, i calcoli delle unità, i dati inseriti dall'utente, la conversione tra i Sistemi e per fornire un corretto output delle unità e dei valori. In futuro, in FreeCAD, nessun parametro dovrebbe essere solo più un numero.
Unità supportate
Il parser dei dati in ingresso di FreeCAD supporta una serie di unità e di Sistemi di unità. FreeCAD supporta la lettera greca 'µ' per micro ma accetta anche 'u' in sostituzione. Un elenco completo di tutte le unità supportate si trova quì.
Le specifiche dettagliate si trovano nel codice:
Rappresentazione interna
Tutte le unità fisiche possono essere espresse come combinazione delle sette Unità SI di base:
Un modo semplice per esprimere una Unità è quello di utilizzare un gruppo di 7 interi, il numero delle unità di base, che definisce di quale unità si tratta.
Le firme delle 7 unità di base sono:
- LUNGHEZZA: [1,0,0,0,0,0,0]
- MASSA: [0,1,0,0,0,0,0]
- TEMPO: [0,0,1,0,0,0,0]
- CORRENTE ELETTRICA: [0,0,0,1,0,0,0]
- TEMPERATURA TERMODINAMICA: [0,0,0,0,1,0,0]
- QUANTITÀ DI SOSTANZA: [0,0,0,0,0,1,0]
- INTENSITÀ LUMINOSA: [0,0,0,0,0,0,1]
Usando queste 7 unità, siamo poi in grado di esprimere tutte le unità derivate definite nella Guida per l'utilizzo del Sistema Internazionale di Unità (SI) e di crearne di nuove, secondo le esigenze, come ad esempio:
- MASS DENSITY: [-3,1,0,0,0,0,0]
- AREA: [0,2,0,0,0,0,0]
Dato che l'angolo è fisicamente adimensionale, ma in un sistema CAD non è meno importante, si è aggiunta una unità virtuale in più per Angle. Questo crea un vettore di 8 dati nella firma delle unità di FreeCAD.
Il convertitore delle unità
Sovente è necessario convertire le unità da un sistema a un altro. Per esempio, quando i parametri sono contenuti in vecchie tabelle e espressi in determinate unità. In questi casi FreeCAD offre uno strumento di conversione denominato Units-Calculator che aiuta a tradurre le unità.
La sua desrizione si trova nella pagina: Std_UnitsCalculator
InputField
Inputfield è un oggetto QLineEdit derivato da un widget Qt e serve per gestire tutti i tipi di interazioni dell'utente con le quantità e con i parametri. Offre le seguenti proprietà:
- analisi dei valori e delle unità arbitrarie inserite
- verifica della corrispondenza dell'unità (se fornita) e restituzione di un riscontro
- speciale menu contestuale per le operazioni sulle Quantità e sui Valori
- gestione della cronologia (salva gli ultimi valori usati)
- salvataggio dei valori usati frequentemente nella scorciatoia del menu contestuale
- selezione dei valori con la rotellina del mouse e con i tasti freccia (tbd)
- selezione con il tasto centrale più il movimento del mouse (tbd)
- integrazione Python per l'utilizzo nella console Python (tbd)
Lo strumento UnitsCalculator usa già InputField.
Documentazione di riferimento: InputField
Codice:
- InputField.h
- InputField.cpp http://sourceforge.net/p/free-cad/code/ci/master/tree/src/Gui/InputField.cpp
Script Python
Come quasi tutto in FreeCAD, anche i sistemi Unit e Quantity sono totalmente accessibili tramite Python.
Unit
La classe Unit rappresenta la firma digitale di qualsiasi unità fisica. Come descritto nella sezione di base, per rappresentare questa firma digitale viene usato un vettore di otto numeri. La classe Unit permette di gestire tutte queste informazioni e di usarle per i calcoli.
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)')
Unit è utilizzata principalmente per descrivere un parametro in un determinato tipo di unità. In FreeCAD si può quindi importare una particolare proprietà Type abbinata a una Unit per controllare e garantire che sia utilizzata l'unità corretta. Una Unit più un valore in virgola mobile sono definiti una quantità.
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
I valori nell'interfaccia utente
Normalmente negli script si può utilizzare Quantity per ogni tipo di calcolo e di verifica, ma può capitare che si debba attendere un po' per ottenere l'uscita dei risultati. Si può utilizzare getValueAs() per forzare una certa unità, ma di solito si definisce il proprio sistema di unità nelle preferenze. Il sistema di unità converte tutta la rappresentazione nel sistema impostato. Al momento sono implementati questi 3 sistemi di unità:
- 1: Internal (mm/kg/s)
- 2: MKS (m/kg/s)
- 3: US customary (in/lb)
In futuro potranno essere facilmente implementati altri sistemi ...
Attualmente, ci sono due modi di utilizzare la classe Quantity per realizzare la conversione del sistema:
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
Questo metodo va bene quando serve solo una stringa. A volte però, si ha bisogno di un controllo maggiore, ad esempio quando si vuole avere una finestra di dialogo con un pulsante su e giù. In questo caso servono ulteriori informazioni sull'uscita della conversione, allora viene usato il metodo getUserPreferred() di quantity:
Units.Quantity('22 m').getUserPreferred() # gets a tuple:('22 m', 1000.0, 'm')
Units.Quantity('2 m').getUserPreferred() # Tuple: ('2000 mm', 1.0, 'mm')
Qui si ottengono due informazioni in più sotto forma di una tupla di 3 elementi. Si ottiene, come prima, la stringa, inoltre il fattore numero viene tradotto e la stringa cruda con solo l'unità scelta dal sistema di conversione. Con queste informazioni è possibile implementare una interazione con l'utente molto più ricca.
Il codice di definizione del sistema di conversione è visibile in:
Precisione
La precisione delle quantità è il numero di decimali specificato nelle finestre di dialogo di FreeCAD nelle preferenze. Per utilizzare queste impostazioni negli script (ad esempio nelle finestre di dialogo), si può usare questo codice:
import FreeCAD
params = App.ParamGet("User parameter:BaseApp/Preferences/Units")
params.GetInt('Decimals') # returns an int
Appendice
Unità supportate dal Parser
Anche se tutte le unità fisiche possono essere descritte con le sette unità SI, la maggior parte delle unità utilizzate nelle aree tecniche sono delle combinazioni di unità comuni (come Pa = N/m^2 Pascal ). Pertanto il parser delle unità di FreeCAD supporta molte combinazioni del SI e del Sistema Imperiale. Queste unità sono definite nel file src/Base/QuantityParser.l e in futuro potranno essere incrementate.
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
- FreeCAD scripting: Python, Introduction to Python, Python scripting tutorial, FreeCAD Scripting Basics
- Modules: Builtin modules, Units, Quantity
- Workbenches: Workbench creation, Gui Commands, Commands, Installing more workbenches
- Meshes and Parts: Mesh Scripting, Topological data scripting, Mesh to Part, PythonOCC
- Parametric objects: Scripted objects, Viewproviders (Custom icon in tree view)
- Scenegraph: Coin (Inventor) scenegraph, Pivy
- Graphical interface: Interface creation, Interface creation completely in Python (1, 2, 3, 4, 5), PySide, PySide examples beginner, intermediate, advanced
- Macros: Macros, How to install macros
- Embedding: Embedding FreeCAD, Embedding FreeCADGui
- Other: Expressions, Code snippets, Line drawing function, FreeCAD vector math library (deprecated)
- Hubs: User hub, Power users hub, Developer hub