Skip to content

Commit

Permalink
Base: extend Python wrapper to allow to create a quantity with Units.…
Browse files Browse the repository at this point in the history
…Quantity(1, 'm') and add a unit test
  • Loading branch information
wwmayer committed Jan 21, 2022
1 parent 2fef77e commit fb84f71
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/Base/QuantityPyImp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/Mod/Test/UnitTests.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,8 @@ def testTrigonometric(self):
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)

0 comments on commit fb84f71

Please sign in to comment.