From 0c47a09517413d171f8095958a73c944c431514c Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Thu, 7 Jul 2022 10:39:10 -0700 Subject: [PATCH] Ensure rectangles are normalized Normalize on creation and fixup rectangles previously saved with inverted coordinates Fixes https://gitlab.com/kicad/code/kicad/issues/11965 --- pcbnew/pcb_shape.cpp | 16 ++++++++++++++++ pcbnew/pcb_shape.h | 2 ++ pcbnew/plugins/kicad/pcb_parser.cpp | 1 + pcbnew/tools/drawing_tool.cpp | 1 + 4 files changed, 20 insertions(+) diff --git a/pcbnew/pcb_shape.cpp b/pcbnew/pcb_shape.cpp index 17358f9d21..b3d2878857 100644 --- a/pcbnew/pcb_shape.cpp +++ b/pcbnew/pcb_shape.cpp @@ -132,6 +132,22 @@ void PCB_SHAPE::Scale( double aScale ) } +void PCB_SHAPE::NormalizeRect() +{ + if( m_shape == SHAPE_T::RECT ) + { + VECTOR2I start = GetStart(); + VECTOR2I end = GetEnd(); + + EDA_RECT rect( start, end - start ); + rect.Normalize(); + + SetStart( rect.GetPosition() ); + SetEnd( rect.GetEnd() ); + } +} + + void PCB_SHAPE::Rotate( const VECTOR2I& aRotCentre, const EDA_ANGLE& aAngle ) { rotate( aRotCentre, aAngle ); diff --git a/pcbnew/pcb_shape.h b/pcbnew/pcb_shape.h index 947982435a..93e7c6a603 100644 --- a/pcbnew/pcb_shape.h +++ b/pcbnew/pcb_shape.h @@ -122,6 +122,8 @@ public: return hitTest( aRect, aContained, aAccuracy ); } + void NormalizeRect(); + virtual void Move( const VECTOR2I& aMoveVector ) override; virtual void Rotate( const VECTOR2I& aRotCentre, const EDA_ANGLE& aAngle ) override; diff --git a/pcbnew/plugins/kicad/pcb_parser.cpp b/pcbnew/plugins/kicad/pcb_parser.cpp index f33f5192ec..f86945a638 100644 --- a/pcbnew/plugins/kicad/pcb_parser.cpp +++ b/pcbnew/plugins/kicad/pcb_parser.cpp @@ -2635,6 +2635,7 @@ PCB_SHAPE* PCB_PARSER::parsePCB_SHAPE() pt.x = parseBoardUnits( "X coordinate" ); pt.y = parseBoardUnits( "Y coordinate" ); shape->SetEnd( pt ); + shape->NormalizeRect(); NeedRIGHT(); break; diff --git a/pcbnew/tools/drawing_tool.cpp b/pcbnew/tools/drawing_tool.cpp index fcd0bfceec..a5a34f9244 100644 --- a/pcbnew/tools/drawing_tool.cpp +++ b/pcbnew/tools/drawing_tool.cpp @@ -388,6 +388,7 @@ int DRAWING_TOOL::DrawRectangle( const TOOL_EVENT& aEvent ) } else { + rect->NormalizeRect(); commit.Add( rect ); commit.Push( isTextBox ? _( "Draw a text box" ) : _( "Draw a rectangle" ) );