From d16a5c1d6c8cf846df7c2bcaf8806f9474e26ade Mon Sep 17 00:00:00 2001 From: Miles McCoo Date: Wed, 15 Nov 2017 14:16:21 +0100 Subject: [PATCH] Changes to python interface enabling net->pads access. Added new file connectivity.i to expose CONNECTIVITY_DATA to python. enables access to d_pads from nets. Added typemap to board_connected_item.i. since board_connected_item doesn't use virtual inheritance, when returning a std::list of these items, the typemap populates the return list with the child types. This enables python scripts to use the full interface of those classes (pad, track, zone) Added typeinfo.i to enable passing a list of KICAD_T values to methods GetNetItems. This list acts as a query filter. typeinfo.i is included from pcbnew.i right after kicad.i (instead of in board.i like the others) typeinfo.h is already being included kicad.i so to ensure the typemap(s) are properly applied I put them next to each other. The two new files (typeinfo.i and connectivity.i) were added to pcbnew's CMakeLists.txt as dependencies. --- pcbnew/CMakeLists.txt | 2 + pcbnew/swig/board.i | 3 +- pcbnew/swig/board_connected_item.i | 67 +++++++++++++++++++++++++++++ pcbnew/swig/connectivity.i | 39 +++++++++++++++++ pcbnew/swig/pcbnew.i | 5 ++- pcbnew/swig/typeinfo.i | 69 ++++++++++++++++++++++++++++++ 6 files changed, 183 insertions(+), 2 deletions(-) create mode 100644 pcbnew/swig/connectivity.i create mode 100644 pcbnew/swig/typeinfo.i diff --git a/pcbnew/CMakeLists.txt b/pcbnew/CMakeLists.txt index 795c7ed667..aa1886f250 100644 --- a/pcbnew/CMakeLists.txt +++ b/pcbnew/CMakeLists.txt @@ -438,6 +438,7 @@ if( KICAD_SCRIPTING ) # Generate pcbnew.py and pcbnew_wrap.cxx using swig DEPENDS swig/board_design_settings.i DEPENDS swig/board_item.i DEPENDS swig/board_item_container.i + DEPENDS swig/connectivity.i DEPENDS swig/dimension.i DEPENDS swig/drawsegment.i DEPENDS swig/edge_mod.i @@ -452,6 +453,7 @@ if( KICAD_SCRIPTING ) # Generate pcbnew.py and pcbnew_wrap.cxx using swig DEPENDS swig/text_mod.i DEPENDS swig/track.i DEPENDS swig/units.i + DEPENDS swig/typeinfo.i DEPENDS swig/zone.i DEPENDS swig/zone_settings.i diff --git a/pcbnew/swig/board.i b/pcbnew/swig/board.i index b9794f1644..cc337bb982 100644 --- a/pcbnew/swig/board.i +++ b/pcbnew/swig/board.i @@ -3,7 +3,7 @@ * * Copyright (C) 2012 NBEE Embedded Systems, Miguel Angel Ajo * Copyright (C) 2016 SoftPLC Corporation, Dick Hollenbeck - * Copyright (C) 1992-2012 KiCad Developers, see AUTHORS.txt for contributors. + * Copyright (C) 1992-2017 KiCad Developers, see AUTHORS.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 @@ -48,6 +48,7 @@ HANDLE_EXCEPTIONS(BOARD::TracksInNetBetweenPoints) %include board_item_container.i %include board_connected_item.i %include board_design_settings.i +%include connectivity.i %include pad.i %include track.i %include zone.i diff --git a/pcbnew/swig/board_connected_item.i b/pcbnew/swig/board_connected_item.i index 9f3e49b8d3..87c15a7688 100644 --- a/pcbnew/swig/board_connected_item.i +++ b/pcbnew/swig/board_connected_item.i @@ -1,3 +1,27 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 1992-2017 KiCad Developers, see AUTHORS.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 + */ + + %include class_board_connected_item.h @@ -5,3 +29,46 @@ #include %} +%typemap(out) std::list { + std::list list = $1; + std::list::const_iterator iter; + + PyObject * retval = $result = PyList_New(0); + + for( iter=list.begin(); iter!=list.end(); iter++ ) { + BOARD_CONNECTED_ITEM* aItem = *iter; + PyObject* obj = 0x0; + + switch( aItem->Type() ) { + case PCB_PAD_T: + obj = SWIG_NewPointerObj( SWIG_as_voidptr(aItem), + SWIGTYPE_p_D_PAD, + 0 | 0 ); + break; + + case PCB_TRACE_T: + case PCB_VIA_T: + obj = SWIG_NewPointerObj( SWIG_as_voidptr(aItem), + SWIGTYPE_p_TRACK, + 0 | 0 ); + break; + + case PCB_ZONE_AREA_T: + obj = SWIG_NewPointerObj( SWIG_as_voidptr(aItem), + SWIGTYPE_p_ZONE_CONTAINER, + 0 | 0 ); + break; + + default: + obj = SWIG_NewPointerObj( SWIG_as_voidptr(aItem), + SWIGTYPE_p_BOARD_CONNECTED_ITEM, + 0 | 0 ); + break; + } + + assert( obj ); + PyList_Append (retval, obj ); + Py_DECREF( obj ); + } + } + diff --git a/pcbnew/swig/connectivity.i b/pcbnew/swig/connectivity.i new file mode 100644 index 0000000000..69b8f4ddb0 --- /dev/null +++ b/pcbnew/swig/connectivity.i @@ -0,0 +1,39 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 1992-2017 KiCad Developers, see AUTHORS.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 + */ + + +// when you ask board for its connectivity, you get a shared_ptr +%include + +// this shared_ptr line has to be before include connectivity_data.h. +%shared_ptr(CONNECTIVITY_DATA) + +%include connectivity_data.h + + + + +%{ +#include +%} + diff --git a/pcbnew/swig/pcbnew.i b/pcbnew/swig/pcbnew.i index 6d6e6f768b..838982c074 100644 --- a/pcbnew/swig/pcbnew.i +++ b/pcbnew/swig/pcbnew.i @@ -2,7 +2,7 @@ * This program source code file is part of KiCad, a free EDA CAD application. * * Copyright (C) 2012 NBEE Embedded Systems, Miguel Angel Ajo - * Copyright (C) 1992-2012 KiCad Developers, see AUTHORS.txt for contributors. + * Copyright (C) 1992-2017 KiCad Developers, see AUTHORS.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 @@ -38,6 +38,9 @@ %include kicad.i +// mostly for KICAD_T +%include typeinfo.i + %include %{ diff --git a/pcbnew/swig/typeinfo.i b/pcbnew/swig/typeinfo.i new file mode 100644 index 0000000000..f04d5f77f1 --- /dev/null +++ b/pcbnew/swig/typeinfo.i @@ -0,0 +1,69 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 1992-2017 KiCad Developers, see AUTHORS.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 + */ + + + +%include core/typeinfo.h + +%{ +#include +%} + +// methods like CONNECTIVITY_DATA::GetNetItems take an array of KICAD_T values, +// terminated by EOT. for example, class_board.cpp calls the method with this argument: +// const KICAD_T types[] = { PCB_PAD_T, PCB_ZONE_AREA_T, EOT }; +// this typemap allows any of the following: +// conn = board.GetConnectivity() +// conn.GetNetItems(net.GetNet(), (pcbnew.PCB_PAD_T, pcbnew.PCB_TRACE_T)) +// conn.GetNetItems(net.GetNet(), [pcbnew.PCB_PAD_T, pcbnew.PCB_TRACE_T]) +// conn.GetNetItems(net.GetNet(), pcbnew.PCB_PAD_T) + + +%typemap(in) KICAD_T [] ( KICAD_T retval[5] ){ + retval[0] = EOT; + $1 = retval; + + int type; + int ecode = SWIG_AsVal_int( $input, &type ); + + if ( SWIG_IsOK( ecode ) ) { + retval[0] = static_cast( type ); + retval[1] = EOT; + } else if ( PySequence_Check( $input ) ) { + // compare less than because we still need space for the EOT terminator + assert( PySequence_Size( $input ) <= + static_cast( sizeof( retval )/sizeof( KICAD_T ) ) ); + for(int i=0; i( type ); + retval[i+1] = EOT; + } + } else { + SWIG_exception_fail( SWIG_ArgError( ecode ), + "expecting KICAD_T enum value" ); + } + }