1 /****************************************************************************
2 ** $Id: rs_simplepython.cpp 1775 2003-11-02 12:20:46Z andrew $
4 ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
6 ** This file is part of the qcadlib Library project.
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.
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.
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.
20 ** See http://www.ribbonsoft.com for further details.
22 ** Contact info@ribbonsoft.com if any conditions of this licensing are
25 **********************************************************************/
28 #ifdef RS_OPT_SIMPLEPYTHON
29 #include "rs_simplepython.h"
32 RS_SimplePython* RS_SimplePython::uniqueInstance = NULL;
36 * Gets the one and only RS_SimplePython instance
37 * (creates a new one on first call only)
39 * @return Pointer to the single instance of this
42 RS_SimplePython* RS_SimplePython::instance() {
43 if(uniqueInstance==NULL) {
44 uniqueInstance = new RS_SimplePython;
46 return uniqueInstance;
51 * Launches the given script.
53 int RS_SimplePython::launch(const RS_String& script) {
55 PyObject *modname, *mod, *mdict, *func, *rslt;
56 //Py_SetProgramName(argv[0]);
59 modname = PyString_FromString(script);
60 mod = PyImport_Import(modname);
63 mdict = PyModule_GetDict(mod);
65 // Borrowed reference to start function
66 func = PyDict_GetItemString(mdict, "start");
69 if (PyCallable_Check(func)) {
70 //printf("calling..\n");
71 rslt = PyObject_CallFunction(func, "(s)", "noparam");
72 //printf("calling ok\n");
74 //printf("c: rslt\n");
75 answer = PyInt_AsLong(rslt);
76 //printf("c: answer is: %ld\n", answer);
81 printf("no such function: start\n");
85 printf("no such module: %s\n", script.latin1());
94 * A test method exposed to Python
97 printf("c: inc called\n");
98 printf("c: parameter from python: %ld\n", i);
103 * The magic that exposes inc(). A wrapper function.
105 static PyObject *py_inc(PyObject* /*self*/, PyObject* args) {
107 printf("c: py_inc called\n");
108 if (!PyArg_ParseTuple(args, "l", &i))
110 return Py_BuildValue("l", inc(i));
114 * Adds a line to the current graphic document.
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);
120 RS_Graphic* graphic = RS_SIMPLEPYTHON->getGraphic();
122 graphic->addEntity(new RS_Line(graphic,
123 RS_LineData(RS_Vector(x1, y1),
124 RS_Vector(x2, y2))));
126 std::cerr << "rsPyAddLine: No graphic object set.\n";
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)) {
139 rsPyAddLine(x1, y1, x2, y2);
140 return Py_BuildValue("d", 1);
144 * The qcadlib module's function table.
146 static PyMethodDef rsQCadMethods[] =
149 "a silly example method"},
150 {"rsPyAddLine", py_rsPyAddLine, 1,
151 "adds a line to the current document"},
152 {NULL, NULL} /* sentinel */
156 * Python will call this when the qcadlib module is imported.
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");