From fb84f715fae0ffe2f2dbdf7146306e795fd37b75 Mon Sep 17 00:00:00 2001 From: wmayer Date: Fri, 21 Jan 2022 14:57:49 +0100 Subject: [PATCH] Base: extend Python wrapper to allow to create a quantity with Units.Quantity(1, 'm') and add a unit test --- src/Base/QuantityPyImp.cpp | 17 ++++++++++++++++- src/Mod/Test/UnitTests.py | 5 +++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Base/QuantityPyImp.cpp b/src/Base/QuantityPyImp.cpp index bb2d79986e..ccfce27c12 100644 --- a/src/Base/QuantityPyImp.cpp +++ b/src/Base/QuantityPyImp.cpp @@ -133,7 +133,22 @@ int QuantityPy::PyInit(PyObject* args, PyObject* /*kwd*/) } catch(const Base::Exception& e) { PyErr_SetString(PyExc_ValueError, e.what()); - return-1; + return -1; + } + + return 0; + } + + PyErr_Clear(); // set by PyArg_ParseTuple() + if (PyArg_ParseTuple(args,"det", &f, "utf-8", &string)) { + QString unit = QString::fromUtf8(string); + PyMem_Free(string); + try { + *self = Quantity(f, unit); + } + catch(const Base::Exception& e) { + PyErr_SetString(PyExc_ValueError, e.what()); + return -1; } return 0; diff --git a/src/Mod/Test/UnitTests.py b/src/Mod/Test/UnitTests.py index 5c6ffea7e6..e9006455a0 100644 --- a/src/Mod/Test/UnitTests.py +++ b/src/Mod/Test/UnitTests.py @@ -147,3 +147,8 @@ class UnitBasicCases(unittest.TestCase): self.failUnless(compare(tu('sin(pi)'), math.sin(math.pi))) self.failUnless(compare(tu('cos(pi)'), math.cos(math.pi))) self.failUnless(compare(tu('tan(pi)'), math.tan(math.pi))) + + def testQuantity(self): + length = FreeCAD.Units.Quantity(1, "m") + self.assertEqual(length.Value, 1000) + self.assertEqual(length.Unit, FreeCAD.Units.Length)