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.

825 lines
29 KiB

18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
  1. /***************************************************************************************/
  2. /* Rastnest calculations: Function to handle existing tracks in rastsnest calculations */
  3. /***************************************************************************************/
  4. #include "fctsys.h"
  5. #include "gr_basic.h"
  6. #include "common.h"
  7. #include "pcbnew.h"
  8. #include "protos.h"
  9. //#include <algorithm>
  10. extern void Merge_SubNets_Connected_By_CopperAreas( BOARD* aPcb );
  11. extern void Merge_SubNets_Connected_By_CopperAreas( BOARD* aPcb, int aNetcode );
  12. /* Local functions */
  13. static void Propagate_SubNet( TRACK* pt_start_conn, TRACK* pt_end_conn );
  14. static void Build_Pads_Info_Connections_By_Tracks( TRACK* pt_start_conn, TRACK* pt_end_conn );
  15. static void RebuildTrackChain( BOARD* pcb );
  16. /*..*/
  17. /**************************************************************************************************/
  18. static int Merge_Two_SubNets( TRACK* pt_start_conn, TRACK* pt_end_conn, int old_val, int new_val )
  19. /**************************************************************************************************/
  20. /** Function Merge_Two_SubNets()
  21. * Used by Propagate_SubNet()
  22. * Change a subnet value to a new value, for tracks ans pads which are connected to corresponding track
  23. * for pads and tracks, this is the .m_Subnet member that is tested and modified
  24. * these members are block numbers (or cluster numbers) for a given net
  25. * The result is merging 2 blocks (or subnets)
  26. * @return modification count
  27. * @param old_val = subnet value to modify
  28. * @param new_val = new subnet value for each item whith have old_val as subnet value
  29. * @param pt_start_conn = first track segment to test
  30. * @param pt_end_conn = last track segment to test
  31. * If pt_end_conn = NULL: search is made from pt_start_conn to end of linked list
  32. */
  33. {
  34. TRACK* pt_conn;
  35. int nb_change = 0;
  36. D_PAD* pt_pad;
  37. if( old_val == new_val )
  38. return 0;
  39. if( (old_val > 0) && (old_val < new_val) )
  40. EXCHG( old_val, new_val );
  41. pt_conn = pt_start_conn;
  42. for( ; pt_conn != NULL; pt_conn = pt_conn->Next() )
  43. {
  44. if( pt_conn->GetSubNet() != old_val )
  45. {
  46. if( pt_conn == pt_end_conn )
  47. break;
  48. continue;
  49. }
  50. nb_change++;
  51. pt_conn->SetSubNet( new_val );
  52. if( pt_conn->start && ( pt_conn->start->Type() == TYPE_PAD) )
  53. {
  54. pt_pad = (D_PAD*) (pt_conn->start);
  55. if( pt_pad->GetSubNet() == old_val )
  56. pt_pad->SetSubNet( pt_conn->GetSubNet() );
  57. }
  58. if( pt_conn->end && (pt_conn->end->Type() == TYPE_PAD) )
  59. {
  60. pt_pad = (D_PAD*) (pt_conn->end);
  61. if( pt_pad->GetSubNet() == old_val )
  62. pt_pad->SetSubNet( pt_conn->GetSubNet() );
  63. }
  64. if( pt_conn == pt_end_conn )
  65. break;
  66. }
  67. return nb_change;
  68. }
  69. /******************************************************************/
  70. static void Propagate_SubNet( TRACK* pt_start_conn, TRACK* pt_end_conn )
  71. /******************************************************************/
  72. /** Function Propagate_SubNet
  73. * Test a list of track segment, to create or propagate a sub netcode to pads and segments connected together
  74. * the track list must be sorted by nets, and all segments from pt_start_conn to pt_end_conn have the same net
  75. * When 2 items are connected (a track to a pad, or a track to an other track) they are grouped in a cluster.
  76. * for pads, this is the .m_physical_connexion member which is a cluster identifier
  77. * for tracks, this is the .m_Subnet member which is a cluster identifier
  78. * For a given net, if all tracks are created, there is only one cluster.
  79. * but if not all tracks are created, there are more than one cluster, and some ratsnets will be shown.
  80. * @param pt_start_conn = first track to test
  81. * @param pt_end_conn = last segment to test
  82. */
  83. {
  84. TRACK* pt_conn;
  85. int sub_netcode;
  86. D_PAD* pt_pad;
  87. TRACK* pt_autre_piste;
  88. BOARD_ITEM* PtStruct;
  89. /* Clear variables used in computations */
  90. pt_conn = pt_start_conn;
  91. for( ; pt_conn != NULL; pt_conn = pt_conn->Next() )
  92. {
  93. pt_conn->SetSubNet( 0 );
  94. PtStruct = pt_conn->start;
  95. if( PtStruct && (PtStruct->Type() == TYPE_PAD) )
  96. ( (D_PAD*) PtStruct )->SetSubNet( 0 );
  97. PtStruct = pt_conn->end;
  98. if( PtStruct && (PtStruct->Type() == TYPE_PAD) )
  99. ( (D_PAD*) PtStruct )->SetSubNet( 0 );
  100. if( pt_conn == pt_end_conn )
  101. break;
  102. }
  103. sub_netcode = 1;
  104. pt_start_conn->SetSubNet( sub_netcode );
  105. /* Start of calculation */
  106. pt_conn = pt_start_conn;
  107. for( ; pt_conn != NULL; pt_conn = pt_conn->Next() )
  108. {
  109. /* First: handling connections to pads */
  110. PtStruct = pt_conn->start;
  111. /* The segment starts on a pad */
  112. if( PtStruct && (PtStruct->Type() == TYPE_PAD) )
  113. {
  114. pt_pad = (D_PAD*) PtStruct;
  115. if( pt_conn->GetSubNet() ) /* the track segment is already a cluster member */
  116. {
  117. if( pt_pad->GetSubNet() > 0 ) /* The pad is already a cluster member, so we can merge the 2 clusters */
  118. {
  119. Merge_Two_SubNets( pt_start_conn, pt_end_conn,
  120. pt_pad->GetSubNet(), pt_conn->GetSubNet() );
  121. }
  122. else /* The pad is not yet attached to a cluster , so we can add this pad to the cluster */
  123. pt_pad->SetSubNet( pt_conn->GetSubNet() );
  124. }
  125. else /* the track segment is not attached to a cluster */
  126. {
  127. if( pt_pad->GetSubNet() > 0 ) /* it is connected to a pad in a cluster, merge this track */
  128. {
  129. pt_conn->SetSubNet( pt_pad->GetSubNet() );
  130. }
  131. else /* it is connected to a pad not in a cluster, so we must create a new cluster (only with the 2 items: the track and the pad) */
  132. {
  133. sub_netcode++;
  134. pt_conn->SetSubNet( sub_netcode );
  135. pt_pad->SetSubNet( pt_conn->GetSubNet() );
  136. }
  137. }
  138. }
  139. PtStruct = pt_conn->end;
  140. if( PtStruct && (PtStruct->Type() == TYPE_PAD) )
  141. /* The segment end on a pad */
  142. {
  143. pt_pad = (D_PAD*) PtStruct;
  144. if( pt_conn->GetSubNet() )
  145. {
  146. if( pt_pad->GetSubNet() > 0 )
  147. {
  148. Merge_Two_SubNets( pt_start_conn, pt_end_conn,
  149. pt_pad->GetSubNet(), pt_conn->GetSubNet() );
  150. }
  151. else
  152. pt_pad->SetSubNet( pt_conn->GetSubNet() );
  153. }
  154. else
  155. {
  156. if( pt_pad->GetSubNet() > 0 )
  157. {
  158. pt_conn->SetSubNet( pt_pad->GetSubNet() );
  159. }
  160. else
  161. {
  162. sub_netcode++;
  163. pt_conn->SetSubNet( sub_netcode );
  164. pt_pad->SetSubNet( pt_conn->GetSubNet() );
  165. }
  166. }
  167. }
  168. /* Test connections between segments */
  169. PtStruct = pt_conn->start;
  170. if( PtStruct && (PtStruct->Type() != TYPE_PAD) )
  171. {
  172. /* The segment starts on an other track */
  173. pt_autre_piste = (TRACK*) PtStruct;
  174. if( pt_conn->GetSubNet() ) /* the track segment is already a cluster member */
  175. {
  176. if( pt_autre_piste->GetSubNet() ) /* The other track is already a cluster member, so we can merge the 2 clusters */
  177. {
  178. Merge_Two_SubNets( pt_start_conn, pt_end_conn,
  179. pt_autre_piste->GetSubNet(), pt_conn->GetSubNet() );
  180. }
  181. else /* The other track is not yet attached to a cluster , so we can add this other track to the cluster */
  182. {
  183. pt_autre_piste->SetSubNet( pt_conn->GetSubNet() );
  184. }
  185. }
  186. else /* the track segment is not yet attached to a cluster */
  187. {
  188. if( pt_autre_piste->GetSubNet() ) /* The other track is already a cluster member, so we can add the segment to the cluster */
  189. {
  190. pt_conn->SetSubNet( pt_autre_piste->GetSubNet() );
  191. }
  192. else /* it is connected to an other segment not in a cluster, so we must create a new cluster (only with the 2 track segments) */
  193. {
  194. sub_netcode++;
  195. pt_conn->SetSubNet( sub_netcode );
  196. pt_autre_piste->SetSubNet( pt_conn->GetSubNet() );
  197. }
  198. }
  199. }
  200. PtStruct = pt_conn->end; // Do the same calculations for the segment end point
  201. if( PtStruct && (PtStruct->Type() != TYPE_PAD) )
  202. {
  203. pt_autre_piste = (TRACK*) PtStruct;
  204. if( pt_conn->GetSubNet() ) /* the track segment is already a cluster member */
  205. {
  206. if( pt_autre_piste->GetSubNet() )
  207. {
  208. Merge_Two_SubNets( pt_start_conn, pt_end_conn,
  209. pt_autre_piste->GetSubNet(), pt_conn->GetSubNet() );
  210. }
  211. else
  212. pt_autre_piste->SetSubNet( pt_conn->GetSubNet() );
  213. }
  214. else /* the track segment is not yet attached to a cluster */
  215. {
  216. if( pt_autre_piste->GetSubNet() )
  217. {
  218. pt_conn->SetSubNet( pt_autre_piste->GetSubNet() );
  219. }
  220. else
  221. {
  222. sub_netcode++;
  223. pt_conn->SetSubNet( sub_netcode );
  224. pt_autre_piste->SetSubNet( pt_conn->GetSubNet() );
  225. }
  226. }
  227. }
  228. if( pt_conn == pt_end_conn )
  229. break;
  230. }
  231. }
  232. /***************************************************/
  233. void WinEDA_BasePcbFrame::test_connexions( wxDC* DC )
  234. /***************************************************/
  235. /** Function testing the connections relative to all nets
  236. * This function update the status of the ratsnest ( flag CH_ACTIF = 0 if a connection is found, = 1 else)
  237. * track segments are assumed to be sorted by net codes.
  238. * This is the case because when a new track is added, it is inserted in the linked list according to its net code.
  239. * and when nets are changed (when a new netlist is read) tracks are sorted before using this function
  240. * @param DC = current Device Context
  241. */
  242. {
  243. // Clear the cluster identifier for all pads
  244. for( unsigned i = 0; i< m_Pcb->GetPadsCount(); ++i )
  245. {
  246. D_PAD* pad = m_Pcb->m_NetInfo->GetPad(i);
  247. pad->SetZoneSubNet( 0 );
  248. pad->SetSubNet( 0 );
  249. }
  250. m_Pcb->Test_Connections_To_Copper_Areas();
  251. // Test existing connections net by net
  252. for( TRACK* track = m_Pcb->m_Track; track; )
  253. {
  254. // this is the current net because pt_start_conn is the first segment of the net
  255. int current_net_code = track->GetNet();
  256. // this is the last segment of the current net
  257. TRACK* pt_end_conn = track->GetEndNetCode( current_net_code );
  258. Build_Pads_Info_Connections_By_Tracks( track, pt_end_conn );
  259. track = pt_end_conn->Next(); // this is now the first segment of the next net
  260. }
  261. Merge_SubNets_Connected_By_CopperAreas( m_Pcb );
  262. return;
  263. }
  264. /*************************************************************************/
  265. void WinEDA_BasePcbFrame::test_1_net_connexion( wxDC* DC, int net_code )
  266. /*************************************************************************/
  267. /** Function testing the connections relative to a given net
  268. * track segments are assumed to be sorted by net codes
  269. * @param DC = current Device Context
  270. * @param net_code = net code to test
  271. */
  272. {
  273. wxString msg;
  274. if( net_code == 0 )
  275. return;
  276. if( (m_Pcb->m_Status_Pcb & LISTE_RATSNEST_ITEM_OK) == 0 )
  277. Compile_Ratsnest( DC, TRUE );
  278. for( unsigned i = 0; i<m_Pcb->GetPadsCount(); ++i )
  279. {
  280. D_PAD* pad = m_Pcb->m_NetInfo->GetPad(i);
  281. int pad_net_code = pad->GetNet();
  282. if( pad_net_code < net_code )
  283. continue;
  284. if( pad_net_code > net_code )
  285. break;
  286. pad->SetSubNet( 0 );
  287. }
  288. m_Pcb->Test_Connections_To_Copper_Areas( net_code );
  289. /* Search for the first and the last segment relative to the given net code */
  290. if( m_Pcb->m_Track )
  291. {
  292. TRACK* pt_start_conn;
  293. TRACK* pt_end_conn = NULL;
  294. pt_start_conn = m_Pcb->m_Track.GetFirst()->GetStartNetCode( net_code );
  295. if( pt_start_conn )
  296. pt_end_conn = pt_start_conn->GetEndNetCode( net_code );
  297. if( pt_start_conn && pt_end_conn ) // c.a.d. s'il y a des segments
  298. {
  299. Build_Pads_Info_Connections_By_Tracks( pt_start_conn, pt_end_conn );
  300. }
  301. }
  302. Merge_SubNets_Connected_By_CopperAreas( m_Pcb, net_code );
  303. /* Test the rastnest for this net */
  304. int nb_net_noconnect = Test_1_Net_Ratsnest( DC, net_code );
  305. /* Display results */
  306. msg.Printf( wxT( "links %d nc %d net:nc %d" ),
  307. m_Pcb->GetRatsnestsCount(), m_Pcb->GetNoconnectCount(),
  308. nb_net_noconnect );
  309. Affiche_Message( msg );
  310. return;
  311. }
  312. /*******************************************************************************************/
  313. static void Build_Pads_Info_Connections_By_Tracks( TRACK* pt_start_conn, TRACK* pt_end_conn )
  314. /*******************************************************************************************/
  315. /** Used after a track change (delete a track ou add a track)
  316. * Compute connections (initialize the .start and .end members) for a single net.
  317. * tracks must be sorted by net, as usual
  318. * @param pt_start_conn = first segment of the net
  319. * @param pt_end_conn = last segment of the net
  320. * Connections to pads are assumed to be already initialized.
  321. * If a track is deleted, the other pointers to pads do not change.
  322. * When a track is added, its pointers to pads are already initialized
  323. */
  324. {
  325. TRACK* Track;
  326. /* Reset the old connections type track to track */
  327. for( Track = pt_start_conn; Track != NULL; Track = Track->Next() )
  328. {
  329. Track->SetSubNet( 0 );
  330. if( Track->GetState( BEGIN_ONPAD ) == 0 )
  331. Track->start = NULL;
  332. if( Track->GetState( END_ONPAD ) == 0 )
  333. Track->end = NULL;
  334. if( Track == pt_end_conn )
  335. break;
  336. }
  337. /* Update connections type track to track */
  338. for( Track = pt_start_conn; Track != NULL; Track = Track->Next() )
  339. {
  340. if( Track->Type() == TYPE_VIA ) // A via can connect many tracks, we must search for all track segments in this net
  341. {
  342. TRACK* pt_segm;
  343. int layermask = Track->ReturnMaskLayer();
  344. for( pt_segm = pt_start_conn; pt_segm != NULL; pt_segm = pt_segm->Next() )
  345. {
  346. int curlayermask = pt_segm->ReturnMaskLayer();
  347. if( !pt_segm->start && (pt_segm->m_Start == Track->m_Start)
  348. && ( layermask & curlayermask ) )
  349. {
  350. pt_segm->start = Track;
  351. }
  352. if( !pt_segm->end && (pt_segm->m_End == Track->m_Start)
  353. && (layermask & curlayermask) )
  354. {
  355. pt_segm->end = Track;
  356. }
  357. if( pt_segm == pt_end_conn )
  358. break;
  359. }
  360. }
  361. if( Track->start == NULL ) // end track not already connected, search a connection
  362. {
  363. Track->start = Locate_Piste_Connectee( Track, Track, pt_end_conn, START );
  364. }
  365. if( Track->end == NULL ) // end track not already connected, search a connection
  366. {
  367. Track->end = Locate_Piste_Connectee( Track, Track, pt_end_conn, END );
  368. }
  369. if( Track == pt_end_conn )
  370. break;
  371. }
  372. /* Creates sub nets (cluster) for the current net: */
  373. Propagate_SubNet( pt_start_conn, pt_end_conn );
  374. }
  375. #define POS_AFF_CHREF 62
  376. /**
  377. * Function SuperFast_Locate_Pad_Connecte
  378. * locates the pad connected to a track ended at coord px, py.
  379. * A track is seen as connected if the px, py position is same as the pad position.
  380. *
  381. * @param aPcb = the board.
  382. * @param pt_liste = Pointers to pads buffer
  383. * This buffer is a list like the list created by build_liste_pad, but sorted by increasing X pad coordinate
  384. * @param posref = reference coordinate
  385. * @param masque_layer = Layers (bit to bit) to consider
  386. * @return : pointer on the connected pad
  387. * This function uses a fast search in this sorted pad list and it is faster than Fast_Locate_Pad_connecte(),
  388. * But this sorted pad list must be built before calling this function.
  389. *
  390. * (Note: The usual pad list (created by build_liste_pad) m_Pcb->m_Pads is sorted by increasing netcodes )
  391. */
  392. static D_PAD* SuperFast_Locate_Pad_Connecte( BOARD* aPcb, LISTE_PAD* pt_liste,
  393. const wxPoint& posref, int masque_layer )
  394. {
  395. D_PAD* pad;
  396. int ii;
  397. int nb_pad = aPcb->GetPadsCount();
  398. LISTE_PAD* ptr_pad = pt_liste;
  399. LISTE_PAD* lim = pt_liste + nb_pad - 1;
  400. ptr_pad = pt_liste;
  401. while( nb_pad )
  402. {
  403. pad = *ptr_pad;
  404. ii = nb_pad;
  405. nb_pad >>= 1;
  406. if( (ii & 1) && ( ii > 1 ) )
  407. nb_pad++;
  408. if( pad->m_Pos.x < posref.x ) /* Must search after this item */
  409. {
  410. ptr_pad += nb_pad;
  411. if( ptr_pad > lim )
  412. ptr_pad = lim;
  413. continue;
  414. }
  415. if( pad->m_Pos.x > posref.x ) /* Must search before this item */
  416. {
  417. ptr_pad -= nb_pad;
  418. if( ptr_pad < pt_liste )
  419. ptr_pad = pt_liste;
  420. continue;
  421. }
  422. if( pad->m_Pos.x == posref.x ) /* A suitable block is found (X coordinate matches the px reference: but wue must matches the Y coordinate */
  423. {
  424. /* Search the beginning of the block */
  425. while( ptr_pad >= pt_liste )
  426. {
  427. pad = *ptr_pad;
  428. if( pad->m_Pos.x == posref.x )
  429. ptr_pad--;
  430. else
  431. break;
  432. }
  433. ptr_pad++; /* ptr_pad = first pad which have pad->m_Pos.x = px */
  434. for( ; ; ptr_pad++ )
  435. {
  436. if( ptr_pad > lim )
  437. return NULL; /* outside suitable block */
  438. pad = *ptr_pad;
  439. if( pad->m_Pos.x != posref.x )
  440. return NULL; /* outside suitable block */
  441. if( pad->m_Pos.y != posref.y )
  442. continue;
  443. /* A Pad if found here: but it must mach the layer */
  444. if( pad->m_Masque_Layer & masque_layer ) // Matches layer => a connected pad is found !
  445. return pad;
  446. }
  447. }
  448. }
  449. return NULL;
  450. }
  451. /**
  452. * Function SortPadsByXCoord
  453. * is used to Sort a pad list by x coordinate value.
  454. */
  455. static int SortPadsByXCoord( const void* pt_ref, const void* pt_comp )
  456. {
  457. D_PAD* ref = *(LISTE_PAD*) pt_ref;
  458. D_PAD* comp = *(LISTE_PAD*) pt_comp;
  459. return ref->m_Pos.x - comp->m_Pos.x;
  460. }
  461. /*****************************************************************************/
  462. void CreateSortedPadListByXCoord( BOARD* aBoard, std::vector<D_PAD*>* aVector )
  463. /*****************************************************************************/
  464. {
  465. aVector->insert( aVector->end(), aBoard->m_NetInfo->m_PadsFullList.begin(), aBoard->m_NetInfo->m_PadsFullList.end() );
  466. qsort( &(*aVector)[0], aBoard->GetPadsCount(), sizeof( D_PAD*), SortPadsByXCoord );
  467. }
  468. /********************************************************************/
  469. void WinEDA_BasePcbFrame::RecalculateAllTracksNetcode( )
  470. /********************************************************************/
  471. /* search connections between tracks and pads, and propagate pad net codes to the track segments
  472. * This is a 2 pass computation.
  473. * First:
  474. * We search a connection between a track segment and a pad: if found : this segment netcode is set to the pad netcode
  475. */
  476. {
  477. TRACK* pt_piste;
  478. TRACK* pt_next;
  479. int a_color;
  480. char new_passe_request = 1, flag;
  481. std::vector<D_PAD*> sortedPads;
  482. BOARD_ITEM* PtStruct;
  483. int masque_layer;
  484. wxString msg;
  485. // Build the net info list
  486. GetBoard()->m_NetInfo->BuildListOfNets();
  487. if( m_Pcb->GetPadsCount() == 0 ) // If no pad, reset pointers and netcode, and do nothing else
  488. {
  489. pt_piste = m_Pcb->m_Track;
  490. for( ; pt_piste != NULL; pt_piste = pt_piste->Next() )
  491. {
  492. pt_piste->start = NULL;
  493. pt_piste->SetState( BEGIN_ONPAD | END_ONPAD, OFF );
  494. pt_piste->SetNet( 0 );
  495. pt_piste->end = NULL;
  496. }
  497. return;
  498. }
  499. /**************************************************************/
  500. /* Pass 1: search the connections between track ends and pads */
  501. /**************************************************************/
  502. CreateSortedPadListByXCoord( m_Pcb, &sortedPads );
  503. /* Reset variables and flags used in computation */
  504. pt_piste = m_Pcb->m_Track;
  505. for( ; pt_piste != NULL; pt_piste = pt_piste->Next() )
  506. {
  507. pt_piste->SetState( BUSY | EDIT | BEGIN_ONPAD | END_ONPAD, OFF );
  508. pt_piste->SetZoneSubNet( 0 );
  509. pt_piste->SetNet( 0 ); // net code = 0 means not connected
  510. }
  511. /* First pass: search connection between a track segment and a pad.
  512. * if found, set the track net code to the pad netcode
  513. */
  514. pt_piste = m_Pcb->m_Track;
  515. for( ; pt_piste != NULL; pt_piste = pt_piste->Next() )
  516. {
  517. flag = 0;
  518. masque_layer = g_TabOneLayerMask[pt_piste->GetLayer()];
  519. /* Search for a pad on the segment starting point */
  520. pt_piste->start = SuperFast_Locate_Pad_Connecte( m_Pcb,
  521. &sortedPads[0],
  522. pt_piste->m_Start,
  523. masque_layer );
  524. if( pt_piste->start != NULL )
  525. {
  526. pt_piste->SetState( BEGIN_ONPAD, ON );
  527. pt_piste->SetNet( ( (D_PAD*) (pt_piste->start) )->GetNet() );
  528. }
  529. /* Search for a pad on the segment ending point */
  530. pt_piste->end = SuperFast_Locate_Pad_Connecte( m_Pcb,
  531. &sortedPads[0],
  532. pt_piste->m_End,
  533. masque_layer );
  534. if( pt_piste->end != NULL )
  535. {
  536. pt_piste->SetState( END_ONPAD, ON );
  537. pt_piste->SetNet( ( (D_PAD*) (pt_piste->end) )->GetNet() );
  538. }
  539. }
  540. /*****************************************************/
  541. /* Pass 2: search the connections between track ends */
  542. /*****************************************************/
  543. /* the .start et .end member pointers are updated, only if NULLs
  544. * (if not nuls, the end is already connected to a pad).
  545. * the connection (if found) is between segments
  546. * when a track has a net code and the other has a null net code, the null net code is changed
  547. */
  548. for( pt_piste = m_Pcb->m_Track; pt_piste != NULL; pt_piste = pt_piste->Next() )
  549. {
  550. if( pt_piste->start == NULL )
  551. {
  552. pt_piste->start = Locate_Piste_Connectee( pt_piste, m_Pcb->m_Track, NULL, START );
  553. }
  554. if( pt_piste->end == NULL )
  555. {
  556. pt_piste->end = Locate_Piste_Connectee( pt_piste, m_Pcb->m_Track, NULL, END );
  557. }
  558. }
  559. /**********************************************************/
  560. /* Propagate net codes from a segment to an other segment */
  561. /**********************************************************/
  562. a_color = YELLOW;
  563. while( new_passe_request )
  564. {
  565. bool reset_flag = FALSE;
  566. new_passe_request = 0;
  567. /* look for vias which could be connect many tracks */
  568. for( TRACK* via = m_Pcb->m_Track; via != NULL; via = via->Next() )
  569. {
  570. if( via->Type() != TYPE_VIA )
  571. continue;
  572. if( via->GetNet() > 0 )
  573. continue; // Netcode already known
  574. // Lock for a connection to a track with a known netcode
  575. pt_next = m_Pcb->m_Track;
  576. while( ( pt_next = Locate_Piste_Connectee( via, pt_next, NULL, START ) ) != NULL )
  577. {
  578. if( pt_next->GetNet() )
  579. {
  580. via->SetNet( pt_next->GetNet() );
  581. break;
  582. }
  583. pt_next->SetState( BUSY, ON );
  584. reset_flag = TRUE;
  585. }
  586. }
  587. if( reset_flag )
  588. for( pt_piste = m_Pcb->m_Track; pt_piste != NULL; pt_piste = pt_piste->Next() )
  589. {
  590. pt_piste->SetState( BUSY, OFF );
  591. }
  592. /* set the netcode of connected tracks: if at track is connected to a pad, its net code is already set.
  593. * if the current track is connected to an other track:
  594. * if a track has a net code, it is used for the other track.
  595. * Thus there is a propagation of the netcode from a track to an other.
  596. * if none of the 2 track has a net code we do nothing
  597. * the iteration is stopped when no new change occurs
  598. */
  599. for( pt_piste = m_Pcb->m_Track; pt_piste != NULL; pt_piste = pt_piste->Next() )
  600. {
  601. /* look for the connection to the current segment starting point */
  602. PtStruct = (BOARD_ITEM*) pt_piste->start;
  603. if( PtStruct && (PtStruct->Type() != TYPE_PAD) )
  604. {
  605. // Begin on an other track segment
  606. pt_next = (TRACK*) PtStruct;
  607. if( pt_piste->GetNet() )
  608. {
  609. if( pt_next->GetNet() == 0 ) // the current track has a netcode, we use it for the other track
  610. {
  611. new_passe_request = 1; // A change is made: a new iteration is requested.
  612. pt_next->SetNet( pt_piste->GetNet() );
  613. }
  614. }
  615. else
  616. {
  617. if( pt_next->GetNet() != 0 ) // the other track has a netcode, we use it for the current track
  618. {
  619. pt_piste->SetNet( pt_next->GetNet() );
  620. new_passe_request = 1;
  621. }
  622. }
  623. }
  624. /* look for the connection to the current segment ending point */
  625. PtStruct = pt_piste->end;
  626. if( PtStruct &&(PtStruct->Type() != TYPE_PAD) )
  627. {
  628. pt_next = (TRACK*) PtStruct;
  629. // End on an other track: propagate netcode if possible
  630. if( pt_piste->GetNet() )
  631. {
  632. if( pt_next->GetNet() == 0 )
  633. {
  634. new_passe_request = 1;
  635. pt_next->SetNet( pt_piste->GetNet() );
  636. }
  637. }
  638. else
  639. {
  640. if( pt_next->GetNet() != 0 )
  641. {
  642. pt_piste->SetNet( pt_next->GetNet() );
  643. new_passe_request = 1;
  644. }
  645. }
  646. }
  647. }
  648. }
  649. /* Sort the track list by net codes: */
  650. RebuildTrackChain( m_Pcb );
  651. }
  652. /**
  653. * Function Sort_By_NetCode
  654. * sorts track segments used in RebuildTrackChain() (for the qsort C function)
  655. * The sorting is made by net code.
  656. */
  657. static int Sort_By_NetCode( const void* left, const void* right )
  658. {
  659. TRACK* pt_ref = *(TRACK**) left;
  660. TRACK* pt_compare = *(TRACK**) right;
  661. int ret = pt_ref->GetNet() - pt_compare->GetNet();
  662. return ret;
  663. }
  664. /**
  665. * Function RebuildTrackChain
  666. * rebuilds the track segment linked list in order to have a chain
  667. * sorted by increasing netcodes.
  668. * @param pcb = board to rebuild
  669. */
  670. static void RebuildTrackChain( BOARD* pcb )
  671. {
  672. if( pcb->m_Track == NULL )
  673. return;
  674. int nbsegm = pcb->m_Track.GetCount();
  675. TRACK** array = (TRACK**) MyZMalloc( nbsegm * sizeof(TRACK*) );
  676. for( int i = 0; i<nbsegm; ++i )
  677. {
  678. array[i] = pcb->m_Track.PopFront();
  679. wxASSERT( array[i] );
  680. }
  681. // the list is empty now
  682. wxASSERT( pcb->m_Track == NULL && pcb->m_Track.GetCount()==0 );
  683. qsort( array, nbsegm, sizeof(TRACK*), Sort_By_NetCode );
  684. // add them back to the list
  685. for( int i = 0; i<nbsegm; ++i )
  686. {
  687. pcb->m_Track.PushBack( array[i] );
  688. }
  689. MyFree( array );
  690. }