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