Base: make UnitsApi::toNumber method of Quantity
This static method takes Quantity as a parameter, so make it method of Quantity directly.
This commit is contained in:
@@ -26,6 +26,7 @@
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
#include <numbers>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#endif
|
||||
|
||||
@@ -243,7 +244,26 @@ Quantity Quantity::operator-() const
|
||||
|
||||
std::string Quantity::toString(const QuantityFormat& format) const
|
||||
{
|
||||
return fmt::format("'{} {}'", UnitsApi::toNumber(myValue, format), myUnit.getString());
|
||||
return fmt::format("'{} {}'", toNumber(format), myUnit.getString());
|
||||
}
|
||||
|
||||
std::string Quantity::toNumber(const QuantityFormat& format) const
|
||||
{
|
||||
std::stringstream ss;
|
||||
|
||||
switch (format.format) {
|
||||
case QuantityFormat::Fixed:
|
||||
ss << std::fixed;
|
||||
break;
|
||||
case QuantityFormat::Scientific:
|
||||
ss << std::scientific;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
ss << std::setprecision(format.precision) << myValue;
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
std::string Quantity::getUserString() const
|
||||
|
||||
@@ -160,6 +160,9 @@ public:
|
||||
std::string
|
||||
toString(const QuantityFormat& format = QuantityFormat(QuantityFormat::Default)) const;
|
||||
|
||||
std::string
|
||||
toNumber(const QuantityFormat& format = QuantityFormat(QuantityFormat::Default)) const;
|
||||
|
||||
std::string getUserString() const;
|
||||
/// transfer to user preferred unit/potence
|
||||
std::string getUserString(double& factor, std::string& unitString) const;
|
||||
|
||||
@@ -23,7 +23,6 @@
|
||||
#include "PreCompiled.h"
|
||||
#ifndef _PreComp_
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
#endif
|
||||
|
||||
#include <CXX/WrapPython.h>
|
||||
@@ -93,30 +92,6 @@ void UnitsApi::setSchema(const size_t num)
|
||||
schemas->select(num);
|
||||
}
|
||||
|
||||
std::string UnitsApi::toNumber(const Quantity& quantity, const QuantityFormat& format)
|
||||
{
|
||||
return toNumber(quantity.getValue(), format);
|
||||
}
|
||||
|
||||
std::string UnitsApi::toNumber(const double value, const QuantityFormat& format)
|
||||
{
|
||||
std::stringstream ss;
|
||||
|
||||
switch (format.format) {
|
||||
case QuantityFormat::Fixed:
|
||||
ss << std::fixed;
|
||||
break;
|
||||
case QuantityFormat::Scientific:
|
||||
ss << std::scientific;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
ss << std::setprecision(format.precision) << value;
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
double UnitsApi::toDouble(PyObject* args, const Base::Unit& u)
|
||||
{
|
||||
if (PyUnicode_Check(args)) {
|
||||
|
||||
@@ -51,21 +51,6 @@ public:
|
||||
|
||||
static std::string schemaTranslate(const Quantity& quant);
|
||||
|
||||
/**
|
||||
* Quantity to string. Optionally apply format
|
||||
* The string is a number in C locale (i.e. the decimal separator is always a dot)
|
||||
* Scientific notation (if needed).
|
||||
*/
|
||||
|
||||
/** Does NOT include unit */
|
||||
static std::string
|
||||
toNumber(const Quantity& quantity,
|
||||
const QuantityFormat& format = QuantityFormat(QuantityFormat::Default));
|
||||
|
||||
/** Does NOT include unit */
|
||||
static std::string
|
||||
toNumber(double value, const QuantityFormat& format = QuantityFormat(QuantityFormat::Default));
|
||||
|
||||
static double toDouble(PyObject* args, const Base::Unit& u = Base::Unit());
|
||||
|
||||
static void setDecimals(std::size_t);
|
||||
|
||||
@@ -217,5 +217,6 @@ PyObject* UnitsApi::sToNumber(PyObject* /*self*/, PyObject* args)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return Py::new_reference_to(Py::String(toNumber(value, qf)));
|
||||
const Quantity quantity {value};
|
||||
return Py::new_reference_to(Py::String(quantity.toNumber(qf)));
|
||||
}
|
||||
|
||||
@@ -29,7 +29,6 @@
|
||||
#include <App/Document.h>
|
||||
#include <Base/Interpreter.h>
|
||||
#include <Base/PyObjectBase.h>
|
||||
#include <Base/UnitsApi.h>
|
||||
#include <Gui/Application.h>
|
||||
#include <Gui/Command.h>
|
||||
#include <Gui/Document.h>
|
||||
@@ -132,9 +131,9 @@ void DlgRegularSolidImp::onCreateSolidButtonClicked()
|
||||
"App.ActiveDocument.{0}.Length={1}\n"
|
||||
"App.ActiveDocument.{0}.Width={2}\n"
|
||||
"App.ActiveDocument.{0}.Height={3}\n", name,
|
||||
Base::UnitsApi::toNumber(ui->boxLength->value()),
|
||||
Base::UnitsApi::toNumber(ui->boxWidth->value()),
|
||||
Base::UnitsApi::toNumber(ui->boxHeight->value()));
|
||||
ui->boxLength->value().toNumber(),
|
||||
ui->boxWidth->value().toNumber(),
|
||||
ui->boxHeight->value().toNumber());
|
||||
break;
|
||||
case 1:
|
||||
name = doc->getUniqueObjectName("Cylinder");
|
||||
@@ -145,9 +144,9 @@ void DlgRegularSolidImp::onCreateSolidButtonClicked()
|
||||
"App.ActiveDocument.{0}.EdgeLength={3}\n"
|
||||
"App.ActiveDocument.{0}.Closed={4}\n"
|
||||
"App.ActiveDocument.{0}.Sampling={5}\n", name,
|
||||
Base::UnitsApi::toNumber(ui->cylinderRadius->value()),
|
||||
Base::UnitsApi::toNumber(ui->cylinderLength->value()),
|
||||
Base::UnitsApi::toNumber(ui->cylinderEdgeLength->value()),
|
||||
ui->cylinderRadius->value().toNumber(),
|
||||
ui->cylinderLength->value().toNumber(),
|
||||
ui->cylinderEdgeLength->value().toNumber(),
|
||||
ui->cylinderClosed->isChecked() ? "True" : "False",
|
||||
ui->cylinderCount->value());
|
||||
break;
|
||||
@@ -161,10 +160,10 @@ void DlgRegularSolidImp::onCreateSolidButtonClicked()
|
||||
"App.ActiveDocument.{0}.EdgeLength={4}\n"
|
||||
"App.ActiveDocument.{0}.Closed={5}\n"
|
||||
"App.ActiveDocument.{0}.Sampling={6}\n", name,
|
||||
Base::UnitsApi::toNumber(ui->coneRadius1->value()),
|
||||
Base::UnitsApi::toNumber(ui->coneRadius2->value()),
|
||||
Base::UnitsApi::toNumber(ui->coneLength->value()),
|
||||
Base::UnitsApi::toNumber(ui->coneEdgeLength->value()),
|
||||
ui->coneRadius1->value().toNumber(),
|
||||
ui->coneRadius2->value().toNumber(),
|
||||
ui->coneLength->value().toNumber(),
|
||||
ui->coneEdgeLength->value().toNumber(),
|
||||
ui->coneClosed->isChecked() ? "True" : "False",
|
||||
ui->coneCount->value());
|
||||
break;
|
||||
@@ -174,7 +173,7 @@ void DlgRegularSolidImp::onCreateSolidButtonClicked()
|
||||
"App.ActiveDocument.addObject(\"Mesh::Sphere\",\"{0}\")\n"
|
||||
"App.ActiveDocument.{0}.Radius={1}\n"
|
||||
"App.ActiveDocument.{0}.Sampling={2}\n", name,
|
||||
Base::UnitsApi::toNumber(ui->sphereRadius->value()),
|
||||
ui->sphereRadius->value().toNumber(),
|
||||
ui->sphereCount->value());
|
||||
break;
|
||||
case 4:
|
||||
@@ -184,8 +183,8 @@ void DlgRegularSolidImp::onCreateSolidButtonClicked()
|
||||
"App.ActiveDocument.{0}.Radius1={1}\n"
|
||||
"App.ActiveDocument.{0}.Radius2={2}\n"
|
||||
"App.ActiveDocument.{0}.Sampling={3}\n", name,
|
||||
Base::UnitsApi::toNumber(ui->ellipsoidRadius1->value()),
|
||||
Base::UnitsApi::toNumber(ui->ellipsoidRadius2->value()),
|
||||
ui->ellipsoidRadius1->value().toNumber(),
|
||||
ui->ellipsoidRadius2->value().toNumber(),
|
||||
ui->ellipsoidCount->value());
|
||||
break;
|
||||
case 5:
|
||||
@@ -195,8 +194,8 @@ void DlgRegularSolidImp::onCreateSolidButtonClicked()
|
||||
"App.ActiveDocument.{0}.Radius1={1}\n"
|
||||
"App.ActiveDocument.{0}.Radius2={2}\n"
|
||||
"App.ActiveDocument.{0}.Sampling={3}\n", name,
|
||||
Base::UnitsApi::toNumber(ui->toroidRadius1->value()),
|
||||
Base::UnitsApi::toNumber(ui->toroidRadius2->value()),
|
||||
ui->toroidRadius1->value().toNumber(),
|
||||
ui->toroidRadius2->value().toNumber(),
|
||||
ui->toroidCount->value());
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user