first finished implementation of Quantity parser

This commit is contained in:
jriegel
2013-09-22 18:03:28 +02:00
parent 4db2355159
commit 3ee7b20927
13 changed files with 1988 additions and 1184 deletions

View File

@@ -31,6 +31,8 @@
#include "Exception.h"
/// Here the FreeCAD includes sorted by Base,App,Gui......
#include "UnitsApi.h"
#include "Quantity.h"
#include "QuantityPy.h"
@@ -66,6 +68,9 @@ PyMethodDef UnitsApi::Methods[] = {
" Temperature \n"
},
{"parseQuantity", (PyCFunction) UnitsApi::sParseQuantity ,1,
"parseQuantity(string) -> Base.Quantity()\n\n"
},
{NULL, NULL, 0, NULL} /* Sentinel */
};
@@ -116,3 +121,25 @@ PyObject* UnitsApi::sGetWithPrefs(PyObject * /*self*/, PyObject *args,PyObject *
}
}
PyObject* UnitsApi::sParseQuantity(PyObject * /*self*/, PyObject *args,PyObject * /*kwd*/)
{
char *pstr;
if (!PyArg_ParseTuple(args, "s", &pstr)) // convert args: Python->C
return NULL; // NULL triggers exception
Quantity rtn;
try {
rtn = Quantity::parse(pstr);
}
catch (const Base::Exception&) {
PyErr_Format(PyExc_IOError, "invalid unit expression \n");
return 0L;
}
catch (const std::exception&) {
PyErr_Format(PyExc_IOError, "invalid unit expression \n");
return 0L;
}
return new QuantityPy(new Quantity(rtn));
}