From d311915f9da96c93ebfe0288dc66baa6b3c1d20d Mon Sep 17 00:00:00 2001 From: "Salvador E. Tropea" Date: Sat, 18 Feb 2023 21:41:56 -0300 Subject: [PATCH] Fix the CONNECTIVITY_DATA::GetNetItems Python API - The C++ member now uses std::vector - The Python side can use a single value or a list, like in v6. The std::vector translation is based on the v6 KICAD_T[] translation. --- pcbnew/connectivity/connectivity_data.cpp | 3 ++- pcbnew/connectivity/connectivity_data.h | 3 ++- pcbnew/python/swig/typeinfo.i | 29 +++++++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/pcbnew/connectivity/connectivity_data.cpp b/pcbnew/connectivity/connectivity_data.cpp index 3f4ad79247..c08216042c 100644 --- a/pcbnew/connectivity/connectivity_data.cpp +++ b/pcbnew/connectivity/connectivity_data.cpp @@ -556,8 +556,9 @@ CONNECTIVITY_DATA::GetConnectedItems( const BOARD_CONNECTED_ITEM *aItem, } +// We don't use std::initializer_list& to allow the use from Python const std::vector -CONNECTIVITY_DATA::GetNetItems( int aNetCode, const std::initializer_list& aTypes ) const +CONNECTIVITY_DATA::GetNetItems( int aNetCode, const std::vector& aTypes ) const { std::vector items; items.reserve( 32 ); diff --git a/pcbnew/connectivity/connectivity_data.h b/pcbnew/connectivity/connectivity_data.h index ab0b09f11c..3ffa14a31a 100644 --- a/pcbnew/connectivity/connectivity_data.h +++ b/pcbnew/connectivity/connectivity_data.h @@ -269,8 +269,9 @@ public: * @param aNetCode is the net code. * @param aTypes allows one to filter by item types. */ + // We don't use std::initializer_list& to allow the use from Python const std::vector - GetNetItems( int aNetCode, const std::initializer_list& aTypes ) const; + GetNetItems( int aNetCode, const std::vector& aTypes ) const; void BlockRatsnestItems( const std::vector& aItems ); diff --git a/pcbnew/python/swig/typeinfo.i b/pcbnew/python/swig/typeinfo.i index af743f77fa..88c27eee65 100644 --- a/pcbnew/python/swig/typeinfo.i +++ b/pcbnew/python/swig/typeinfo.i @@ -29,5 +29,34 @@ #include %} +// Methods like CONNECTIVITY_DATA::GetNetItems take an std::vector +// 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) std::vector< KICAD_T,std::allocator< KICAD_T > > const & ( std::vector vec ) { + $1 = &vec; + // Try with a single element + int value; + int ecode = SWIG_AsVal_int( $input, &value ); + + if ( SWIG_IsOK( ecode ) ) { + vec.push_back( static_cast( value ) ); + } else if ( PySequence_Check( $input ) ) { // Now try with a sequence + int elements = PySequence_Size( $input ); + for(int i=0; i( value ) ); + } + } else { + SWIG_exception_fail( SWIG_ArgError( ecode ), + "expecting KICAD_T enum value" ); + } + }