pcbnew_footprint_wizards.cpp 7.93 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * 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
 */

25 26
/**
 * @file  pcbnew_footprint_wizards.cpp
27
 * @brief Class PCBNEW_PYTHON_FOOTPRINT_WIZARDS
28 29 30
 */

#include "pcbnew_footprint_wizards.h"
31
#include <python_scripting.h>
32
#include <stdio.h>
33
#include <macros.h>
34

35 36

PYTHON_FOOTPRINT_WIZARD::PYTHON_FOOTPRINT_WIZARD( PyObject* aWizard )
37
{
38
    PyLOCK lock;
39

40
    this->m_PyWizard = aWizard;
41
    Py_XINCREF( aWizard );
42 43
}

44

45
PYTHON_FOOTPRINT_WIZARD::~PYTHON_FOOTPRINT_WIZARD()
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
            wxMessageBox( PyErrStringWithTraceback(),
68
                          wxT( "Exception on python footprint wizard code" ),
69
                          wxICON_ERROR | wxOK );
70
        }
71

72
        if( result )
73
        {
74 75
            Py_XDECREF( pFunc );
            return result;
76 77 78 79
        }
    }
    else
    {
80
        printf( "method not found, or not callable: %s\n", aMethod );
81
    }
82

83
    if( pFunc )
84
    {
85
        Py_XDECREF( pFunc );
86
    }
87

88 89 90
    return NULL;
}

91 92

wxString PYTHON_FOOTPRINT_WIZARD::CallRetStrMethod( const char* aMethod, PyObject* aArglist )
93
{
94 95
    wxString    ret;
    PyLOCK      lock;
96

97 98
    PyObject*   result = CallMethod( aMethod, aArglist );

99
    if( result )
100
    {
101 102 103
        const char* str_res = PyString_AsString( result );
        ret = FROM_UTF8( str_res );
        Py_DECREF( result );
104
    }
105

106 107
    return ret;
}
108

109

110 111
wxArrayString PYTHON_FOOTPRINT_WIZARD::CallRetArrayStrMethod( const char*   aMethod,
                                                              PyObject*     aArglist )
112
{
113 114 115
    wxArrayString   ret;
    wxString        str_item;
    PyLOCK          lock;
116

117
    PyObject*       result = CallMethod( aMethod, aArglist );
118

119
    if( result )
120
    {
121 122 123 124 125 126 127 128
        if( !PyList_Check( result ) )
        {
            Py_DECREF( result );
            ret.Add( wxT(
                         "PYTHON_FOOTPRINT_WIZARD::CallRetArrayStrMethod, result is not a list" ),
                     1 );
            return ret;
        }
129

130
        ret = PyArrayStringToWx( result );
131

132
        Py_DECREF( result );
133
    }
134

135
    return ret;
136 137
}

138

139
wxString PYTHON_FOOTPRINT_WIZARD::GetName()
140
{
141
    PyLOCK lock;
142

143
    return CallRetStrMethod( "GetName" );
144 145
}

146

147
wxString PYTHON_FOOTPRINT_WIZARD::GetImage()
148
{
149
    PyLOCK lock;
150

151
    return CallRetStrMethod( "GetImage" );
152 153
}

154

155
wxString PYTHON_FOOTPRINT_WIZARD::GetDescription()
156
{
157
    PyLOCK lock;
158

159
    return CallRetStrMethod( "GetDescription" );
160 161
}

162

163
int PYTHON_FOOTPRINT_WIZARD::GetNumParameterPages()
164
{
165 166
    int         ret = 0;
    PyLOCK      lock;
167

168
    // Time to call the callback
169
    PyObject*   result = CallMethod( "GetNumParameterPages", NULL );
170

171
    if( result )
172
    {
173
        if( !PyInt_Check( result ) )
174
            return -1;
175

176 177
        ret = PyInt_AsLong( result );
        Py_DECREF( result );
178
    }
179

180 181 182
    return ret;
}

183

184
wxString PYTHON_FOOTPRINT_WIZARD::GetParameterPageName( int aPage )
185
{
186 187 188 189
    wxString    ret;
    PyLOCK      lock;

    // Time to call the callback
190 191
    PyObject*   arglist = Py_BuildValue( "(i)", aPage );
    PyObject*   result  = CallMethod( "GetParameterPageName", arglist );
192

193
    Py_DECREF( arglist );
194

195
    if( result )
196
    {
197 198 199
        const char* str_res = PyString_AsString( result );
        ret = FROM_UTF8( str_res );
        Py_DECREF( result );
200
    }
201

202 203 204
    return ret;
}

205

206
wxArrayString PYTHON_FOOTPRINT_WIZARD::GetParameterNames( int aPage )
207
{
208 209 210 211
    wxArrayString   ret;
    PyLOCK          lock;

    PyObject*       arglist = Py_BuildValue( "(i)", aPage );
212

213 214
    ret = CallRetArrayStrMethod( "GetParameterNames", arglist );
    Py_DECREF( arglist );
215

216
    for( unsigned i = 0; i<ret.GetCount(); i++ )
217
    {
218 219 220
        wxString    rest;
        wxString    item = ret[i];

221
        if( item.StartsWith( wxT( "*" ), &rest ) )
222
        {
223
            ret[i] = rest;
224 225 226 227 228 229
        }
    }

    return ret;
}

230

231
wxArrayString PYTHON_FOOTPRINT_WIZARD::GetParameterTypes( int aPage )
232
{
233 234
    wxArrayString   ret;
    PyLOCK          lock;
235

236 237
    PyObject*       arglist = Py_BuildValue( "(i)", aPage );

238
    ret = CallRetArrayStrMethod( "GetParameterNames", arglist );
239
    Py_DECREF( arglist );
240

241
    for( unsigned i = 0; i<ret.GetCount(); i++ )
242
    {
243 244 245
        wxString    rest;
        wxString    item = ret[i];

246
        if( item.StartsWith( wxT( "*" ), &rest ) )
247
        {
248
            ret[i] = wxT( "UNITS" );    // units
249 250 251
        }
        else
        {
252
            ret[i] = wxT( "IU" );       // internal units
253 254 255
        }
    }

256
    return ret;
257 258
}

259

260
wxArrayString PYTHON_FOOTPRINT_WIZARD::GetParameterValues( int aPage )
261
{
262
    PyLOCK          lock;
263

264
    PyObject*       arglist = Py_BuildValue( "(i)", aPage );
265

266
    wxArrayString   ret = CallRetArrayStrMethod( "GetParameterValues", arglist );
267

268
    Py_DECREF( arglist );
269

270
    return ret;
271 272
}

273

274
wxArrayString PYTHON_FOOTPRINT_WIZARD::GetParameterErrors( int aPage )
275
{
276 277 278
    PyLOCK          lock;

    PyObject*       arglist = Py_BuildValue( "(i)", aPage );
279

280
    wxArrayString   ret = CallRetArrayStrMethod( "GetParameterErrors", arglist );
281

282
    Py_DECREF( arglist );
283

284 285 286
    return ret;
}

287

288
wxString PYTHON_FOOTPRINT_WIZARD::SetParameterValues( int aPage, wxArrayString& aValues )
289
{
290
    int         len = aValues.size();
291

292
    PyLOCK      lock;
293

294
    PyObject*   py_list = PyList_New( len );
295

296
    for( int i = 0; i<len; i++ )
297
    {
298 299
        wxString    str     = aValues[i];
        PyObject*   py_str  = PyString_FromString( (const char*) str.mb_str() );
300
        PyList_SetItem( py_list, i, py_str );
301
    }
302

303
    PyObject*   arglist;
304

305
    arglist = Py_BuildValue( "(i,O)", aPage, py_list );
306
    wxString    res = CallRetStrMethod( "SetParameterValues", arglist );
307
    Py_DECREF( arglist );
308

309 310 311
    return res;
}

312

313 314 315 316 317
// this is a SWIG function declaration -from module.i
MODULE* PyModule_to_MODULE( PyObject* obj0 );


MODULE* PYTHON_FOOTPRINT_WIZARD::GetModule()
318
{
319
    PyLOCK      lock;
320

321
    PyObject*   result = CallMethod( "GetModule", NULL );
322

323 324
    if( !result )
        return NULL;
325

326
    PyObject* obj = PyObject_GetAttrString( result, "this" );
327

328
    if( PyErr_Occurred() )
329
    {
330
        PyErr_Print();
331 332
        PyErr_Clear();
    }
333

334
    MODULE* mod = PyModule_to_MODULE( obj );
335 336

    return mod;
337 338 339
}


340 341 342 343 344 345
void* PYTHON_FOOTPRINT_WIZARD::GetObject()
{
    return (void*) m_PyWizard;
}


346
void PYTHON_FOOTPRINT_WIZARDS::register_wizard( PyObject* aPyWizard )
347
{
348
    PYTHON_FOOTPRINT_WIZARD* fw = new PYTHON_FOOTPRINT_WIZARD( aPyWizard );
349 350

    fw->register_wizard();
351
}
352 353 354 355 356 357 358


void PYTHON_FOOTPRINT_WIZARDS::deregister_wizard( PyObject* aPyWizard )
{
    // deregister also destroyes the previously created "PYTHON_FOOTPRINT_WIZARD object"
    FOOTPRINT_WIZARDS::deregister_object( (void*) aPyWizard );
}