| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| |
|
| | #ifdef RS_OPT_SIMPLEPYTHON |
| | #include "rs_simplepython.h" |
| |
|
| |
|
| | RS_SimplePython* RS_SimplePython::uniqueInstance = NULL; |
| |
|
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | RS_SimplePython* RS_SimplePython::instance() { |
| | if(uniqueInstance==NULL) { |
| | uniqueInstance = new RS_SimplePython; |
| | } |
| | return uniqueInstance; |
| | } |
| |
|
| |
|
| | |
| | |
| | |
| | int RS_SimplePython::launch(const QString& script) { |
| | long answer; |
| | PyObject *modname, *mod, *mdict, *func, *rslt; |
| | |
| | Py_Initialize(); |
| | init_pyextension(); |
| | modname = PyString_FromString(script); |
| | mod = PyImport_Import(modname); |
| | if (mod) { |
| | |
| | mdict = PyModule_GetDict(mod); |
| |
|
| | |
| | func = PyDict_GetItemString(mdict, "start"); |
| | if (func) { |
| | |
| | if (PyCallable_Check(func)) { |
| | |
| | rslt = PyObject_CallFunction(func, "(s)", "noparam"); |
| | |
| | if (rslt) { |
| | |
| | answer = PyInt_AsLong(rslt); |
| | |
| | Py_XDECREF(rslt); |
| | } |
| | } |
| | } else { |
| | printf("no such function: start\n"); |
| | } |
| | Py_XDECREF(mod); |
| | } else { |
| | printf("no such module: %s\n", script.latin1()); |
| | } |
| | Py_XDECREF(modname); |
| | Py_Finalize(); |
| | return 0; |
| | } |
| |
|
| |
|
| | |
| | |
| | |
| | long inc(long i) { |
| | printf("c: inc called\n"); |
| | printf("c: parameter from python: %ld\n", i); |
| | return ++i; |
| | } |
| |
|
| | |
| | |
| | |
| | static PyObject *py_inc(PyObject* , PyObject* args) { |
| | long i; |
| | printf("c: py_inc called\n"); |
| | if (!PyArg_ParseTuple(args, "l", &i)) |
| | return NULL; |
| | return Py_BuildValue("l", inc(i)); |
| | } |
| |
|
| | |
| | |
| | |
| | void rsPyAddLine(double x1, double y1, double x2, double y2) { |
| | |
| | |
| |
|
| | RS_Graphic* graphic = RS_SIMPLEPYTHON->getGraphic(); |
| | if (graphic) { |
| | graphic->addEntity(new RS_Line(graphic, |
| | RS_LineData(RS_Vector(x1, y1), |
| | RS_Vector(x2, y2)))); |
| | } else { |
| | std::cerr << "rsPyAddLine: No graphic object set.\n"; |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | static PyObject *py_rsPyAddLine(PyObject* , PyObject* args) { |
| | double x1, y1, x2, y2; |
| | |
| | if (!PyArg_ParseTuple(args, "dddd", &x1, &y1, &x2, &y2)) { |
| | return NULL; |
| | } |
| | rsPyAddLine(x1, y1, x2, y2); |
| | return Py_BuildValue("d", 1); |
| | } |
| |
|
| | |
| | |
| | |
| | static PyMethodDef rsLibreCADMethods[] = |
| | { |
| | {"inc", py_inc, 1, |
| | "a silly example method"}, |
| | {"rsPyAddLine", py_rsPyAddLine, 1, |
| | "adds a line to the current document"}, |
| | {NULL, NULL} |
| | }; |
| |
|
| | |
| | |
| | |
| | void init_pyextension() { |
| | printf("c: adding module: librecad\n"); |
| | PyImport_AddModule("librecad"); |
| | Py_InitModule("librecad", rsLibreCADMethods); |
| | printf("c: module librecad: OK\n"); |
| | } |
| |
|
| | #endif |
| |
|