Commit edee5dc1 authored by Miguel Angel Ajo's avatar Miguel Angel Ajo
Browse files

KiCad scripting plugin architecture, footprint wizards first

parent f114e800
Loading
Loading
Loading
Loading
+5 −3
Original line number Diff line number Diff line
@@ -231,14 +231,15 @@ set(PCBNEW_COMMON_SRCS
# Scripting sources
##

if (KICAD_SCRIPTING)

set(PCBNEW_SCRIPTING_PYTHON_HELPERS    
	   ../scripting/wx_python_helpers.cpp
	   ../scripting/python_scripting.cpp
	   scripting/pcbnew_scripting_helpers.cpp
           scripting/pcbnew_footprint_wizards.cpp
	   )

if (KICAD_SCRIPTING)

	set(PCBNEW_SCRIPTING_SRCS
    	pcbnew_wrap.cxx
    	${PCBNEW_SCRIPTING_PYTHON_HELPERS}
@@ -279,6 +280,7 @@ if (KICAD_SCRIPTING)
                       DEPENDS scripting/board.i
                       DEPENDS scripting/board_item.i
                       DEPENDS scripting/module.i
                       DEPENDS scripting/plugins.i
                       DEPENDS scripting/units.i
                       DEPENDS ../scripting/dlist.i
                       DEPENDS ../scripting/kicad.i
+2 −0
Original line number Diff line number Diff line
@@ -133,5 +133,7 @@

%include "board.i"
%include "module.i"
%include "plugins.i"
%include "units.i"

        
 No newline at end of file
+179 −0
Original line number Diff line number Diff line
/**
 * @file  pcbnew_footprint_wizards.cpp
 * @brief Class PCBNEW_FOOTPRINT_WIZARDS
 */

#include "pcbnew_footprint_wizards.h"
#include <stdio.h>

FOOTPRINT_WIZARD::FOOTPRINT_WIZARD(PyObject *aWizard)
{
    this->py_wizard = aWizard;
    Py_XINCREF(aWizard);    
}

FOOTPRINT_WIZARD::~FOOTPRINT_WIZARD()
{
    Py_XDECREF(this->py_wizard);
}

PyObject* FOOTPRINT_WIZARD::CallMethod(const char* aMethod, PyObject *aArglist)
{
     PyObject *pFunc;
    
    /* pFunc is a new reference to the desired method */
    pFunc = PyObject_GetAttrString(this->py_wizard, aMethod);
    
    if (pFunc && PyCallable_Check(pFunc)) 
    {
        PyObject *result;
        
        result = PyObject_CallObject(pFunc, aArglist);
        
        if (PyErr_Occurred())
        {
            PyObject *t, *v, *b;
            PyErr_Fetch(&t, &v, &b);
            printf ("calling %s()\n",aMethod);
            printf ("Exception: %s\n",PyString_AsString(PyObject_Str(v)));
            printf ("         : %s\n",PyString_AsString(PyObject_Str(b)));
        }
        
        
        if (result)
        {
                Py_XDECREF(pFunc);
                return result;
        }
        
    }
    else
    {
        printf("method not found, or not callable: %s\n",aMethod);
    }
    
    if (pFunc) Py_XDECREF(pFunc);
    
    return NULL;
}

wxString FOOTPRINT_WIZARD::CallRetStrMethod(const char* aMethod, PyObject *aArglist)
{
    wxString ret;
    PyObject *result = CallMethod(aMethod,aArglist);
    if (result)
    {
         const char *str_res = PyString_AsString(result);
         ret  = wxString::FromUTF8(str_res);
         Py_DECREF(result);    
    }
    else
    {
        printf("method not found, or not callable: %s\n",aMethod);
    }
    
    return ret;
}
wxString FOOTPRINT_WIZARD::GetName()
{
    return CallRetStrMethod("GetName");
}

wxString FOOTPRINT_WIZARD::GetImage()
{
    return CallRetStrMethod("GetImage");
}

wxString FOOTPRINT_WIZARD::GetDescription()
{
    return CallRetStrMethod("GetDescription");
}

int FOOTPRINT_WIZARD::GetNumParameterPages()
{
    int ret;
    PyObject *result;
    
    /* Time to call the callback */
    result = CallMethod("GetNumParameterPages",NULL);
   
    if (result)
    {
         if (!PyInt_Check(result)) return -1;
         ret = PyInt_AsLong(result); 
         Py_DECREF(result);    
    }
    return ret;
}

wxString FOOTPRINT_WIZARD::GetParameterPageName(int aPage)
{
    wxString ret;
    PyObject *arglist;
    PyObject *result;
    
    /* Time to call the callback */
    arglist = Py_BuildValue("(i)", aPage);
    result = CallMethod("GetParameterPageName",arglist);
    Py_DECREF(arglist);
    
    if (result)
    {
         const char *str_res = PyString_AsString(result);
         ret  = wxString::FromUTF8(str_res);
         Py_DECREF(result);    
    }
    return ret;
}

wxArrayString FOOTPRINT_WIZARD::GetParameterNames(int aPage)
{
    wxArrayString a;
    return a;
}

wxArrayString FOOTPRINT_WIZARD::GetParameterValues(int aPage)
{
    wxArrayString a;
    return a;
}

wxString FOOTPRINT_WIZARD::SetParameterValues(int aPage,wxArrayString& aValues)
{
    wxString ret;
    return ret;
}

MODULE FOOTPRINT_WIZARD::*GetModule()
{
    return NULL;
}

std::vector<FOOTPRINT_WIZARD*>  FOOTPRINT_WIZARDS::m_FootprintWizards;

void FOOTPRINT_WIZARDS::register_wizard(PyObject* wizard)
{
    FOOTPRINT_WIZARD *fw;
    
    fw = new FOOTPRINT_WIZARD(wizard);
    m_FootprintWizards.push_back(fw);
    
    printf("Registered python footprint wizard '%s'\n",
            (const char*)fw->GetName().mb_str());
    
    int pages = fw->GetNumParameterPages();
    printf("             %d pages\n",pages);
    
    for (int n=0; n<pages; n++)
    {
        printf("                      page %d->'%s'\n",n,
            (const char*)fw->GetParameterPageName(n).mb_str());
    }
    
    
    
}



+45 −0
Original line number Diff line number Diff line
/**
 * @file  pcbnew_footprint_wizards.h
 * @brief Class PCBNEW_FOOTPRINT_WIZARDS
 */

#ifndef PCBNEW_FOOTPRINT_WIZARDS_H
#define	PCBNEW_FOOTPRINT_WIZARDS_H
#include <Python.h>
#include <vector>
#include <wxPcbStruct.h>

class FOOTPRINT_WIZARD
{

    PyObject *py_wizard;
    PyObject *CallMethod(const char *aMethod, PyObject *aArglist=NULL);
    wxString CallRetStrMethod(const char *aMethod, PyObject *aArglist=NULL);
public:
    FOOTPRINT_WIZARD(PyObject *wizard);
    ~FOOTPRINT_WIZARD();
    wxString      GetName();
    wxString      GetImage();  
    wxString      GetDescription();
    int           GetNumParameterPages();
    wxString      GetParameterPageName(int aPage);
    wxArrayString GetParameterNames(int aPage);
    wxArrayString GetParameterValues(int aPage);
    wxString      SetParameterValues(int aPage,wxArrayString& aValues);
    MODULE       *GetModule();
        
};


class FOOTPRINT_WIZARDS 
{
private:
    static    std::vector<FOOTPRINT_WIZARD*>  m_FootprintWizards;

public:
    static void register_wizard(PyObject *wizard);

};

#endif	/* PCBNEW_FOOTPRINT_WIZARDS_H */
+11 −0
Original line number Diff line number Diff line

%{
#include <scripting/pcbnew_footprint_wizards.h>
%}

class FOOTPRINT_WIZARDS 
{
public:
    static void register_wizard(PyObject *wizard);

};
Loading