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
10 // JLH = James L. Hammons <jlhamm@acm.org>
13 // --- ---------- -----------------------------------------------------------
14 // JLH 06/02/2010 Added this text. :-)
17 #ifdef RS_OPT_SIMPLEPYTHON
18 #include "rs_simplepython.h"
21 RS_SimplePython* RS_SimplePython::uniqueInstance = NULL;
25 * Gets the one and only RS_SimplePython instance
26 * (creates a new one on first call only)
28 * @return Pointer to the single instance of this
31 RS_SimplePython* RS_SimplePython::instance() {
32 if(uniqueInstance==NULL) {
33 uniqueInstance = new RS_SimplePython;
35 return uniqueInstance;
40 * Launches the given script.
42 int RS_SimplePython::launch(const QString& script) {
44 PyObject *modname, *mod, *mdict, *func, *rslt;
45 //Py_SetProgramName(argv[0]);
48 modname = PyString_FromString(script);
49 mod = PyImport_Import(modname);
52 mdict = PyModule_GetDict(mod);
54 // Borrowed reference to start function
55 func = PyDict_GetItemString(mdict, "start");
58 if (PyCallable_Check(func)) {
59 //printf("calling..\n");
60 rslt = PyObject_CallFunction(func, "(s)", "noparam");
61 //printf("calling ok\n");
63 //printf("c: rslt\n");
64 answer = PyInt_AsLong(rslt);
65 //printf("c: answer is: %ld\n", answer);
70 printf("no such function: start\n");
74 printf("no such module: %s\n", script.latin1());
83 * A test method exposed to Python
86 printf("c: inc called\n");
87 printf("c: parameter from python: %ld\n", i);
92 * The magic that exposes inc(). A wrapper function.
94 static PyObject *py_inc(PyObject* /*self*/, PyObject* args) {
96 printf("c: py_inc called\n");
97 if (!PyArg_ParseTuple(args, "l", &i))
99 return Py_BuildValue("l", inc(i));
103 * Adds a line to the current graphic document.
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);
109 Drawing* graphic = RS_SIMPLEPYTHON->getGraphic();
111 graphic->addEntity(new RS_Line(graphic,
112 RS_LineData(RS_Vector(x1, y1),
113 RS_Vector(x2, y2))));
115 std::cerr << "rsPyAddLine: No graphic object set.\n";
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)) {
128 rsPyAddLine(x1, y1, x2, y2);
129 return Py_BuildValue("d", 1);
133 * The qcadlib module's function table.
135 static PyMethodDef rsQCadMethods[] =
138 "a silly example method"},
139 {"rsPyAddLine", py_rsPyAddLine, 1,
140 "adds a line to the current document"},
141 {NULL, NULL} /* sentinel */
145 * Python will call this when the qcadlib module is imported.
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");