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.

842 lines
30 KiB

  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 "autorout.h"
  9. #include "protos.h"
  10. /* Loca functions */
  11. static void propage_equipot( TRACK* pt_start_conn, TRACK* pt_end_conn );
  12. static void calcule_connexite_1_net( TRACK* pt_start_conn, TRACK* pt_end_conn );
  13. static void RebuildTrackChain( BOARD* pcb );
  14. static int Sort_By_NetCode( TRACK** pt_ref, TRACK** pt_compare );
  15. /*..*/
  16. /*****************************************************************/
  17. static int change_equipot( TRACK* pt_start_conn, TRACK* pt_end_conn,
  18. int old_val, int new_val )
  19. /*****************************************************************/
  20. /** Function change_equipot()
  21. * Used by propage_equipot()
  22. * Change a subnet value to a new value, for tracks ans pads which are connected to corresponding track
  23. * for pads, this is the .m_physical_connexion member which is tested and modified
  24. * for tracks, this is the .m_Sous_Netcode member which is tested and modified
  25. * these members are block numbers (or cluster numbers) for a given net
  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 = (TRACK*) pt_conn->Pnext )
  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() == TYPEPAD) )
  53. {
  54. pt_pad = (D_PAD*) (pt_conn->start);
  55. if( pt_pad->m_physical_connexion == old_val )
  56. pt_pad->m_physical_connexion = pt_conn->GetSubNet();
  57. }
  58. if( pt_conn->end && (pt_conn->end->Type() == TYPEPAD) )
  59. {
  60. pt_pad = (D_PAD*) (pt_conn->end);
  61. if( pt_pad->m_physical_connexion == old_val )
  62. pt_pad->m_physical_connexion = pt_conn->GetSubNet();
  63. }
  64. if( pt_conn == pt_end_conn )
  65. break;
  66. }
  67. return nb_change;
  68. }
  69. /******************************************************************/
  70. static void propage_equipot( TRACK* pt_start_conn, TRACK* pt_end_conn )
  71. /******************************************************************/
  72. /** Function propage_equipot
  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 save 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_Sous_Netcode 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 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 sous_net_code;
  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 = (TRACK*) pt_conn->Pnext )
  92. {
  93. pt_conn->SetSubNet( 0 );
  94. PtStruct = pt_conn->start;
  95. if( PtStruct && (PtStruct->Type() == TYPEPAD) )
  96. ( (D_PAD*) PtStruct )->m_physical_connexion = 0;
  97. PtStruct = pt_conn->end;
  98. if( PtStruct && (PtStruct->Type() == TYPEPAD) )
  99. ( (D_PAD*) PtStruct )->m_physical_connexion = 0;
  100. if( pt_conn == pt_end_conn )
  101. break;
  102. }
  103. sous_net_code = 1;
  104. pt_start_conn->SetSubNet( sous_net_code );
  105. /* Start of calculation */
  106. pt_conn = pt_start_conn;
  107. for( ; pt_conn != NULL; pt_conn = (TRACK*) pt_conn->Pnext )
  108. {
  109. /* First: handling connections to pads */
  110. PtStruct = pt_conn->start;
  111. /* The segment starts on a pad */
  112. if( PtStruct && (PtStruct->Type() == TYPEPAD) )
  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->m_physical_connexion > 0 ) /* The pad is already a cluster member, so we can merge the 2 clusters */
  118. {
  119. change_equipot( pt_start_conn, pt_end_conn,
  120. pt_pad->m_physical_connexion, 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->m_physical_connexion = pt_conn->GetSubNet();
  124. }
  125. else /* the track segment is not attached to a cluster */
  126. {
  127. if( pt_pad->m_physical_connexion > 0 ) /* it is connected to a pad in a cluster, merge this track */
  128. {
  129. pt_conn->SetSubNet( pt_pad->m_physical_connexion );
  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. sous_net_code++;
  134. pt_conn->SetSubNet( sous_net_code );
  135. pt_pad->m_physical_connexion = pt_conn->GetSubNet();
  136. }
  137. }
  138. }
  139. PtStruct = pt_conn->end;
  140. if( PtStruct && (PtStruct->Type() == TYPEPAD) )
  141. /* The segment end on a pad */
  142. {
  143. pt_pad = (D_PAD*) PtStruct;
  144. if( pt_conn->GetSubNet() )
  145. {
  146. if( pt_pad->m_physical_connexion > 0 )
  147. {
  148. change_equipot( pt_start_conn, pt_end_conn,
  149. pt_pad->m_physical_connexion, pt_conn->GetSubNet() );
  150. }
  151. else
  152. pt_pad->m_physical_connexion = pt_conn->GetSubNet();
  153. }
  154. else
  155. {
  156. if( pt_pad->m_physical_connexion > 0 )
  157. {
  158. pt_conn->SetSubNet( pt_pad->m_physical_connexion );
  159. }
  160. else
  161. {
  162. sous_net_code++;
  163. pt_conn->SetSubNet( sous_net_code );
  164. pt_pad->m_physical_connexion = pt_conn->GetSubNet();
  165. }
  166. }
  167. }
  168. /* Test connections between segments */
  169. PtStruct = pt_conn->start;
  170. if( PtStruct && (PtStruct->Type() != TYPEPAD) )
  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. change_equipot( 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. sous_net_code++;
  195. pt_conn->SetSubNet( sous_net_code );
  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() != TYPEPAD) )
  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. change_equipot( 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. sous_net_code++;
  223. pt_conn->SetSubNet( sous_net_code );
  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 le status du chevelu ( 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 put in the linked link 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. TRACK* pt_start_conn, * pt_end_conn;
  244. int ii;
  245. LISTE_PAD* pt_pad;
  246. int current_net_code;
  247. /* Clear the cluster identifier for all pads */
  248. pt_pad = m_Pcb->m_Pads;
  249. for( ii = 0; ii < m_Pcb->m_NbPads; ii++, pt_pad++ )
  250. {
  251. (*pt_pad)->m_physical_connexion = 0;
  252. }
  253. /* Test existing connections net by net */
  254. pt_start_conn = m_Pcb->m_Track; // this is the first segment of the first net
  255. while( pt_start_conn != NULL )
  256. {
  257. current_net_code = pt_start_conn->GetNet(); // this is the current net because pt_start_conn is the first segment of the net
  258. pt_end_conn = pt_start_conn->GetEndNetCode( current_net_code ); // this is the last segment of the current net
  259. calcule_connexite_1_net( pt_start_conn, pt_end_conn );
  260. pt_start_conn = (TRACK*) pt_end_conn->Pnext; // this is now the first segment of the next net
  261. }
  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. TRACK* pt_start_conn, * pt_end_conn;
  274. int ii, nb_net_noconnect = 0;
  275. LISTE_PAD* pt_pad;
  276. wxString msg;
  277. if( net_code == 0 )
  278. return;
  279. if( (m_Pcb->m_Status_Pcb & LISTE_CHEVELU_OK) == 0 )
  280. Compile_Ratsnest( DC, TRUE );
  281. pt_pad = (LISTE_PAD*) m_Pcb->m_Pads;
  282. for( ii = 0; ii < m_Pcb->m_NbPads; ii++, pt_pad++ )
  283. {
  284. int pad_net_code = (*pt_pad)->GetNet();
  285. if( pad_net_code < net_code )
  286. continue;
  287. if( pad_net_code > net_code )
  288. break;
  289. (*pt_pad)->m_physical_connexion = 0;
  290. }
  291. /* Search for the first and the last segment relative to the given net code */
  292. if( m_Pcb->m_Track )
  293. {
  294. pt_end_conn = NULL;
  295. pt_start_conn = m_Pcb->m_Track->GetStartNetCode( net_code );
  296. if( pt_start_conn )
  297. pt_end_conn = pt_start_conn->GetEndNetCode( net_code );
  298. if( pt_start_conn && pt_end_conn ) // c.a.d. s'il y a des segments
  299. {
  300. calcule_connexite_1_net( pt_start_conn, pt_end_conn );
  301. }
  302. }
  303. /* Test the rastnest for this net */
  304. 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->m_NbLinks, m_Pcb->GetNumNoconnect(),
  308. nb_net_noconnect );
  309. Affiche_Message( msg );
  310. return;
  311. }
  312. /***************************************************************************/
  313. static void calcule_connexite_1_net( 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*) Track->Pnext )
  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*) Track->Pnext )
  339. {
  340. if( Track->Type() == TYPEVIA ) // 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 = (TRACK*) pt_segm->Pnext )
  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. /* Generation des sous equipots du net */
  373. propage_equipot( pt_start_conn, pt_end_conn );
  374. }
  375. #define POS_AFF_CHREF 62
  376. /******************************************************************************/
  377. static D_PAD* SuperFast_Locate_Pad_Connecte( BOARD* pcb, LISTE_PAD* pt_liste,
  378. int px, int py, int masque_layer )
  379. /******************************************************************************/
  380. /** Function SuperFast_Locate_Pad_Connecte
  381. * Locate the pad connected to a track ended at coord px, py
  382. * A track is seen as connected if the px, py position is same as the pad position
  383. * @param px = reference X coordinate
  384. * @param py = reference Y coordinate
  385. * @param masque_layer = Layers (bit to bit) to consider
  386. * @param pt_liste = Pointers to pads buffer
  387. * This buffer is a list like the list created by build_liste_pad, but sorted by increasing X pad coordinate
  388. * @return : pointer on the connected pad
  389. * This function uses a fast search in this sorted pad list and it is faster than Fast_Locate_Pad_connecte(),
  390. * But this sorted pad list must be built before calling this function.
  391. *
  392. * (Note: The usual pad list (created by build_liste_pad) m_Pcb->m_Pads is sorted by increasing netcodes )
  393. */
  394. {
  395. D_PAD* pad;
  396. LISTE_PAD* ptr_pad, * lim;
  397. int nb_pad = pcb->m_NbPads;
  398. int ii;
  399. lim = pt_liste + (pcb->m_NbPads - 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 < px ) /* 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 > px ) /* 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 == px ) /* 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 == px )
  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 != px )
  440. return NULL; /* outside suitable block */
  441. if( pad->m_Pos.y != py )
  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. static int SortPadsByXCoord( const void* pt_ref, const void* pt_comp )
  452. /* used to Sort a pad list by x coordinate value
  453. */
  454. {
  455. D_PAD* ref = *(LISTE_PAD*) pt_ref;
  456. D_PAD* comp = *(LISTE_PAD*) pt_comp;
  457. return ref->m_Pos.x - comp->m_Pos.x;
  458. }
  459. /****************************************************/
  460. LISTE_PAD* CreateSortedPadListByXCoord( BOARD* pcb )
  461. /****************************************************/
  462. /* Create a sorted list of pointers to pads.
  463. * This list is sorted by X ccordinate value.
  464. * The list must be freed by user
  465. */
  466. {
  467. LISTE_PAD* pad_list = (LISTE_PAD*) MyMalloc( pcb->m_NbPads * sizeof(D_PAD*) );
  468. memcpy( pad_list, pcb->m_Pads, pcb->m_NbPads * sizeof( D_PAD*) );
  469. qsort( pad_list, pcb->m_NbPads, sizeof( D_PAD*), SortPadsByXCoord );
  470. return pad_list;
  471. }
  472. /********************************************************************/
  473. void WinEDA_BasePcbFrame::reattribution_reference_piste( int affiche )
  474. /********************************************************************/
  475. /* search connections between tracks and pads, and propagate pad net codes to the track segments
  476. * This is a 2 pass computation.
  477. * The pad netcodes are assumed to be initialized.
  478. * First:
  479. * We search a connection between a track segment and a pad: if found : this segment netcode is set to the pad netcode
  480. */
  481. {
  482. TRACK* pt_piste,
  483. * pt_next;
  484. int a_color;
  485. char new_passe_request = 1, flag;
  486. LISTE_PAD* pt_mem;
  487. BOARD_ITEM* PtStruct;
  488. int masque_layer;
  489. wxString msg;
  490. if( m_Pcb->m_NbPads == 0 )
  491. return;
  492. a_color = CYAN;
  493. if( affiche )
  494. Affiche_1_Parametre( this, POS_AFF_CHREF, wxT( "DataBase" ), wxT( "Netcodes" ), a_color );
  495. recalcule_pad_net_code();
  496. if( affiche )
  497. Affiche_1_Parametre( this, -1, wxEmptyString, wxT( "Gen Pads " ), a_color );
  498. /**************************************************************/
  499. /* Pass 1: search the connections between track ends and pads */
  500. /**************************************************************/
  501. pt_mem = CreateSortedPadListByXCoord( m_Pcb );
  502. if( affiche )
  503. Affiche_1_Parametre( this, -1, wxEmptyString, wxT( "Conn Pads" ), a_color );
  504. /* Reset variables and flags used in computation */
  505. pt_piste = m_Pcb->m_Track;
  506. for( ; pt_piste != NULL; pt_piste = (TRACK*) pt_piste->Pnext )
  507. {
  508. pt_piste->SetState( BUSY | EDIT | BEGIN_ONPAD | END_ONPAD, OFF );
  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 = (TRACK*) pt_piste->Pnext )
  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. pt_mem,
  522. pt_piste->m_Start.x,
  523. pt_piste->m_Start.y,
  524. masque_layer );
  525. if( pt_piste->start != NULL )
  526. {
  527. pt_piste->SetState( BEGIN_ONPAD, ON );
  528. pt_piste->SetNet( ( (D_PAD*) (pt_piste->start) )->GetNet() );
  529. }
  530. /* Search for a pad on the segment ending point */
  531. pt_piste->end = SuperFast_Locate_Pad_Connecte( m_Pcb,
  532. pt_mem,
  533. pt_piste->m_End.x,
  534. pt_piste->m_End.y,
  535. masque_layer );
  536. if( pt_piste->end != NULL )
  537. {
  538. pt_piste->SetState( END_ONPAD, ON );
  539. pt_piste->SetNet( ( (D_PAD*) (pt_piste->end) )->GetNet() );
  540. }
  541. }
  542. MyFree( pt_mem );
  543. /*****************************************************/
  544. /* Pass 2: search the connections between track ends */
  545. /*****************************************************/
  546. /* the .start et .end member pointers are updated, only if NULLs
  547. * (if not nuls, the end is already connected to a pad).
  548. * the connection (if found) is between segments
  549. * when a track has a net code and the other has a null net code, the null net code is changed
  550. */
  551. if( affiche )
  552. Affiche_1_Parametre( this, POS_AFF_CHREF, wxEmptyString, wxT( "Conn Segm" ), a_color );
  553. for( pt_piste = m_Pcb->m_Track; pt_piste != NULL; pt_piste = pt_piste->Next() )
  554. {
  555. if( pt_piste->start == NULL )
  556. {
  557. pt_piste->start = Locate_Piste_Connectee( pt_piste, m_Pcb->m_Track, NULL, START );
  558. }
  559. if( pt_piste->end == NULL )
  560. {
  561. pt_piste->end = Locate_Piste_Connectee( pt_piste, m_Pcb->m_Track, NULL, END );
  562. }
  563. }
  564. /**********************************************************/
  565. /* Propagate net codes from a segment to an other segment */
  566. /**********************************************************/
  567. a_color = YELLOW;
  568. while( new_passe_request )
  569. {
  570. bool reset_flag = FALSE;
  571. new_passe_request = 0;
  572. if( affiche )
  573. {
  574. msg.Printf( wxT( "Net->Segm pass %d " ), new_passe_request + 1 );
  575. Affiche_1_Parametre( this, POS_AFF_CHREF, wxEmptyString, msg, a_color );
  576. }
  577. /* look for vias which could be connect many tracks */
  578. for( TRACK* via = m_Pcb->m_Track; via != NULL; via = via->Next() )
  579. {
  580. if( via->Type() != TYPEVIA )
  581. continue;
  582. if( via->GetNet() > 0 )
  583. continue; // Netcode already known
  584. // Lock for a connection to a track with a known netcode
  585. pt_next = m_Pcb->m_Track;
  586. while( ( pt_next = Locate_Piste_Connectee( via, pt_next, NULL, START ) ) != NULL )
  587. {
  588. if( pt_next->GetNet() )
  589. {
  590. via->SetNet( pt_next->GetNet() );
  591. break;
  592. }
  593. pt_next->SetState( BUSY, ON );
  594. reset_flag = TRUE;
  595. }
  596. }
  597. if( reset_flag )
  598. for( pt_piste = m_Pcb->m_Track; pt_piste != NULL; pt_piste = pt_piste->Next() )
  599. {
  600. pt_piste->SetState( BUSY, OFF );
  601. }
  602. /* set the netcode of connected tracks: if at track is connected to a pad, its net code is already set.
  603. * if the current track is connected to an other track:
  604. * if a track has a net code, it is used for the other track.
  605. * Thus there is a propagation of the netcode from a track to an other.
  606. * if none of the 2 track has a net code we do nothing
  607. * the iteration is stopped when no new change occurs
  608. */
  609. for( pt_piste = m_Pcb->m_Track; pt_piste != NULL; pt_piste = pt_piste->Next() )
  610. {
  611. /* look for the connection to the current segment starting point */
  612. PtStruct = (BOARD_ITEM*) pt_piste->start;
  613. if( PtStruct && (PtStruct->Type() != TYPEPAD) )
  614. {
  615. // Begin on an other track segment
  616. pt_next = (TRACK*) PtStruct;
  617. if( pt_piste->GetNet() )
  618. {
  619. if( pt_next->GetNet() == 0 ) // the current track has a netcode, we use it for the other track
  620. {
  621. new_passe_request = 1; // A change is made: a new iteration is requested.
  622. pt_next->SetNet( pt_piste->GetNet() );
  623. }
  624. }
  625. else
  626. {
  627. if( pt_next->GetNet() != 0 ) // the other track has a netcode, we use it for the current track
  628. {
  629. pt_piste->SetNet( pt_next->GetNet() );
  630. new_passe_request = 1;
  631. }
  632. }
  633. }
  634. /* look for the connection to the current segment ending point */
  635. PtStruct = pt_piste->end;
  636. if( PtStruct &&(PtStruct->Type() != TYPEPAD) )
  637. {
  638. pt_next = (TRACK*) PtStruct;
  639. // End on an other track: propagate netcode if possible
  640. if( pt_piste->GetNet() )
  641. {
  642. if( pt_next->GetNet() == 0 )
  643. {
  644. new_passe_request = 1;
  645. pt_next->SetNet( pt_piste->GetNet() );
  646. }
  647. }
  648. else
  649. {
  650. if( pt_next->GetNet() != 0 )
  651. {
  652. pt_piste->SetNet( pt_next->GetNet() );
  653. new_passe_request = 1;
  654. }
  655. }
  656. }
  657. }
  658. }
  659. /* Sort the track list by net codes: */
  660. if( affiche )
  661. Affiche_1_Parametre( this, -1, wxEmptyString, wxT( "Reorder " ), a_color );
  662. RebuildTrackChain( m_Pcb );
  663. if( affiche )
  664. Affiche_1_Parametre( this, -1, wxEmptyString, wxT( " " ), a_color );
  665. }
  666. /*
  667. * Sort function for track segments used in RebuildTrackChain() (for the qsort C function)
  668. * The sorting is made by net code
  669. */
  670. int Sort_By_NetCode( TRACK** pt_ref, TRACK** pt_compare )
  671. {
  672. int ii;
  673. ii = (*pt_ref)->GetNet() - (*pt_compare)->GetNet();
  674. return ii;
  675. }
  676. /*****************************************/
  677. static void RebuildTrackChain( BOARD* pcb )
  678. /*****************************************/
  679. /** Function RebuildTrackChain()
  680. * @param pcb = board to rebuild
  681. * Rebuild the track segment linked list in order to have a chain sorted by increasing netcodes
  682. */
  683. {
  684. TRACK* Track, ** Liste;
  685. int ii, nbsegm;
  686. /* Count segments */
  687. nbsegm = pcb->GetNumSegmTrack();
  688. if( pcb->m_Track == NULL )
  689. return;
  690. Liste = (TRACK**) MyZMalloc( (nbsegm + 1) * sizeof(TRACK*) );
  691. ii = 0; Track = pcb->m_Track;
  692. for( ; Track != NULL; ii++, Track = (TRACK*) Track->Pnext )
  693. {
  694. Liste[ii] = Track;
  695. }
  696. qsort( Liste, nbsegm, sizeof(TRACK*),
  697. ( int( * ) ( const void*, const void* ) )Sort_By_NetCode );
  698. /* Update the linked list pointers */
  699. Track = Liste[0];
  700. Track->Pback = pcb; Track->Pnext = Liste[1];
  701. pcb->m_Track = Track;
  702. for( ii = 1; ii < nbsegm; ii++ )
  703. {
  704. Track = Liste[ii];
  705. Track->Pback = Liste[ii - 1];
  706. Track->Pnext = Liste[ii + 1];
  707. }
  708. MyFree( Liste );
  709. }