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.

297 lines
9.8 KiB

14 years ago
14 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_EDIT_FRAME* frame, int aCurrentTool, wxSize aGridSize,
  103. wxPoint on_grid, wxPoint* curpos )
  104. {
  105. bool doCheckNet = frame->Settings().m_magneticPads != CAPTURE_ALWAYS && frame->Settings().m_legacyDrcOn;
  106. bool doTrack = false;
  107. bool doPad = false;
  108. bool amMovingVia = false;
  109. BOARD* m_Pcb = frame->GetBoard();
  110. TRACK* currTrack = g_CurrentTrackSegment;
  111. BOARD_ITEM* currItem = frame->GetCurItem();
  112. PCB_SCREEN* screen = frame->GetScreen();
  113. wxPoint pos = frame->RefPos( true );
  114. // D( printf( "currTrack=%p currItem=%p currTrack->Type()=%d currItem->Type()=%d\n", currTrack, currItem, currTrack ? currTrack->Type() : 0, currItem ? currItem->Type() : 0 ); )
  115. if( !currTrack && currItem && currItem->Type()==PCB_VIA_T && currItem->GetFlags() )
  116. {
  117. // moving a VIA
  118. currTrack = (TRACK*) currItem;
  119. amMovingVia = true;
  120. return false; // comment this return out and play with it.
  121. }
  122. else if( currItem != currTrack )
  123. {
  124. currTrack = NULL;
  125. }
  126. if( frame->Settings().m_magneticPads == CAPTURE_ALWAYS )
  127. doPad = true;
  128. if( frame->Settings().m_magneticTracks == CAPTURE_ALWAYS )
  129. doTrack = true;
  130. if( aCurrentTool == ID_TRACK_BUTT || amMovingVia )
  131. {
  132. int q = CAPTURE_CURSOR_IN_TRACK_TOOL;
  133. if( frame->Settings().m_magneticPads == q )
  134. doPad = true;
  135. if( frame->Settings().m_magneticTracks == q )
  136. doTrack = true;
  137. }
  138. // The search precedence order is pads, then tracks/vias
  139. if( doPad )
  140. {
  141. LSET layer_mask( screen->m_Active_Layer );
  142. D_PAD* pad = m_Pcb->GetPad( pos, layer_mask );
  143. if( pad )
  144. {
  145. if( doCheckNet && currTrack && currTrack->GetNetCode() != pad->GetNetCode() )
  146. return false;
  147. *curpos = pad->GetPosition();
  148. return true;
  149. }
  150. }
  151. // after pads, only track & via tests remain, skip them if not desired
  152. if( doTrack )
  153. {
  154. PCB_LAYER_ID layer = screen->m_Active_Layer;
  155. for( TRACK* via = m_Pcb->m_Track;
  156. via && (via = via->GetVia( *curpos, layer )) != NULL;
  157. via = via->Next() )
  158. {
  159. if( via != currTrack ) // a via cannot influence itself
  160. {
  161. if( !doCheckNet || !currTrack || currTrack->GetNetCode() == via->GetNetCode() )
  162. {
  163. *curpos = via->GetStart();
  164. // D(printf("via hit\n");)
  165. return true;
  166. }
  167. }
  168. }
  169. if( !currTrack )
  170. {
  171. LSET layers( layer );
  172. TRACK* track = m_Pcb->GetVisibleTrack( m_Pcb->m_Track, pos, layers );
  173. if( !track || track->Type() != PCB_TRACE_T )
  174. {
  175. return false;
  176. }
  177. return FindBestGridPointOnTrack( curpos, on_grid, track );
  178. }
  179. /*
  180. * In two segment mode, ignore the final segment if it's inside a grid square.
  181. */
  182. if( !amMovingVia && currTrack && frame->Settings().m_legacyUseTwoSegmentTracks && currTrack->Back()
  183. && currTrack->GetStart().x - aGridSize.x < currTrack->GetEnd().x
  184. && currTrack->GetStart().x + aGridSize.x > currTrack->GetEnd().x
  185. && currTrack->GetStart().y - aGridSize.y < currTrack->GetEnd().y
  186. && currTrack->GetStart().y + aGridSize.y > currTrack->GetEnd().y )
  187. {
  188. currTrack = currTrack->Back();
  189. }
  190. for( TRACK* track = m_Pcb->m_Track; track; track = track->Next() )
  191. {
  192. if( track->Type() != PCB_TRACE_T )
  193. continue;
  194. if( doCheckNet && currTrack && currTrack->GetNetCode() != track->GetNetCode() )
  195. continue;
  196. if( m_Pcb->IsLayerVisible( track->GetLayer() ) == false )
  197. continue;
  198. // omit the layer check if moving a via
  199. if( !amMovingVia && !track->IsOnLayer( layer ) )
  200. continue;
  201. if( !track->HitTest( *curpos ) )
  202. continue;
  203. if( Join( curpos, track->GetStart(), track->GetEnd(), currTrack->GetStart(), currTrack->GetEnd() ) )
  204. {
  205. return true;
  206. }
  207. if( aCurrentTool == ID_TRACK_BUTT || amMovingVia )
  208. {
  209. // At this point we have a drawing mouse on a track, we are drawing
  210. // a new track and that new track is parallel to the track the
  211. // mouse is on. Find the nearest end point of the track under mouse
  212. // to the mouse and return that.
  213. double distStart = GetLineLength( *curpos, track->GetStart() );
  214. double distEnd = GetLineLength( *curpos, track->GetEnd() );
  215. // if track not via, or if its a via dragging but not with its adjacent track
  216. if( currTrack->Type() != PCB_VIA_T ||
  217. ( currTrack->GetStart() != track->GetStart() && currTrack->GetStart() != track->GetEnd() ))
  218. {
  219. double max_dist = currTrack->GetWidth() / 2.0f;
  220. if( distStart <= max_dist )
  221. {
  222. // D(printf("nearest end is start\n");)
  223. *curpos = track->GetStart();
  224. return true;
  225. }
  226. if( distEnd <= max_dist )
  227. {
  228. // D(printf("nearest end is end\n");)
  229. *curpos = track->GetEnd();
  230. return true;
  231. }
  232. // @todo otherwise confine curpos such that it stays centered within "track"
  233. }
  234. }
  235. }
  236. }
  237. return false;
  238. }