1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2013 NBEE Embedded Systems SL, Miguel Angel Ajo <miguelangel@ajo.es>
* Copyright (C) 2013 KiCad Developers, see CHANGELOG.TXT for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* @file pcbnew_footprint_wizards.cpp
* @brief Class PCBNEW_PYTHON_FOOTPRINT_WIZARDS
*/
#include "pcbnew_footprint_wizards.h"
#include <python_scripting.h>
#include <stdio.h>
#include <macros.h>
PYTHON_FOOTPRINT_WIZARD::PYTHON_FOOTPRINT_WIZARD( PyObject* aWizard )
{
PyLOCK lock;
this->m_PyWizard = aWizard;
Py_XINCREF( aWizard );
}
PYTHON_FOOTPRINT_WIZARD::~PYTHON_FOOTPRINT_WIZARD()
{
PyLOCK lock;
Py_XDECREF( this->m_PyWizard );
}
PyObject* PYTHON_FOOTPRINT_WIZARD::CallMethod( const char* aMethod, PyObject* aArglist )
{
PyLOCK lock;
PyErr_Clear();
// pFunc is a new reference to the desired method
PyObject* pFunc = PyObject_GetAttrString( this->m_PyWizard, aMethod );
if( pFunc && PyCallable_Check( pFunc ) )
{
PyObject* result = PyObject_CallObject( pFunc, aArglist );
if( PyErr_Occurred() )
{
#if 1 // defined(DEBUG)
wxMessageBox( PyErrStringWithTraceback(),
wxT( "Exception on python footprint wizard code" ),
wxICON_ERROR | wxOK );
#endif
}
if( result )
{
Py_XDECREF( pFunc );
return result;
}
}
else
{
printf( "method not found, or not callable: %s\n", aMethod );
}
if( pFunc )
{
Py_XDECREF( pFunc );
}
return NULL;
}
wxString PYTHON_FOOTPRINT_WIZARD::CallRetStrMethod( const char* aMethod, PyObject* aArglist )
{
wxString ret;
PyLOCK lock;
PyObject* result = CallMethod( aMethod, aArglist );
if( result )
{
const char* str_res = PyString_AsString( result );
ret = FROM_UTF8( str_res );
Py_DECREF( result );
}
return ret;
}
wxArrayString PYTHON_FOOTPRINT_WIZARD::CallRetArrayStrMethod( const char* aMethod,
PyObject* aArglist )
{
wxArrayString ret;
wxString str_item;
PyLOCK lock;
PyObject* result = CallMethod( aMethod, aArglist );
if( result )
{
if( !PyList_Check( result ) )
{
Py_DECREF( result );
ret.Add( wxT(
"PYTHON_FOOTPRINT_WIZARD::CallRetArrayStrMethod, result is not a list" ),
1 );
return ret;
}
ret = PyArrayStringToWx( result );
Py_DECREF( result );
}
return ret;
}
wxString PYTHON_FOOTPRINT_WIZARD::GetName()
{
PyLOCK lock;
return CallRetStrMethod( "GetName" );
}
wxString PYTHON_FOOTPRINT_WIZARD::GetImage()
{
PyLOCK lock;
return CallRetStrMethod( "GetImage" );
}
wxString PYTHON_FOOTPRINT_WIZARD::GetDescription()
{
PyLOCK lock;
return CallRetStrMethod( "GetDescription" );
}
int PYTHON_FOOTPRINT_WIZARD::GetNumParameterPages()
{
int ret = 0;
PyLOCK lock;
// Time to call the callback
PyObject* result = CallMethod( "GetNumParameterPages", NULL );
if( result )
{
if( !PyInt_Check( result ) )
return -1;
ret = PyInt_AsLong( result );
Py_DECREF( result );
}
return ret;
}
wxString PYTHON_FOOTPRINT_WIZARD::GetParameterPageName( int aPage )
{
wxString ret;
PyLOCK lock;
// Time to call the callback
PyObject* arglist = Py_BuildValue( "(i)", aPage );
PyObject* result = CallMethod( "GetParameterPageName", arglist );
Py_DECREF( arglist );
if( result )
{
const char* str_res = PyString_AsString( result );
ret = FROM_UTF8( str_res );
Py_DECREF( result );
}
return ret;
}
wxArrayString PYTHON_FOOTPRINT_WIZARD::GetParameterNames( int aPage )
{
wxArrayString ret;
PyLOCK lock;
PyObject* arglist = Py_BuildValue( "(i)", aPage );
ret = CallRetArrayStrMethod( "GetParameterNames", arglist );
Py_DECREF( arglist );
for( unsigned i = 0; i < ret.GetCount(); i++ )
{
wxString rest;
wxString item = ret[i];
if( item.StartsWith( wxT( "*" ), &rest ) )
{
ret[i] = rest;
}
}
return ret;
}
wxArrayString PYTHON_FOOTPRINT_WIZARD::GetParameterTypes( int aPage )
{
wxArrayString ret;
PyLOCK lock;
PyObject* arglist = Py_BuildValue( "(i)", aPage );
ret = CallRetArrayStrMethod( "GetParameterNames", arglist );
Py_DECREF( arglist );
for( unsigned i = 0; i<ret.GetCount(); i++ )
{
wxString rest;
wxString item = ret[i];
if( item.StartsWith( wxT( "*" ), &rest ) )
{
ret[i] = wxT( "UNITS" ); // units
}
else
{
ret[i] = wxT( "IU" ); // internal units
}
}
return ret;
}
wxArrayString PYTHON_FOOTPRINT_WIZARD::GetParameterValues( int aPage )
{
PyLOCK lock;
PyObject* arglist = Py_BuildValue( "(i)", aPage );
wxArrayString ret = CallRetArrayStrMethod( "GetParameterValues", arglist );
Py_DECREF( arglist );
return ret;
}
wxArrayString PYTHON_FOOTPRINT_WIZARD::GetParameterErrors( int aPage )
{
PyLOCK lock;
PyObject* arglist = Py_BuildValue( "(i)", aPage );
wxArrayString ret = CallRetArrayStrMethod( "GetParameterErrors", arglist );
Py_DECREF( arglist );
return ret;
}
wxString PYTHON_FOOTPRINT_WIZARD::SetParameterValues( int aPage, wxArrayString& aValues )
{
int len = aValues.size();
PyLOCK lock;
PyObject* py_list = PyList_New( len );
for( int i = 0; i < len; i++ )
{
wxString& str = aValues[i];
PyObject* py_str = PyString_FromString( (const char*) str.mb_str() );
PyList_SetItem( py_list, i, py_str );
}
PyObject* arglist;
arglist = Py_BuildValue( "(i,O)", aPage, py_list );
wxString res = CallRetStrMethod( "SetParameterValues", arglist );
Py_DECREF( arglist );
return res;
}
// this is a SWIG function declaration -from module.i
MODULE* PyModule_to_MODULE( PyObject* obj0 );
MODULE* PYTHON_FOOTPRINT_WIZARD::GetFootprint( wxString * aMessages )
{
PyLOCK lock;
PyObject* result = CallMethod( "GetFootprint", NULL );
if( aMessages )
*aMessages = CallRetStrMethod( "GetBuildMessages", NULL );
if( !result )
return NULL;
PyObject* obj = PyObject_GetAttrString( result, "this" );
if( PyErr_Occurred() )
{
PyErr_Print();
PyErr_Clear();
}
MODULE* mod = PyModule_to_MODULE( obj );
return mod;
}
void* PYTHON_FOOTPRINT_WIZARD::GetObject()
{
return (void*) m_PyWizard;
}
void PYTHON_FOOTPRINT_WIZARDS::register_wizard( PyObject* aPyWizard )
{
PYTHON_FOOTPRINT_WIZARD* fw = new PYTHON_FOOTPRINT_WIZARD( aPyWizard );
fw->register_wizard();
}
void PYTHON_FOOTPRINT_WIZARDS::deregister_wizard( PyObject* aPyWizard )
{
// deregister also destroyes the previously created "PYTHON_FOOTPRINT_WIZARD object"
FOOTPRINT_WIZARDS::deregister_object( (void*) aPyWizard );
}
|