From b32c2a6c9029c36bcb2afbe006a43ebfa72d273b Mon Sep 17 00:00:00 2001 From: qu1ck Date: Sat, 21 Nov 2020 03:26:57 -0800 Subject: [PATCH] Refactor EDA_TEXT::TransformToSegmentList() to return a vector of points This allows reading text object as it is rendered through python API. --- common/eda_text.cpp | 13 ++++++++----- include/eda_text.h | 4 +--- pcbnew/router/pns_kicad_iface.cpp | 4 +--- qa/testcases/test_001_pcb_load.py | 18 ++++++++++++++++++ 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/common/eda_text.cpp b/common/eda_text.cpp index 48cb2f07e1..e5ef0e1894 100644 --- a/common/eda_text.cpp +++ b/common/eda_text.cpp @@ -50,6 +50,7 @@ #include #include #include +#include #include // for wxASSERT @@ -580,8 +581,9 @@ static void addTextSegmToBuffer( int x0, int y0, int xf, int yf, void* aData ) } -void EDA_TEXT::TransformTextShapeToSegmentList( std::vector& aCornerBuffer ) const +std::vector EDA_TEXT::TransformToSegmentList() const { + std::vector cornerBuffer; wxSize size = GetTextSize(); if( IsMirrored() ) @@ -605,15 +607,17 @@ void EDA_TEXT::TransformTextShapeToSegmentList( std::vector& aCornerBuf wxString txt = strings_list.Item( ii ); GRText( NULL, positions[ii], color, txt, GetDrawRotation(), size, GetHorizJustify(), GetVertJustify(), penWidth, IsItalic(), forceBold, addTextSegmToBuffer, - &aCornerBuffer ); + &cornerBuffer ); } } else { GRText( NULL, GetTextPos(), color, GetText(), GetDrawRotation(), size, GetHorizJustify(), GetVertJustify(), penWidth, IsItalic(), forceBold, addTextSegmToBuffer, - &aCornerBuffer ); + &cornerBuffer ); } + + return cornerBuffer; } @@ -621,8 +625,7 @@ std::shared_ptr EDA_TEXT::GetEffectiveTextShape( ) const { std::shared_ptr shape = std::make_shared(); int penWidth = GetEffectiveTextPenWidth(); - std::vector pts; - TransformTextShapeToSegmentList( pts ); + std::vector pts = TransformToSegmentList(); for( unsigned jj = 0; jj < pts.size(); jj += 2 ) shape->AddShape( new SHAPE_SEGMENT( pts[jj], pts[jj+1], penWidth ) ); diff --git a/include/eda_text.h b/include/eda_text.h index c10268f068..db0a0e1911 100644 --- a/include/eda_text.h +++ b/include/eda_text.h @@ -280,10 +280,8 @@ public: * * Each segment is stored as 2 wxPoints: the starting point and the ending point * there are therefore 2*n points. - * - * @param aCornerBuffer = a buffer to store the polygon */ - void TransformTextShapeToSegmentList( std::vector& aCornerBuffer ) const; + std::vector TransformToSegmentList() const; /** * Convert the text bounding box to a rectangular polygon depending on the text diff --git a/pcbnew/router/pns_kicad_iface.cpp b/pcbnew/router/pns_kicad_iface.cpp index 4fa0f159b0..8faa592bd1 100644 --- a/pcbnew/router/pns_kicad_iface.cpp +++ b/pcbnew/router/pns_kicad_iface.cpp @@ -991,9 +991,7 @@ bool PNS_KICAD_IFACE_BASE::syncTextItem( PNS::NODE* aWorld, EDA_TEXT* aText, PCB return false; int textWidth = aText->GetEffectiveTextPenWidth(); - std::vector textShape; - - aText->TransformTextShapeToSegmentList( textShape ); + std::vector textShape = aText->TransformToSegmentList(); if( textShape.size() < 2 ) return false; diff --git a/qa/testcases/test_001_pcb_load.py b/qa/testcases/test_001_pcb_load.py index 2d0916e770..5c349a9eb9 100644 --- a/qa/testcases/test_001_pcb_load.py +++ b/qa/testcases/test_001_pcb_load.py @@ -60,6 +60,24 @@ class TestPCBLoad(unittest.TestCase): self.verify_text(text[1], 176149000, 64643000, pcbnew.B_Cu, u'Actionneur\nPiezo New Amp\nV02') + def test_text_as_segments(self): + footprint = self.pcb.FindFootprintByReference("U1") + reference = footprint.Reference() + segments = [[p.x, p.y] for p in reference.TransformToSegmentList()] + expected_segments = [ + [141901333, 69196857], [143340666, 69196857], [143340666, 69196857], + [143510000, 69142428], [143510000, 69142428], [143594666, 69088000], + [143594666, 69088000], [143679333, 68979142], [143679333, 68979142], + [143679333, 68761428], [143679333, 68761428], [143594666, 68652571], + [143594666, 68652571], [143510000, 68598142], [143510000, 68598142], + [143340666, 68543714], [143340666, 68543714], [141901333, 68543714], + [143679333, 67400714], [143679333, 68053857], [143679333, 67727285], + [141901333, 67727285], [141901333, 67727285], [142155333, 67836142], + [142155333, 67836142], [142324666, 67945000], [142324666, 67945000], + [142409333, 68053857] + ] + self.assertEqual(segments, expected_segments) + def verify_text(self, text, x, y, layer, s): self.assertEquals(list(text.GetPosition()), [x, y]) self.assertEquals(text.GetLayer(), layer)