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();