SWIG mappings for all shapes

This commit is contained in:
qu1ck 2022-01-13 20:55:50 -08:00 committed by Seth Hillbrand
parent 567168fffc
commit a041492736
6 changed files with 165 additions and 19 deletions

View File

@ -143,29 +143,12 @@ typedef long time_t;
// KiCad plugin handling
%include "kicadplugins.i"
%shared_ptr(SHAPE)
%shared_ptr(SHAPE_BASE)
%shared_ptr(SHAPE_POLY_SET)
%shared_ptr(SHAPE_LINE_CHAIN_BASE)
%shared_ptr(SHAPE_LINE_CHAIN)
#include <geometry/shape.h>
%include <geometry/shape.h>
// Contains VECTOR2I
%include math.i
%template(VECTOR_VECTOR2I) std::vector<VECTOR2I>;
#include <geometry/eda_angle.h>
%include <geometry/eda_angle.h>
// ignore warning from nested classes
#pragma SWIG nowarn=325
#include <geometry/shape_line_chain.h>
%include <geometry/shape_line_chain.h>
#include <geometry/shape_poly_set.h>
%include <geometry/shape_poly_set.h>
// Shapes/geometry
%include shape.i
// ignore warning relative to operator = and operator ++:
#pragma SWIG nowarn=362,383

View File

@ -31,6 +31,7 @@
%rename(getWxPoint) operator wxPoint;
%rename(getWxSize) operator wxSize;
#include <math/vector2d.h>
%include <math/vector2d.h>
%template(VECTOR2I) VECTOR2<int>;

153
common/swig/shape.i Normal file
View File

@ -0,0 +1,153 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2022 Andrew Lutsenko, anlutsenko at gmail dot com
* Copyright (C) 1992-2022 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
*/
/**
* @file shape.i
* @brief General wrappers for geometry/shape headers
*/
%include <std_shared_ptr.i>
%include <std_vector.i>
%{
#include <geometry/seg.h>
#include <geometry/shape.h>
#include <geometry/shape_arc.h>
#include <geometry/shape_circle.h>
#include <geometry/shape_compound.h>
#include <geometry/shape_line_chain.h>
#include <geometry/shape_poly_set.h>
#include <geometry/shape_simple.h>
#include <geometry/shape_rect.h>
#include <geometry/shape_segment.h>
#include <geometry/shape_simple.h>
%}
%shared_ptr(SHAPE)
%shared_ptr(SHAPE_BASE)
%shared_ptr(SHAPE_COMPOUND)
%shared_ptr(SHAPE_POLY_SET)
%shared_ptr(SHAPE_LINE_CHAIN_BASE)
%shared_ptr(SHAPE_LINE_CHAIN)
%shared_ptr(SHAPE_SIMPLE)
%shared_ptr(SHAPE_ARC)
%shared_ptr(SHAPE_CIRCLE)
%shared_ptr(SHAPE_RECT)
%shared_ptr(SHAPE_SEGMENT)
// ignore warning from nested classes
#pragma SWIG nowarn=325
%include <geometry/seg.h>
%include <geometry/shape.h>
%include <geometry/shape_arc.h>
%include <geometry/shape_circle.h>
%include <geometry/shape_compound.h>
%include <geometry/shape_line_chain.h>
%include <geometry/shape_poly_set.h>
%include <geometry/shape_rect.h>
%include <geometry/shape_segment.h>
%include <geometry/shape_simple.h>
%template(VECTOR_SHAPEPTR) std::vector<std::shared_ptr<SHAPE>>;
static std::shared_ptr<SHAPE_ARC> Cast_to_SHAPE_ARC( std::shared_ptr<SHAPE> self );
static std::shared_ptr<SHAPE_CIRCLE> Cast_to_SHAPE_CIRCLE( std::shared_ptr<SHAPE> self );
static std::shared_ptr<SHAPE_COMPOUND> Cast_to_SHAPE_COMPOUND( std::shared_ptr<SHAPE> self );
static std::shared_ptr<SHAPE_LINE_CHAIN> Cast_to_SHAPE_LINE_CHAIN( std::shared_ptr<SHAPE> self );
static std::shared_ptr<SHAPE_POLY_SET> Cast_to_SHAPE_POLY_SET( std::shared_ptr<SHAPE> self );
static std::shared_ptr<SHAPE_RECT> Cast_to_SHAPE_RECT( std::shared_ptr<SHAPE> self );
static std::shared_ptr<SHAPE_SEGMENT> Cast_to_SHAPE_SEGMENT( std::shared_ptr<SHAPE> self );
static std::shared_ptr<SHAPE_SIMPLE> Cast_to_SHAPE_SIMPLE( std::shared_ptr<SHAPE> self );
%{
static std::shared_ptr<SHAPE_ARC> Cast_to_SHAPE_ARC( std::shared_ptr<SHAPE> self ) { return std::dynamic_pointer_cast<SHAPE_ARC>( self ); }
static std::shared_ptr<SHAPE_CIRCLE> Cast_to_SHAPE_CIRCLE( std::shared_ptr<SHAPE> self ) { return std::dynamic_pointer_cast<SHAPE_CIRCLE>( self ); }
static std::shared_ptr<SHAPE_COMPOUND> Cast_to_SHAPE_COMPOUND( std::shared_ptr<SHAPE> self ) { return std::dynamic_pointer_cast<SHAPE_COMPOUND>( self ); }
static std::shared_ptr<SHAPE_LINE_CHAIN> Cast_to_SHAPE_LINE_CHAIN( std::shared_ptr<SHAPE> self ) { return std::dynamic_pointer_cast<SHAPE_LINE_CHAIN>( self ); }
static std::shared_ptr<SHAPE_POLY_SET> Cast_to_SHAPE_POLY_SET( std::shared_ptr<SHAPE> self ) { return std::dynamic_pointer_cast<SHAPE_POLY_SET>( self ); }
static std::shared_ptr<SHAPE_RECT> Cast_to_SHAPE_RECT( std::shared_ptr<SHAPE> self ) { return std::dynamic_pointer_cast<SHAPE_RECT>( self ); }
static std::shared_ptr<SHAPE_SEGMENT> Cast_to_SHAPE_SEGMENT( std::shared_ptr<SHAPE> self ) { return std::dynamic_pointer_cast<SHAPE_SEGMENT>( self ); }
static std::shared_ptr<SHAPE_SIMPLE> Cast_to_SHAPE_SIMPLE( std::shared_ptr<SHAPE> self ) { return std::dynamic_pointer_cast<SHAPE_SIMPLE>( self ); }
%}
%extend SHAPE
{
%pythoncode
%{
def Cast(self):
shape_type = SHAPE_TYPE_asString(self.Type())
if shape_type == "SH_ARC":
return Cast_to_SHAPE_ARC(self)
elif shape_type == "SH_CIRCLE":
return Cast_to_SHAPE_CIRCLE(self)
elif shape_type == "SH_COMPOUND":
return Cast_to_SHAPE_COMPOUND(self)
elif shape_type == "SH_LINE_CHAIN":
return Cast_to_SHAPE_LINE_CHAIN(self)
elif shape_type == "SH_POLY_SET":
return Cast_to_SHAPE_POLY_SET(self)
elif shape_type == "SH_RECT":
return Cast_to_SHAPE_RECT(self)
elif shape_type == "SH_SEGMENT":
return Cast_to_SHAPE_SEGMENT(self)
elif shape_type == "SH_SIMPLE":
return Cast_to_SHAPE_SIMPLE(self)
else:
raise TypeError("Unsupported shape class: %s" % shape_type)
%}
}
%extend SHAPE_COMPOUND
{
std::vector<std::shared_ptr<SHAPE> > GetSubshapes()
{
std::vector<std::shared_ptr<SHAPE>> result;
std::for_each( $self->Shapes().begin(), $self->Shapes().end(),
[&]( SHAPE* shape )
{
// creating empty shared_ptr with non-null pointer
// so that it doesn't "own" the raw pointer
result.emplace_back( std::shared_ptr<SHAPE>(), shape );
} );
return result;
}
}
%extend std::vector<std::shared_ptr<SHAPE>>
{
%pythoncode
%{
def __iter__(self):
it = self.iterator()
try:
while True:
item = it.next() # throws StopIteration when iterator reached the end.
yield item.Cast()
except StopIteration:
return
%}
}

View File

@ -489,6 +489,7 @@ add_custom_command( OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/pcbnew_wrap.cxx
DEPENDS ${CMAKE_SOURCE_DIR}/common/swig/kicad.i
DEPENDS ${CMAKE_SOURCE_DIR}/common/swig/wx.i
DEPENDS ${CMAKE_SOURCE_DIR}/common/swig/math.i
DEPENDS ${CMAKE_SOURCE_DIR}/common/swig/shape.i
DEPENDS ${CMAKE_SOURCE_DIR}/common/swig/ki_exception.i
DEPENDS ${CMAKE_SOURCE_DIR}/common/swig/netclass.i
DEPENDS ${CMAKE_SOURCE_DIR}/scripting/kicadplugins.i

View File

@ -48,6 +48,7 @@ class PCB_DIM_ALIGNED;
class PCB_DIM_ORTHOGONAL;
class PCB_DIM_LEADER;
class PCB_DIM_CENTER;
class PCB_DIM_RADIAL;
class FOOTPRINT;
class PCB_GROUP;
class FP_TEXT;
@ -76,6 +77,7 @@ static PCB_DIM_ALIGNED* Cast_to_PCB_DIM_ALIGNED( BOARD_ITEM* );
static PCB_DIM_ORTHOGONAL* Cast_to_PCB_DIM_ORTHOGONAL( BOARD_ITEM* );
static PCB_DIM_LEADER* Cast_to_PCB_DIM_LEADER( BOARD_ITEM* );
static PCB_DIM_CENTER* Cast_to_PCB_DIM_CENTER( BOARD_ITEM* );
static PCB_DIM_RADIAL* Cast_to_PCB_DIM_RADIAL( BOARD_ITEM* );
static FOOTPRINT* Cast_to_FOOTPRINT( BOARD_ITEM* );
static PCB_GROUP* Cast_to_PCB_GROUP( BOARD_ITEM* );
static FP_TEXT* Cast_to_FP_TEXT( BOARD_ITEM* );
@ -104,6 +106,7 @@ static PCB_DIM_ALIGNED* Cast_to_PCB_DIM_ALIGNED( BOARD_ITEM* );
static PCB_DIM_ORTHOGONAL* Cast_to_PCB_DIM_ORTHOGONAL( BOARD_ITEM* );
static PCB_DIM_LEADER* Cast_to_PCB_DIM_LEADER( BOARD_ITEM* );
static PCB_DIM_CENTER* Cast_to_PCB_DIM_CENTER( BOARD_ITEM* );
static PCB_DIM_RADIAL* Cast_to_PCB_DIM_RADIAL( BOARD_ITEM* );
static FOOTPRINT* Cast_to_FOOTPRINT( BOARD_ITEM* );
static PCB_GROUP* Cast_to_PCB_GROUP( BOARD_ITEM* );
static FP_TEXT* Cast_to_FP_TEXT( BOARD_ITEM* );
@ -141,6 +144,8 @@ static PCB_TARGET* Cast_to_PCB_TARGET( BOARD_ITEM* );
return Cast_to_PCB_DIM_LEADER(self)
elif ct=="PCB_DIM_CENTER":
return Cast_to_PCB_DIM_CENTER(self)
elif ct=="PCB_DIM_RADIAL":
return Cast_to_PCB_DIM_RADIAL(self)
elif ct=="PCB_DIM_ORTHOGONAL":
return Cast_to_PCB_DIM_ORTHOGONAL(self)
elif ct=="PCB_SHAPE":
@ -201,6 +206,7 @@ static PCB_DIM_ALIGNED* Cast_to_PCB_DIM_ALIGNED( BOARD_ITEM* self ) {
static PCB_DIM_ORTHOGONAL* Cast_to_PCB_DIM_ORTHOGONAL( BOARD_ITEM* self ) { return dynamic_cast<PCB_DIM_ORTHOGONAL *>(self); }
static PCB_DIM_LEADER* Cast_to_PCB_DIM_LEADER( BOARD_ITEM* self ) { return dynamic_cast<PCB_DIM_LEADER *>(self); }
static PCB_DIM_CENTER* Cast_to_PCB_DIM_CENTER( BOARD_ITEM* self ) { return dynamic_cast<PCB_DIM_CENTER *>(self); }
static PCB_DIM_RADIAL* Cast_to_PCB_DIM_RADIAL( BOARD_ITEM* self ) { return dynamic_cast<PCB_DIM_RADIAL *>(self); }
static FOOTPRINT* Cast_to_FOOTPRINT( BOARD_ITEM* self ) { return dynamic_cast<FOOTPRINT*>(self); }
static PCB_GROUP* Cast_to_PCB_GROUP( BOARD_ITEM* self ) { return dynamic_cast<PCB_GROUP*>(self); }
static FP_TEXT* Cast_to_FP_TEXT( BOARD_ITEM* self ) { return dynamic_cast<FP_TEXT*>(self); }

View File

@ -1,9 +1,11 @@
%ignore EDA_SHAPE::getCenter;
%{
#include <geometry/eda_angle.h>
#include <eda_shape.h>
#include <pcb_shape.h>
%}
%include geometry/eda_angle.h
%include eda_shape.h
%include pcb_shape.h