KiCad PCB EDA Suite
pcbnew_footprint_wizards.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2013 NBEE Embedded Systems SL, Miguel Angel Ajo <miguelangel@ajo.es>
5  * Copyright (C) 2016 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program 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 General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
31 #include <cstdio>
32 #include <macros.h>
33 #include <python_scripting.h>
34 
35 
37 {
38  PyLOCK lock;
39 
40  this->m_PyWizard = aWizard;
41  Py_XINCREF( aWizard );
42 }
43 
44 
46 {
47  PyLOCK lock;
48 
49  Py_XDECREF( this->m_PyWizard );
50 }
51 
52 
53 PyObject* PYTHON_FOOTPRINT_WIZARD::CallMethod( const char* aMethod, PyObject* aArglist )
54 {
55  PyLOCK lock;
56 
57  PyErr_Clear();
58  // pFunc is a new reference to the desired method
59  PyObject* pFunc = PyObject_GetAttrString( this->m_PyWizard, aMethod );
60 
61  if( pFunc && PyCallable_Check( pFunc ) )
62  {
63  PyObject* result = PyObject_CallObject( pFunc, aArglist );
64 
65  if( PyErr_Occurred() )
66  {
67 #if 1 // defined(DEBUG)
68  wxMessageBox( PyErrStringWithTraceback(),
69  _( "Exception on python footprint wizard code" ),
70  wxICON_ERROR | wxOK );
71 #endif
72  }
73 
74  if( result )
75  {
76  Py_XDECREF( pFunc );
77  return result;
78  }
79  }
80  else
81  {
82  wxString msg = wxString::Format(_( "Method \"%s\" not found, or not callable" ), aMethod );
83  wxMessageBox( msg, _( "Unknown Method" ), wxICON_ERROR | wxOK );
84  }
85 
86  if( pFunc )
87  {
88  Py_XDECREF( pFunc );
89  }
90 
91  return NULL;
92 }
93 
94 
95 wxString PYTHON_FOOTPRINT_WIZARD::CallRetStrMethod( const char* aMethod, PyObject* aArglist )
96 {
97  wxString ret;
98  PyLOCK lock;
99 
100  PyObject* result = CallMethod( aMethod, aArglist );
101 
102  if( result == Py_None )
103  {
104  Py_DECREF( result );
105  return ret;
106  }
107 
108  ret = PyStringToWx( result );
109  Py_XDECREF( result );
110 
111  return ret;
112 }
113 
114 
115 wxArrayString PYTHON_FOOTPRINT_WIZARD::CallRetArrayStrMethod( const char* aMethod,
116  PyObject* aArglist )
117 {
118  wxArrayString ret;
119  wxString str_item;
120  PyLOCK lock;
121 
122  PyObject* result = CallMethod( aMethod, aArglist );
123 
124  if( result )
125  {
126  if( !PyList_Check( result ) )
127  {
128  Py_DECREF( result );
129  ret.Add( wxT(
130  "PYTHON_FOOTPRINT_WIZARD::CallRetArrayStrMethod, result is not a list" ),
131  1 );
132  return ret;
133  }
134 
135  ret = PyArrayStringToWx( result );
136 
137  Py_DECREF( result );
138  }
139 
140  return ret;
141 }
142 
143 
145 {
146  PyLOCK lock;
147 
148  return CallRetStrMethod( "GetName" );
149 }
150 
151 
153 {
154  PyLOCK lock;
155 
156  return CallRetStrMethod( "GetImage" );
157 }
158 
159 
161 {
162  PyLOCK lock;
163 
164  return CallRetStrMethod( "GetDescription" );
165 }
166 
167 
169 {
170  int ret = 0;
171  PyLOCK lock;
172 
173  // Time to call the callback
174  PyObject* result = CallMethod( "GetNumParameterPages", NULL );
175 
176  if( result )
177  {
178 #if PY_MAJOR_VERSION >= 3
179  if( !PyLong_Check( result ) )
180  return -1;
181 
182  ret = PyLong_AsLong( result );
183 #else
184  if( !PyInt_Check( result ) )
185  return -1;
186 
187  ret = PyInt_AsLong( result );
188 #endif
189  Py_DECREF( result );
190  }
191 
192  return ret;
193 }
194 
195 
197 {
198  wxString ret;
199  PyLOCK lock;
200 
201  // Time to call the callback
202  PyObject* arglist = Py_BuildValue( "(i)", aPage );
203  PyObject* result = CallMethod( "GetParameterPageName", arglist );
204 
205  Py_DECREF( arglist );
206 
207  if( result == Py_None )
208  {
209  Py_DECREF( result );
210  return ret;
211  }
212 
213  ret = PyStringToWx( result );
214  Py_XDECREF( result );
215 
216  return ret;
217 }
218 
219 
221 {
222  wxArrayString ret;
223  PyLOCK lock;
224 
225  PyObject* arglist = Py_BuildValue( "(i)", aPage );
226 
227  ret = CallRetArrayStrMethod( "GetParameterNames", arglist );
228  Py_DECREF( arglist );
229 
230  for( unsigned i = 0; i < ret.GetCount(); i++ )
231  {
232  wxString rest;
233  wxString item = ret[i];
234 
235  if( item.StartsWith( wxT( "*" ), &rest ) )
236  {
237  ret[i] = rest;
238  }
239  }
240 
241  return ret;
242 }
243 
244 
246 {
247  wxArrayString ret;
248  PyLOCK lock;
249 
250  PyObject* arglist = Py_BuildValue( "(i)", aPage );
251 
252  ret = CallRetArrayStrMethod( "GetParameterTypes", arglist );
253  Py_DECREF( arglist );
254 
255  return ret;
256 }
257 
258 
260 {
261  PyLOCK lock;
262 
263  PyObject* arglist = Py_BuildValue( "(i)", aPage );
264  wxArrayString ret = CallRetArrayStrMethod( "GetParameterValues", arglist );
265 
266  Py_DECREF( arglist );
267 
268  return ret;
269 }
270 
271 
273 {
274  PyLOCK lock;
275 
276  PyObject* arglist = Py_BuildValue( "(i)", aPage );
277  wxArrayString ret = CallRetArrayStrMethod( "GetParameterErrors", arglist );
278 
279  Py_DECREF( arglist );
280 
281  return ret;
282 }
283 
285 {
286  PyLOCK lock;
287 
288  PyObject* arglist = Py_BuildValue( "(i)", aPage );
289  wxArrayString ret = CallRetArrayStrMethod( "GetParameterHints", arglist );
290 
291  Py_DECREF( arglist );
292 
293  return ret;
294 }
295 
297 {
298  PyLOCK lock;
299 
300  PyObject* arglist = Py_BuildValue( "(i)", aPage );
301  wxArrayString ret = CallRetArrayStrMethod( "GetParameterDesignators", arglist );
302 
303  Py_DECREF( arglist );
304 
305  return ret;
306 }
307 
308 wxString PYTHON_FOOTPRINT_WIZARD::SetParameterValues( int aPage, wxArrayString& aValues )
309 {
310  int len = aValues.size();
311 
312  PyLOCK lock;
313 
314  PyObject* py_list = PyList_New( len );
315 
316  for( int i = 0; i < len; i++ )
317  {
318  wxString& str = aValues[i];
319 #if PY_MAJOR_VERSION >= 3
320  PyObject* py_str = PyUnicode_FromString( (const char*) str.mb_str() );
321 #else
322  PyObject* py_str = PyString_FromString( (const char*) str.mb_str() );
323 #endif
324  PyList_SetItem( py_list, i, py_str );
325  }
326 
327  PyObject* arglist;
328 
329  arglist = Py_BuildValue( "(i,O)", aPage, py_list );
330  wxString res = CallRetStrMethod( "SetParameterValues", arglist );
331  Py_DECREF( arglist );
332 
333  return res;
334 }
335 
337 {
338  PyLOCK lock;
339 
340  CallMethod( "ResetWizard", NULL );
341 }
342 
343 
344 // this is a SWIG function declaration -from module.i
345 MODULE* PyModule_to_MODULE( PyObject* obj0 );
346 
347 
349 {
350  PyLOCK lock;
351 
352  PyObject* result = CallMethod( "GetFootprint", NULL );
353 
354  if( aMessages )
355  *aMessages = CallRetStrMethod( "GetBuildMessages", NULL );
356 
357  if( !result )
358  return NULL;
359 
360  PyObject* obj = PyObject_GetAttrString( result, "this" );
361 
362  if( PyErr_Occurred() )
363  {
364  PyErr_Print();
365  PyErr_Clear();
366  }
367 
368  MODULE* mod = PyModule_to_MODULE( obj );
369 
370  return mod;
371 }
372 
373 
375 {
376  return (void*) m_PyWizard;
377 }
378 
379 
381 {
382  PYTHON_FOOTPRINT_WIZARD* fw = new PYTHON_FOOTPRINT_WIZARD( aPyWizard );
383 
384  fw->register_wizard();
385 }
386 
387 
389 {
390  // deregister also destroyes the previously created "PYTHON_FOOTPRINT_WIZARD object"
391  FOOTPRINT_WIZARD_LIST::deregister_object( (void*) aPyWizard );
392 }
wxString PyStringToWx(PyObject *aString)
Class PCBNEW_FOOTPRINT_WIZARDS.
MODULE * PyModule_to_MODULE(PyObject *obj0)
void register_wizard()
Function register_wizard It's the standard method of a "FOOTPRINT_WIZARD" to register itself into the...
void ResetParameters() override
Function ResetParameters Reset all wizard parameters to default values.
wxArrayString GetParameterTypes(int aPage) override
Function GetParameterTypes.
This file contains miscellaneous commonly used macros and functions.
wxArrayString CallRetArrayStrMethod(const char *aMethod, PyObject *aArglist=NULL)
PYTHON_FOOTPRINT_WIZARD(PyObject *wizard)
wxArrayString GetParameterNames(int aPage) override
Function GetParameterNames.
wxArrayString GetParameterHints(int aPage) override
Function GetParameterHints.
#define NULL
wxArrayString GetParameterErrors(int aPage) override
Function GetParameterErrors.
wxArrayString PyArrayStringToWx(PyObject *aArrayString)
wxArrayString GetParameterDesignators(int aPage=0) override
Function GetParamaterDesignators.
int GetNumParameterPages() override
Function GetNumParameterPages.
PyObject * CallMethod(const char *aMethod, PyObject *aArglist=NULL)
wxString CallRetStrMethod(const char *aMethod, PyObject *aArglist=NULL)
static void deregister_wizard(PyObject *aPyWizard)
wxString SetParameterValues(int aPage, wxArrayString &aValues) override
Function SetParameterValues.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
#define _(s)
Definition: 3d_actions.cpp:33
wxString GetImage() override
Function GetImage.
wxString PyErrStringWithTraceback()
MODULE * GetFootprint(wxString *aMessages) override
Function GetModule This method builds the module itself and returns it to the caller function.
static void register_wizard(PyObject *aPyWizard)
wxArrayString GetParameterValues(int aPage) override
Function GetParameterValues.
void * GetObject() override
Function GetObject This method gets the pointer to the object from where this wizard constructs.
static bool deregister_object(void *aObject)
Function deregister_object Anyone calls this method to deregister an object which builds a wizard,...
wxString GetName() override
Function GetName.
wxString GetDescription() override
Function GetDescription.
wxString GetParameterPageName(int aPage) override
Function GetParameterPageName.