UnitsApiPy.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "PreCompiled.h"
00025
00026 #ifndef _PreComp_
00027 # include <Python.h>
00028 #endif
00029
00030 #include <CXX/Objects.hxx>
00031 #include "Exception.h"
00033 #include "UnitsApi.h"
00034
00035
00036
00037 using namespace Base;
00038
00039
00040
00041
00042
00043 PyMethodDef UnitsApi::Methods[] = {
00044 {"translateUnit", (PyCFunction) UnitsApi::sTranslateUnit ,1,
00045 "translateUnit(string) -> double\n\n"
00046 "calculate a mathematical expression with units to a number. \n"
00047 "can be used for simple unit translation like: \n"
00048 " translateUnit('10m')\n"
00049 " or for more complex espressions:\n"
00050 " translateUnit('sin(pi)/50.0 m/s^2')\n"
00051 },
00052 {"getWithPrefs", (PyCFunction) UnitsApi::sGetWithPrefs ,1,
00053 "getWithPrefs(type,[string|float|int]) -> double\n\n"
00054 "Translation to internal regarding user prefs \n"
00055 " That means if no unit is issued the user prefs are in \n"
00056 " charge. If one unit is used the user prefs get ignored\n"
00057 " type can be: \n"
00058 " Length \n"
00059 " Area \n"
00060 " Volume \n"
00061 " Angle \n"
00062 " TimeSpan \n"
00063 " Velocity \n"
00064 " Acceleration \n"
00065 " Mass \n"
00066 " Temperature \n"
00067
00068 },
00069
00070 {NULL, NULL, 0, NULL}
00071 };
00072
00073 PyObject* UnitsApi::sTranslateUnit(PyObject * , PyObject *args,PyObject * )
00074 {
00075 char *pstr;
00076 if (!PyArg_ParseTuple(args, "s", &pstr))
00077 return NULL;
00078 try {
00079 return Py::new_reference_to(Py::Object(Py::Float(UnitsApi::translateUnit(pstr))));
00080 }
00081 catch (const Base::Exception& e) {
00082 PyErr_Format(PyExc_IOError, "invalid unit expression %s: %s\n", pstr, e.what());
00083 return 0L;
00084 }
00085 catch (const std::exception& e) {
00086 PyErr_Format(PyExc_IOError, "invalid unit expression %s: %s\n", pstr, e.what());
00087 return 0L;
00088 }
00089 }
00090
00091 PyObject* UnitsApi::sGetWithPrefs(PyObject * , PyObject *args,PyObject * )
00092 {
00093 char *type;
00094 PyObject *obj;
00095 if (!PyArg_ParseTuple(args, "sO", &type,&obj))
00096 return NULL;
00097 try {
00098 QuantityType t;
00099 if(strcmp("Length",type)==0)
00100 t = Length;
00101 else{
00102 PyErr_Format(PyExc_IOError, "invalid quantity type: %s!", type);
00103 return 0L;
00104 }
00105
00106 double result = toDblWithUserPrefs(t,obj);
00107 return Py::new_reference_to(Py::Object(Py::Float(result)));
00108 }
00109 catch (const Base::Exception&) {
00110 PyErr_Format(PyExc_IOError, "invalid unit expression \n");
00111 return 0L;
00112 }
00113 catch (const std::exception&) {
00114 PyErr_Format(PyExc_IOError, "invalid unit expression \n");
00115 return 0L;
00116 }
00117 }
00118