You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

304 lines
10 KiB

13 years ago
13 years ago
14 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2009-2014 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 1992-2015 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. /**
  25. * @file magnetic_tracks_functions.cpp
  26. */
  27. /* functions used to control the cursor position, when creating a track
  28. * and when the "magnetic tracks" option is on
  29. * (the current created track is kept near existing tracks
  30. * the distance is the clearance between tracks)
  31. */
  32. #include <fctsys.h>
  33. #include <pcbnew.h>
  34. #include <pcb_edit_frame.h>
  35. #include <macros.h>
  36. #include <class_board.h>
  37. #include <class_track.h>
  38. #include <pcbnew_id.h>
  39. /**
  40. * Function Join
  41. * finds the point where line segment (b1,b0) intersects with segment (a1,a0).
  42. * If that point would be outside of (a0,a1), the respective endpoint is used.
  43. * Join returns the point in "res" and "true" if a suitable point was found,
  44. * "false" if both lines are parallel or if the length of either segment is zero.
  45. */
  46. static bool Join( wxPoint* aIntersectPoint, wxPoint a0, wxPoint a1, wxPoint b0, wxPoint b1 )
  47. {
  48. /* References:
  49. http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/
  50. http://www.gekkou.co.uk/blogs/monologues/2007/12/13/1197586800000.html
  51. */
  52. double denom;
  53. double t;
  54. // if either segment is zero length
  55. if( a1.x==a0.x && a1.y==a0.y )
  56. return false;
  57. if( b1.x==b0.x && b1.y==b0.y )
  58. return false;
  59. a1 -= a0;
  60. b1 -= b0;
  61. b0 -= a0;
  62. denom = (double) b1.y * a1.x - (double) b1.x * a1.y;
  63. if( !denom )
  64. {
  65. return false; // parallel
  66. }
  67. t = ((double) b1.y * b0.x - (double) b1.x * b0.y ) / denom;
  68. t = std::min( std::max( t, 0.0 ), 1.0 );
  69. aIntersectPoint->x = KiROUND( a0.x + t * a1.x );
  70. aIntersectPoint->y = KiROUND( a0.y + t * a1.y );
  71. return true;
  72. }
  73. /*
  74. * "Project" finds the projection of a grid point on a track. This is the point
  75. * from where we want to draw new orthogonal tracks when starting on a track.
  76. */
  77. bool FindBestGridPointOnTrack( wxPoint* aNearPos, wxPoint on_grid, const TRACK* track )
  78. {
  79. if( track->GetStart ()== track->GetEnd() )
  80. return false;
  81. wxPoint vec = track->GetEnd() - track->GetStart();
  82. double t = double( on_grid.x - track->GetStart().x ) * vec.x +
  83. double( on_grid.y - track->GetStart().y ) * vec.y;
  84. t /= (double) vec.x * vec.x + (double) vec.y * vec.y;
  85. t = std::min( std::max( t, 0.0 ), 1.0 );
  86. aNearPos->x = KiROUND( track->GetStart().x + t * vec.x );
  87. aNearPos->y = KiROUND( track->GetStart().y + t * vec.y );
  88. return true;
  89. }
  90. /**
  91. * Function Magnetize
  92. * tests to see if there are any magnetic items within near reach of the given
  93. * "curpos". If yes, then curpos is adjusted appropriately according to that
  94. * near magnetic item and true is returned.
  95. * @param frame = the current frame
  96. * @param aCurrentTool = the current tool id (from vertical right toolbar)
  97. * @param aGridSize = the current grid size
  98. * @param on_grid = the on grid position near initial position ( often on_grid = curpos)
  99. * @param curpos The initial position, and what to adjust if a change is needed.
  100. * @return bool - true if the position was adjusted magnetically, else false.
  101. */
  102. bool Magnetize( PCB_BASE_EDIT_FRAME* frame, int aCurrentTool, wxSize aGridSize,
  103. wxPoint on_grid, wxPoint* curpos )
  104. {
  105. // Note: only used for routing in the Legacy Toolset
  106. // Can be greatly simplified when the Legacy Toolset is retired.
  107. bool doCheckNet = frame->Settings().m_magneticPads != CAPTURE_ALWAYS && frame->Settings().m_legacyDrcOn;
  108. bool doCheckLayer = aCurrentTool == ID_TRACK_BUTT;
  109. bool doTrack = false;
  110. bool doPad = false;
  111. bool amMovingVia = false;
  112. BOARD* m_Pcb = frame->GetBoard();
  113. TRACK* currTrack = g_CurrentTrackSegment;
  114. BOARD_ITEM* currItem = frame->GetCurItem();
  115. PCB_SCREEN* screen = frame->GetScreen();
  116. wxPoint pos = frame->RefPos( true );
  117. // D( printf( "currTrack=%p currItem=%p currTrack->Type()=%d currItem->Type()=%d\n", currTrack, currItem, currTrack ? currTrack->Type() : 0, currItem ? currItem->Type() : 0 ); )
  118. if( !currTrack && currItem && currItem->Type()==PCB_VIA_T && currItem->GetFlags() )
  119. {
  120. // moving a VIA
  121. currTrack = (TRACK*) currItem;
  122. amMovingVia = true;
  123. return false; // comment this return out and play with it.
  124. }
  125. else if( currItem != currTrack )
  126. {
  127. currTrack = NULL;
  128. }
  129. if( frame->Settings().m_magneticPads == CAPTURE_ALWAYS )
  130. doPad = true;
  131. if( frame->Settings().m_magneticTracks == CAPTURE_ALWAYS )
  132. doTrack = true;
  133. if( aCurrentTool == ID_TRACK_BUTT || amMovingVia )
  134. {
  135. int q = CAPTURE_CURSOR_IN_TRACK_TOOL;
  136. if( frame->Settings().m_magneticPads == q )
  137. doPad = true;
  138. if( frame->Settings().m_magneticTracks == q )
  139. doTrack = true;
  140. }
  141. // The search precedence order is pads, then tracks/vias
  142. if( doPad )
  143. {
  144. D_PAD* pad;
  145. if( doCheckLayer )
  146. pad = m_Pcb->GetPad( pos, LSET( screen->m_Active_Layer ) );
  147. else
  148. pad = m_Pcb->GetPad( pos );
  149. if( pad )
  150. {
  151. if( doCheckNet && currTrack && currTrack->GetNetCode() != pad->GetNetCode() )
  152. return false;
  153. *curpos = pad->GetPosition();
  154. return true;
  155. }
  156. }
  157. // after pads, only track & via tests remain, skip them if not desired
  158. if( doTrack )
  159. {
  160. PCB_LAYER_ID layer = screen->m_Active_Layer;
  161. for( TRACK* via = m_Pcb->m_Track;
  162. via && (via = via->GetVia( *curpos, layer )) != NULL;
  163. via = via->Next() )
  164. {
  165. if( via != currTrack ) // a via cannot influence itself
  166. {
  167. if( !doCheckNet || !currTrack || currTrack->GetNetCode() == via->GetNetCode() )
  168. {
  169. *curpos = via->GetStart();
  170. // D(printf("via hit\n");)
  171. return true;
  172. }
  173. }
  174. }
  175. if( !currTrack )
  176. {
  177. LSET layers( layer );
  178. TRACK* track = m_Pcb->GetVisibleTrack( m_Pcb->m_Track, pos, layers );
  179. if( !track || track->Type() != PCB_TRACE_T )
  180. {
  181. return false;
  182. }
  183. return FindBestGridPointOnTrack( curpos, on_grid, track );
  184. }
  185. /*
  186. * In two segment mode, ignore the final segment if it's inside a grid square.
  187. */
  188. if( !amMovingVia && currTrack && frame->Settings().m_legacyUseTwoSegmentTracks && currTrack->Back()
  189. && currTrack->GetStart().x - aGridSize.x < currTrack->GetEnd().x
  190. && currTrack->GetStart().x + aGridSize.x > currTrack->GetEnd().x
  191. && currTrack->GetStart().y - aGridSize.y < currTrack->GetEnd().y
  192. && currTrack->GetStart().y + aGridSize.y > currTrack->GetEnd().y )
  193. {
  194. currTrack = currTrack->Back();
  195. }
  196. for( TRACK* track = m_Pcb->m_Track; track; track = track->Next() )
  197. {
  198. if( track->Type() != PCB_TRACE_T )
  199. continue;
  200. if( doCheckNet && currTrack && currTrack->GetNetCode() != track->GetNetCode() )
  201. continue;
  202. if( m_Pcb->IsLayerVisible( track->GetLayer() ) == false )
  203. continue;
  204. // omit the layer check if moving a via
  205. if( !amMovingVia && !track->IsOnLayer( layer ) )
  206. continue;
  207. if( !track->HitTest( *curpos ) )
  208. continue;
  209. if( Join( curpos, track->GetStart(), track->GetEnd(), currTrack->GetStart(), currTrack->GetEnd() ) )
  210. {
  211. return true;
  212. }
  213. if( aCurrentTool == ID_TRACK_BUTT || amMovingVia )
  214. {
  215. // At this point we have a drawing mouse on a track, we are drawing
  216. // a new track and that new track is parallel to the track the
  217. // mouse is on. Find the nearest end point of the track under mouse
  218. // to the mouse and return that.
  219. double distStart = GetLineLength( *curpos, track->GetStart() );
  220. double distEnd = GetLineLength( *curpos, track->GetEnd() );
  221. // if track not via, or if its a via dragging but not with its adjacent track
  222. if( currTrack->Type() != PCB_VIA_T ||
  223. ( currTrack->GetStart() != track->GetStart() && currTrack->GetStart() != track->GetEnd() ))
  224. {
  225. double max_dist = currTrack->GetWidth() / 2.0f;
  226. if( distStart <= max_dist )
  227. {
  228. // D(printf("nearest end is start\n");)
  229. *curpos = track->GetStart();
  230. return true;
  231. }
  232. if( distEnd <= max_dist )
  233. {
  234. // D(printf("nearest end is end\n");)
  235. *curpos = track->GetEnd();
  236. return true;
  237. }
  238. // @todo otherwise confine curpos such that it stays centered within "track"
  239. }
  240. }
  241. }
  242. }
  243. return false;
  244. }