Commit 562d2461 authored by Miguel Angel Ajo's avatar Miguel Angel Ajo
Browse files

wxString, wxPoint and wxChar wrappers

parent 3dacab96
Loading
Loading
Loading
Loading
+2 −0
Original line number Original line Diff line number Diff line
@@ -18,6 +18,7 @@ include_directories(
    ${Boost_INCLUDE_DIR}
    ${Boost_INCLUDE_DIR}
    ../polygon
    ../polygon
    ../common/dialogs
    ../common/dialogs
    ./scripting
    ${INC_AFTER}
    ${INC_AFTER}
    )
    )


@@ -224,6 +225,7 @@ set(PCBNEW_COMMON_SRCS
set(PCBNEW_SCRIPTING_SRCS
set(PCBNEW_SCRIPTING_SRCS
   kicad_wrap.cxx
   kicad_wrap.cxx
   pcbnew_wrap.cxx
   pcbnew_wrap.cxx
   scripting/helpers.cpp
   )
   )


##
##
+119 −0
Original line number Original line Diff line number Diff line
#include <Python.h>
#include <wx/intl.h>
#include <wx/string.h>

#define WX_DEFAULTENCODING_SIZE 64

static char wxPythonEncoding[WX_DEFAULTENCODING_SIZE] = "ascii";

wxString* newWxStringFromPy(PyObject* src) {

    bool must_unref_str=false;

    wxString* result = NULL;
    PyObject *obj=src;

#if wxUSE_UNICODE
    bool must_unref_obj=false;    
    // Unicode string to python unicode string 
    PyObject* uni_str = src;

    // if not an str or unicode, try to str(src)
    if (!PyString_Check(src) && !PyUnicode_Check(src)) 
    {
        obj = PyObject_Str(src);
	must_unref_obj = true;
        if (PyErr_Occurred()) return NULL;
    }

    if (PyString_Check(obj)) 
    {
        uni_str = PyUnicode_FromEncodedObject(obj, wxPythonEncoding, "strict");
        must_unref_str = true;
        if (PyErr_Occurred()) return NULL;
    }

    result = new wxString();
    size_t len = PyUnicode_GET_SIZE(uni_str);
    if (len) 
    {
        PyUnicode_AsWideChar((PyUnicodeObject*)uni_str,
                             wxStringBuffer(*result, len),len);
    }

    if (must_unref_str) Py_DECREF(uni_str);
    if (must_unref_obj) Py_DECREF(obj);
#else
    // normal string (or object) to normal python string 
    PyObject* str = src;

    if (PyUnicode_Check(src)) // if it's unicode convert to normal string
    {
        str = PyUnicode_AsEncodedString(src, wxPythonEncoding, "strict");
        if (PyErr_Occurred()) return NULL;
    }
    else if (!PyString_Check(src)) // if it's not a string, str(obj)
    {
        str = PyObject_Str(src);
 	must_unref_str = true;
        if (PyErr_Occurred()) return NULL;
    }

    // get the string pointer and size
    char* str_ptr; 
    Py_ssize_t str_size;
    PyString_AsStringAndSize(str, &str_ptr, &str_size);

    // build the wxString from our pointer / size
    result = new wxString(str_ptr, str_size);

    if (must_unref_str) Py_DECREF(str);
#endif 

    return result;
}


wxString Py2wxString(PyObject* src)
{
    wxString result;
    wxString* resPtr = newWxStringFromPy(src);    
    
    // In case of exception clear it and return an empty string  
    if (resPtr==NULL) 
    {
	PyErr_Clear();
	return wxEmptyString;
    }
    
    result = *resPtr;

    delete resPtr;    

    return result;
}


PyObject* wx2PyString(const wxString& src)
{
    PyObject* str;
#if wxUSE_UNICODE
    str = PyUnicode_FromWideChar(src.c_str(), src.Len());
#else
    str = PyString_FromStringAndSize(src.c_str(), src.Len());
#endif
    return str;
}



void wxSetDefaultPyEncoding(const char* encoding)
{
    strncpy(wxPythonEncoding, encoding, WX_DEFAULTENCODING_SIZE);
}

const char* wxGetDefaultPyEncoding()
{
    return wxPythonEncoding;
}
+1 −0
Original line number Original line Diff line number Diff line
@@ -17,6 +17,7 @@
	#include <dlist.h>
	#include <dlist.h>
	#include <base_struct.h>
	#include <base_struct.h>
	#include <common.h>
	#include <common.h>
	#include <wx_helpers.h>


%}
%}


+1 −0
Original line number Original line Diff line number Diff line
@@ -8,6 +8,7 @@
	#include <class_track.h>	
	#include <class_track.h>	
	#include <class_pad.h>
	#include <class_pad.h>
	#include <dlist.h>
	#include <dlist.h>
        #include <wx_helpers.h>




	BOARD *GetBoard();
	BOARD *GetBoard();
+14 −0
Original line number Original line Diff line number Diff line
pcb = pcbnew.GetBoard()

m = pcb.m_Modules.item()

while m:
	print m.GetReference(),"(",m.GetValue(),") at ", m.GetPosition()
        m.SetValue("pepe")
        p  = m.m_Pads.item()
	while p:
		print "    pad",p.GetPadName(), "at",p.GetPosition()
		p = p.Next()

	m = m.Next()		
Loading