diff --git a/common/swig/kicad.i b/common/swig/kicad.i index a934630e4d..d9557638af 100644 --- a/common/swig/kicad.i +++ b/common/swig/kicad.i @@ -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 -%include - // Contains VECTOR2I %include math.i %template(VECTOR_VECTOR2I) std::vector; -#include -%include - -// ignore warning from nested classes -#pragma SWIG nowarn=325 -#include -%include - -#include -%include +// Shapes/geometry +%include shape.i // ignore warning relative to operator = and operator ++: #pragma SWIG nowarn=362,383 diff --git a/common/swig/math.i b/common/swig/math.i index 7b49fa7506..6e4752fbdd 100644 --- a/common/swig/math.i +++ b/common/swig/math.i @@ -31,6 +31,7 @@ %rename(getWxPoint) operator wxPoint; +%rename(getWxSize) operator wxSize; #include %include %template(VECTOR2I) VECTOR2; diff --git a/common/swig/shape.i b/common/swig/shape.i new file mode 100644 index 0000000000..88ee7029f2 --- /dev/null +++ b/common/swig/shape.i @@ -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 +%include + +%{ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +%} + +%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 +%include +%include +%include +%include +%include +%include +%include +%include +%include + +%template(VECTOR_SHAPEPTR) std::vector>; + +static std::shared_ptr Cast_to_SHAPE_ARC( std::shared_ptr self ); +static std::shared_ptr Cast_to_SHAPE_CIRCLE( std::shared_ptr self ); +static std::shared_ptr Cast_to_SHAPE_COMPOUND( std::shared_ptr self ); +static std::shared_ptr Cast_to_SHAPE_LINE_CHAIN( std::shared_ptr self ); +static std::shared_ptr Cast_to_SHAPE_POLY_SET( std::shared_ptr self ); +static std::shared_ptr Cast_to_SHAPE_RECT( std::shared_ptr self ); +static std::shared_ptr Cast_to_SHAPE_SEGMENT( std::shared_ptr self ); +static std::shared_ptr Cast_to_SHAPE_SIMPLE( std::shared_ptr self ); + +%{ +static std::shared_ptr Cast_to_SHAPE_ARC( std::shared_ptr self ) { return std::dynamic_pointer_cast( self ); } +static std::shared_ptr Cast_to_SHAPE_CIRCLE( std::shared_ptr self ) { return std::dynamic_pointer_cast( self ); } +static std::shared_ptr Cast_to_SHAPE_COMPOUND( std::shared_ptr self ) { return std::dynamic_pointer_cast( self ); } +static std::shared_ptr Cast_to_SHAPE_LINE_CHAIN( std::shared_ptr self ) { return std::dynamic_pointer_cast( self ); } +static std::shared_ptr Cast_to_SHAPE_POLY_SET( std::shared_ptr self ) { return std::dynamic_pointer_cast( self ); } +static std::shared_ptr Cast_to_SHAPE_RECT( std::shared_ptr self ) { return std::dynamic_pointer_cast( self ); } +static std::shared_ptr Cast_to_SHAPE_SEGMENT( std::shared_ptr self ) { return std::dynamic_pointer_cast( self ); } +static std::shared_ptr Cast_to_SHAPE_SIMPLE( std::shared_ptr self ) { return std::dynamic_pointer_cast( 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 > GetSubshapes() + { + std::vector> 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 ); + } ); + return result; + } +} + + +%extend std::vector> +{ + %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 + %} +} diff --git a/pcbnew/CMakeLists.txt b/pcbnew/CMakeLists.txt index feb682181d..2b705ea1c2 100644 --- a/pcbnew/CMakeLists.txt +++ b/pcbnew/CMakeLists.txt @@ -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 diff --git a/pcbnew/python/swig/board_item.i b/pcbnew/python/swig/board_item.i index 8e0433cae9..6310c010dd 100644 --- a/pcbnew/python/swig/board_item.i +++ b/pcbnew/python/swig/board_item.i @@ -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(self); } static PCB_DIM_LEADER* Cast_to_PCB_DIM_LEADER( BOARD_ITEM* self ) { return dynamic_cast(self); } static PCB_DIM_CENTER* Cast_to_PCB_DIM_CENTER( BOARD_ITEM* self ) { return dynamic_cast(self); } +static PCB_DIM_RADIAL* Cast_to_PCB_DIM_RADIAL( BOARD_ITEM* self ) { return dynamic_cast(self); } static FOOTPRINT* Cast_to_FOOTPRINT( BOARD_ITEM* self ) { return dynamic_cast(self); } static PCB_GROUP* Cast_to_PCB_GROUP( BOARD_ITEM* self ) { return dynamic_cast(self); } static FP_TEXT* Cast_to_FP_TEXT( BOARD_ITEM* self ) { return dynamic_cast(self); } diff --git a/pcbnew/python/swig/pcb_shape.i b/pcbnew/python/swig/pcb_shape.i index c03cbe434b..0a24153923 100644 --- a/pcbnew/python/swig/pcb_shape.i +++ b/pcbnew/python/swig/pcb_shape.i @@ -1,9 +1,11 @@ %ignore EDA_SHAPE::getCenter; %{ +#include #include #include %} +%include geometry/eda_angle.h %include eda_shape.h %include pcb_shape.h