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 CHANGELOG.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 <python_scripting.h>
32 #include <stdio.h>
33 #include <macros.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  wxT( "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  printf( "method not found, or not callable: %s\n", aMethod );
83  }
84 
85  if( pFunc )
86  {
87  Py_XDECREF( pFunc );
88  }
89 
90  return NULL;
91 }
92 
93 
94 wxString PYTHON_FOOTPRINT_WIZARD::CallRetStrMethod( const char* aMethod, PyObject* aArglist )
95 {
96  wxString ret;
97  PyLOCK lock;
98 
99  PyObject* result = CallMethod( aMethod, aArglist );
100 
101  if( result )
102  {
103  const char* str_res = PyString_AsString( result );
104  ret = FROM_UTF8( str_res );
105  Py_DECREF( result );
106  }
107 
108  return ret;
109 }
110 
111 
112 wxArrayString PYTHON_FOOTPRINT_WIZARD::CallRetArrayStrMethod( const char* aMethod,
113  PyObject* aArglist )
114 {
115  wxArrayString ret;
116  wxString str_item;
117  PyLOCK lock;
118 
119  PyObject* result = CallMethod( aMethod, aArglist );
120 
121  if( result )
122  {
123  if( !PyList_Check( result ) )
124  {
125  Py_DECREF( result );
126  ret.Add( wxT(
127  "PYTHON_FOOTPRINT_WIZARD::CallRetArrayStrMethod, result is not a list" ),
128  1 );
129  return ret;
130  }
131 
132  ret = PyArrayStringToWx( result );
133 
134  Py_DECREF( result );
135  }
136 
137  return ret;
138 }
139 
140 
142 {
143  PyLOCK lock;
144 
145  return CallRetStrMethod( "GetName" );
146 }
147 
148 
150 {
151  PyLOCK lock;
152 
153  return CallRetStrMethod( "GetImage" );
154 }
155 
156 
158 {
159  PyLOCK lock;
160 
161  return CallRetStrMethod( "GetDescription" );
162 }
163 
164 
166 {
167  int ret = 0;
168  PyLOCK lock;
169 
170  // Time to call the callback
171  PyObject* result = CallMethod( "GetNumParameterPages", NULL );
172 
173  if( result )
174  {
175  if( !PyInt_Check( result ) )
176  return -1;
177 
178  ret = PyInt_AsLong( result );
179  Py_DECREF( result );
180  }
181 
182  return ret;
183 }
184 
185 
187 {
188  wxString ret;
189  PyLOCK lock;
190 
191  // Time to call the callback
192  PyObject* arglist = Py_BuildValue( "(i)", aPage );
193  PyObject* result = CallMethod( "GetParameterPageName", arglist );
194 
195  Py_DECREF( arglist );
196 
197  if( result )
198  {
199  const char* str_res = PyString_AsString( result );
200  ret = FROM_UTF8( str_res );
201  Py_DECREF( result );
202  }
203 
204  return ret;
205 }
206 
207 
209 {
210  wxArrayString ret;
211  PyLOCK lock;
212 
213  PyObject* arglist = Py_BuildValue( "(i)", aPage );
214 
215  ret = CallRetArrayStrMethod( "GetParameterNames", arglist );
216  Py_DECREF( arglist );
217 
218  for( unsigned i = 0; i < ret.GetCount(); i++ )
219  {
220  wxString rest;
221  wxString item = ret[i];
222 
223  if( item.StartsWith( wxT( "*" ), &rest ) )
224  {
225  ret[i] = rest;
226  }
227  }
228 
229  return ret;
230 }
231 
232 
234 {
235  wxArrayString ret;
236  PyLOCK lock;
237 
238  PyObject* arglist = Py_BuildValue( "(i)", aPage );
239 
240  ret = CallRetArrayStrMethod( "GetParameterTypes", arglist );
241  Py_DECREF( arglist );
242 
243  return ret;
244 }
245 
246 
248 {
249  PyLOCK lock;
250 
251  PyObject* arglist = Py_BuildValue( "(i)", aPage );
252  wxArrayString ret = CallRetArrayStrMethod( "GetParameterValues", arglist );
253 
254  Py_DECREF( arglist );
255 
256  return ret;
257 }
258 
259 
261 {
262  PyLOCK lock;
263 
264  PyObject* arglist = Py_BuildValue( "(i)", aPage );
265  wxArrayString ret = CallRetArrayStrMethod( "GetParameterErrors", arglist );
266 
267  Py_DECREF( arglist );
268 
269  return ret;
270 }
271 
273 {
274  PyLOCK lock;
275 
276  PyObject* arglist = Py_BuildValue( "(i)", aPage );
277  wxArrayString ret = CallRetArrayStrMethod( "GetParameterHints", 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( "GetParameterDesignators", arglist );
290 
291  Py_DECREF( arglist );
292 
293  return ret;
294 }
295 
296 wxString PYTHON_FOOTPRINT_WIZARD::SetParameterValues( int aPage, wxArrayString& aValues )
297 {
298  int len = aValues.size();
299 
300  PyLOCK lock;
301 
302  PyObject* py_list = PyList_New( len );
303 
304  for( int i = 0; i < len; i++ )
305  {
306  wxString& str = aValues[i];
307  PyObject* py_str = PyString_FromString( (const char*) str.mb_str() );
308  PyList_SetItem( py_list, i, py_str );
309  }
310 
311  PyObject* arglist;
312 
313  arglist = Py_BuildValue( "(i,O)", aPage, py_list );
314  wxString res = CallRetStrMethod( "SetParameterValues", arglist );
315  Py_DECREF( arglist );
316 
317  return res;
318 }
319 
321 {
322  PyLOCK lock;
323 
324  CallMethod( "ResetWizard", NULL );
325 }
326 
327 
328 // this is a SWIG function declaration -from module.i
329 MODULE* PyModule_to_MODULE( PyObject* obj0 );
330 
331 
333 {
334  PyLOCK lock;
335 
336  PyObject* result = CallMethod( "GetFootprint", NULL );
337 
338  if( aMessages )
339  *aMessages = CallRetStrMethod( "GetBuildMessages", NULL );
340 
341  if( !result )
342  return NULL;
343 
344  PyObject* obj = PyObject_GetAttrString( result, "this" );
345 
346  if( PyErr_Occurred() )
347  {
348  PyErr_Print();
349  PyErr_Clear();
350  }
351 
352  MODULE* mod = PyModule_to_MODULE( obj );
353 
354  return mod;
355 }
356 
357 
359 {
360  return (void*) m_PyWizard;
361 }
362 
363 
364 void PYTHON_FOOTPRINT_WIZARDS::register_wizard( PyObject* aPyWizard )
365 {
366  PYTHON_FOOTPRINT_WIZARD* fw = new PYTHON_FOOTPRINT_WIZARD( aPyWizard );
367 
368  fw->register_wizard();
369 }
370 
371 
373 {
374  // deregister also destroyes the previously created "PYTHON_FOOTPRINT_WIZARD object"
375  FOOTPRINT_WIZARDS::deregister_object( (void*) aPyWizard );
376 }
static wxString FROM_UTF8(const char *cstring)
function FROM_UTF8 converts a UTF8 encoded C string to a wxString for all wxWidgets build modes...
Definition: macros.h:53
static void deregister_wizard(PyObject *aPyWizard)
Class PCBNEW_FOOTPRINT_WIZARDS.
static void register_wizard(PyObject *aPyWizard)
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.
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)
wxString SetParameterValues(int aPage, wxArrayString &aValues) override
Function SetParameterValues.
static bool deregister_object(void *aObject)
Function deregister_object Anyone calls this method to deregister an object which builds a wizard...
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...
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...
wxString GetName() override
Function GetName.
wxString GetDescription() override
Function GetDescription.
#define mod(a, n)
Definition: greymap.cpp:24
wxString GetParameterPageName(int aPage) override
Function GetParameterPageName.