From 184711beb46e87dfde10a77b0a69cfd9c9f88030 Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Fri, 25 Jan 2019 07:12:04 -0800 Subject: [PATCH] pcbnew: Fix via snapping to multiple tracks Rather than selecting an arbitrary track to snap, we select the closest track to our point, allowing the via to be placed along the full track length. Fixes: lp:1813324 * https://bugs.launchpad.net/kicad/+bug/1813324 --- pcbnew/tools/drawing_tool.cpp | 20 ++++++++++++++++++-- pcbnew/tools/grid_helper.cpp | 4 ++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/pcbnew/tools/drawing_tool.cpp b/pcbnew/tools/drawing_tool.cpp index c849b6abff..4c271ab2af 100644 --- a/pcbnew/tools/drawing_tool.cpp +++ b/pcbnew/tools/drawing_tool.cpp @@ -1507,6 +1507,7 @@ int DRAWING_TOOL::DrawVia( const TOOL_EVENT& aEvent ) std::vector items; BOX2I bbox = aVia->GetBoundingBox(); auto view = m_frame->GetGalCanvas()->GetView(); + std::vector possible_tracks; view->Query( bbox, items ); @@ -1521,11 +1522,26 @@ int DRAWING_TOOL::DrawVia( const TOOL_EVENT& aEvent ) { if( TestSegmentHit( aVia->GetPosition(), track->GetStart(), track->GetEnd(), ( track->GetWidth() + aVia->GetWidth() ) / 2 ) ) - return track; + possible_tracks.push_back( track ); } } - return nullptr; + TRACK* return_track = nullptr; + int min_d = std::numeric_limits::max(); + for( auto track : possible_tracks ) + { + SEG test( track->GetStart(), track->GetEnd() ); + auto dist = ( test.NearestPoint( aVia->GetPosition() ) - + VECTOR2I( aVia->GetPosition() ) ).EuclideanNorm(); + + if( dist < min_d ) + { + min_d = dist; + return_track = track; + } + } + + return return_track; } diff --git a/pcbnew/tools/grid_helper.cpp b/pcbnew/tools/grid_helper.cpp index f395326beb..7a4433334d 100644 --- a/pcbnew/tools/grid_helper.cpp +++ b/pcbnew/tools/grid_helper.cpp @@ -160,8 +160,8 @@ VECTOR2I GRID_HELPER::AlignToSegment( const VECTOR2I& aPoint, const SEG& aSeg ) pts[0] = aSeg.A; pts[1] = aSeg.B; - pts[2] = aSeg.IntersectLines( SEG( nearest, nearest + VECTOR2I( 1, 0 ) ) ); - pts[3] = aSeg.IntersectLines( SEG( nearest, nearest + VECTOR2I( 0, 1 ) ) ); + pts[2] = aSeg.IntersectLines( SEG( nearest + VECTOR2I( -1, 1 ), nearest + VECTOR2I( 1, -1 ) ) ); + pts[3] = aSeg.IntersectLines( SEG( nearest + VECTOR2I( -1, -1 ), nearest + VECTOR2I( 1, 1 ) ) ); int min_d = std::numeric_limits::max();