3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // (C) 2010 Underground Software
8 // JLH = James L. Hammons <jlhamm@acm.org>
11 // --- ---------- -----------------------------------------------------------
12 // JLH 06/02/2010 Added this text. :-)
15 #ifdef RS_OPT_SIMPLEPYTHON
16 #include "rs_simplepython.h"
19 RS_SimplePython* RS_SimplePython::uniqueInstance = NULL;
23 * Gets the one and only RS_SimplePython instance
24 * (creates a new one on first call only)
26 * @return Pointer to the single instance of this
29 RS_SimplePython* RS_SimplePython::instance() {
30 if(uniqueInstance==NULL) {
31 uniqueInstance = new RS_SimplePython;
33 return uniqueInstance;
38 * Launches the given script.
40 int RS_SimplePython::launch(const QString& script) {
42 PyObject *modname, *mod, *mdict, *func, *rslt;
43 //Py_SetProgramName(argv[0]);
46 modname = PyString_FromString(script);
47 mod = PyImport_Import(modname);
50 mdict = PyModule_GetDict(mod);
52 // Borrowed reference to start function
53 func = PyDict_GetItemString(mdict, "start");
56 if (PyCallable_Check(func)) {
57 //printf("calling..\n");
58 rslt = PyObject_CallFunction(func, "(s)", "noparam");
59 //printf("calling ok\n");
61 //printf("c: rslt\n");
62 answer = PyInt_AsLong(rslt);
63 //printf("c: answer is: %ld\n", answer);
68 printf("no such function: start\n");
72 printf("no such module: %s\n", script.latin1());
81 * A test method exposed to Python
84 printf("c: inc called\n");
85 printf("c: parameter from python: %ld\n", i);
90 * The magic that exposes inc(). A wrapper function.
92 static PyObject *py_inc(PyObject* /*self*/, PyObject* args) {
94 printf("c: py_inc called\n");
95 if (!PyArg_ParseTuple(args, "l", &i))
97 return Py_BuildValue("l", inc(i));
101 * Adds a line to the current graphic document.
103 void rsPyAddLine(double x1, double y1, double x2, double y2) {
104 //printf("c: addLine called\n");
105 //printf("c: parameter from python: %f\n", x1);
107 Drawing* graphic = RS_SIMPLEPYTHON->getGraphic();
109 graphic->addEntity(new RS_Line(graphic,
110 RS_LineData(RS_Vector(x1, y1),
111 RS_Vector(x2, y2))));
113 std::cerr << "rsPyAddLine: No graphic object set.\n";
120 static PyObject *py_rsPyAddLine(PyObject* /*self*/, PyObject* args) {
121 double x1, y1, x2, y2;
122 //printf("c: py_rsPyAddLine called\n");
123 if (!PyArg_ParseTuple(args, "dddd", &x1, &y1, &x2, &y2)) {
126 rsPyAddLine(x1, y1, x2, y2);
127 return Py_BuildValue("d", 1);
131 * The qcadlib module's function table.
133 static PyMethodDef rsQCadMethods[] =
136 "a silly example method"},
137 {"rsPyAddLine", py_rsPyAddLine, 1,
138 "adds a line to the current document"},
139 {NULL, NULL} /* sentinel */
143 * Python will call this when the qcadlib module is imported.
145 void init_pyextension() {
146 printf("c: adding module: qcad\n");
147 PyImport_AddModule("qcad");
148 Py_InitModule("qcad", rsQCadMethods);
149 printf("c: module qcad: OK\n");