2014-10-23 17:53:38 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009-2014 Jean-Pierre Charras, jp.charras at wanadoo.fr
|
2015-02-21 08:11:58 +00:00
|
|
|
* Copyright (C) 1992-2015 KiCad Developers, see AUTHORS.txt for contributors.
|
2014-10-23 17:53:38 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2011-09-23 13:57:12 +00:00
|
|
|
/**
|
|
|
|
* @file magnetic_tracks_functions.cpp
|
|
|
|
*/
|
2009-10-10 17:27:53 +00:00
|
|
|
|
2011-09-14 20:04:58 +00:00
|
|
|
/* functions used to control the cursor position, when creating a track
|
2009-10-10 17:27:53 +00:00
|
|
|
* and when the "magnetic tracks" option is on
|
|
|
|
* (the current created track is kept near existing tracks
|
|
|
|
* the distance is the clearance between tracks)
|
|
|
|
*/
|
|
|
|
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <fctsys.h>
|
|
|
|
#include <pcbnew.h>
|
2018-01-29 20:58:58 +00:00
|
|
|
#include <pcb_edit_frame.h>
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <macros.h>
|
2011-09-23 13:57:12 +00:00
|
|
|
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <class_board.h>
|
|
|
|
#include <class_track.h>
|
2011-09-23 13:57:12 +00:00
|
|
|
|
2017-01-29 12:04:47 +00:00
|
|
|
#include <pcbnew_id.h>
|
|
|
|
|
2009-10-10 17:27:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function Join
|
|
|
|
* finds the point where line segment (b1,b0) intersects with segment (a1,a0).
|
|
|
|
* If that point would be outside of (a0,a1), the respective endpoint is used.
|
|
|
|
* Join returns the point in "res" and "true" if a suitable point was found,
|
|
|
|
* "false" if both lines are parallel or if the length of either segment is zero.
|
|
|
|
*/
|
2012-08-28 06:51:18 +00:00
|
|
|
static bool Join( wxPoint* aIntersectPoint, wxPoint a0, wxPoint a1, wxPoint b0, wxPoint b1 )
|
2009-10-10 17:27:53 +00:00
|
|
|
{
|
|
|
|
/* References:
|
|
|
|
http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/
|
|
|
|
http://www.gekkou.co.uk/blogs/monologues/2007/12/13/1197586800000.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
double denom;
|
|
|
|
double t;
|
|
|
|
|
|
|
|
// if either segment is zero length
|
|
|
|
if( a1.x==a0.x && a1.y==a0.y )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( b1.x==b0.x && b1.y==b0.y )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
a1 -= a0;
|
|
|
|
b1 -= b0;
|
|
|
|
|
|
|
|
b0 -= a0;
|
|
|
|
|
|
|
|
denom = (double) b1.y * a1.x - (double) b1.x * a1.y;
|
2011-09-07 19:41:04 +00:00
|
|
|
|
2009-10-10 17:27:53 +00:00
|
|
|
if( !denom )
|
|
|
|
{
|
|
|
|
return false; // parallel
|
|
|
|
}
|
|
|
|
|
|
|
|
t = ((double) b1.y * b0.x - (double) b1.x * b0.y ) / denom;
|
|
|
|
|
2012-08-03 15:43:15 +00:00
|
|
|
t = std::min( std::max( t, 0.0 ), 1.0 );
|
2009-10-10 17:27:53 +00:00
|
|
|
|
2012-08-28 06:51:18 +00:00
|
|
|
aIntersectPoint->x = KiROUND( a0.x + t * a1.x );
|
|
|
|
aIntersectPoint->y = KiROUND( a0.y + t * a1.y );
|
2009-10-10 17:27:53 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* "Project" finds the projection of a grid point on a track. This is the point
|
|
|
|
* from where we want to draw new orthogonal tracks when starting on a track.
|
|
|
|
*/
|
2017-05-04 06:54:30 +00:00
|
|
|
bool FindBestGridPointOnTrack( wxPoint* aNearPos, wxPoint on_grid, const TRACK* track )
|
2009-10-10 17:27:53 +00:00
|
|
|
{
|
2013-01-13 00:04:00 +00:00
|
|
|
if( track->GetStart ()== track->GetEnd() )
|
2009-10-10 17:27:53 +00:00
|
|
|
return false;
|
|
|
|
|
2013-01-13 00:04:00 +00:00
|
|
|
wxPoint vec = track->GetEnd() - track->GetStart();
|
2009-10-10 17:27:53 +00:00
|
|
|
|
2013-01-13 00:04:00 +00:00
|
|
|
double t = double( on_grid.x - track->GetStart().x ) * vec.x +
|
|
|
|
double( on_grid.y - track->GetStart().y ) * vec.y;
|
2009-10-10 17:27:53 +00:00
|
|
|
|
|
|
|
t /= (double) vec.x * vec.x + (double) vec.y * vec.y;
|
2012-08-03 15:43:15 +00:00
|
|
|
t = std::min( std::max( t, 0.0 ), 1.0 );
|
2009-10-10 17:27:53 +00:00
|
|
|
|
2013-01-13 00:04:00 +00:00
|
|
|
aNearPos->x = KiROUND( track->GetStart().x + t * vec.x );
|
|
|
|
aNearPos->y = KiROUND( track->GetStart().y + t * vec.y );
|
2009-10-10 17:27:53 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function Magnetize
|
|
|
|
* tests to see if there are any magnetic items within near reach of the given
|
|
|
|
* "curpos". If yes, then curpos is adjusted appropriately according to that
|
|
|
|
* near magnetic item and true is returned.
|
2010-12-29 17:47:32 +00:00
|
|
|
* @param frame = the current frame
|
|
|
|
* @param aCurrentTool = the current tool id (from vertical right toolbar)
|
2012-08-28 06:51:18 +00:00
|
|
|
* @param aGridSize = the current grid size
|
|
|
|
* @param on_grid = the on grid position near initial position ( often on_grid = curpos)
|
2009-10-10 17:27:53 +00:00
|
|
|
* @param curpos The initial position, and what to adjust if a change is needed.
|
|
|
|
* @return bool - true if the position was adjusted magnetically, else false.
|
|
|
|
*/
|
2018-05-02 11:57:18 +00:00
|
|
|
bool Magnetize( PCB_BASE_EDIT_FRAME* frame, int aCurrentTool, wxSize aGridSize,
|
2011-02-01 15:46:25 +00:00
|
|
|
wxPoint on_grid, wxPoint* curpos )
|
2009-10-10 17:27:53 +00:00
|
|
|
{
|
2018-10-05 04:19:12 +00:00
|
|
|
// Note: only used for routing in the Legacy Toolset
|
|
|
|
// Can be greatly simplified when the Legacy Toolset is retired.
|
2018-05-02 11:57:18 +00:00
|
|
|
|
2017-08-04 12:43:02 +00:00
|
|
|
bool doCheckNet = frame->Settings().m_magneticPads != CAPTURE_ALWAYS && frame->Settings().m_legacyDrcOn;
|
2018-05-02 11:57:18 +00:00
|
|
|
bool doCheckLayer = aCurrentTool == ID_TRACK_BUTT;
|
2009-10-10 17:27:53 +00:00
|
|
|
bool doTrack = false;
|
|
|
|
bool doPad = false;
|
|
|
|
bool amMovingVia = false;
|
|
|
|
|
2018-05-02 11:57:18 +00:00
|
|
|
BOARD* m_Pcb = frame->GetBoard();
|
2009-10-10 17:27:53 +00:00
|
|
|
TRACK* currTrack = g_CurrentTrackSegment;
|
|
|
|
BOARD_ITEM* currItem = frame->GetCurItem();
|
2011-02-01 15:46:25 +00:00
|
|
|
PCB_SCREEN* screen = frame->GetScreen();
|
2013-08-03 05:15:23 +00:00
|
|
|
wxPoint pos = frame->RefPos( true );
|
2009-10-10 17:27:53 +00:00
|
|
|
|
|
|
|
// D( printf( "currTrack=%p currItem=%p currTrack->Type()=%d currItem->Type()=%d\n", currTrack, currItem, currTrack ? currTrack->Type() : 0, currItem ? currItem->Type() : 0 ); )
|
|
|
|
|
2011-12-21 13:42:02 +00:00
|
|
|
if( !currTrack && currItem && currItem->Type()==PCB_VIA_T && currItem->GetFlags() )
|
2009-10-10 17:27:53 +00:00
|
|
|
{
|
|
|
|
// moving a VIA
|
|
|
|
currTrack = (TRACK*) currItem;
|
|
|
|
amMovingVia = true;
|
|
|
|
|
|
|
|
return false; // comment this return out and play with it.
|
|
|
|
}
|
|
|
|
else if( currItem != currTrack )
|
|
|
|
{
|
|
|
|
currTrack = NULL;
|
|
|
|
}
|
|
|
|
|
2017-08-04 12:43:02 +00:00
|
|
|
if( frame->Settings().m_magneticPads == CAPTURE_ALWAYS )
|
2009-10-10 17:27:53 +00:00
|
|
|
doPad = true;
|
|
|
|
|
2017-08-04 12:43:02 +00:00
|
|
|
if( frame->Settings().m_magneticTracks == CAPTURE_ALWAYS )
|
2009-10-10 17:27:53 +00:00
|
|
|
doTrack = true;
|
|
|
|
|
|
|
|
if( aCurrentTool == ID_TRACK_BUTT || amMovingVia )
|
|
|
|
{
|
2017-01-29 11:58:04 +00:00
|
|
|
int q = CAPTURE_CURSOR_IN_TRACK_TOOL;
|
2009-10-10 17:27:53 +00:00
|
|
|
|
2017-08-04 12:43:02 +00:00
|
|
|
if( frame->Settings().m_magneticPads == q )
|
2009-10-10 17:27:53 +00:00
|
|
|
doPad = true;
|
|
|
|
|
2017-08-04 12:43:02 +00:00
|
|
|
if( frame->Settings().m_magneticTracks == q )
|
2009-10-10 17:27:53 +00:00
|
|
|
doTrack = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The search precedence order is pads, then tracks/vias
|
|
|
|
if( doPad )
|
|
|
|
{
|
2018-05-02 11:57:18 +00:00
|
|
|
D_PAD* pad;
|
|
|
|
if( doCheckLayer )
|
|
|
|
pad = m_Pcb->GetPad( pos, LSET( screen->m_Active_Layer ) );
|
|
|
|
else
|
|
|
|
pad = m_Pcb->GetPad( pos );
|
2011-02-01 15:46:25 +00:00
|
|
|
|
2009-10-10 17:27:53 +00:00
|
|
|
if( pad )
|
|
|
|
{
|
2014-02-25 10:40:34 +00:00
|
|
|
if( doCheckNet && currTrack && currTrack->GetNetCode() != pad->GetNetCode() )
|
2009-10-10 17:27:53 +00:00
|
|
|
return false;
|
|
|
|
|
2012-02-19 04:02:19 +00:00
|
|
|
*curpos = pad->GetPosition();
|
2009-10-10 17:27:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// after pads, only track & via tests remain, skip them if not desired
|
|
|
|
if( doTrack )
|
|
|
|
{
|
2017-03-13 03:19:33 +00:00
|
|
|
PCB_LAYER_ID layer = screen->m_Active_Layer;
|
2009-10-10 17:27:53 +00:00
|
|
|
|
|
|
|
for( TRACK* via = m_Pcb->m_Track;
|
2014-06-24 16:17:18 +00:00
|
|
|
via && (via = via->GetVia( *curpos, layer )) != NULL;
|
|
|
|
via = via->Next() )
|
2009-10-10 17:27:53 +00:00
|
|
|
{
|
|
|
|
if( via != currTrack ) // a via cannot influence itself
|
|
|
|
{
|
2014-02-25 10:40:34 +00:00
|
|
|
if( !doCheckNet || !currTrack || currTrack->GetNetCode() == via->GetNetCode() )
|
2009-10-10 17:27:53 +00:00
|
|
|
{
|
2013-01-13 00:04:00 +00:00
|
|
|
*curpos = via->GetStart();
|
2009-10-10 17:27:53 +00:00
|
|
|
// D(printf("via hit\n");)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !currTrack )
|
|
|
|
{
|
2016-07-12 19:05:54 +00:00
|
|
|
LSET layers( layer );
|
2009-10-10 17:27:53 +00:00
|
|
|
|
2016-07-12 19:05:54 +00:00
|
|
|
TRACK* track = m_Pcb->GetVisibleTrack( m_Pcb->m_Track, pos, layers );
|
2011-02-01 15:46:25 +00:00
|
|
|
|
2011-10-01 19:24:27 +00:00
|
|
|
if( !track || track->Type() != PCB_TRACE_T )
|
2009-10-10 17:27:53 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-04 06:54:30 +00:00
|
|
|
return FindBestGridPointOnTrack( curpos, on_grid, track );
|
2009-10-10 17:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-09-14 20:04:58 +00:00
|
|
|
* In two segment mode, ignore the final segment if it's inside a grid square.
|
2009-10-10 17:27:53 +00:00
|
|
|
*/
|
2017-08-04 12:43:02 +00:00
|
|
|
if( !amMovingVia && currTrack && frame->Settings().m_legacyUseTwoSegmentTracks && currTrack->Back()
|
2013-01-13 00:04:00 +00:00
|
|
|
&& currTrack->GetStart().x - aGridSize.x < currTrack->GetEnd().x
|
|
|
|
&& currTrack->GetStart().x + aGridSize.x > currTrack->GetEnd().x
|
|
|
|
&& currTrack->GetStart().y - aGridSize.y < currTrack->GetEnd().y
|
|
|
|
&& currTrack->GetStart().y + aGridSize.y > currTrack->GetEnd().y )
|
2009-10-10 17:27:53 +00:00
|
|
|
{
|
|
|
|
currTrack = currTrack->Back();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-28 06:51:18 +00:00
|
|
|
for( TRACK* track = m_Pcb->m_Track; track; track = track->Next() )
|
2009-10-10 17:27:53 +00:00
|
|
|
{
|
2011-10-01 19:24:27 +00:00
|
|
|
if( track->Type() != PCB_TRACE_T )
|
2009-10-10 17:27:53 +00:00
|
|
|
continue;
|
|
|
|
|
2014-02-25 10:40:34 +00:00
|
|
|
if( doCheckNet && currTrack && currTrack->GetNetCode() != track->GetNetCode() )
|
2009-10-10 17:27:53 +00:00
|
|
|
continue;
|
|
|
|
|
2010-01-31 20:01:46 +00:00
|
|
|
if( m_Pcb->IsLayerVisible( track->GetLayer() ) == false )
|
2009-10-10 17:27:53 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// omit the layer check if moving a via
|
|
|
|
if( !amMovingVia && !track->IsOnLayer( layer ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if( !track->HitTest( *curpos ) )
|
|
|
|
continue;
|
|
|
|
|
2013-01-13 00:04:00 +00:00
|
|
|
if( Join( curpos, track->GetStart(), track->GetEnd(), currTrack->GetStart(), currTrack->GetEnd() ) )
|
2009-10-10 17:27:53 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( aCurrentTool == ID_TRACK_BUTT || amMovingVia )
|
|
|
|
{
|
|
|
|
// At this point we have a drawing mouse on a track, we are drawing
|
|
|
|
// a new track and that new track is parallel to the track the
|
|
|
|
// mouse is on. Find the nearest end point of the track under mouse
|
|
|
|
// to the mouse and return that.
|
2013-05-01 17:32:36 +00:00
|
|
|
double distStart = GetLineLength( *curpos, track->GetStart() );
|
|
|
|
double distEnd = GetLineLength( *curpos, track->GetEnd() );
|
2009-10-10 17:27:53 +00:00
|
|
|
|
|
|
|
// if track not via, or if its a via dragging but not with its adjacent track
|
2015-11-03 16:55:31 +00:00
|
|
|
if( currTrack->Type() != PCB_VIA_T ||
|
|
|
|
( currTrack->GetStart() != track->GetStart() && currTrack->GetStart() != track->GetEnd() ))
|
2009-10-10 17:27:53 +00:00
|
|
|
{
|
2015-02-21 08:11:58 +00:00
|
|
|
double max_dist = currTrack->GetWidth() / 2.0f;
|
|
|
|
|
|
|
|
if( distStart <= max_dist )
|
2009-10-10 17:27:53 +00:00
|
|
|
{
|
|
|
|
// D(printf("nearest end is start\n");)
|
2013-01-13 00:04:00 +00:00
|
|
|
*curpos = track->GetStart();
|
2009-10-10 17:27:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-02-21 08:11:58 +00:00
|
|
|
if( distEnd <= max_dist )
|
2009-10-10 17:27:53 +00:00
|
|
|
{
|
|
|
|
// D(printf("nearest end is end\n");)
|
2013-01-13 00:04:00 +00:00
|
|
|
*curpos = track->GetEnd();
|
2009-10-10 17:27:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-09-14 20:04:58 +00:00
|
|
|
// @todo otherwise confine curpos such that it stays centered within "track"
|
2009-10-10 17:27:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|