]> Shamusworld >> Repos - architektonas/blob - src/base/rs_simplepython.cpp
99ad24b4c09c25f2bc29d1551b125a3c77847bf3
[architektonas] / src / base / rs_simplepython.cpp
1 // rs_simplepython.cpp
2 //
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
7 //
8 // JLH = James L. Hammons <jlhamm@acm.org>
9 //
10 // Who  When        What
11 // ---  ----------  -----------------------------------------------------------
12 // JLH  06/02/2010  Added this text. :-)
13 //
14
15 #ifdef RS_OPT_SIMPLEPYTHON
16 #include "rs_simplepython.h"
17
18
19 RS_SimplePython* RS_SimplePython::uniqueInstance = NULL;
20
21
22 /**
23  *  Gets the one and only RS_SimplePython instance
24  *  (creates a new one on first call only)
25  *
26  *  @return Pointer to the single instance of this
27  * singleton class
28  */
29 RS_SimplePython* RS_SimplePython::instance() {
30     if(uniqueInstance==NULL) {
31         uniqueInstance = new RS_SimplePython;
32     }
33     return uniqueInstance;
34 }
35
36
37 /**
38  * Launches the given script.
39  */
40 int RS_SimplePython::launch(const QString& script) {
41     long answer;
42     PyObject *modname, *mod, *mdict, *func, *rslt;
43     //Py_SetProgramName(argv[0]);
44     Py_Initialize();
45     init_pyextension();
46     modname = PyString_FromString(script);
47     mod = PyImport_Import(modname);
48     if (mod) {
49         //printf( "mod\n");
50         mdict = PyModule_GetDict(mod);
51
52         // Borrowed reference to start function
53         func = PyDict_GetItemString(mdict, "start");
54         if (func) {
55             //printf( "func\n");
56             if (PyCallable_Check(func)) {
57                 //printf("calling..\n");
58                 rslt = PyObject_CallFunction(func, "(s)", "noparam");
59                 //printf("calling ok\n");
60                 if (rslt) {
61                     //printf("c: rslt\n");
62                     answer = PyInt_AsLong(rslt);
63                     //printf("c: answer is: %ld\n", answer);
64                     Py_XDECREF(rslt);
65                 }
66             }
67         } else {
68             printf("no such function: start\n");
69         }
70         Py_XDECREF(mod);
71     } else {
72         printf("no such module: %s\n", script.latin1());
73     }
74     Py_XDECREF(modname);
75     Py_Finalize();
76     return 0;
77 }
78
79
80 /**
81  * A test method exposed to Python
82  */
83 long inc(long i) {
84     printf("c: inc called\n");
85     printf("c: parameter from python: %ld\n", i);
86     return ++i;
87 }
88
89 /**
90  * The magic that exposes inc(). A wrapper function.
91  */
92 static PyObject *py_inc(PyObject* /*self*/, PyObject* args) {
93     long i;
94     printf("c: py_inc called\n");
95     if (!PyArg_ParseTuple(args, "l", &i))
96         return NULL;
97     return Py_BuildValue("l", inc(i));
98 }
99
100 /**
101  * Adds a line to the current graphic document.
102  */
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);
106
107     Drawing* graphic = RS_SIMPLEPYTHON->getGraphic();
108     if (graphic!=NULL) {
109         graphic->addEntity(new RS_Line(graphic,
110                                        RS_LineData(RS_Vector(x1, y1),
111                                                    RS_Vector(x2, y2))));
112     } else {
113         std::cerr << "rsPyAddLine: No graphic object set.\n";
114     }
115 }
116
117 /**
118  * Python wrapper.
119  */
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)) {
124         return NULL;
125     }
126     rsPyAddLine(x1, y1, x2, y2);
127     return Py_BuildValue("d", 1);
128 }
129
130 /**
131  * The qcadlib module's function table.
132  */
133 static PyMethodDef rsQCadMethods[] =
134     {
135         {"inc",     py_inc,     1,
136          "a silly example method"},
137         {"rsPyAddLine", py_rsPyAddLine, 1,
138          "adds a line to the current document"},
139         {NULL,      NULL}       /* sentinel */
140     };
141
142 /**
143  * Python will call this when the qcadlib module is imported.
144  */
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");
150 }
151
152 #endif