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.

1006 lines
33 KiB

14 years ago
14 years ago
* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
  1. /**
  2. * @file connect.cpp
  3. * @brief Functions to handle existing tracks in ratsnest calculations.
  4. */
  5. /*
  6. * This program source code file is part of KiCad, a free EDA CAD application.
  7. *
  8. * Copyright (C) 2012 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
  9. * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  10. * Copyright (C) 1992-2012 KiCad Developers, see AUTHORS.txt for contributors.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version 2
  15. * of the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, you may find one here:
  24. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  25. * or you may search the http://www.gnu.org website for the version 2 license,
  26. * or you may write to the Free Software Foundation, Inc.,
  27. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  28. */
  29. #include <fctsys.h>
  30. #include <common.h>
  31. #include <macros.h>
  32. #include <wxBasePcbFrame.h>
  33. #include <pcbnew.h>
  34. // Helper classes to handle connection points
  35. #include <connect.h>
  36. extern void Merge_SubNets_Connected_By_CopperAreas( BOARD* aPcb );
  37. extern void Merge_SubNets_Connected_By_CopperAreas( BOARD* aPcb, int aNetcode );
  38. // Local functions
  39. static void RebuildTrackChain( BOARD* pcb );
  40. CONNECTIONS::CONNECTIONS( BOARD * aBrd )
  41. {
  42. m_brd = aBrd;
  43. }
  44. /* Fills m_sortedPads with all pads that be connected to tracks
  45. * pads are sorted by X coordinate ( and Y coordinates for same X value )
  46. * aNetcode = net code to filter pads or < 0 to put all pads in list
  47. */
  48. void CONNECTIONS::BuildPadsList( int aNetcode )
  49. {
  50. // Creates sorted pad list if not exists
  51. m_sortedPads.clear();
  52. m_brd->GetSortedPadListByXthenYCoord( m_sortedPads, aNetcode < 0 ? -1 : aNetcode );
  53. }
  54. /* Explores the list of pads and adds to m_PadsConnected member
  55. * of each pad pads connected to
  56. * Here, connections are due to intersecting pads, not tracks
  57. */
  58. void CONNECTIONS::SearchConnectionsPadsToIntersectingPads()
  59. {
  60. std::vector<CONNECTED_POINT*> candidates;
  61. BuildPadsCandidatesList();
  62. for( unsigned ii = 0; ii < m_sortedPads.size(); ii++ )
  63. {
  64. D_PAD* pad = m_sortedPads[ii];
  65. pad->m_PadsConnected.clear();
  66. candidates.clear();
  67. CollectItemsNearTo( candidates, pad->ShapePos(), pad->GetBoundingRadius() );
  68. // add pads to pad.m_PadsConnected, if they are connected
  69. for( unsigned jj = 0; jj < candidates.size(); jj++ )
  70. {
  71. CONNECTED_POINT* item = candidates[jj];
  72. D_PAD* candidate_pad = item->GetPad();
  73. if( pad == candidate_pad )
  74. continue;
  75. if( !( pad->GetLayerSet() & candidate_pad->GetLayerSet() ).any() )
  76. continue;
  77. if( pad->HitTest( item->GetPoint() ) )
  78. {
  79. pad->m_PadsConnected.push_back( candidate_pad );
  80. }
  81. }
  82. }
  83. }
  84. /* Explores the list of pads
  85. * Adds to m_PadsConnected member of each track the pad(s) connected to
  86. * Adds to m_TracksConnected member of each pad the track(s) connected to
  87. * D_PAD::m_TracksConnected is cleared before adding items
  88. * TRACK::m_PadsConnected is not cleared
  89. */
  90. void CONNECTIONS::SearchTracksConnectedToPads( bool add_to_padlist, bool add_to_tracklist)
  91. {
  92. std::vector<CONNECTED_POINT*> candidates;
  93. for( unsigned ii = 0; ii < m_sortedPads.size(); ii++ )
  94. {
  95. D_PAD * pad = m_sortedPads[ii];
  96. pad->m_TracksConnected.clear();
  97. candidates.clear();
  98. CollectItemsNearTo( candidates, pad->GetPosition(), pad->GetBoundingRadius() );
  99. // add this pad to track.m_PadsConnected, if it is connected
  100. for( unsigned jj = 0; jj < candidates.size(); jj++ )
  101. {
  102. CONNECTED_POINT* cp_item = candidates[jj];
  103. if( !( pad->GetLayerSet() & cp_item->GetTrack()->GetLayerSet() ).any() )
  104. continue;
  105. if( pad->HitTest( cp_item->GetPoint() ) )
  106. {
  107. if( add_to_padlist )
  108. cp_item->GetTrack()->m_PadsConnected.push_back( pad );
  109. if( add_to_tracklist )
  110. pad->m_TracksConnected.push_back( cp_item->GetTrack() );
  111. }
  112. }
  113. }
  114. }
  115. void CONNECTIONS::CollectItemsNearTo( std::vector<CONNECTED_POINT*>& aList,
  116. const wxPoint& aPosition, int aDistMax )
  117. {
  118. /* Search items in m_Candidates that position is <= aDistMax from aPosition
  119. * (Rectilinear distance)
  120. * m_Candidates is sorted by X then Y values, so a fast binary search is used
  121. * to locate the "best" entry point in list
  122. * The best entry is a pad having its m_Pos.x == (or near) aPosition.x
  123. * All candidates are near this candidate in list
  124. * So from this entry point, a linear search is made to find all candidates
  125. */
  126. int idxmax = m_candidates.size()-1;
  127. int delta = m_candidates.size();
  128. int idx = 0; // Starting index is the beginning of list
  129. while( delta )
  130. {
  131. // Calculate half size of remaining interval to test.
  132. // Ensure the computed value is not truncated (too small)
  133. if( (delta & 1) && ( delta > 1 ) )
  134. delta++;
  135. delta /= 2;
  136. CONNECTED_POINT& item = m_candidates[idx];
  137. int dist = item.GetPoint().x - aPosition.x;
  138. if( abs(dist) <= aDistMax )
  139. {
  140. break; // A good entry point is found. The list can be scanned from this point.
  141. }
  142. else if( item.GetPoint().x < aPosition.x ) // We should search after this item
  143. {
  144. idx += delta;
  145. if( idx > idxmax )
  146. idx = idxmax;
  147. }
  148. else // We should search before this item
  149. {
  150. idx -= delta;
  151. if( idx < 0 )
  152. idx = 0;
  153. }
  154. }
  155. /* Now explore the candidate list from the "best" entry point found
  156. * (candidate "near" aPosition.x)
  157. * We explore the list until abs(candidate->m_Point.x - aPosition.x) > aDistMax
  158. * because the list is sorted by X position (and for a given X pos, by Y pos)
  159. * Currently a linear search is made because the number of candidates
  160. * having the right X position is usually small
  161. */
  162. // search next candidates in list
  163. wxPoint diff;
  164. for( int ii = idx; ii <= idxmax; ii++ )
  165. {
  166. CONNECTED_POINT* item = &m_candidates[ii];
  167. diff = item->GetPoint() - aPosition;
  168. if( abs(diff.x) > aDistMax )
  169. break; // Exit: the distance is to long, we cannot find other candidates
  170. if( abs(diff.y) > aDistMax )
  171. continue; // the y distance is to long, but we can find other candidates
  172. // We have here a good candidate: add it
  173. aList.push_back( item );
  174. }
  175. // search previous candidates in list
  176. for( int ii = idx-1; ii >=0; ii-- )
  177. {
  178. CONNECTED_POINT * item = &m_candidates[ii];
  179. diff = item->GetPoint() - aPosition;
  180. if( abs(diff.x) > aDistMax )
  181. break;
  182. if( abs(diff.y) > aDistMax )
  183. continue;
  184. // We have here a good candidate:add it
  185. aList.push_back( item );
  186. }
  187. }
  188. void CONNECTIONS::BuildPadsCandidatesList()
  189. {
  190. m_candidates.clear();
  191. m_candidates.reserve( m_sortedPads.size() );
  192. for( unsigned ii = 0; ii < m_sortedPads.size(); ii++ )
  193. {
  194. D_PAD * pad = m_sortedPads[ii];
  195. CONNECTED_POINT candidate( pad, pad->GetPosition() );
  196. m_candidates.push_back( candidate );
  197. }
  198. }
  199. /* sort function used to sort .m_Connected by X the Y values
  200. * items are sorted by X coordinate value,
  201. * and for same X value, by Y coordinate value.
  202. */
  203. static bool sortConnectedPointByXthenYCoordinates( const CONNECTED_POINT & aRef,
  204. const CONNECTED_POINT & aTst )
  205. {
  206. if( aRef.GetPoint().x == aTst.GetPoint().x )
  207. return aRef.GetPoint().y < aTst.GetPoint().y;
  208. return aRef.GetPoint().x < aTst.GetPoint().x;
  209. }
  210. void CONNECTIONS::BuildTracksCandidatesList( TRACK* aBegin, TRACK* aEnd)
  211. {
  212. m_candidates.clear();
  213. m_firstTrack = m_lastTrack = aBegin;
  214. unsigned ii = 0;
  215. // Count candidates ( i.e. end points )
  216. for( const TRACK* track = aBegin; track; track = track->Next() )
  217. {
  218. if( track->Type() == PCB_VIA_T )
  219. ii++;
  220. else
  221. ii += 2;
  222. m_lastTrack = track;
  223. if( track == aEnd )
  224. break;
  225. }
  226. // Build candidate list
  227. m_candidates.reserve( ii );
  228. for( TRACK* track = aBegin; track; track = track->Next() )
  229. {
  230. CONNECTED_POINT candidate( track, track->GetStart() );
  231. m_candidates.push_back( candidate );
  232. if( track->Type() != PCB_VIA_T )
  233. {
  234. CONNECTED_POINT candidate2( track, track->GetEnd());
  235. m_candidates.push_back( candidate2 );
  236. }
  237. if( track == aEnd )
  238. break;
  239. }
  240. // Sort list by increasing X coordinate,
  241. // and for increasing Y coordinate when items have the same X coordinate
  242. // So candidates to the same location are consecutive in list.
  243. sort( m_candidates.begin(), m_candidates.end(), sortConnectedPointByXthenYCoordinates );
  244. }
  245. /* Populates .m_connected with tracks/vias connected to aTrack
  246. * param aTrack = track or via to use as reference
  247. * For calculation time reason, an exhaustive search cannot be made
  248. * and a proximity search is made:
  249. * Only tracks with one end near one end of aTrack are collected.
  250. * near means dist <= aTrack width / 2
  251. * because with this constraint we can make a fast search in track list
  252. * m_candidates is expected to be populated by the track candidates ends list
  253. */
  254. int CONNECTIONS::SearchConnectedTracks( const TRACK* aTrack )
  255. {
  256. int count = 0;
  257. m_connected.clear();
  258. LSET layerMask = aTrack->GetLayerSet();
  259. // Search for connections to starting point:
  260. #define USE_EXTENDED_SEARCH
  261. #ifdef USE_EXTENDED_SEARCH
  262. int dist_max = aTrack->GetWidth() / 2;
  263. static std::vector<CONNECTED_POINT*> tracks_candidates;
  264. #endif
  265. wxPoint position = aTrack->GetStart();
  266. for( int kk = 0; kk < 2; kk++ )
  267. {
  268. #ifndef USE_EXTENDED_SEARCH
  269. int idx = searchEntryPointInCandidatesList( position );
  270. if( idx >= 0 )
  271. {
  272. // search after:
  273. for( unsigned ii = idx; ii < m_candidates.size(); ii ++ )
  274. {
  275. if( m_candidates[ii].GetTrack() == aTrack )
  276. continue;
  277. if( m_candidates[ii].GetPoint() != position )
  278. break;
  279. if( ( m_candidates[ii].GetTrack()->GetLayerSet() & layerMask ).any() )
  280. m_connected.push_back( m_candidates[ii].GetTrack() );
  281. }
  282. // search before:
  283. for( int ii = idx-1; ii >= 0; ii -- )
  284. {
  285. if( m_candidates[ii].GetTrack() == aTrack )
  286. continue;
  287. if( m_candidates[ii].GetPoint() != position )
  288. break;
  289. if( ( m_candidates[ii].GetTrack()->GetLayerSet() & layerMask ).any() )
  290. m_connected.push_back( m_candidates[ii].GetTrack() );
  291. }
  292. }
  293. #else
  294. tracks_candidates.clear();
  295. CollectItemsNearTo( tracks_candidates, position, dist_max );
  296. for( unsigned ii = 0; ii < tracks_candidates.size(); ii++ )
  297. {
  298. TRACK* ctrack = tracks_candidates[ii]->GetTrack();
  299. if( !( ctrack->GetLayerSet() & layerMask ).any() )
  300. continue;
  301. if( ctrack == aTrack )
  302. continue;
  303. // We have a good candidate: calculate the actual distance
  304. // between ends, which should be <= dist max.
  305. wxPoint delta = tracks_candidates[ii]->GetPoint() - position;
  306. int dist = KiROUND( EuclideanNorm( delta ) );
  307. if( dist > dist_max )
  308. continue;
  309. m_connected.push_back( ctrack );
  310. }
  311. #endif
  312. // Search for connections to ending point:
  313. if( aTrack->Type() == PCB_VIA_T )
  314. break;
  315. position = aTrack->GetEnd();
  316. }
  317. return count;
  318. }
  319. int CONNECTIONS::searchEntryPointInCandidatesList( const wxPoint& aPoint )
  320. {
  321. // Search the aPoint coordinates in m_Candidates
  322. // m_Candidates is sorted by X then Y values, and a fast binary search is used
  323. int idxmax = m_candidates.size()-1;
  324. int delta = m_candidates.size();
  325. int idx = 0; // Starting index is the beginning of list
  326. while( delta )
  327. {
  328. // Calculate half size of remaining interval to test.
  329. // Ensure the computed value is not truncated (too small)
  330. if( ( delta & 1 ) && ( delta > 1 ) )
  331. delta++;
  332. delta /= 2;
  333. CONNECTED_POINT& candidate = m_candidates[idx];
  334. if( candidate.GetPoint() == aPoint ) // candidate found
  335. {
  336. return idx;
  337. }
  338. // Not found: test the middle of the remaining sub list
  339. if( candidate.GetPoint().x == aPoint.x ) // Must search considering Y coordinate
  340. {
  341. if(candidate.GetPoint().y < aPoint.y) // Must search after this item
  342. {
  343. idx += delta;
  344. if( idx > idxmax )
  345. idx = idxmax;
  346. }
  347. else // Must search before this item
  348. {
  349. idx -= delta;
  350. if( idx < 0 )
  351. idx = 0;
  352. }
  353. }
  354. else if( candidate.GetPoint().x < aPoint.x ) // Must search after this item
  355. {
  356. idx += delta;
  357. if( idx > idxmax )
  358. idx = idxmax;
  359. }
  360. else // Must search before this item
  361. {
  362. idx -= delta;
  363. if( idx < 0 )
  364. idx = 0;
  365. }
  366. }
  367. return -1;
  368. }
  369. /* Used after a track change (delete a track ou add a track)
  370. * Connections to pads are recalculated
  371. * Note also aFirstTrack (and aLastTrack ) can be NULL
  372. */
  373. void CONNECTIONS::Build_CurrNet_SubNets_Connections( TRACK* aFirstTrack, TRACK* aLastTrack, int aNetcode )
  374. {
  375. m_firstTrack = aFirstTrack; // The first track used to build m_Candidates
  376. m_lastTrack = aLastTrack; // The last track used to build m_Candidates
  377. // Pads subnets are expected already cleared, because this function
  378. // does not know the full list of pads
  379. BuildTracksCandidatesList( aFirstTrack, aLastTrack );
  380. TRACK* curr_track;
  381. for( curr_track = aFirstTrack; curr_track != NULL; curr_track = curr_track->Next() )
  382. {
  383. // Clear track subnet id (Pads subnets are cleared outside this function)
  384. curr_track->SetSubNet( 0 );
  385. curr_track->m_TracksConnected.clear();
  386. curr_track->m_PadsConnected.clear();
  387. // Update connections between tracks:
  388. SearchConnectedTracks( curr_track );
  389. curr_track->m_TracksConnected = m_connected;
  390. if( curr_track == aLastTrack )
  391. break;
  392. }
  393. // Update connections between tracks and pads
  394. BuildPadsList( aNetcode );
  395. SearchTracksConnectedToPads();
  396. // Update connections between intersecting pads (no tracks)
  397. SearchConnectionsPadsToIntersectingPads();
  398. // Creates sub nets (clusters) for the current net:
  399. Propagate_SubNets();
  400. }
  401. /**
  402. * Change a subnet value to a new value, in m_sortedPads pad list
  403. * After that, 2 cluster (or subnets) are merged into only one.
  404. * Note: the resulting subnet value is the smallest between aOldSubNet et aNewSubNet
  405. */
  406. int CONNECTIONS::Merge_PadsSubNets( int aOldSubNet, int aNewSubNet )
  407. {
  408. int change_count = 0;
  409. if( aOldSubNet == aNewSubNet )
  410. return 0;
  411. if( (aOldSubNet > 0) && (aOldSubNet < aNewSubNet) )
  412. EXCHG( aOldSubNet, aNewSubNet );
  413. // Examine connections between intersecting pads
  414. for( unsigned ii = 0; ii < m_sortedPads.size(); ii++ )
  415. {
  416. D_PAD * curr_pad = m_sortedPads[ii];
  417. if( curr_pad->GetSubNet() != aOldSubNet )
  418. continue;
  419. change_count++;
  420. curr_pad->SetSubNet( aNewSubNet );
  421. }
  422. return change_count;
  423. }
  424. /*
  425. * Change a subnet value to a new value, for tracks and pads which are connected to.
  426. * The result is merging 2 clusters (or subnets) into only one cluster.
  427. * Note: the resulting sub net value is the smallest between aOldSubNet et aNewSubNet
  428. */
  429. int CONNECTIONS::Merge_SubNets( int aOldSubNet, int aNewSubNet )
  430. {
  431. TRACK* curr_track;
  432. int change_count = 0;
  433. if( aOldSubNet == aNewSubNet )
  434. return 0;
  435. if( (aOldSubNet > 0) && (aOldSubNet < aNewSubNet) )
  436. EXCHG( aOldSubNet, aNewSubNet );
  437. curr_track = (TRACK*)m_firstTrack;
  438. for( ; curr_track != NULL; curr_track = curr_track->Next() )
  439. {
  440. if( curr_track->GetSubNet() != aOldSubNet )
  441. {
  442. if( curr_track == m_lastTrack )
  443. break;
  444. continue;
  445. }
  446. change_count++;
  447. curr_track->SetSubNet( aNewSubNet );
  448. for( unsigned ii = 0; ii < curr_track->m_PadsConnected.size(); ii++ )
  449. {
  450. D_PAD * pad = curr_track->m_PadsConnected[ii];
  451. if( pad->GetSubNet() == aOldSubNet )
  452. {
  453. pad->SetSubNet( curr_track->GetSubNet() );
  454. }
  455. }
  456. if( curr_track == m_lastTrack )
  457. break;
  458. }
  459. return change_count;
  460. }
  461. /* Test a list of track segments, to create or propagate a sub netcode to pads and
  462. * segments connected together.
  463. * The track list must be sorted by nets, and all segments
  464. * from m_firstTrack to m_lastTrack have the same net
  465. * When 2 items are connected (a track to a pad, or a track to an other track),
  466. * they are grouped in a cluster.
  467. * The .m_Subnet member is the cluster identifier (subnet id)
  468. * For a given net, if all tracks are created, there is only one cluster.
  469. * but if not all tracks are created, there are more than one cluster,
  470. * and some ratsnests will be left active.
  471. * A ratsnest is active when it "connect" 2 items having different subnet id
  472. */
  473. void CONNECTIONS::Propagate_SubNets()
  474. {
  475. int sub_netcode = 1;
  476. TRACK* curr_track = (TRACK*)m_firstTrack;
  477. if( curr_track )
  478. curr_track->SetSubNet( sub_netcode );
  479. // Examine connections between tracks and pads
  480. for( ; curr_track != NULL; curr_track = curr_track->Next() )
  481. {
  482. // First: handling connections to pads
  483. for( unsigned ii = 0; ii < curr_track->m_PadsConnected.size(); ii++ )
  484. {
  485. D_PAD * pad = curr_track->m_PadsConnected[ii];
  486. if( curr_track->GetSubNet() ) // the track segment is already a cluster member
  487. {
  488. if( pad->GetSubNet() > 0 )
  489. {
  490. // The pad is already a cluster member, so we can merge the 2 clusters
  491. Merge_SubNets( pad->GetSubNet(), curr_track->GetSubNet() );
  492. }
  493. else
  494. {
  495. /* The pad is not yet attached to a cluster , so we can add this pad to
  496. * the cluster */
  497. pad->SetSubNet( curr_track->GetSubNet() );
  498. }
  499. }
  500. else // the track segment is not attached to a cluster
  501. {
  502. if( pad->GetSubNet() > 0 )
  503. {
  504. // it is connected to a pad in a cluster, merge this track
  505. curr_track->SetSubNet( pad->GetSubNet() );
  506. }
  507. else
  508. {
  509. /* it is connected to a pad not in a cluster, so we must create a new
  510. * cluster (only with the 2 items: the track and the pad) */
  511. sub_netcode++;
  512. curr_track->SetSubNet( sub_netcode );
  513. pad->SetSubNet( curr_track->GetSubNet() );
  514. }
  515. }
  516. }
  517. // Test connections between segments
  518. for( unsigned ii = 0; ii < curr_track->m_TracksConnected.size(); ii++ )
  519. {
  520. BOARD_CONNECTED_ITEM* track = curr_track->m_TracksConnected[ii];
  521. if( curr_track->GetSubNet() ) // The current track is already a cluster member
  522. {
  523. // The other track is already a cluster member, so we can merge the 2 clusters
  524. if( track->GetSubNet() )
  525. {
  526. Merge_SubNets( track->GetSubNet(), curr_track->GetSubNet() );
  527. }
  528. else
  529. {
  530. // The other track is not yet attached to a cluster , so we can add this
  531. // other track to the cluster
  532. track->SetSubNet( curr_track->GetSubNet() );
  533. }
  534. }
  535. else // the current track segment is not yet attached to a cluster
  536. {
  537. if( track->GetSubNet() )
  538. {
  539. // The other track is already a cluster member, so we can add
  540. // the current segment to the cluster
  541. curr_track->SetSubNet( track->GetSubNet() );
  542. }
  543. else
  544. {
  545. // it is connected to an other segment not in a cluster, so we must
  546. // create a new cluster (only with the 2 track segments)
  547. sub_netcode++;
  548. curr_track->SetSubNet( sub_netcode );
  549. track->SetSubNet( curr_track->GetSubNet() );
  550. }
  551. }
  552. }
  553. if( curr_track == m_lastTrack )
  554. break;
  555. }
  556. // Examine connections between intersecting pads, and propagate
  557. // sub_netcodes to intersecting pads
  558. for( unsigned ii = 0; ii < m_sortedPads.size(); ii++ )
  559. {
  560. D_PAD* curr_pad = m_sortedPads[ii];
  561. for( unsigned jj = 0; jj < curr_pad->m_PadsConnected.size(); jj++ )
  562. {
  563. D_PAD* pad = curr_pad->m_PadsConnected[jj];
  564. if( curr_pad->GetSubNet() ) // the current pad is already attached to a cluster
  565. {
  566. if( pad->GetSubNet() > 0 )
  567. {
  568. // The pad is already a cluster member, so we can merge the 2 clusters
  569. // Store the initial subnets, which will be modified by Merge_PadsSubNets
  570. int subnet1 = pad->GetSubNet();
  571. int subnet2 = curr_pad->GetSubNet();
  572. // merge subnets of pads only, even those not connected by tracks
  573. Merge_PadsSubNets( subnet1, subnet2 );
  574. // merge subnets of tracks (and pads, which are already merged)
  575. Merge_SubNets( subnet1, subnet2 );
  576. }
  577. else
  578. {
  579. // The pad is not yet attached to a cluster,
  580. // so we can add this pad to the cluster
  581. pad->SetSubNet( curr_pad->GetSubNet() );
  582. }
  583. }
  584. else // the current pad is not attached to a cluster
  585. {
  586. if( pad->GetSubNet() > 0 )
  587. {
  588. // the connected pad is in a cluster,
  589. // so we can add the current pad to the cluster
  590. curr_pad->SetSubNet( pad->GetSubNet() );
  591. }
  592. else
  593. {
  594. // the connected pad is not in a cluster,
  595. // so we must create a new cluster, with the 2 pads.
  596. sub_netcode++;
  597. curr_pad->SetSubNet( sub_netcode );
  598. pad->SetSubNet( curr_pad->GetSubNet() );
  599. }
  600. }
  601. }
  602. }
  603. }
  604. /*
  605. * Test all connections of the board,
  606. * and update subnet variable of pads and tracks
  607. * TestForActiveLinksInRatsnest must be called after this function
  608. * to update active/inactive ratsnest items status
  609. */
  610. void PCB_BASE_FRAME::TestConnections()
  611. {
  612. // Clear the cluster identifier for all pads
  613. for( unsigned i = 0; i< m_Pcb->GetPadCount(); ++i )
  614. {
  615. D_PAD* pad = m_Pcb->GetPad(i);
  616. pad->SetZoneSubNet( 0 );
  617. pad->SetSubNet( 0 );
  618. }
  619. m_Pcb->Test_Connections_To_Copper_Areas();
  620. // Test existing connections net by net
  621. // note some nets can have no tracks, and pads intersecting
  622. // so Build_CurrNet_SubNets_Connections must be called for each net
  623. CONNECTIONS connections( m_Pcb );
  624. int last_net_tested = 0;
  625. int current_net_code = 0;
  626. for( TRACK* track = m_Pcb->m_Track; track; )
  627. {
  628. // At this point, track is the first track of a given net
  629. current_net_code = track->GetNetCode();
  630. // Get last track of the current net
  631. TRACK* lastTrack = track->GetEndNetCode( current_net_code );
  632. if( current_net_code > 0 ) // do not spend time if net code = 0 ( dummy net )
  633. {
  634. // Test all previous nets having no tracks
  635. for( int net = last_net_tested+1; net < current_net_code; net++ )
  636. connections.Build_CurrNet_SubNets_Connections( NULL, NULL, net );
  637. connections.Build_CurrNet_SubNets_Connections( track, lastTrack, current_net_code );
  638. last_net_tested = current_net_code;
  639. }
  640. track = lastTrack->Next(); // this is now the first track of the next net
  641. }
  642. // Test last nets without tracks, if any
  643. int netsCount = m_Pcb->GetNetCount();
  644. for( int net = last_net_tested+1; net < netsCount; net++ )
  645. connections.Build_CurrNet_SubNets_Connections( NULL, NULL, net );
  646. Merge_SubNets_Connected_By_CopperAreas( m_Pcb );
  647. return;
  648. }
  649. void PCB_BASE_FRAME::TestNetConnection( wxDC* aDC, int aNetCode )
  650. {
  651. wxString msg;
  652. if( aNetCode <= 0 ) // -1 = not existing net, 0 = dummy net
  653. return;
  654. if( (m_Pcb->m_Status_Pcb & LISTE_RATSNEST_ITEM_OK) == 0 )
  655. Compile_Ratsnest( aDC, true );
  656. // Clear the cluster identifier (subnet) of pads for this net
  657. for( unsigned i = 0; i < m_Pcb->GetPadCount(); ++i )
  658. {
  659. D_PAD* pad = m_Pcb->GetPad(i);
  660. int pad_net_code = pad->GetNetCode();
  661. if( pad_net_code < aNetCode )
  662. continue;
  663. if( pad_net_code > aNetCode )
  664. break;
  665. pad->SetSubNet( 0 );
  666. }
  667. m_Pcb->Test_Connections_To_Copper_Areas( aNetCode );
  668. // Search for the first and the last segment relative to the given net code
  669. if( m_Pcb->m_Track )
  670. {
  671. CONNECTIONS connections( m_Pcb );
  672. TRACK* firstTrack;
  673. TRACK* lastTrack = NULL;
  674. firstTrack = m_Pcb->m_Track.GetFirst()->GetStartNetCode( aNetCode );
  675. if( firstTrack )
  676. lastTrack = firstTrack->GetEndNetCode( aNetCode );
  677. if( firstTrack && lastTrack ) // i.e. if there are segments
  678. {
  679. connections.Build_CurrNet_SubNets_Connections( firstTrack, lastTrack, firstTrack->GetNetCode() );
  680. }
  681. }
  682. Merge_SubNets_Connected_By_CopperAreas( m_Pcb, aNetCode );
  683. // rebuild the active ratsnest for this net
  684. DrawGeneralRatsnest( aDC, aNetCode );
  685. TestForActiveLinksInRatsnest( aNetCode );
  686. DrawGeneralRatsnest( aDC, aNetCode );
  687. // Display results
  688. int net_notconnected_count = 0;
  689. NETINFO_ITEM* net = m_Pcb->FindNet( aNetCode );
  690. if( net ) // Should not occur, but ...
  691. {
  692. for( unsigned ii = net->m_RatsnestStartIdx; ii < net->m_RatsnestEndIdx; ii++ )
  693. {
  694. if( m_Pcb->m_FullRatsnest[ii].IsActive() )
  695. net_notconnected_count++;
  696. }
  697. msg.Printf( wxT( "links %d nc %d net:nc %d" ),
  698. m_Pcb->GetRatsnestsCount(), m_Pcb->GetUnconnectedNetCount(),
  699. net_notconnected_count );
  700. }
  701. else
  702. msg.Printf( wxT( "net not found: netcode %d" ),aNetCode );
  703. SetStatusText( msg );
  704. return;
  705. }
  706. /* search connections between tracks and pads and propagate pad net codes to the track
  707. * segments.
  708. * Pads netcodes are assumed to be up to date.
  709. */
  710. void PCB_BASE_FRAME::RecalculateAllTracksNetcode()
  711. {
  712. // Build the net info list
  713. GetBoard()->BuildListOfNets();
  714. // Reset variables and flags used in computation
  715. for( TRACK* t = m_Pcb->m_Track; t; t = t->Next() )
  716. {
  717. t->m_TracksConnected.clear();
  718. t->m_PadsConnected.clear();
  719. t->start = NULL;
  720. t->end = NULL;
  721. t->SetState( BUSY | IN_EDIT | BEGIN_ONPAD | END_ONPAD, false );
  722. t->SetZoneSubNet( 0 );
  723. t->SetNetCode( NETINFO_LIST::UNCONNECTED );
  724. }
  725. // If no pad, reset pointers and netcode, and do nothing else
  726. if( m_Pcb->GetPadCount() == 0 )
  727. return;
  728. CONNECTIONS connections( m_Pcb );
  729. connections.BuildPadsList();
  730. connections.BuildTracksCandidatesList(m_Pcb->m_Track);
  731. // First pass: build connections between track segments and pads.
  732. connections.SearchTracksConnectedToPads();
  733. // For tracks connected to at least one pad,
  734. // set the track net code to the pad netcode
  735. for( TRACK* t = m_Pcb->m_Track; t; t = t->Next() )
  736. {
  737. if( t->m_PadsConnected.size() )
  738. t->SetNetCode( t->m_PadsConnected[0]->GetNetCode() );
  739. }
  740. // Pass 2: build connections between track ends
  741. for( TRACK* t = m_Pcb->m_Track; t; t = t->Next() )
  742. {
  743. connections.SearchConnectedTracks( t );
  744. connections.GetConnectedTracks( t );
  745. }
  746. // Propagate net codes from a segment to other connected segments
  747. bool new_pass_request = true; // set to true if a track has its netcode changed from 0
  748. // to a known netcode to re-evaluate netcodes
  749. // of connected items
  750. while( new_pass_request )
  751. {
  752. new_pass_request = false;
  753. for( TRACK* t = m_Pcb->m_Track; t; t = t->Next() )
  754. {
  755. int netcode = t->GetNetCode();
  756. if( netcode == 0 )
  757. {
  758. // try to find a connected item having a netcode
  759. for( unsigned kk = 0; kk < t->m_TracksConnected.size(); kk++ )
  760. {
  761. int altnetcode = t->m_TracksConnected[kk]->GetNetCode();
  762. if( altnetcode )
  763. {
  764. new_pass_request = true;
  765. netcode = altnetcode;
  766. t->SetNetCode(netcode);
  767. break;
  768. }
  769. }
  770. }
  771. if( netcode ) // this track has a netcode
  772. {
  773. // propagate this netcode to connected tracks having no netcode
  774. for( unsigned kk = 0; kk < t->m_TracksConnected.size(); kk++ )
  775. {
  776. int altnetcode = t->m_TracksConnected[kk]->GetNetCode();
  777. if( altnetcode == 0 )
  778. {
  779. t->m_TracksConnected[kk]->SetNetCode(netcode);
  780. new_pass_request = true;
  781. }
  782. }
  783. }
  784. }
  785. }
  786. // Sort the track list by net codes:
  787. RebuildTrackChain( m_Pcb );
  788. }
  789. /*
  790. * Function SortTracksByNetCode used in RebuildTrackChain()
  791. * to sort track segments by net code.
  792. */
  793. static bool SortTracksByNetCode( const TRACK* const & ref, const TRACK* const & compare )
  794. {
  795. // For items having the same Net, keep the order in list
  796. if( ref->GetNetCode() == compare->GetNetCode())
  797. return ref->m_Param < compare->m_Param;
  798. return ref->GetNetCode() < compare->GetNetCode();
  799. }
  800. /**
  801. * Helper function RebuildTrackChain
  802. * rebuilds the track segment linked list in order to have a chain
  803. * sorted by increasing netcodes.
  804. * We try to keep order of track segments in list, when possible
  805. * @param pcb = board to rebuild
  806. */
  807. static void RebuildTrackChain( BOARD* pcb )
  808. {
  809. if( pcb->m_Track == NULL )
  810. return;
  811. int item_count = pcb->m_Track.GetCount();
  812. std::vector<TRACK*> trackList;
  813. trackList.reserve( item_count );
  814. // Put track list in a temporary list to sort tracks by netcode
  815. // We try to keep the initial order of track segments in list, when possible
  816. // so we use m_Param (a member variable used for temporary storage)
  817. // to temporary keep trace of the order of segments
  818. // The sort function uses this variable to sort items that
  819. // have the same net code.
  820. // Without this, during sorting, the initial order is sometimes lost
  821. // by the sort algorithm
  822. for( int ii = 0; ii < item_count; ++ii )
  823. {
  824. pcb->m_Track->m_Param = ii;
  825. trackList.push_back( pcb->m_Track.PopFront() );
  826. }
  827. // the list is empty now
  828. wxASSERT( pcb->m_Track == NULL && pcb->m_Track.GetCount()==0 );
  829. sort( trackList.begin(), trackList.end(), SortTracksByNetCode );
  830. // add them back to the list
  831. for( int i = 0; i < item_count; ++i )
  832. pcb->m_Track.PushBack( trackList[i] );
  833. }