Fix the CONNECTIVITY_DATA::GetNetItems Python API

- The C++ member now uses std::vector<KICAD_T>
- 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.
This commit is contained in:
Salvador E. Tropea 2023-02-18 21:41:56 -03:00 committed by Seth Hillbrand
parent 3a95826b42
commit d311915f9d
3 changed files with 33 additions and 2 deletions

View File

@ -556,8 +556,9 @@ CONNECTIVITY_DATA::GetConnectedItems( const BOARD_CONNECTED_ITEM *aItem,
}
// We don't use std::initializer_list<KICAD_T>& to allow the use from Python
const std::vector<BOARD_CONNECTED_ITEM*>
CONNECTIVITY_DATA::GetNetItems( int aNetCode, const std::initializer_list<KICAD_T>& aTypes ) const
CONNECTIVITY_DATA::GetNetItems( int aNetCode, const std::vector<KICAD_T>& aTypes ) const
{
std::vector<BOARD_CONNECTED_ITEM*> items;
items.reserve( 32 );

View File

@ -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<KICAD_T>& to allow the use from Python
const std::vector<BOARD_CONNECTED_ITEM*>
GetNetItems( int aNetCode, const std::initializer_list<KICAD_T>& aTypes ) const;
GetNetItems( int aNetCode, const std::vector<KICAD_T>& aTypes ) const;
void BlockRatsnestItems( const std::vector<BOARD_ITEM*>& aItems );

View File

@ -29,5 +29,34 @@
#include <core/typeinfo.h>
%}
// Methods like CONNECTIVITY_DATA::GetNetItems take an std::vector<KICAD_T>
// 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<KICAD_T> 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<KICAD_T>( value ) );
} else if ( PySequence_Check( $input ) ) { // Now try with a sequence
int elements = PySequence_Size( $input );
for(int i=0; i<elements; i++) {
int ecode = SWIG_AsVal_int( PySequence_GetItem( $input, i ), &value );
if ( !SWIG_IsOK( ecode ) ) {
SWIG_exception_fail( SWIG_ArgError( ecode ),
"expecting KICAD_T enum values" );
}
vec.push_back( static_cast<KICAD_T>( value ) );
}
} else {
SWIG_exception_fail( SWIG_ArgError( ecode ),
"expecting KICAD_T enum value" );
}
}