| 1 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
from Metadata import export, constmethod |
| 4 |
from PyObjectBase import PyObjectBase |
| 5 |
from typing import overload, Final, Tuple, Union |
| 6 |
from Unit import Unit as UnitPy |
| 7 |
|
| 8 |
@export( |
| 9 |
NumberProtocol=True, |
| 10 |
RichCompare=True, |
| 11 |
Constructor=True, |
| 12 |
Delete=True, |
| 13 |
) |
| 14 |
class Quantity(PyObjectBase): |
| 15 |
""" |
| 16 |
Quantity |
| 17 |
defined by a value and a unit. |
| 18 |
|
| 19 |
The following constructors are supported: |
| 20 |
Quantity() -- empty constructor |
| 21 |
Quantity(Value) -- empty constructor |
| 22 |
Quantity(Value,Unit) -- empty constructor |
| 23 |
Quantity(Quantity) -- copy constructor |
| 24 |
Quantity(string) -- arbitrary mixture of numbers and chars defining a Quantity |
| 25 |
|
| 26 |
Author: Juergen Riegel ([email protected]) |
| 27 |
Licence: LGPL |
| 28 |
""" |
| 29 |
|
| 30 |
Value: float = ... |
| 31 |
"""Numeric Value of the Quantity (in internal system mm,kg,s)""" |
| 32 |
|
| 33 |
Unit: UnitPy = ... |
| 34 |
"""Unit of the Quantity""" |
| 35 |
|
| 36 |
UserString: Final[str] = ... |
| 37 |
"""Unit of the Quantity""" |
| 38 |
|
| 39 |
Format: dict = ... |
| 40 |
"""Format of the Quantity""" |
| 41 |
|
| 42 |
# fmt: off |
| 43 |
@overload |
| 44 |
def __init__(self) -> None: ... |
| 45 |
@overload |
| 46 |
def __init__(self, value: float) -> None: ... |
| 47 |
@overload |
| 48 |
def __init__(self, value: float, unit: UnitPy) -> None: ... |
| 49 |
@overload |
| 50 |
def __init__(self, quantity: "Quantity") -> None: ... |
| 51 |
@overload |
| 52 |
def __init__(self, string: str) -> None: ... |
| 53 |
# fmt: on |
| 54 |
|
| 55 |
@constmethod |
| 56 |
def toStr(self, decimals: int = ...) -> str: |
| 57 |
""" |
| 58 |
toStr([decimals]) |
| 59 |
|
| 60 |
Returns a string representation rounded to number of decimals. If no decimals are specified then |
| 61 |
the internal precision is used |
| 62 |
""" |
| 63 |
... |
| 64 |
|
| 65 |
@overload |
| 66 |
def toStr(self) -> str: ... |
| 67 |
@overload |
| 68 |
def toStr(self, decimals: int) -> str: ... |
| 69 |
@constmethod |
| 70 |
def getUserPreferred(self) -> Tuple["Quantity", str]: |
| 71 |
""" |
| 72 |
Returns a quantity with the translation factor and a string with the prevered unit |
| 73 |
""" |
| 74 |
... |
| 75 |
|
| 76 |
@overload |
| 77 |
def getValueAs(self, unit: str) -> float: ... |
| 78 |
@overload |
| 79 |
def getValueAs(self, translation: float, unit_signature: int) -> float: ... |
| 80 |
@overload |
| 81 |
def getValueAs(self, unit: UnitPy) -> float: ... |
| 82 |
@overload |
| 83 |
def getValueAs(self, quantity: "Quantity") -> float: ... |
| 84 |
@constmethod |
| 85 |
def getValueAs(self, *args) -> float: |
| 86 |
""" |
| 87 |
Returns a floating point value as the provided unit |
| 88 |
|
| 89 |
Following parameters are allowed: |
| 90 |
getValueAs('m/s') # unit string to parse |
| 91 |
getValueAs(2.45,1) # translation value and unit signature |
| 92 |
getValueAs(FreeCAD.Units.Pascal) # predefined standard units |
| 93 |
getValueAs(Qantity('N/m^2')) # a quantity |
| 94 |
getValueAs(Unit(0,1,0,0,0,0,0,0)) # a unit |
| 95 |
""" |
| 96 |
... |
| 97 |
|
| 98 |
@constmethod |
| 99 |
def __round__(self, ndigits: int = ...) -> Union[int, float]: |
| 100 |
""" |
| 101 |
Returns the Integral closest to x, rounding half toward even. |
| 102 |
When an argument is passed, work like built-in round(x, ndigits). |
| 103 |
""" |
| 104 |
... |
| 105 |
|
| 106 |
@overload |
| 107 |
def __round__(self) -> int: ... |
| 108 |
@overload |
| 109 |
def __round__(self, ndigits: int) -> float: ... |