From aff2d5c0d3e3b206704222243c92c41c49b97169 Mon Sep 17 00:00:00 2001 From: Jon Evans Date: Thu, 4 Apr 2024 18:55:58 -0400 Subject: [PATCH] Factor out SHAPE_LINE_CHAIN utilities --- common/api/api_utils.cpp | 52 ++++++++++++++++++++++++++++++ include/api/api_utils.h | 9 +++++- pcbnew/pcb_shape.cpp | 68 +++++----------------------------------- 3 files changed, 67 insertions(+), 62 deletions(-) diff --git a/common/api/api_utils.cpp b/common/api/api_utils.cpp index e25d575cab..cd83dde614 100644 --- a/common/api/api_utils.cpp +++ b/common/api/api_utils.cpp @@ -77,4 +77,56 @@ VECTOR2I UnpackVector2( const types::Vector2& aInput ) return VECTOR2I( aInput.x_nm(), aInput.y_nm() ); } + +void PackPolyLine( kiapi::common::types::PolyLine& aOutput, const SHAPE_LINE_CHAIN& aSlc ) +{ + for( int vertex = 0; vertex < aSlc.PointCount(); vertex = aSlc.NextShape( vertex ) ) + { + kiapi::common::types::PolyLineNode* node = aOutput.mutable_nodes()->Add(); + + if( aSlc.IsPtOnArc( vertex ) ) + { + const SHAPE_ARC& arc = aSlc.Arc( aSlc.ArcIndex( vertex ) ); + node->mutable_arc()->mutable_start()->set_x_nm( arc.GetP0().x ); + node->mutable_arc()->mutable_start()->set_y_nm( arc.GetP0().y ); + node->mutable_arc()->mutable_mid()->set_x_nm( arc.GetArcMid().x ); + node->mutable_arc()->mutable_mid()->set_y_nm( arc.GetArcMid().y ); + node->mutable_arc()->mutable_end()->set_x_nm( arc.GetP1().x ); + node->mutable_arc()->mutable_end()->set_y_nm( arc.GetP1().y ); + } + else + { + node->mutable_point()->set_x_nm( aSlc.CPoint( vertex ).x ); + node->mutable_point()->set_y_nm( aSlc.CPoint( vertex ).y ); + } + } + + aOutput.set_closed( aSlc.IsClosed() ); +} + + +SHAPE_LINE_CHAIN UnpackPolyLine( const kiapi::common::types::PolyLine& aInput ) +{ + SHAPE_LINE_CHAIN slc; + + for( const kiapi::common::types::PolyLineNode& node : aInput.nodes() ) + { + if( node.has_point() ) + { + slc.Append( VECTOR2I( node.point().x_nm(), node.point().y_nm() ) ); + } + else if( node.has_arc() ) + { + slc.Append( SHAPE_ARC( VECTOR2I( node.arc().start().x_nm(), node.arc().start().y_nm() ), + VECTOR2I( node.arc().mid().x_nm(), node.arc().mid().y_nm() ), + VECTOR2I( node.arc().end().x_nm(), node.arc().end().y_nm() ), + 0 /* don't care about width here */ ) ); + } + } + + slc.SetClosed( aInput.closed() ); + + return slc; +} + } // namespace kiapi::common diff --git a/include/api/api_utils.h b/include/api/api_utils.h index 38404a7d24..8abfb62527 100644 --- a/include/api/api_utils.h +++ b/include/api/api_utils.h @@ -22,14 +22,17 @@ #define KICAD_API_UTILS_H #include +#include #include #include #include -#include #include +#include #include +class SHAPE_LINE_CHAIN; + namespace kiapi::common { @@ -43,6 +46,10 @@ void PackVector2( kiapi::common::types::Vector2& aOutput, const VECTOR2I aInput VECTOR2I UnpackVector2( const types::Vector2& aInput ); +void PackPolyLine( kiapi::common::types::PolyLine& aOutput, const SHAPE_LINE_CHAIN& aSlc ); + +SHAPE_LINE_CHAIN UnpackPolyLine( const kiapi::common::types::PolyLine& aInput ); + } // namespace kiapi::common #endif //KICAD_API_UTILS_H diff --git a/pcbnew/pcb_shape.cpp b/pcbnew/pcb_shape.cpp index bb88535cb9..109a230389 100644 --- a/pcbnew/pcb_shape.cpp +++ b/pcbnew/pcb_shape.cpp @@ -61,37 +61,6 @@ PCB_SHAPE::~PCB_SHAPE() { } -// TODO: lift out -kiapi::common::types::PolyLine lineChainToProto( const SHAPE_LINE_CHAIN& aSlc ) -{ - kiapi::common::types::PolyLine msg; - - for( int vertex = 0; vertex < aSlc.PointCount(); vertex = aSlc.NextShape( vertex ) ) - { - kiapi::common::types::PolyLineNode* node = msg.mutable_nodes()->Add(); - - if( aSlc.IsPtOnArc( vertex ) ) - { - const SHAPE_ARC& arc = aSlc.Arc( aSlc.ArcIndex( vertex ) ); - node->mutable_arc()->mutable_start()->set_x_nm( arc.GetP0().x ); - node->mutable_arc()->mutable_start()->set_y_nm( arc.GetP0().y ); - node->mutable_arc()->mutable_mid()->set_x_nm( arc.GetArcMid().x ); - node->mutable_arc()->mutable_mid()->set_y_nm( arc.GetArcMid().y ); - node->mutable_arc()->mutable_end()->set_x_nm( arc.GetP1().x ); - node->mutable_arc()->mutable_end()->set_y_nm( arc.GetP1().y ); - } - else - { - node->mutable_point()->set_x_nm( aSlc.CPoint( vertex ).x ); - node->mutable_point()->set_y_nm( aSlc.CPoint( vertex ).y ); - } - } - - msg.set_closed( aSlc.IsClosed() ); - - return msg; -} - void PCB_SHAPE::Serialize( google::protobuf::Any &aContainer ) const { @@ -174,12 +143,15 @@ void PCB_SHAPE::Serialize( google::protobuf::Any &aContainer ) const continue; kiapi::common::types::PolygonWithHoles* polyMsg = polyset->mutable_polygons()->Add(); - polyMsg->mutable_outline()->CopyFrom( lineChainToProto( poly.front() ) ); + kiapi::common::PackPolyLine( *polyMsg->mutable_outline(), poly.front() ); if( poly.size() > 1 ) { for( size_t hole = 1; hole < poly.size(); ++hole ) - polyMsg->mutable_holes()->Add( lineChainToProto( poly[hole] ) ); + { + kiapi::common::types::PolyLine* pl = polyMsg->mutable_holes()->Add(); + kiapi::common::PackPolyLine( *pl, poly[hole] ); + } } } break; @@ -203,32 +175,6 @@ void PCB_SHAPE::Serialize( google::protobuf::Any &aContainer ) const } -// TODO(JE) lift out -SHAPE_LINE_CHAIN lineChainFromProto( const kiapi::common::types::PolyLine& aProto ) -{ - SHAPE_LINE_CHAIN slc; - - for( const kiapi::common::types::PolyLineNode& node : aProto.nodes() ) - { - if( node.has_point() ) - { - slc.Append( VECTOR2I( node.point().x_nm(), node.point().y_nm() ) ); - } - else if( node.has_arc() ) - { - slc.Append( SHAPE_ARC( VECTOR2I( node.arc().start().x_nm(), node.arc().start().y_nm() ), - VECTOR2I( node.arc().mid().x_nm(), node.arc().mid().y_nm() ), - VECTOR2I( node.arc().end().x_nm(), node.arc().end().y_nm() ), - 0 /* don't care about width here */ ) ); - } - } - - slc.SetClosed( aProto.closed() ); - - return slc; -} - - bool PCB_SHAPE::Deserialize( const google::protobuf::Any &aContainer ) { kiapi::board::types::GraphicShape msg; @@ -303,10 +249,10 @@ bool PCB_SHAPE::Deserialize( const google::protobuf::Any &aContainer ) { SHAPE_POLY_SET::POLYGON polygon; - polygon.emplace_back( lineChainFromProto( polygonWithHoles.outline() ) ); + polygon.emplace_back( kiapi::common::UnpackPolyLine( polygonWithHoles.outline() ) ); for( const kiapi::common::types::PolyLine& holeMsg : polygonWithHoles.holes() ) - polygon.emplace_back( lineChainFromProto( holeMsg ) ); + polygon.emplace_back( kiapi::common::UnpackPolyLine( holeMsg ) ); sps.AddPolygon( polygon ); }