Base: expose enum NumberFormat to Python
This commit is contained in:
@@ -909,6 +909,13 @@ class Scheme(IntEnum):
|
||||
|
||||
App.Units.Scheme = Scheme
|
||||
|
||||
class NumberFormat(IntEnum):
|
||||
Default = 0
|
||||
Fixed = 1
|
||||
Scientific = 2
|
||||
|
||||
App.Units.NumberFormat = NumberFormat
|
||||
|
||||
class ScaleType(IntEnum):
|
||||
Other = -1
|
||||
NoScaling = 0
|
||||
|
||||
@@ -51,7 +51,7 @@ Quantity(string) -- arbitrary mixture of numbers and chars defining a Quantity
|
||||
Following parameters are allowed:
|
||||
getValueAs('m/s') # unit string to parse
|
||||
getValueAs(2.45,1) # translation value and unit signature
|
||||
getValueAs(FreeCAD.Units.Pascal) # predefined standard units
|
||||
getValueAs(FreeCAD.Units.Pascal) # predefined standard units
|
||||
getValueAs(Qantity('N/m^2')) # a quantity
|
||||
getValueAs(Unit(0,1,0,0,0,0,0,0)) # a unit
|
||||
</UserDocu>
|
||||
|
||||
@@ -628,15 +628,24 @@ void QuantityPy::setFormat(Py::Dict arg)
|
||||
}
|
||||
|
||||
if (arg.hasKey("NumberFormat")) {
|
||||
Py::Char form(arg.getItem("NumberFormat"));
|
||||
std::string fmtstr = static_cast<std::string>(Py::String(form));
|
||||
if (fmtstr.size() != 1)
|
||||
throw Py::ValueError("Invalid format character");
|
||||
Py::Object item = arg.getItem("NumberFormat");
|
||||
if (item.isNumeric()) {
|
||||
int format = static_cast<int>(Py::Int(item));
|
||||
if (format < 0 || format > QuantityFormat::Scientific)
|
||||
throw Py::ValueError("Invalid format value");
|
||||
fmt.format = static_cast<QuantityFormat::NumberFormat>(format);
|
||||
}
|
||||
else {
|
||||
Py::Char form(item);
|
||||
std::string fmtstr = static_cast<std::string>(Py::String(form));
|
||||
if (fmtstr.size() != 1)
|
||||
throw Py::ValueError("Invalid format character");
|
||||
|
||||
bool ok;
|
||||
fmt.format = Base::QuantityFormat::toFormat(fmtstr[0], &ok);
|
||||
if (!ok)
|
||||
throw Py::ValueError("Invalid format character");
|
||||
bool ok;
|
||||
fmt.format = Base::QuantityFormat::toFormat(fmtstr[0], &ok);
|
||||
if (!ok)
|
||||
throw Py::ValueError("Invalid format character");
|
||||
}
|
||||
}
|
||||
|
||||
if (arg.hasKey("Denominator")) {
|
||||
|
||||
@@ -110,9 +110,7 @@ class UnitBasicCases(unittest.TestCase):
|
||||
self.assertAlmostEqual(1, v.Value, msg="Failed with \"{0}\" scheme: {1} != 1 (delta: {2})".format(schemes[i], v.Value, self.delta), delta=self.delta)
|
||||
|
||||
vacuum_permittivity = FreeCAD.Units.parseQuantity("1F/m")
|
||||
format = vacuum_permittivity.Format
|
||||
format["NumberFormat"] = "e" # scientific notation
|
||||
vacuum_permittivity.Format = format
|
||||
vacuum_permittivity.Format = {"NumberFormat" : FreeCAD.Units.NumberFormat.Scientific}
|
||||
for i in range(num):
|
||||
t = FreeCAD.Units.schemaTranslate(vacuum_permittivity, i)
|
||||
v = FreeCAD.Units.parseQuantity(t[0]).getValueAs("F/m")
|
||||
|
||||
Reference in New Issue
Block a user