]> Shamusworld >> Repos - architektonas/blob - src/base/rs_simplepython.cpp
Initial import
[architektonas] / src / base / rs_simplepython.cpp
1 /****************************************************************************
2 ** $Id: rs_simplepython.cpp 1775 2003-11-02 12:20:46Z andrew $
3 **
4 ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
5 **
6 ** This file is part of the qcadlib Library project.
7 **
8 ** This file may be distributed and/or modified under the terms of the
9 ** GNU General Public License version 2 as published by the Free Software
10 ** Foundation and appearing in the file LICENSE.GPL included in the
11 ** packaging of this file.
12 **
13 ** Licensees holding valid qcadlib Professional Edition licenses may use 
14 ** this file in accordance with the qcadlib Commercial License
15 ** Agreement provided with the Software.
16 **
17 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
18 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 **
20 ** See http://www.ribbonsoft.com for further details.
21 **
22 ** Contact info@ribbonsoft.com if any conditions of this licensing are
23 ** not clear to you.
24 **
25 **********************************************************************/
26
27
28 #ifdef RS_OPT_SIMPLEPYTHON
29 #include "rs_simplepython.h"
30
31
32 RS_SimplePython* RS_SimplePython::uniqueInstance = NULL;
33
34
35 /**
36  *  Gets the one and only RS_SimplePython instance
37  *  (creates a new one on first call only)
38  *
39  *  @return Pointer to the single instance of this
40  * singleton class
41  */
42 RS_SimplePython* RS_SimplePython::instance() {
43     if(uniqueInstance==NULL) {
44         uniqueInstance = new RS_SimplePython;
45     }
46     return uniqueInstance;
47 }
48
49
50 /**
51  * Launches the given script.
52  */
53 int RS_SimplePython::launch(const QString& script) {
54     long answer;
55     PyObject *modname, *mod, *mdict, *func, *rslt;
56     //Py_SetProgramName(argv[0]);
57     Py_Initialize();
58     init_pyextension();
59     modname = PyString_FromString(script);
60     mod = PyImport_Import(modname);
61     if (mod) {
62         //printf( "mod\n");
63         mdict = PyModule_GetDict(mod);
64
65         // Borrowed reference to start function
66         func = PyDict_GetItemString(mdict, "start");
67         if (func) {
68             //printf( "func\n");
69             if (PyCallable_Check(func)) {
70                 //printf("calling..\n");
71                 rslt = PyObject_CallFunction(func, "(s)", "noparam");
72                 //printf("calling ok\n");
73                 if (rslt) {
74                     //printf("c: rslt\n");
75                     answer = PyInt_AsLong(rslt);
76                     //printf("c: answer is: %ld\n", answer);
77                     Py_XDECREF(rslt);
78                 }
79             }
80         } else {
81             printf("no such function: start\n");
82         }
83         Py_XDECREF(mod);
84     } else {
85         printf("no such module: %s\n", script.latin1());
86     }
87     Py_XDECREF(modname);
88     Py_Finalize();
89     return 0;
90 }
91
92
93 /**
94  * A test method exposed to Python 
95  */
96 long inc(long i) {
97     printf("c: inc called\n");
98     printf("c: parameter from python: %ld\n", i);
99     return ++i;
100 }
101
102 /**
103  * The magic that exposes inc(). A wrapper function.
104  */
105 static PyObject *py_inc(PyObject* /*self*/, PyObject* args) {
106     long i;
107     printf("c: py_inc called\n");
108     if (!PyArg_ParseTuple(args, "l", &i))
109         return NULL;
110     return Py_BuildValue("l", inc(i));
111 }
112
113 /**
114  * Adds a line to the current graphic document.
115  */
116 void rsPyAddLine(double x1, double y1, double x2, double y2) {
117     //printf("c: addLine called\n");
118     //printf("c: parameter from python: %f\n", x1);
119
120     RS_Graphic* graphic = RS_SIMPLEPYTHON->getGraphic();
121     if (graphic!=NULL) {
122         graphic->addEntity(new RS_Line(graphic,
123                                        RS_LineData(RS_Vector(x1, y1),
124                                                    RS_Vector(x2, y2))));
125     } else {
126         std::cerr << "rsPyAddLine: No graphic object set.\n";
127     }
128 }
129
130 /**
131  * Python wrapper.
132  */
133 static PyObject *py_rsPyAddLine(PyObject* /*self*/, PyObject* args) {
134     double x1, y1, x2, y2;
135     //printf("c: py_rsPyAddLine called\n");
136     if (!PyArg_ParseTuple(args, "dddd", &x1, &y1, &x2, &y2)) {
137         return NULL;
138     }
139     rsPyAddLine(x1, y1, x2, y2);
140     return Py_BuildValue("d", 1);
141 }
142
143 /**
144  * The qcadlib module's function table.
145  */
146 static PyMethodDef rsQCadMethods[] =
147     {
148         {"inc",     py_inc,     1,
149          "a silly example method"},
150         {"rsPyAddLine", py_rsPyAddLine, 1,
151          "adds a line to the current document"},
152         {NULL,      NULL}       /* sentinel */
153     };
154
155 /**
156  * Python will call this when the qcadlib module is imported.
157  */
158 void init_pyextension() {
159     printf("c: adding module: qcad\n");
160     PyImport_AddModule("qcad");
161     Py_InitModule("qcad", rsQCadMethods);
162     printf("c: module qcad: OK\n");
163 }
164
165 #endif