/
UnitsApiPy.cpp
169 lines (147 loc) · 6.33 KB
/
UnitsApiPy.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/***************************************************************************
* Copyright (c) Juergen Riegel <juergen.riegel@web.de> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
# include <Python.h>
#endif
#include <CXX/Objects.hxx>
#include "Exception.h"
/// Here the FreeCAD includes sorted by Base,App,Gui......
#include "UnitsApi.h"
#include "Quantity.h"
#include "QuantityPy.h"
using namespace Base;
//**************************************************************************
// Python stuff of UnitsApi
// UnitsApi Methods // Methods structure
PyMethodDef UnitsApi::Methods[] = {
//{"translateUnit", (PyCFunction) UnitsApi::sTranslateUnit ,1,
// "translateUnit(string) -> double\n\n"
// "calculate a mathematical expression with units to a number. \n"
// "can be used for simple unit translation like: \n"
// " translateUnit('10m')\n"
// " or for more complex espressions:\n"
// " translateUnit('sin(pi)/50.0 m/s^2')\n"
//},
//{"getWithPrefs", (PyCFunction) UnitsApi::sGetWithPrefs ,1,
// "getWithPrefs(type,[string|float|int]) -> double\n\n"
// "Translation to internal regarding user prefs \n"
// " That means if no unit is issued the user prefs are in \n"
// " charge. If one unit is used the user prefs get ignored\n"
// " type can be: \n"
// " Length \n"
// " Area \n"
// " Volume \n"
// " Angle \n"
// " TimeSpan \n"
// " Velocity \n"
// " Acceleration \n"
// " Mass \n"
// " Temperature \n"
//},
{"parseQuantity", (PyCFunction) UnitsApi::sParseQuantity ,1,
"parseQuantity(string) -> Base.Quantity()\n\n"
"calculate a mathematical expression with units to a quantity object. \n"
"can be used for simple unit translation like: \n"
" parseQuantity('10m')\n"
" or for more complex espressions:\n"
" parseQuantity('sin(pi)/50.0 m/s^2')\n"
},
{NULL, NULL, 0, NULL} /* Sentinel */
};
//PyObject* UnitsApi::sTranslateUnit(PyObject * /*self*/, PyObject *args,PyObject * /*kwd*/)
//{
// char *pstr;
// if (!PyArg_ParseTuple(args, "s", &pstr)) // convert args: Python->C
// return NULL; // NULL triggers exception
// try {
// return Py::new_reference_to(Py::Object(Py::Float(UnitsApi::translateUnit(pstr))));
// }
// catch (const Base::Exception& e) {
// PyErr_Format(PyExc_IOError, "invalid unit expression %s: %s\n", pstr, e.what());
// return 0L;
// }
// catch (const std::exception& e) {
// PyErr_Format(PyExc_IOError, "invalid unit expression %s: %s\n", pstr, e.what());
// return 0L;
// }
//}
//
//PyObject* UnitsApi::sGetWithPrefs(PyObject * /*self*/, PyObject *args,PyObject * /*kwd*/)
//{
// char *type;
// PyObject *obj;
// if (!PyArg_ParseTuple(args, "sO", &type,&obj)) // convert args: Python->C
// return NULL; // NULL triggers exception
// try {
// QuantityType t;
// if(strcmp("Length",type)==0)
// t = Length;
// else{
// PyErr_Format(PyExc_IOError, "invalid quantity type: %s!", type);
// return 0L;
// }
//
// double result = toDblWithUserPrefs(t,obj);
// return Py::new_reference_to(Py::Object(Py::Float(result)));
// }
// 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;
// }
//}
PyObject* UnitsApi::sParseQuantity(PyObject * /*self*/, PyObject *args,PyObject * /*kwd*/)
{
PyObject * object;
if (PyArg_ParseTuple(args,"O", &object)) {
if (PyString_Check(object) || PyUnicode_Check(object)) {
QString qstr;
if (PyUnicode_Check(object)) {
PyObject * utf8str = PyUnicode_AsUTF8String(object);
qstr = QString::fromUtf8(PyString_AsString(utf8str));
Py_DECREF(utf8str);
}
else {
qstr = QString::fromUtf8(PyString_AsString(object));
}
Quantity rtn;
try {
rtn = Quantity::parse(qstr);
}
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));
} //string or unicode
} //if one object
PyErr_Format(PyExc_IOError, "invalid unit expression \n");
return 0L;
}