1
/***************************************************************************
2
* (c) Jļæ½rgen Riegel (juergen.riegel@web.de) 2002 *
4
* This file is part of the FreeCAD CAx development system. *
6
* This program is free software; you can redistribute it and/or modify *
7
* it under the terms of the GNU Library General Public License (LGPL) *
8
* as published by the Free Software Foundation; either version 2 of *
9
* the License, or (at your option) any later version. *
10
* for detail see the LICENCE text file. *
12
* FreeCAD is distributed in the hope that it will be useful, *
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15
* GNU Library General Public License for more details. *
17
* You should have received a copy of the GNU Library General Public *
18
* License along with FreeCAD; if not, write to the Free Software *
19
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
22
* Juergen Riegel 2002 *
23
***************************************************************************/
26
#include "PreCompiled.h"
31
# include <sys/types.h>
32
# include <sys/stat.h>
35
# include <xercesc/sax/SAXParseException.hpp>
46
#include "Parameter.h"
47
#include "Exception.h"
49
#include "PyObjectBase.h"
53
//**************************************************************************
54
//**************************************************************************
56
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
60
/** The ParameterGrp wrapper class
62
class ParameterGrpPy :public Base::PyObjectBase
64
/** always start with Py_Header */
74
//---------------------------------------------------------------------
75
// construction / destruction +++++++++++++++++++++++++++++++++++++++++
76
//---------------------------------------------------------------------
79
ParameterGrpPy(const Base::Reference<ParameterGrp> &rcParamGrp, PyTypeObject *T = &Type);
80
/// for Construction in python
81
static PyObject *PyMake(PyObject *, PyObject *);
83
//---------------------------------------------------------------------
84
// python exports ++++++++++++++++++++++++++++++++++++++++++++++++++++
85
//---------------------------------------------------------------------
87
PyObject *_getattr(char *attr); // __getattr__ function
89
int _setattr(char *attr, PyObject *value); // __setattr__ function
91
PYFUNCDEF_D (ParameterGrpPy,PyGetGrp);
92
PYFUNCDEF_D (ParameterGrpPy,PyRemGrp);
93
PYFUNCDEF_D (ParameterGrpPy,PyHasGroup);
94
PYFUNCDEF_D (ParameterGrpPy,PyIsEmpty);
95
PYFUNCDEF_D (ParameterGrpPy,PyClear);
96
PYFUNCDEF_D (ParameterGrpPy,PyNotify);
97
PYFUNCDEF_D (ParameterGrpPy,PyNotifyAll);
99
PYFUNCDEF_D (ParameterGrpPy,PySetBool);
100
PYFUNCDEF_D (ParameterGrpPy,PyGetBool);
101
PYFUNCDEF_D (ParameterGrpPy,PyRemBool);
103
PYFUNCDEF_D (ParameterGrpPy,PySetInt);
104
PYFUNCDEF_D (ParameterGrpPy,PyGetInt);
105
PYFUNCDEF_D (ParameterGrpPy,PyRemInt);
107
PYFUNCDEF_D (ParameterGrpPy,PySetUnsigned);
108
PYFUNCDEF_D (ParameterGrpPy,PyGetUnsigned);
109
PYFUNCDEF_D (ParameterGrpPy,PyRemUnsigned);
111
PYFUNCDEF_D (ParameterGrpPy,PySetFloat);
112
PYFUNCDEF_D (ParameterGrpPy,PyGetFloat);
113
PYFUNCDEF_D (ParameterGrpPy,PyRemFloat);
115
PYFUNCDEF_D (ParameterGrpPy,PySetString);
116
PYFUNCDEF_D (ParameterGrpPy,PyGetString);
117
PYFUNCDEF_D (ParameterGrpPy,PyRemString);
119
PYFUNCDEF_D (ParameterGrpPy,importFrom);
120
PYFUNCDEF_D (ParameterGrpPy,insert);
121
PYFUNCDEF_D (ParameterGrpPy,exportTo);
124
/// Pointer to the FCDocument where the label comes from
125
Base::Reference<ParameterGrp> _cParamGrp;
129
//--------------------------------------------------------------------------
131
//--------------------------------------------------------------------------
133
PyTypeObject ParameterGrpPy::Type = {
134
PyObject_HEAD_INIT(&PyType_Type)
136
"ParameterGrp", /*tp_name*/
137
sizeof(ParameterGrpPy), /*tp_basicsize*/
140
PyDestructor, /*tp_dealloc*/
142
__getattr, /*tp_getattr*/
143
__setattr, /*tp_setattr*/
147
0, /*tp_as_sequence*/
154
/* --- Functions to access object as input/output buffer ---------*/
155
0, /* tp_as_buffer */
156
/* --- Flags to define presence of optional/expanded features */
158
"Python interface class to set parameters", /*tp_doc */
161
0, /*tp_richcompare */
162
0, /*tp_weaklistoffset */
172
0, /*tp_dictoffset */
176
0, /*tp_free Low-level free-memory routine */
177
0, /*tp_is_gc For PyObject_IS_GC */
179
0, /*tp_mro method resolution order */
181
0, /*tp_subclasses */
186
//--------------------------------------------------------------------------
188
//--------------------------------------------------------------------------
189
PyMethodDef ParameterGrpPy::Methods[] = {
190
{"GetGroup", (PyCFunction) sPyGetGrp, Py_NEWARGS, 0},
191
{"RemGroup", (PyCFunction) sPyRemGrp, Py_NEWARGS, 0},
192
{"HasGroup", (PyCFunction) sPyHasGroup, Py_NEWARGS, 0},
193
{"IsEmpty", (PyCFunction) sPyIsEmpty, Py_NEWARGS, 0},
194
{"Clear", (PyCFunction) sPyClear, Py_NEWARGS, 0},
195
{"Notify", (PyCFunction) sPyNotify, Py_NEWARGS, 0},
196
{"NotifyAll", (PyCFunction) sPyNotifyAll, Py_NEWARGS, 0},
198
{"SetBool", (PyCFunction) sPySetBool, Py_NEWARGS, 0},
199
{"GetBool", (PyCFunction) sPyGetBool, Py_NEWARGS, 0},
200
{"RemBool", (PyCFunction) sPyRemBool, Py_NEWARGS, 0},
202
{"SetInt", (PyCFunction) sPySetInt, Py_NEWARGS, 0},
203
{"GetInt", (PyCFunction) sPyGetInt, Py_NEWARGS, 0},
204
{"RemInt", (PyCFunction) sPyRemInt, Py_NEWARGS, 0},
206
{"SetUnsigned", (PyCFunction) sPySetUnsigned, Py_NEWARGS, 0},
207
{"GetUnsigned", (PyCFunction) sPyGetUnsigned, Py_NEWARGS, 0},
208
{"RemUnsigned", (PyCFunction) sPyRemUnsigned, Py_NEWARGS, 0},
210
{"SetFloat", (PyCFunction) sPySetFloat, Py_NEWARGS, 0},
211
{"GetFloat", (PyCFunction) sPyGetFloat, Py_NEWARGS, 0},
212
{"RemFloat", (PyCFunction) sPyRemFloat, Py_NEWARGS, 0},
214
{"SetString", (PyCFunction) sPySetString, Py_NEWARGS, 0},
215
{"GetString", (PyCFunction) sPyGetString, Py_NEWARGS, 0},
216
{"RemString", (PyCFunction) sPyRemString, Py_NEWARGS, 0},
218
{"Import", (PyCFunction) simportFrom, Py_NEWARGS, 0},
219
{"Insert", (PyCFunction) sinsert, Py_NEWARGS, 0},
220
{"Export", (PyCFunction) sexportTo, Py_NEWARGS, 0},
222
{NULL, NULL, 0, NULL} /* Sentinel */
225
//--------------------------------------------------------------------------
227
//--------------------------------------------------------------------------
228
PyParentObject ParameterGrpPy::Parents[] = {&PyObjectBase::Type,&ParameterGrpPy::Type, NULL};
230
//--------------------------------------------------------------------------
232
//--------------------------------------------------------------------------
233
ParameterGrpPy::ParameterGrpPy(const Base::Reference<ParameterGrp> &rcParamGrp, PyTypeObject *T )
234
: PyObjectBase(0, T),_cParamGrp(rcParamGrp)
236
//Console().Log("Create Param Group %p\n",this);
239
PyObject *ParameterGrpPy::PyMake(PyObject* /*ignored*/, PyObject* /*args*/) // Python wrapper
241
//return new ParameterGrpPy(); // Make new Python-able object
245
//--------------------------------------------------------------------------
246
// FCPyParametrGrp destructor
247
//--------------------------------------------------------------------------
248
ParameterGrpPy::~ParameterGrpPy() // Everything handled in parent
250
//Console().Log("Destroy ParameterGrp %p\n",this);
253
//--------------------------------------------------------------------------
254
// FCPyParametrGrp Attributes
255
//--------------------------------------------------------------------------
256
PyObject *ParameterGrpPy::_getattr(char *attr) // __getattr__ function: note only need to handle new state
258
_getattr_up(PyObjectBase); // send to parent
261
int ParameterGrpPy::_setattr(char *attr, PyObject *value) // __setattr__ function: note only need to handle new state
263
return PyObjectBase::_setattr(attr, value); // send up to parent
267
//--------------------------------------------------------------------------
269
//--------------------------------------------------------------------------
271
PyObject *ParameterGrpPy::importFrom(PyObject *args)
274
if (!PyArg_ParseTuple(args, "s", &pstr)) // convert args: Python->C
275
return NULL; // NULL triggers exception
277
_cParamGrp->importFrom(pstr);
282
PyObject *ParameterGrpPy::insert(PyObject *args)
285
if (!PyArg_ParseTuple(args, "s", &pstr)) // convert args: Python->C
286
return NULL; // NULL triggers exception
288
_cParamGrp->insert(pstr);
293
PyObject *ParameterGrpPy::exportTo(PyObject *args)
296
if (!PyArg_ParseTuple(args, "s", &pstr)) // convert args: Python->C
297
return NULL; // NULL triggers exception
299
_cParamGrp->exportTo(pstr);
304
PyObject *ParameterGrpPy::PyGetGrp(PyObject *args)
307
if (!PyArg_ParseTuple(args, "s", &pstr)) // convert args: Python->C
308
return NULL; // NULL triggers exception
310
// get the Handle of the wanted group
311
Base::Reference<ParameterGrp> handle = _cParamGrp->GetGroup(pstr);
312
if(handle.isValid()){
313
// crate a python wrapper class
314
ParameterGrpPy *pcParamGrp = new ParameterGrpPy(handle);
315
// increment the reff count
316
//pcParamGrp->_INCREF();
319
PyErr_SetString(PyExc_IOError, "GetGroup failed");
325
PyObject *ParameterGrpPy::PySetBool(PyObject *args)
329
if (!PyArg_ParseTuple(args, "si", &pstr,&Bool)) // convert args: Python->C
330
return NULL; // NULL triggers exception
332
_cParamGrp->SetBool(pstr,Bool!=0);
337
PyObject *ParameterGrpPy::PyGetBool(PyObject *args)
341
if (!PyArg_ParseTuple(args, "s|i", &pstr,&Bool)) // convert args: Python->C
342
return NULL; // NULL triggers exception
344
return Py_BuildValue("i",_cParamGrp->GetBool(pstr,Bool!=0));
348
PyObject *ParameterGrpPy::PySetInt(PyObject *args)
352
if (!PyArg_ParseTuple(args, "si", &pstr,&Int)) // convert args: Python->C
353
return NULL; // NULL triggers exception
355
_cParamGrp->SetInt(pstr,Int);
360
PyObject *ParameterGrpPy::PyGetInt(PyObject *args)
364
if (!PyArg_ParseTuple(args, "s|i", &pstr,&Int)) // convert args: Python->C
365
return NULL; // NULL triggers exception
367
return Py_BuildValue("i",_cParamGrp->GetInt(pstr,Int));
371
PyObject *ParameterGrpPy::PySetUnsigned(PyObject *args)
375
if (!PyArg_ParseTuple(args, "sI", &pstr,&UInt)) // convert args: Python->C
376
return NULL; // NULL triggers exception
378
_cParamGrp->SetUnsigned(pstr,UInt);
383
PyObject *ParameterGrpPy::PyGetUnsigned(PyObject *args)
386
unsigned long UInt=0;
387
if (!PyArg_ParseTuple(args, "s|I", &pstr,&UInt)) // convert args: Python->C
388
return NULL; // NULL triggers exception
390
return Py_BuildValue("I",_cParamGrp->GetUnsigned(pstr,UInt));
394
PyObject *ParameterGrpPy::PySetFloat(PyObject *args)
398
if (!PyArg_ParseTuple(args, "sd", &pstr,&Float)) // convert args: Python->C
399
return NULL; // NULL triggers exception
401
_cParamGrp->SetFloat(pstr,Float);
406
PyObject *ParameterGrpPy::PyGetFloat(PyObject *args)
410
if (!PyArg_ParseTuple(args, "s|d", &pstr,&Float)) // convert args: Python->C
411
return NULL; // NULL triggers exception
413
return Py_BuildValue("d",_cParamGrp->GetFloat(pstr,Float));
417
PyObject *ParameterGrpPy::PySetString(PyObject *args)
421
if (!PyArg_ParseTuple(args, "ss", &pstr,&str)) // convert args: Python->C
422
return NULL; // NULL triggers exception
424
_cParamGrp->SetASCII(pstr,str);
429
PyObject *ParameterGrpPy::PyGetString(PyObject *args)
433
if (!PyArg_ParseTuple(args, "s|s", &pstr,&str)) // convert args: Python->C
434
return NULL; // NULL triggers exception
436
return Py_BuildValue("s",_cParamGrp->GetASCII(pstr,str).c_str());
442
PyObject *ParameterGrpPy::PyRemInt(PyObject *args)
445
if (!PyArg_ParseTuple(args, "s", &pstr)) // convert args: Python->C
446
return NULL; // NULL triggers exception
448
_cParamGrp->RemoveInt(pstr);
453
PyObject *ParameterGrpPy::PyRemUnsigned(PyObject *args)
456
if (!PyArg_ParseTuple(args, "s", &pstr)) // convert args: Python->C
457
return NULL; // NULL triggers exception
459
_cParamGrp->RemoveUnsigned(pstr);
464
PyObject *ParameterGrpPy::PyRemBool(PyObject *args)
467
if (!PyArg_ParseTuple(args, "s", &pstr)) // convert args: Python->C
468
return NULL; // NULL triggers exception
470
_cParamGrp->RemoveBool(pstr);
475
PyObject *ParameterGrpPy::PyRemGrp(PyObject *args)
478
if (!PyArg_ParseTuple(args, "s", &pstr)) // convert args: Python->C
479
return NULL; // NULL triggers exception
481
_cParamGrp->RemoveGrp(pstr);
486
PyObject *ParameterGrpPy::PyRemFloat(PyObject *args)
489
if (!PyArg_ParseTuple(args, "s", &pstr)) // convert args: Python->C
490
return NULL; // NULL triggers exception
492
_cParamGrp->RemoveFloat(pstr);
497
PyObject *ParameterGrpPy::PyRemString(PyObject *args)
500
if (!PyArg_ParseTuple(args, "s", &pstr)) // convert args: Python->C
501
return NULL; // NULL triggers exception
503
_cParamGrp->RemoveASCII(pstr);
508
PyObject *ParameterGrpPy::PyClear(PyObject *args)
510
if (!PyArg_ParseTuple(args, "")) // convert args: Python->C
511
return NULL; // NULL triggers exception
518
PyObject *ParameterGrpPy::PyIsEmpty(PyObject *args)
520
if (!PyArg_ParseTuple(args, "")) // convert args: Python->C
521
return NULL; // NULL triggers exception
523
return Py_BuildValue("i",_cParamGrp->IsEmpty());
527
PyObject *ParameterGrpPy::PyHasGroup(PyObject *args)
530
if (!PyArg_ParseTuple(args, "s", &pstr)) // convert args: Python->C
531
return NULL; // NULL triggers exception
533
return Py_BuildValue("i",_cParamGrp->HasGroup(pstr));
537
PyObject *ParameterGrpPy::PyNotify(PyObject *args)
540
if (!PyArg_ParseTuple(args, "s", &pstr)) // convert args: Python->C
541
return NULL; // NULL triggers exception
543
_cParamGrp->Notify(pstr);
547
PyObject *ParameterGrpPy::PyNotifyAll(PyObject *args)
549
if (!PyArg_ParseTuple(args, "")) // convert args: Python->C
550
return NULL; // NULL triggers exception
553
_cParamGrp->NotifyAll();
558
/** python wrapper function
560
PyObject* GetPyObject( const Base::Reference<ParameterGrp> &hcParamGrp)
562
return new ParameterGrpPy(hcParamGrp);