| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* Copyright (c) Jürgen Riegel <[email protected]> * |
| 5 |
* * |
| 6 |
* This file is part of the FreeCAD CAx development system. * |
| 7 |
* * |
| 8 |
* This library is free software; you can redistribute it and/or * |
| 9 |
* modify it under the terms of the GNU Library General Public * |
| 10 |
* License as published by the Free Software Foundation; either * |
| 11 |
* version 2 of the License, or (at your option) any later version. * |
| 12 |
* * |
| 13 |
* This library is distributed in the hope that it will be useful, * |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 16 |
* GNU Library General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Library General Public * |
| 19 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 20 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 21 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 22 |
* * |
| 23 |
***************************************************************************/ |
| 24 |
|
| 25 |
#include <memory> |
| 26 |
|
| 27 |
#include <CXX/Objects.hxx> |
| 28 |
|
| 29 |
#include "UnitsApi.h" |
| 30 |
#include "Quantity.h" |
| 31 |
#include "QuantityPy.h" |
| 32 |
|
| 33 |
|
| 34 |
using namespace Base; |
| 35 |
|
| 36 |
//************************************************************************** |
| 37 |
// Python stuff of UnitsApi |
| 38 |
|
| 39 |
PyMethodDef UnitsApi::Methods[] = { |
| 40 |
{"parseQuantity", |
| 41 |
sParseQuantity, |
| 42 |
METH_VARARGS, |
| 43 |
"parseQuantity(string) -> Base.Quantity()\n\n" |
| 44 |
"calculate a mathematical expression with units to a quantity object. \n" |
| 45 |
"can be used for simple unit translation like: \n" |
| 46 |
"parseQuantity('10m')\n" |
| 47 |
"or for more complex espressions:\n" |
| 48 |
"parseQuantity('sin(pi)/50.0 m/s^2')\n"}, |
| 49 |
{"listSchemas", |
| 50 |
sListSchemas, |
| 51 |
METH_VARARGS, |
| 52 |
"listSchemas() -> a tuple of schemas\n\n" |
| 53 |
"listSchemas(int) -> description of the given schema\n\n"}, |
| 54 |
{"getSchema", |
| 55 |
sGetSchema, |
| 56 |
METH_VARARGS, |
| 57 |
"getSchema() -> int\n\n" |
| 58 |
"The int is the position of the tuple returned by listSchemas"}, |
| 59 |
{"setSchema", |
| 60 |
sSetSchema, |
| 61 |
METH_VARARGS, |
| 62 |
"setSchema(int) -> None\n\n" |
| 63 |
"Sets the current schema to the given number, if possible"}, |
| 64 |
{"schemaTranslate", |
| 65 |
sSchemaTranslate, |
| 66 |
METH_VARARGS, |
| 67 |
"schemaTranslate(Quantity, int) -> tuple\n\n" |
| 68 |
"Translate a quantity to a given schema"}, |
| 69 |
{"toNumber", |
| 70 |
sToNumber, |
| 71 |
METH_VARARGS, |
| 72 |
"toNumber(Quantity or float, [format='g', decimals=-1]) -> str\n\n" |
| 73 |
"Convert a quantity or float to a string"}, |
| 74 |
|
| 75 |
{nullptr, nullptr, 0, nullptr} /* Sentinel */ |
| 76 |
}; |
| 77 |
|
| 78 |
PyObject* UnitsApi::sParseQuantity(PyObject* /*self*/, PyObject* args) |
| 79 |
{ |
| 80 |
char* pstr {}; |
| 81 |
if (!PyArg_ParseTuple(args, "et", "utf-8", &pstr)) { |
| 82 |
return nullptr; |
| 83 |
} |
| 84 |
|
| 85 |
const std::string str {pstr}; |
| 86 |
PyMem_Free(pstr); |
| 87 |
try { |
| 88 |
return new QuantityPy(new Quantity(Quantity::parse(str))); |
| 89 |
} |
| 90 |
catch (const ParserError&) { |
| 91 |
PyErr_Format(PyExc_ValueError, "invalid unit expression: '%s'\n", str.c_str()); |
| 92 |
return nullptr; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
PyObject* UnitsApi::sListSchemas(PyObject* /*self*/, PyObject* args) |
| 97 |
{ |
| 98 |
auto names = UnitsApi::getNames(); |
| 99 |
const int num = static_cast<int>(names.size()); |
| 100 |
|
| 101 |
if (PyArg_ParseTuple(args, "")) { |
| 102 |
Py::Tuple tuple {num}; |
| 103 |
|
| 104 |
auto addItem = [&, i {0}](const std::string& name) mutable { |
| 105 |
tuple.setItem(i++, Py::String {name.c_str()}); |
| 106 |
}; |
| 107 |
|
| 108 |
std::for_each(names.begin(), names.end(), addItem); |
| 109 |
|
| 110 |
return Py::new_reference_to(tuple); |
| 111 |
} |
| 112 |
|
| 113 |
PyErr_Clear(); |
| 114 |
int index {}; |
| 115 |
if (PyArg_ParseTuple(args, "i", &index)) { |
| 116 |
if (index < 0 || index >= num) { |
| 117 |
PyErr_SetString(PyExc_ValueError, "invalid schema value"); |
| 118 |
return nullptr; |
| 119 |
} |
| 120 |
|
| 121 |
const auto description = schemas->descriptions().at(index); |
| 122 |
return Py_BuildValue("s", description.c_str()); |
| 123 |
} |
| 124 |
|
| 125 |
PyErr_SetString(PyExc_TypeError, "int or empty argument list expected"); |
| 126 |
return nullptr; |
| 127 |
} |
| 128 |
|
| 129 |
PyObject* UnitsApi::sGetSchema(PyObject* /*self*/, PyObject* args) |
| 130 |
{ |
| 131 |
if (!PyArg_ParseTuple(args, "")) { |
| 132 |
return nullptr; |
| 133 |
} |
| 134 |
|
| 135 |
return Py_BuildValue("i", schemas->currentSchema()->getNum()); |
| 136 |
} |
| 137 |
|
| 138 |
PyObject* UnitsApi::sSetSchema(PyObject* /*self*/, PyObject* args) |
| 139 |
{ |
| 140 |
PyErr_Clear(); |
| 141 |
int index {}; |
| 142 |
if (PyArg_ParseTuple(args, "i", &index) != 0) { |
| 143 |
|
| 144 |
if (index < 0 || index >= static_cast<int>(count())) { |
| 145 |
PyErr_SetString(PyExc_ValueError, "invalid schema value"); |
| 146 |
return nullptr; |
| 147 |
} |
| 148 |
|
| 149 |
schemas->select(index); |
| 150 |
} |
| 151 |
Py_Return; |
| 152 |
} |
| 153 |
|
| 154 |
PyObject* UnitsApi::sSchemaTranslate(PyObject* /*self*/, PyObject* args) |
| 155 |
{ |
| 156 |
PyObject* py {}; |
| 157 |
int index {}; |
| 158 |
if (!PyArg_ParseTuple(args, "O!i", &QuantityPy::Type, &py, &index)) { |
| 159 |
return nullptr; |
| 160 |
} |
| 161 |
|
| 162 |
if (index < 0 || index >= static_cast<int>(count())) { |
| 163 |
PyErr_SetString( |
| 164 |
PyExc_ValueError, |
| 165 |
std::string {"invalid schema index: " + std::to_string(index)}.c_str() |
| 166 |
); |
| 167 |
return nullptr; |
| 168 |
} |
| 169 |
|
| 170 |
const Quantity quant {*static_cast<QuantityPy*>(py)->getQuantityPtr()}; |
| 171 |
|
| 172 |
double factor {}; |
| 173 |
std::string unitStr; |
| 174 |
auto schema = std::make_unique<UnitsSchema>(schemas->spec(index)); |
| 175 |
const std::string unitStrLocalised = schema->translate(quant, factor, unitStr); |
| 176 |
|
| 177 |
Py::Tuple res {3}; |
| 178 |
res[0] = Py::String {unitStrLocalised, "utf-8"}; |
| 179 |
res[1] = Py::Float {factor}; |
| 180 |
res[2] = Py::String {unitStr, "utf-8"}; |
| 181 |
|
| 182 |
return Py::new_reference_to(res); |
| 183 |
} |
| 184 |
|
| 185 |
PyObject* UnitsApi::sToNumber(PyObject* /*self*/, PyObject* args) |
| 186 |
{ |
| 187 |
double value {}; |
| 188 |
const char* format = "g"; |
| 189 |
int decimals {}; |
| 190 |
do { |
| 191 |
PyObject* py {}; |
| 192 |
if (PyArg_ParseTuple(args, "O!|si", &(QuantityPy::Type), &py, &format, &decimals)) { |
| 193 |
value = static_cast<QuantityPy*>(py)->getQuantityPtr()->getValue(); |
| 194 |
break; |
| 195 |
} |
| 196 |
|
| 197 |
PyErr_Clear(); |
| 198 |
if (PyArg_ParseTuple(args, "d|si", &value, &format, &decimals)) { |
| 199 |
break; |
| 200 |
} |
| 201 |
|
| 202 |
PyErr_SetString(PyExc_TypeError, "toNumber(Quantity or float, [format='g', decimals=-1])"); |
| 203 |
return nullptr; |
| 204 |
} while (false); |
| 205 |
|
| 206 |
if (strlen(format) != 1) { |
| 207 |
PyErr_SetString(PyExc_ValueError, "Format string hasn't length of 1"); |
| 208 |
return nullptr; |
| 209 |
} |
| 210 |
|
| 211 |
bool ok {}; |
| 212 |
QuantityFormat qf {QuantityFormat::toFormat(format[0], &ok), decimals}; |
| 213 |
|
| 214 |
if (!ok) { |
| 215 |
PyErr_SetString(PyExc_ValueError, "Invalid format string"); |
| 216 |
return nullptr; |
| 217 |
} |
| 218 |
|
| 219 |
const Quantity quantity {value}; |
| 220 |
return Py::new_reference_to(Py::String(quantity.toNumber(qf))); |
| 221 |
} |