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 <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  _( "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 )
103  {
104  const char* str_res = PyString_AsString( result );
105  ret = FROM_UTF8( str_res );
106  Py_DECREF( result );
107  }
108 
109  return ret;
110 }
111 
112 
113 wxArrayString PYTHON_FOOTPRINT_WIZARD::CallRetArrayStrMethod( const char* aMethod,
114  PyObject* aArglist )
115 {
116  wxArrayString ret;
117  wxString str_item;
118  PyLOCK lock;
119 
120  PyObject* result = CallMethod( aMethod, aArglist );
121 
122  if( result )
123  {
124  if( !PyList_Check( result ) )
125  {
126  Py_DECREF( result );
127  ret.Add( wxT(
128  "PYTHON_FOOTPRINT_WIZARD::CallRetArrayStrMethod, result is not a list" ),
129  1 );
130  return ret;
131  }
132 
133  ret = PyArrayStringToWx( result );
134 
135  Py_DECREF( result );
136  }
137 
138  return ret;
139 }
140 
141 
143 {
144  PyLOCK lock;
145 
146  return CallRetStrMethod( "GetName" );
147 }
148 
149 
151 {
152  PyLOCK lock;
153 
154  return CallRetStrMethod( "GetImage" );
155 }
156 
157 
159 {
160  PyLOCK lock;
161 
162  return CallRetStrMethod( "GetDescription" );
163 }
164 
165 
167 {
168  int ret = 0;
169  PyLOCK lock;
170 
171  // Time to call the callback
172  PyObject* result = CallMethod( "GetNumParameterPages", NULL );
173 
174  if( result )
175  {
176  if( !PyInt_Check( result ) )
177  return -1;
178 
179  ret = PyInt_AsLong( result );
180  Py_DECREF( result );
181  }
182 
183  return ret;
184 }
185 
186 
188 {
189  wxString ret;
190  PyLOCK lock;
191 
192  // Time to call the callback
193  PyObject* arglist = Py_BuildValue( "(i)", aPage );
194  PyObject* result = CallMethod( "GetParameterPageName", arglist );
195 
196  Py_DECREF( arglist );
197 
198  if( result )
199  {
200  const char* str_res = PyString_AsString( result );
201  ret = FROM_UTF8( str_res );
202  Py_DECREF( result );
203  }
204 
205  return ret;
206 }
207 
208 
210 {
211  wxArrayString ret;
212  PyLOCK lock;
213 
214  PyObject* arglist = Py_BuildValue( "(i)", aPage );
215 
216  ret = CallRetArrayStrMethod( "GetParameterNames", arglist );
217  Py_DECREF( arglist );
218 
219  for( unsigned i = 0; i < ret.GetCount(); i++ )
220  {
221  wxString rest;
222  wxString item = ret[i];
223 
224  if( item.StartsWith( wxT( "*" ), &rest ) )
225  {
226  ret[i] = rest;
227  }
228  }
229 
230  return ret;
231 }
232 
233 
235 {
236  wxArrayString ret;
237  PyLOCK lock;
238 
239  PyObject* arglist = Py_BuildValue( "(i)", aPage );
240 
241  ret = CallRetArrayStrMethod( "GetParameterTypes", arglist );
242  Py_DECREF( arglist );
243 
244  return ret;
245 }
246 
247 
249 {
250  PyLOCK lock;
251 
252  PyObject* arglist = Py_BuildValue( "(i)", aPage );
253  wxArrayString ret = CallRetArrayStrMethod( "GetParameterValues", arglist );
254 
255  Py_DECREF( arglist );
256 
257  return ret;
258 }
259 
260 
262 {
263  PyLOCK lock;
264 
265  PyObject* arglist = Py_BuildValue( "(i)", aPage );
266  wxArrayString ret = CallRetArrayStrMethod( "GetParameterErrors", arglist );
267 
268  Py_DECREF( arglist );
269 
270  return ret;
271 }
272 
274 {
275  PyLOCK lock;
276 
277  PyObject* arglist = Py_BuildValue( "(i)", aPage );
278  wxArrayString ret = CallRetArrayStrMethod( "GetParameterHints", arglist );
279 
280  Py_DECREF( arglist );
281 
282  return ret;
283 }
284 
286 {
287  PyLOCK lock;
288 
289  PyObject* arglist = Py_BuildValue( "(i)", aPage );
290  wxArrayString ret = CallRetArrayStrMethod( "GetParameterDesignators", arglist );
291 
292  Py_DECREF( arglist );
293 
294  return ret;
295 }
296 
297 wxString PYTHON_FOOTPRINT_WIZARD::SetParameterValues( int aPage, wxArrayString& aValues )
298 {
299  int len = aValues.size();
300 
301  PyLOCK lock;
302 
303  PyObject* py_list = PyList_New( len );
304 
305  for( int i = 0; i < len; i++ )
306  {
307  wxString& str = aValues[i];
308  PyObject* py_str = PyString_FromString( (const char*) str.mb_str() );
309  PyList_SetItem( py_list, i, py_str );
310  }
311 
312  PyObject* arglist;
313 
314  arglist = Py_BuildValue( "(i,O)", aPage, py_list );
315  wxString res = CallRetStrMethod( "SetParameterValues", arglist );
316  Py_DECREF( arglist );
317 
318  return res;
319 }
320 
322 {
323  PyLOCK lock;
324 
325  CallMethod( "ResetWizard", NULL );
326 }
327 
328 
329 // this is a SWIG function declaration -from module.i
330 MODULE* PyModule_to_MODULE( PyObject* obj0 );
331 
332 
334 {
335  PyLOCK lock;
336 
337  PyObject* result = CallMethod( "GetFootprint", NULL );
338 
339  if( aMessages )
340  *aMessages = CallRetStrMethod( "GetBuildMessages", NULL );
341 
342  if( !result )
343  return NULL;
344 
345  PyObject* obj = PyObject_GetAttrString( result, "this" );
346 
347  if( PyErr_Occurred() )
348  {
349  PyErr_Print();
350  PyErr_Clear();
351  }
352 
353  MODULE* mod = PyModule_to_MODULE( obj );
354 
355  return mod;
356 }
357 
358 
360 {
361  return (void*) m_PyWizard;
362 }
363 
364 
366 {
367  PYTHON_FOOTPRINT_WIZARD* fw = new PYTHON_FOOTPRINT_WIZARD( aPyWizard );
368 
369  fw->register_wizard();
370 }
371 
372 
374 {
375  // deregister also destroyes the previously created "PYTHON_FOOTPRINT_WIZARD object"
376  FOOTPRINT_WIZARD_LIST::deregister_object( (void*) aPyWizard );
377 }
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
Class PCBNEW_FOOTPRINT_WIZARDS.
MODULE * PyModule_to_MODULE(PyObject *obj0)
void register_wizard()
Function register_wizard It&#39;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.
const string & str
Definition: json11.cpp:596
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)
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
size_t i
Definition: json11.cpp:597
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.
#define mod(a, n)
Definition: greymap.cpp:24
wxString GetParameterPageName(int aPage) override
Function GetParameterPageName.