2012-05-05 22:18:47 +02:00
|
|
|
/**
|
|
|
|
* @file pcbnew_footprint_wizards.cpp
|
2012-05-09 19:37:25 +02:00
|
|
|
* @brief Class PCBNEW_PYTHON_FOOTPRINT_WIZARDS
|
2012-05-05 22:18:47 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pcbnew_footprint_wizards.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2012-05-09 19:37:25 +02:00
|
|
|
PYTHON_FOOTPRINT_WIZARD::PYTHON_FOOTPRINT_WIZARD(PyObject *aWizard)
|
2012-05-05 22:18:47 +02:00
|
|
|
{
|
2012-05-09 19:37:25 +02:00
|
|
|
this->m_PyWizard= aWizard;
|
2012-05-05 22:18:47 +02:00
|
|
|
Py_XINCREF(aWizard);
|
|
|
|
}
|
|
|
|
|
2012-05-09 19:37:25 +02:00
|
|
|
PYTHON_FOOTPRINT_WIZARD::~PYTHON_FOOTPRINT_WIZARD()
|
2012-05-05 22:18:47 +02:00
|
|
|
{
|
2012-05-09 19:37:25 +02:00
|
|
|
Py_XDECREF(this->m_PyWizard);
|
2012-05-05 22:18:47 +02:00
|
|
|
}
|
|
|
|
|
2012-05-09 19:37:25 +02:00
|
|
|
PyObject* PYTHON_FOOTPRINT_WIZARD::CallMethod(const char* aMethod, PyObject *aArglist)
|
2012-05-05 22:18:47 +02:00
|
|
|
{
|
|
|
|
PyObject *pFunc;
|
|
|
|
|
|
|
|
/* pFunc is a new reference to the desired method */
|
2012-05-09 19:37:25 +02:00
|
|
|
pFunc = PyObject_GetAttrString(this->m_PyWizard, aMethod);
|
2012-05-05 22:18:47 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-05-09 19:37:25 +02:00
|
|
|
wxString PYTHON_FOOTPRINT_WIZARD::CallRetStrMethod(const char* aMethod, PyObject *aArglist)
|
2012-05-05 22:18:47 +02:00
|
|
|
{
|
|
|
|
wxString ret;
|
2012-05-10 08:44:08 +02:00
|
|
|
|
|
|
|
ret.Clear();
|
2012-05-05 22:18:47 +02:00
|
|
|
PyObject *result = CallMethod(aMethod,aArglist);
|
|
|
|
if (result)
|
|
|
|
{
|
|
|
|
const char *str_res = PyString_AsString(result);
|
|
|
|
ret = wxString::FromUTF8(str_res);
|
|
|
|
Py_DECREF(result);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2012-05-10 01:04:08 +02:00
|
|
|
|
|
|
|
wxArrayString PYTHON_FOOTPRINT_WIZARD::CallRetArrayStrMethod
|
|
|
|
(const char *aMethod, PyObject *aArglist)
|
|
|
|
{
|
2012-05-10 08:44:08 +02:00
|
|
|
PyObject *result, *element;
|
|
|
|
wxArrayString ret;
|
|
|
|
wxString str_item;
|
|
|
|
|
|
|
|
result = CallMethod(aMethod,aArglist);
|
|
|
|
|
|
|
|
if (result)
|
|
|
|
{
|
|
|
|
if (!PyList_Check(result))
|
|
|
|
{
|
|
|
|
Py_DECREF(result);
|
|
|
|
ret.Add(wxT("PYTHON_FOOTPRINT_WIZARD::CallRetArrayStrMethod, "
|
|
|
|
"result is not a list"),1);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
int list_size = PyList_Size(result);
|
|
|
|
|
|
|
|
for (int n=0;n<list_size;n++)
|
|
|
|
{
|
|
|
|
element = PyList_GetItem(result,n);
|
2012-05-10 01:04:08 +02:00
|
|
|
|
2012-05-10 08:44:08 +02:00
|
|
|
const char *str_res = PyString_AsString(element);
|
|
|
|
str_item = wxString::FromUTF8(str_res);
|
|
|
|
ret.Add(str_item,1);
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_DECREF(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
2012-05-10 01:04:08 +02:00
|
|
|
}
|
|
|
|
|
2012-05-09 19:37:25 +02:00
|
|
|
wxString PYTHON_FOOTPRINT_WIZARD::GetName()
|
2012-05-05 22:18:47 +02:00
|
|
|
{
|
|
|
|
return CallRetStrMethod("GetName");
|
|
|
|
}
|
|
|
|
|
2012-05-09 19:37:25 +02:00
|
|
|
wxString PYTHON_FOOTPRINT_WIZARD::GetImage()
|
2012-05-05 22:18:47 +02:00
|
|
|
{
|
|
|
|
return CallRetStrMethod("GetImage");
|
|
|
|
}
|
|
|
|
|
2012-05-09 19:37:25 +02:00
|
|
|
wxString PYTHON_FOOTPRINT_WIZARD::GetDescription()
|
2012-05-05 22:18:47 +02:00
|
|
|
{
|
|
|
|
return CallRetStrMethod("GetDescription");
|
|
|
|
}
|
|
|
|
|
2012-05-09 19:37:25 +02:00
|
|
|
int PYTHON_FOOTPRINT_WIZARD::GetNumParameterPages()
|
2012-05-05 22:18:47 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-05-09 19:37:25 +02:00
|
|
|
wxString PYTHON_FOOTPRINT_WIZARD::GetParameterPageName(int aPage)
|
2012-05-05 22:18:47 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-05-09 19:37:25 +02:00
|
|
|
wxArrayString PYTHON_FOOTPRINT_WIZARD::GetParameterNames(int aPage)
|
2012-05-05 22:18:47 +02:00
|
|
|
{
|
2012-05-10 08:44:08 +02:00
|
|
|
|
2012-05-10 01:04:08 +02:00
|
|
|
PyObject *arglist;
|
2012-05-10 08:44:08 +02:00
|
|
|
wxArrayString ret;
|
2012-05-10 01:04:08 +02:00
|
|
|
|
|
|
|
arglist = Py_BuildValue("(i)", aPage);
|
2012-05-10 08:44:08 +02:00
|
|
|
ret = CallRetArrayStrMethod("GetParameterNames",arglist);
|
2012-05-10 01:04:08 +02:00
|
|
|
Py_DECREF(arglist);
|
2012-07-23 00:23:17 +02:00
|
|
|
|
|
|
|
for (unsigned i=0;i<ret.GetCount();i++)
|
|
|
|
{
|
|
|
|
wxString rest;
|
|
|
|
wxString item = ret[i];
|
|
|
|
if (item.StartsWith(wxT("*"),&rest))
|
|
|
|
{
|
|
|
|
ret[i]=rest;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxArrayString PYTHON_FOOTPRINT_WIZARD::GetParameterTypes(int aPage)
|
|
|
|
{
|
|
|
|
|
|
|
|
PyObject *arglist;
|
|
|
|
wxArrayString ret;
|
|
|
|
|
|
|
|
arglist = Py_BuildValue("(i)", aPage);
|
|
|
|
ret = CallRetArrayStrMethod("GetParameterNames",arglist);
|
|
|
|
Py_DECREF(arglist);
|
|
|
|
|
|
|
|
for (unsigned i=0;i<ret.GetCount();i++)
|
|
|
|
{
|
|
|
|
wxString rest;
|
|
|
|
wxString item = ret[i];
|
|
|
|
if (item.StartsWith(wxT("*"),&rest))
|
|
|
|
{
|
|
|
|
ret[i]=wxT("UNITS"); /* units */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret[i]=wxT("IU"); /* internal units */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-10 01:04:08 +02:00
|
|
|
|
2012-05-10 08:44:08 +02:00
|
|
|
return ret;
|
2012-05-05 22:18:47 +02:00
|
|
|
}
|
|
|
|
|
2012-07-23 00:23:17 +02:00
|
|
|
|
|
|
|
|
2012-05-09 19:37:25 +02:00
|
|
|
wxArrayString PYTHON_FOOTPRINT_WIZARD::GetParameterValues(int aPage)
|
2012-05-05 22:18:47 +02:00
|
|
|
{
|
2012-05-10 10:53:05 +02:00
|
|
|
PyObject *arglist;
|
|
|
|
wxArrayString ret;
|
|
|
|
|
|
|
|
arglist = Py_BuildValue("(i)", aPage);
|
|
|
|
ret = CallRetArrayStrMethod("GetParameterValues",arglist);
|
|
|
|
Py_DECREF(arglist);
|
|
|
|
|
|
|
|
return ret;
|
2012-05-05 22:18:47 +02:00
|
|
|
}
|
|
|
|
|
2012-05-16 11:35:18 +02:00
|
|
|
wxArrayString PYTHON_FOOTPRINT_WIZARD::GetParameterErrors(int aPage)
|
2012-05-05 22:18:47 +02:00
|
|
|
{
|
2012-05-16 11:35:18 +02:00
|
|
|
PyObject *arglist;
|
|
|
|
wxArrayString ret;
|
|
|
|
|
|
|
|
arglist = Py_BuildValue("(i)", aPage);
|
|
|
|
ret = CallRetArrayStrMethod("GetParameterErrors",arglist);
|
|
|
|
Py_DECREF(arglist);
|
|
|
|
|
2012-05-05 22:18:47 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-05-16 11:35:18 +02:00
|
|
|
wxString PYTHON_FOOTPRINT_WIZARD::SetParameterValues(int aPage,wxArrayString& aValues)
|
|
|
|
{
|
|
|
|
|
|
|
|
int len = aValues.size();
|
|
|
|
PyObject *py_list;
|
|
|
|
|
|
|
|
py_list = PyList_New(len);
|
|
|
|
|
|
|
|
for (int i=0;i<len;i++)
|
|
|
|
{
|
|
|
|
wxString str = aValues[i];
|
|
|
|
PyObject *py_str = PyString_FromString((const char*)str.mb_str());
|
|
|
|
PyList_SetItem(py_list, i, py_str);
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *arglist;
|
|
|
|
|
|
|
|
arglist = Py_BuildValue("(i,O)", aPage,py_list);
|
|
|
|
wxString res = CallRetStrMethod("SetParameterValues",arglist);
|
|
|
|
Py_DECREF(arglist);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2012-05-10 10:53:05 +02:00
|
|
|
/* this is a SWIG function declaration -from module.i*/
|
|
|
|
MODULE *PyModule_to_MODULE(PyObject *obj0);
|
|
|
|
|
2012-05-09 19:37:25 +02:00
|
|
|
MODULE *PYTHON_FOOTPRINT_WIZARD::GetModule()
|
2012-05-05 22:18:47 +02:00
|
|
|
{
|
2012-05-10 10:53:05 +02:00
|
|
|
PyObject *result, *obj;
|
|
|
|
result = CallMethod("GetModule",NULL);
|
|
|
|
if (!result) return NULL;
|
|
|
|
|
2012-05-16 11:35:18 +02:00
|
|
|
|
2012-05-10 10:53:05 +02:00
|
|
|
obj = PyObject_GetAttrString(result, "this");
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
{
|
2012-05-16 11:35:18 +02:00
|
|
|
/*
|
2012-05-10 10:53:05 +02:00
|
|
|
PyObject *t, *v, *b;
|
|
|
|
PyErr_Fetch(&t, &v, &b);
|
|
|
|
printf ("calling GetModule()\n");
|
|
|
|
printf ("Exception: %s\n",PyString_AsString(PyObject_Str(v)));
|
|
|
|
printf (" : %s\n",PyString_AsString(PyObject_Str(b)));
|
2012-05-16 11:35:18 +02:00
|
|
|
*/
|
|
|
|
PyErr_Print();
|
2012-05-10 10:53:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return PyModule_to_MODULE(obj);
|
2012-05-05 22:18:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-09 19:37:25 +02:00
|
|
|
void PYTHON_FOOTPRINT_WIZARDS::register_wizard(PyObject* aPyWizard)
|
2012-05-05 22:18:47 +02:00
|
|
|
{
|
2012-05-09 19:37:25 +02:00
|
|
|
PYTHON_FOOTPRINT_WIZARD *fw;
|
2012-05-05 22:18:47 +02:00
|
|
|
|
2012-05-09 19:37:25 +02:00
|
|
|
fw = new PYTHON_FOOTPRINT_WIZARD(aPyWizard);
|
2012-05-05 22:18:47 +02:00
|
|
|
|
|
|
|
printf("Registered python footprint wizard '%s'\n",
|
2012-05-09 19:37:25 +02:00
|
|
|
(const char*)fw->GetName().mb_str()
|
|
|
|
);
|
|
|
|
|
|
|
|
// this get the wizard registered in the common
|
|
|
|
// FOOTPRINT_WIZARDS class
|
2012-05-05 22:18:47 +02:00
|
|
|
|
2012-05-09 19:37:25 +02:00
|
|
|
fw->register_wizard();
|
|
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* just to test if it works correctly */
|
2012-05-05 22:18:47 +02:00
|
|
|
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());
|
|
|
|
}
|
2012-05-09 19:37:25 +02:00
|
|
|
#endif
|
2012-05-05 22:18:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|