1
/***************************************************************************
2
* Copyright (c) Jürgen Riegel <juergen.riegel@web.de> *
4
* This file is part of the FreeCAD CAx development system. *
6
* This library is free software; you can redistribute it and/or *
7
* modify it under the terms of the GNU Library General Public *
8
* License as published by the Free Software Foundation; either *
9
* version 2 of the License, or (at your option) any later version. *
11
* This library is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
* GNU Library General Public License for more details. *
16
* You should have received a copy of the GNU Library General Public *
17
* License along with this library; see the file COPYING.LIB. If not, *
18
* write to the Free Software Foundation, Inc., 59 Temple Place, *
19
* Suite 330, Boston, MA 02111-1307, USA *
21
***************************************************************************/
24
#include "PreCompiled.h"
30
#include <CXX/Objects.hxx>
34
#include "QuantityPy.h"
39
//**************************************************************************
40
// Python stuff of UnitsApi
43
PyMethodDef UnitsApi::Methods[] = {
45
UnitsApi::sParseQuantity,
47
"parseQuantity(string) -> Base.Quantity()\n\n"
48
"calculate a mathematical expression with units to a quantity object. \n"
49
"can be used for simple unit translation like: \n"
50
"parseQuantity('10m')\n"
51
"or for more complex espressions:\n"
52
"parseQuantity('sin(pi)/50.0 m/s^2')\n"},
54
UnitsApi::sListSchemas,
56
"listSchemas() -> a tuple of schemas\n\n"
57
"listSchemas(int) -> description of the given schema\n\n"},
61
"getSchema() -> int\n\n"
62
"The int is the position of the tuple returned by listSchemas"},
66
"setSchema(int) -> None\n\n"
67
"Sets the current schema to the given number, if possible"},
69
UnitsApi::sSchemaTranslate,
71
"schemaTranslate(Quantity, int) -> tuple\n\n"
72
"Translate a quantity to a given schema"},
76
"toNumber(Quantity or float, [format='g', decimals=-1]) -> str\n\n"
77
"Convert a quantity or float to a string"},
79
{nullptr, nullptr, 0, nullptr} /* Sentinel */
82
PyObject* UnitsApi::sParseQuantity(PyObject* /*self*/, PyObject* args)
85
if (!PyArg_ParseTuple(args, "et", "utf-8", &pstr)) {
90
QString qstr = QString::fromUtf8(pstr);
93
rtn = Quantity::parse(qstr);
95
catch (const Base::ParserError&) {
96
PyErr_Format(PyExc_ValueError, "invalid unit expression \n");
100
return new QuantityPy(new Quantity(rtn));
103
PyObject* UnitsApi::sListSchemas(PyObject* /*self*/, PyObject* args)
105
if (PyArg_ParseTuple(args, "")) {
106
int num = static_cast<int>(UnitSystem::NumUnitSystemTypes);
107
Py::Tuple tuple(num);
108
for (int i = 0; i < num; i++) {
109
const auto description {
110
UnitsApi::getDescription(static_cast<UnitSystem>(i)).toStdString()};
111
tuple.setItem(i, Py::String(description.c_str()));
114
return Py::new_reference_to(tuple);
119
if (PyArg_ParseTuple(args, "i", &index)) {
120
int num = static_cast<int>(UnitSystem::NumUnitSystemTypes);
121
if (index < 0 || index >= num) {
122
PyErr_SetString(PyExc_ValueError, "invalid schema value");
126
const auto description {
127
UnitsApi::getDescription(static_cast<UnitSystem>(index)).toStdString()};
128
return Py_BuildValue("s", description.c_str());
131
PyErr_SetString(PyExc_TypeError, "int or empty argument list expected");
135
PyObject* UnitsApi::sGetSchema(PyObject* /*self*/, PyObject* args)
137
if (!PyArg_ParseTuple(args, "")) {
141
return Py_BuildValue("i", static_cast<int>(currentSystem));
144
PyObject* UnitsApi::sSetSchema(PyObject* /*self*/, PyObject* args)
148
if (PyArg_ParseTuple(args, "i", &index)) {
149
int num = static_cast<int>(UnitSystem::NumUnitSystemTypes);
150
if (index < 0 || index >= num) {
151
PyErr_SetString(PyExc_ValueError, "invalid schema value");
154
setSchema(static_cast<UnitSystem>(index));
159
PyObject* UnitsApi::sSchemaTranslate(PyObject* /*self*/, PyObject* args)
163
if (!PyArg_ParseTuple(args, "O!i", &(QuantityPy::Type), &py, &index)) {
168
quant = *static_cast<Base::QuantityPy*>(py)->getQuantityPtr();
170
std::unique_ptr<UnitsSchema> schema(createSchema(static_cast<UnitSystem>(index)));
172
PyErr_SetString(PyExc_ValueError, "invalid schema value");
178
QString uss = schema->schemaTranslate(quant, factor, uus);
181
res[0] = Py::String(uss.toUtf8(), "utf-8");
182
res[1] = Py::Float(factor);
183
res[2] = Py::String(uus.toUtf8(), "utf-8");
185
return Py::new_reference_to(res);
188
PyObject* UnitsApi::sToNumber(PyObject* /*self*/, PyObject* args)
191
const char* format = "g";
195
if (PyArg_ParseTuple(args, "O!|si", &(QuantityPy::Type), &py, &format, &decimals)) {
196
value = static_cast<QuantityPy*>(py)->getQuantityPtr()->getValue();
201
if (PyArg_ParseTuple(args, "d|si", &value, &format, &decimals)) {
205
PyErr_SetString(PyExc_TypeError, "toNumber(Quantity or float, [format='g', decimals=-1])");
209
if (strlen(format) != 1) {
210
PyErr_SetString(PyExc_ValueError, "Format string hasn't length of 1");
216
qf.format = QuantityFormat::toFormat(format[0], &ok);
217
qf.precision = decimals;
220
PyErr_SetString(PyExc_ValueError, "Invalid format string");
224
QString string = toNumber(value, qf);
225
return Py::new_reference_to(Py::String(string.toStdString()));