KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 <[email protected]>
5 * Copyright (C) 2016-2021 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 <wx/msgdlg.h>
34#include "../../scripting/python_scripting.h"
35
36
38{
39 PyLOCK lock;
40
41 m_PyWizard = aWizard;
42 Py_XINCREF( aWizard );
43}
44
45
47{
48 PyLOCK lock;
49
50 Py_XDECREF( m_PyWizard );
51}
52
53
54PyObject* PYTHON_FOOTPRINT_WIZARD::CallMethod( const char* aMethod, PyObject* aArglist )
55{
56 PyLOCK lock;
57
58 PyErr_Clear();
59 // pFunc is a new reference to the desired method
60 PyObject* pFunc = PyObject_GetAttrString( m_PyWizard, aMethod );
61
62 if( pFunc && PyCallable_Check( pFunc ) )
63 {
64 PyObject* result = PyObject_CallObject( pFunc, aArglist );
65
66 if( PyErr_Occurred() )
67 {
68#if 1 // defined(DEBUG)
69 wxMessageBox( PyErrStringWithTraceback(),
70 _( "Exception on python footprint wizard code" ),
71 wxICON_ERROR | wxOK );
72#endif
73 }
74
75 if( result )
76 {
77 Py_XDECREF( pFunc );
78 return result;
79 }
80 }
81 else
82 {
83 wxString msg = wxString::Format(_( "Method '%s' not found, or not callable" ), aMethod );
84 wxMessageBox( msg, _( "Unknown Method" ), wxICON_ERROR | wxOK );
85 }
86
87 if( pFunc )
88 {
89 Py_XDECREF( pFunc );
90 }
91
92 return nullptr;
93}
94
95
96wxString PYTHON_FOOTPRINT_WIZARD::CallRetStrMethod( const char* aMethod, PyObject* aArglist )
97{
98 wxString ret;
99 PyLOCK lock;
100
101 PyObject* result = CallMethod( aMethod, aArglist );
102
103 if( result == Py_None )
104 {
105 Py_DECREF( result );
106 return ret;
107 }
108
109 ret = PyStringToWx( result );
110 Py_XDECREF( result );
111
112 return ret;
113}
114
115
116wxArrayString PYTHON_FOOTPRINT_WIZARD::CallRetArrayStrMethod( const char* aMethod,
117 PyObject* aArglist )
118{
119 wxArrayString ret;
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( "PYTHON_FOOTPRINT_WIZARD::CallRetArrayStrMethod, result is not a list" ),
130 1 );
131 return ret;
132 }
133
134 ret = PyArrayStringToWx( result );
135
136 Py_DECREF( result );
137 }
138
139 return ret;
140}
141
142
144{
145 PyLOCK lock;
146
147 return CallRetStrMethod( "GetName" );
148}
149
150
152{
153 PyLOCK lock;
154
155 return CallRetStrMethod( "GetImage" );
156}
157
158
160{
161 PyLOCK lock;
162
163 return CallRetStrMethod( "GetDescription" );
164}
165
166
168{
169 int ret = 0;
170 PyLOCK lock;
171
172 // Time to call the callback
173 PyObject* result = CallMethod( "GetNumParameterPages", nullptr );
174
175 if( result )
176 {
177 if( !PyLong_Check( result ) )
178 return -1;
179
180 ret = PyLong_AsLong( result );
181 Py_DECREF( result );
182 }
183
184 return ret;
185}
186
187
189{
190 wxString ret;
191 PyLOCK lock;
192
193 // Time to call the callback
194 PyObject* arglist = Py_BuildValue( "(i)", aPage );
195 PyObject* result = CallMethod( "GetParameterPageName", arglist );
196
197 Py_DECREF( arglist );
198
199 if( result == Py_None )
200 {
201 Py_DECREF( result );
202 return ret;
203 }
204
205 ret = PyStringToWx( result );
206 Py_XDECREF( result );
207
208 return ret;
209}
210
211
213{
214 wxArrayString ret;
215 PyLOCK lock;
216
217 PyObject* arglist = Py_BuildValue( "(i)", aPage );
218
219 ret = CallRetArrayStrMethod( "GetParameterNames", arglist );
220 Py_DECREF( arglist );
221
222 for( unsigned i = 0; i < ret.GetCount(); i++ )
223 {
224 wxString rest;
225 wxString item = ret[i];
226
227 if( item.StartsWith( wxT( "*" ), &rest ) )
228 {
229 ret[i] = rest;
230 }
231 }
232
233 return ret;
234}
235
236
238{
239 wxArrayString ret;
240 PyLOCK lock;
241
242 PyObject* arglist = Py_BuildValue( "(i)", aPage );
243
244 ret = CallRetArrayStrMethod( "GetParameterTypes", arglist );
245 Py_DECREF( arglist );
246
247 return ret;
248}
249
250
252{
253 PyLOCK lock;
254
255 PyObject* arglist = Py_BuildValue( "(i)", aPage );
256 wxArrayString ret = CallRetArrayStrMethod( "GetParameterValues", arglist );
257
258 Py_DECREF( arglist );
259
260 return ret;
261}
262
263
265{
266 PyLOCK lock;
267
268 PyObject* arglist = Py_BuildValue( "(i)", aPage );
269 wxArrayString ret = CallRetArrayStrMethod( "GetParameterErrors", arglist );
270
271 Py_DECREF( arglist );
272
273 return ret;
274}
275
277{
278 PyLOCK lock;
279
280 PyObject* arglist = Py_BuildValue( "(i)", aPage );
281 wxArrayString ret = CallRetArrayStrMethod( "GetParameterHints", arglist );
282
283 Py_DECREF( arglist );
284
285 return ret;
286}
287
289{
290 PyLOCK lock;
291
292 PyObject* arglist = Py_BuildValue( "(i)", aPage );
293 wxArrayString ret = CallRetArrayStrMethod( "GetParameterDesignators", arglist );
294
295 Py_DECREF( arglist );
296
297 return ret;
298}
299
300wxString PYTHON_FOOTPRINT_WIZARD::SetParameterValues( int aPage, wxArrayString& aValues )
301{
302 int len = aValues.size();
303
304 PyLOCK lock;
305
306 PyObject* py_list = PyList_New( len );
307
308 for( int i = 0; i < len; i++ )
309 {
310 wxString& str = aValues[i];
311 PyObject* py_str = PyUnicode_FromString( (const char*) str.mb_str() );
312 PyList_SetItem( py_list, i, py_str );
313 }
314
315 PyObject* arglist;
316
317 arglist = Py_BuildValue( "(i,O)", aPage, py_list );
318 wxString res = CallRetStrMethod( "SetParameterValues", arglist );
319 Py_DECREF( arglist );
320
321 return res;
322}
323
325{
326 PyLOCK lock;
327
328 CallMethod( "ResetWizard", nullptr );
329}
330
331
332// this is a SWIG function declaration -from footprint.i
334
335
337{
338 PyLOCK lock;
339
340 PyObject* result = CallMethod( "GetFootprint", nullptr );
341
342 if( aMessages )
343 *aMessages = CallRetStrMethod( "GetBuildMessages", nullptr );
344
345 if( !result )
346 return nullptr;
347
348 PyObject* obj = PyObject_GetAttrString( result, "this" );
349
350 if( PyErr_Occurred() )
351 {
352 PyErr_Print();
353 PyErr_Clear();
354 }
355
356 FOOTPRINT* footprint = PyFootprint_to_FOOTPRINT( obj );
357
358 return footprint;
359}
360
361
363{
364 return (void*) m_PyWizard;
365}
366
367
369{
371
372 fw->register_wizard();
373}
374
375
377{
378 // deregister also destroys the previously created "PYTHON_FOOTPRINT_WIZARD object"
379 FOOTPRINT_WIZARD_LIST::deregister_object( (void*) aPyWizard );
380}
static bool deregister_object(void *aObject)
Unregister an object which builds a wizard.
void register_wizard()
The standard method of a "FOOTPRINT_WIZARD" to register itself into the FOOTPRINT_WIZARD_LIST singlet...
static void deregister_wizard(PyObject *aPyWizard)
static void register_wizard(PyObject *aPyWizard)
wxArrayString GetParameterTypes(int aPage) override
wxArrayString GetParameterErrors(int aPage) override
wxArrayString GetParameterDesignators(int aPage=0) override
wxString CallRetStrMethod(const char *aMethod, PyObject *aArglist=nullptr)
void ResetParameters() override
Reset all wizard parameters to default values.
wxString GetParameterPageName(int aPage) override
void * GetObject() override
Get the object from where this wizard constructs.
wxArrayString GetParameterValues(int aPage) override
FOOTPRINT * GetFootprint(wxString *aMessages) override
Build the footprint itself and returns it to the caller function.
PYTHON_FOOTPRINT_WIZARD(PyObject *wizard)
wxArrayString CallRetArrayStrMethod(const char *aMethod, PyObject *aArglist=nullptr)
wxArrayString GetParameterNames(int aPage) override
wxString SetParameterValues(int aPage, wxArrayString &aValues) override
wxArrayString GetParameterHints(int aPage) override
PyObject * CallMethod(const char *aMethod, PyObject *aArglist=nullptr)
#define _(s)
This file contains miscellaneous commonly used macros and functions.
FOOTPRINT * PyFootprint_to_FOOTPRINT(PyObject *obj0)
Class PCBNEW_FOOTPRINT_WIZARDS.
VECTOR3I res