connect.cpp 29.5 KB
Newer Older
1 2 3
/***************************************************************************************/
/* Rastnest calculations: Function to handle existing tracks in rastsnest calculations */
/***************************************************************************************/
4 5 6 7 8 9 10 11 12 13 14

#include "fctsys.h"
#include "gr_basic.h"

#include "common.h"
#include "pcbnew.h"
#include "autorout.h"

#include "protos.h"


15
/* Loca functions */
16 17 18
static void propage_equipot( TRACK* pt_start_conn, TRACK* pt_end_conn );
static void calcule_connexite_1_net( TRACK* pt_start_conn, TRACK* pt_end_conn );
static void RebuildTrackChain( BOARD* pcb );
19
static int  Sort_By_NetCode( TRACK** pt_ref, TRACK** pt_compare );
20 21 22 23 24

/*..*/


/*****************************************************************/
25 26
static int change_equipot( TRACK* pt_start_conn, TRACK* pt_end_conn,
                           int old_val, int new_val )
27
/*****************************************************************/
28

29 30 31 32 33 34 35 36 37 38 39 40
/** Function change_equipot()
 * Used by propage_equipot()
 * Change a subnet value to a new value, for tracks ans pads which are connected to corresponding track
 * for pads, this is the .m_physical_connexion member which is tested and modified
 * for tracks, this is the .m_Sous_Netcode member which is tested and modified
 * these members are block numbers (or cluster numbers) for a given net
 * @return modification count
 * @param old_val = subnet value to modify
 * @param new_val = new subnet value for each item whith have old_val as subnet value
 * @param pt_start_conn = first track segment to test
 * @param pt_end_conn = last track segment to test
 * If pt_end_conn = NULL: search is made from pt_start_conn to end of linked list
41
 */
42
{
43 44 45 46 47 48 49 50 51 52 53 54 55
    TRACK* pt_conn;
    int    nb_change = 0;
    D_PAD* pt_pad;

    if( old_val == new_val )
        return 0;

    if( (old_val > 0) && (old_val < new_val) )
        EXCHG( old_val, new_val );

    pt_conn = pt_start_conn;
    for( ; pt_conn != NULL; pt_conn = (TRACK*) pt_conn->Pnext )
    {
56
        if( pt_conn->GetSubNet() != old_val )
57 58 59 60 61 62 63
        {
            if( pt_conn == pt_end_conn )
                break;
            continue;
        }

        nb_change++;
64
        pt_conn->SetSubNet( new_val );
65

66
        if( pt_conn->start && ( pt_conn->start->Type() == TYPEPAD) )
67 68 69
        {
            pt_pad = (D_PAD*) (pt_conn->start);
            if( pt_pad->m_physical_connexion == old_val )
70
                pt_pad->m_physical_connexion = pt_conn->GetSubNet();
71 72
        }

73
        if( pt_conn->end && (pt_conn->end->Type() == TYPEPAD) )
74 75 76
        {
            pt_pad = (D_PAD*) (pt_conn->end);
            if( pt_pad->m_physical_connexion == old_val )
77
                pt_pad->m_physical_connexion = pt_conn->GetSubNet();
78 79 80 81 82 83
        }
        if( pt_conn == pt_end_conn )
            break;
    }

    return nb_change;
84 85
}

86

87
/******************************************************************/
88
static void propage_equipot( TRACK* pt_start_conn, TRACK* pt_end_conn )
89
/******************************************************************/
90

91 92 93 94 95 96 97 98 99 100
/** Function propage_equipot
 * Test a list of track segment, to create or propagate a sub netcode to pads and segments connected together
 * the track list must be sorted by nets, and all segments from pt_start_conn to pt_end_conn have the save net
 * When 2 items are connected (a track to a pad, or a track to an other track) they are grouped in a cluster.
 * for pads, this is the .m_physical_connexion member which is a cluster identifier
 * for tracks, this is the .m_Sous_Netcode member which is a cluster identifier
 * For a given net, if all tracks are created, there is only one cluster.
 * but if not all tracks are created, there are are more than one cluster, and some ratsnets will be shown.
 * @param pt_start_conn = first track to test
 * @param pt_end_conn = last segment to test
101
 */
102
{
103 104 105 106 107
    TRACK*      pt_conn;
    int         sous_net_code;
    D_PAD*      pt_pad;
    TRACK*      pt_autre_piste;
    BOARD_ITEM* PtStruct;
108

109
    /* Clear variables used in computations */
110 111 112
    pt_conn = pt_start_conn;
    for( ; pt_conn != NULL; pt_conn = (TRACK*) pt_conn->Pnext )
    {
113
        pt_conn->SetSubNet( 0 );
114
        PtStruct = pt_conn->start;
115
        if( PtStruct && (PtStruct->Type() == TYPEPAD) )
116 117 118
            ( (D_PAD*) PtStruct )->m_physical_connexion = 0;

        PtStruct = pt_conn->end;
119
        if( PtStruct && (PtStruct->Type() == TYPEPAD) )
120 121 122 123 124 125
            ( (D_PAD*) PtStruct )->m_physical_connexion = 0;

        if( pt_conn == pt_end_conn )
            break;
    }

126
    sous_net_code = 1;
127
    pt_start_conn->SetSubNet( sous_net_code );
128

129
    /* Start of calculation */
130 131 132
    pt_conn = pt_start_conn;
    for( ; pt_conn != NULL; pt_conn = (TRACK*) pt_conn->Pnext )
    {
133
        /* First: handling connections to pads */
134
        PtStruct = pt_conn->start;
135

136
        /* The segment starts on a pad */
137
        if( PtStruct && (PtStruct->Type() == TYPEPAD) )
138 139
        {
            pt_pad = (D_PAD*) PtStruct;
140
            if( pt_conn->GetSubNet() )                  /* the track segment is already a cluster member */
141
            {
142
                if( pt_pad->m_physical_connexion > 0 )  /* The pad is already a cluster member, so we can merge the 2 clusters */
143 144
                {
                    change_equipot( pt_start_conn, pt_end_conn,
145
                                   pt_pad->m_physical_connexion, pt_conn->GetSubNet() );
146
                }
147
                else  /* The pad is not yet attached to a cluster , so we can add this pad to the cluster */
148
                    pt_pad->m_physical_connexion = pt_conn->GetSubNet();
149
            }
150
            else                                        /* the track segment is not attached to a cluster */
151
            {
152
                if( pt_pad->m_physical_connexion > 0 )  /* it is connected to a pad in a cluster, merge this track */
153
                {
154
                    pt_conn->SetSubNet( pt_pad->m_physical_connexion );
155
                }
156
                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) */
157
                {
158
                    sous_net_code++;
159 160
                    pt_conn->SetSubNet( sous_net_code );
                    pt_pad->m_physical_connexion = pt_conn->GetSubNet();
161 162 163 164 165
                }
            }
        }

        PtStruct = pt_conn->end;
166
        if( PtStruct && (PtStruct->Type() == TYPEPAD) )
167
        /* The segment end on a pad */
168 169
        {
            pt_pad = (D_PAD*) PtStruct;
170
            if( pt_conn->GetSubNet() )
171 172 173 174
            {
                if( pt_pad->m_physical_connexion > 0 )
                {
                    change_equipot( pt_start_conn, pt_end_conn,
175
                                   pt_pad->m_physical_connexion, pt_conn->GetSubNet() );
176 177
                }
                else
178
                    pt_pad->m_physical_connexion = pt_conn->GetSubNet();
179 180 181 182 183
            }
            else
            {
                if( pt_pad->m_physical_connexion > 0 )
                {
184
                    pt_conn->SetSubNet( pt_pad->m_physical_connexion );
185 186 187
                }
                else
                {
188
                    sous_net_code++;
189 190
                    pt_conn->SetSubNet( sous_net_code );
                    pt_pad->m_physical_connexion = pt_conn->GetSubNet();
191 192 193 194 195
                }
            }
        }


196
        /* Test connections between segments */
197
        PtStruct = pt_conn->start;
198
        if( PtStruct && (PtStruct->Type() != TYPEPAD) )
199
        {
200
            /* The segment starts on an other track */
201 202
            pt_autre_piste = (TRACK*) PtStruct;

203
            if( pt_conn->GetSubNet() )              /* the track segment is already a cluster member */
204
            {
205
                if( pt_autre_piste->GetSubNet() )   /* The other track is already a cluster member, so we can merge the 2 clusters */
206 207
                {
                    change_equipot( pt_start_conn, pt_end_conn,
208
                                   pt_autre_piste->GetSubNet(), pt_conn->GetSubNet() );
209
                }
210
                else /* The other track is not yet attached to a cluster , so we can add this other track to the cluster */
211
                {
212
                    pt_autre_piste->SetSubNet( pt_conn->GetSubNet() );
213 214
                }
            }
215
            else                                    /* the track segment is not yet attached to a cluster */
216
            {
217
                if( pt_autre_piste->GetSubNet() )   /* The other track is already a cluster member, so we can add the segment to the cluster */
218
                {
219
                    pt_conn->SetSubNet( pt_autre_piste->GetSubNet() );
220
                }
221
                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) */
222
                {
223
                    sous_net_code++;
224 225
                    pt_conn->SetSubNet( sous_net_code );
                    pt_autre_piste->SetSubNet( pt_conn->GetSubNet() );
226 227 228 229
                }
            }
        }

230
        PtStruct = pt_conn->end;    // Do the same calculations for the segment end point
231
        if( PtStruct && (PtStruct->Type() != TYPEPAD) )
232
        {
233
            pt_autre_piste = (TRACK*) PtStruct;
234

235
            if( pt_conn->GetSubNet() )  /* the track segment is already a cluster member */
236
            {
237
                if( pt_autre_piste->GetSubNet() )
238 239
                {
                    change_equipot( pt_start_conn, pt_end_conn,
240
                                   pt_autre_piste->GetSubNet(), pt_conn->GetSubNet() );
241 242
                }
                else
243
                    pt_autre_piste->SetSubNet( pt_conn->GetSubNet() );
244
            }
245
            else      /* the track segment is not yet attached to a cluster */
246
            {
247
                if( pt_autre_piste->GetSubNet() )
248
                {
249
                    pt_conn->SetSubNet( pt_autre_piste->GetSubNet() );
250 251 252
                }
                else
                {
253
                    sous_net_code++;
254 255
                    pt_conn->SetSubNet( sous_net_code );
                    pt_autre_piste->SetSubNet( pt_conn->GetSubNet() );
256 257 258 259 260 261
                }
            }
        }
        if( pt_conn == pt_end_conn )
            break;
    }
262 263
}

264

265
/***************************************************/
266
void WinEDA_BasePcbFrame::test_connexions( wxDC* DC )
267
/***************************************************/
268

269 270 271 272 273 274
/** Function testing the connections relative to all nets
 *  This function update le status du chevelu ( flag CH_ACTIF = 0 if a connection is found, = 1 else)
 * track segments are assumed to be sorted by net codes.
 * This is the case because when a new track is added, it is put in the linked link according to its net code.
 * and when nets are changed (when a new netlist is read) tracks are sorted before using this function
 * @param DC = current Device Context
275
 */
276
{
277 278 279 280 281
    TRACK*     pt_start_conn, * pt_end_conn;
    int        ii;
    LISTE_PAD* pt_pad;
    int        current_net_code;

282
    /* Clear the cluster identifier for all pads */
283 284 285 286 287 288
    pt_pad = m_Pcb->m_Pads;
    for( ii = 0; ii < m_Pcb->m_NbPads; ii++, pt_pad++ )
    {
        (*pt_pad)->m_physical_connexion = 0;
    }

289 290
    /* Test existing connections net by net */
    pt_start_conn = m_Pcb->m_Track;     // this is the first segment of the first net
291 292
    while( pt_start_conn != NULL )
    {
293 294
        current_net_code = pt_start_conn->GetNet();                         // this is the current net because pt_start_conn is the first segment of the net
        pt_end_conn = pt_start_conn->GetEndNetCode( current_net_code );     // this is the last segment of the current net
295 296 297

        calcule_connexite_1_net( pt_start_conn, pt_end_conn );

298
        pt_start_conn = (TRACK*) pt_end_conn->Pnext;    // this is now the first segment of the next net
299 300 301
    }

    return;
302 303
}

304

305
/*************************************************************************/
306
void WinEDA_BasePcbFrame::test_1_net_connexion( wxDC* DC, int net_code )
307
/*************************************************************************/
308

309 310 311 312
/** Function testing the connections relative to a given net
 * track segments are assumed to be sorted by net codes
 * @param DC = current Device Context
 * @param net_code = net code to test
313
 */
314
{
315 316 317 318 319 320 321 322 323 324 325 326 327 328
    TRACK*     pt_start_conn, * pt_end_conn;
    int        ii, nb_net_noconnect = 0;
    LISTE_PAD* pt_pad;
    wxString   msg;

    if( net_code == 0 )
        return;

    if( (m_Pcb->m_Status_Pcb & LISTE_CHEVELU_OK) == 0 )
        Compile_Ratsnest( DC, TRUE );

    pt_pad = (LISTE_PAD*) m_Pcb->m_Pads;
    for( ii = 0; ii < m_Pcb->m_NbPads; ii++, pt_pad++ )
    {
329
        int pad_net_code = (*pt_pad)->GetNet();
330 331
        if( pad_net_code < net_code )
            continue;
332

333 334
        if( pad_net_code > net_code )
            break;
335

336 337 338
        (*pt_pad)->m_physical_connexion = 0;
    }

339
    /* Search for the first and the last segment relative to the given net code */
340 341 342 343 344 345 346 347 348 349 350 351 352 353
    if( m_Pcb->m_Track )
    {
        pt_end_conn   = NULL;
        pt_start_conn = m_Pcb->m_Track->GetStartNetCode( net_code );

        if( pt_start_conn )
            pt_end_conn = pt_start_conn->GetEndNetCode( net_code );

        if( pt_start_conn && pt_end_conn ) // c.a.d. s'il y a des segments
        {
            calcule_connexite_1_net( pt_start_conn, pt_end_conn );
        }
    }

354
    /* Test the rastnest for this net */
355 356
    nb_net_noconnect = Test_1_Net_Ratsnest( DC, net_code );

357
    /* Display results */
358 359 360 361 362 363
    msg.Printf( wxT( "links %d nc %d  net:nc %d" ),
                m_Pcb->m_NbLinks, m_Pcb->GetNumNoconnect(),
                nb_net_noconnect );

    Affiche_Message( msg );
    return;
364 365 366 367
}


/***************************************************************************/
368
static void calcule_connexite_1_net( TRACK* pt_start_conn, TRACK* pt_end_conn )
369
/***************************************************************************/
370

371 372 373 374 375 376 377 378
/**  Used after a track change (delete a track ou add a track)
 * Compute connections (initialize the .start and .end members) for a single net.
 * tracks must be sorted by net, as usual
 *  @param pt_start_conn = first segment of the net
 *  @param pt_end_conn = last segment of the net
 *  Connections to pads are assumed to be already initialized.
 *  If a track is deleted, the other pointers to pads do not change.
 *  When a track is added, its pointers to pads are already initialized
379
 */
380
{
381 382
    TRACK* Track;

383
    /* Reset the old connections type track to track */
384 385
    for( Track = pt_start_conn; Track != NULL; Track = (TRACK*) Track->Pnext )
    {
386
        Track->SetSubNet( 0 );
387 388 389

        if( Track->GetState( BEGIN_ONPAD ) == 0 )
            Track->start = NULL;
390

391 392 393 394 395 396 397
        if( Track->GetState( END_ONPAD ) == 0 )
            Track->end = NULL;

        if( Track == pt_end_conn )
            break;
    }

398
    /* Update connections type track to track */
399 400
    for( Track = pt_start_conn; Track != NULL; Track = (TRACK*) Track->Pnext )
    {
401
        if( Track->Type() == TYPEVIA )  // A via can connect many tracks, we must search for all track segments in this net
402 403 404 405 406 407
        {
            TRACK* pt_segm;
            int    layermask = Track->ReturnMaskLayer();
            for( pt_segm = pt_start_conn; pt_segm != NULL; pt_segm = (TRACK*) pt_segm->Pnext )
            {
                int curlayermask = pt_segm->ReturnMaskLayer();
408

409 410 411 412 413
                if( !pt_segm->start && (pt_segm->m_Start == Track->m_Start)
                   && ( layermask & curlayermask ) )
                {
                    pt_segm->start = Track;
                }
414

415 416 417 418 419 420 421 422 423 424
                if( !pt_segm->end && (pt_segm->m_End == Track->m_Start)
                   && (layermask & curlayermask) )
                {
                    pt_segm->end = Track;
                }
                if( pt_segm == pt_end_conn )
                    break;
            }
        }

425
        if( Track->start == NULL )  // end track not already connected, search a connection
426
        {
427
            Track->start = Locate_Piste_Connectee( Track, Track, pt_end_conn, START );
428 429
        }

430
        if( Track->end == NULL )    // end track not already connected, search a connection
431
        {
432
            Track->end = Locate_Piste_Connectee( Track, Track, pt_end_conn, END );
433 434 435 436 437 438 439
        }
        if( Track == pt_end_conn )
            break;
    }

    /* Generation des sous equipots du net */
    propage_equipot( pt_start_conn, pt_end_conn );
440 441 442 443
}


#define POS_AFF_CHREF 62
444

445 446 447 448
/******************************************************************************/
static D_PAD* SuperFast_Locate_Pad_Connecte( BOARD* pcb, LISTE_PAD* pt_liste,
                                             int px, int py, int masque_layer )
/******************************************************************************/
449 450 451 452 453 454 455 456 457 458 459 460

/** Function SuperFast_Locate_Pad_Connecte
 * Locate the pad connected to a track ended at coord px, py
 * A track is seen as connected if the px, py position is same as the pad position
 * @param px = reference X coordinate
 * @param py = reference Y coordinate
 * @param masque_layer = Layers (bit to bit) to consider
 * @param pt_liste = Pointers to pads buffer
 *      This buffer is a list like the list created by build_liste_pad, but sorted by increasing X pad coordinate
 * @return : pointer on the connected pad
 *  This function uses a fast search in this sorted pad list and it is faster than Fast_Locate_Pad_connecte(),
 *  But this sorted pad list must be built before calling this function.
461
 *
462
 *  (Note: The usual pad list (created by build_liste_pad) m_Pcb->m_Pads is sorted by increasing netcodes )
463
 */
464
{
465 466 467 468 469 470 471 472 473 474 475
    D_PAD*     pad;
    LISTE_PAD* ptr_pad, * lim;
    int        nb_pad = pcb->m_NbPads;
    int        ii;

    lim     = pt_liste + (pcb->m_NbPads - 1 );
    ptr_pad = pt_liste;
    while( nb_pad )
    {
        pad      = *ptr_pad;
        ii       = nb_pad;
476
        nb_pad >>= 1;
477

478
        if( (ii & 1) && ( ii > 1 ) )
479
            nb_pad++;
480

481
        if( pad->m_Pos.x < px ) /* Must search after this item */
482
        {
483
            ptr_pad += nb_pad;
484
            if( ptr_pad > lim )
485 486 487
                ptr_pad = lim;
            continue;
        }
488
        if( pad->m_Pos.x > px ) /* Must search before this item */
489 490 491 492 493 494 495
        {
            ptr_pad -= nb_pad;
            if( ptr_pad < pt_liste )
                ptr_pad = pt_liste;
            continue;
        }

496
        if( pad->m_Pos.x == px )  /* A suitable block is found (X coordinate matches the px reference: but wue must matches the Y coordinate */
497
        {
498
            /* Search the beginning of the block */
499 500 501 502 503 504 505 506 507
            while( ptr_pad >= pt_liste )
            {
                pad = *ptr_pad;
                if( pad->m_Pos.x == px )
                    ptr_pad--;
                else
                    break;
            }

508
            ptr_pad++;  /* ptr_pad = first pad which have pad->m_Pos.x = px */
509 510 511 512

            for( ; ; ptr_pad++ )
            {
                if( ptr_pad > lim )
513
                    return NULL; /* outside suitable block */
514

515 516
                pad = *ptr_pad;
                if( pad->m_Pos.x != px )
517
                    return NULL; /* outside suitable block */
518

519 520
                if( pad->m_Pos.y != py )
                    continue;
521

522 523
                /* A Pad if found here: but it must mach the layer */
                if( pad->m_Masque_Layer & masque_layer )  // Matches layer => a connected pad is found !
524 525 526 527 528 529
                    return pad;
            }
        }
    }

    return NULL;
530 531 532
}


533
static int SortPadsByXCoord( const void* pt_ref, const void* pt_comp )
534

535
/* used to Sort a pad list by x coordinate value
536
 */
537
{
538 539 540 541
    D_PAD* ref  = *(LISTE_PAD*) pt_ref;
    D_PAD* comp = *(LISTE_PAD*) pt_comp;

    return ref->m_Pos.x - comp->m_Pos.x;
542 543
}

544

545
/****************************************************/
546
LISTE_PAD* CreateSortedPadListByXCoord( BOARD* pcb )
547
/****************************************************/
548

549
/* Create a sorted list of pointers to pads.
550
 *  This list is sorted by X ccordinate value.
551
 *  The list must be freed by user
552
 */
553
{
554
    LISTE_PAD* pad_list = (LISTE_PAD*) MyMalloc( pcb->m_NbPads * sizeof(D_PAD*) );
555

556 557
    memcpy( pad_list, pcb->m_Pads, pcb->m_NbPads * sizeof( D_PAD*) );
    qsort( pad_list, pcb->m_NbPads, sizeof( D_PAD*), SortPadsByXCoord );
558
    return pad_list;
559 560
}

561

562
/********************************************************************/
563
void WinEDA_BasePcbFrame::reattribution_reference_piste( int affiche )
564 565 566
/********************************************************************/

/* search connections between tracks and pads, and propagate pad net codes to the track segments
567 568 569 570
 * This is a 2 pass computation.
 * The pad netcodes are assumed to be initialized.
 * First:
 * We search a connection between a track segment and a pad: if found : this segment  netcode is set to the pad netcode
571
 */
572
{
573
    TRACK*      pt_piste,
574
    * pt_next;
575 576 577 578 579 580
    int         a_color;
    char        new_passe_request = 1, flag;
    LISTE_PAD*  pt_mem;
    BOARD_ITEM* PtStruct;
    int         masque_layer;
    wxString    msg;
581 582 583

    if( m_Pcb->m_NbPads == 0 )
        return;
584

585
    a_color = CYAN;
586

587 588 589 590 591 592 593 594
    if( affiche )
        Affiche_1_Parametre( this, POS_AFF_CHREF, wxT( "DataBase" ), wxT( "Netcodes" ), a_color );

    recalcule_pad_net_code();

    if( affiche )
        Affiche_1_Parametre( this, -1, wxEmptyString, wxT( "Gen Pads " ), a_color );

595 596 597
    /**************************************************************/
    /* Pass 1: search the connections between track ends and pads */
    /**************************************************************/
598 599 600 601 602
    pt_mem = CreateSortedPadListByXCoord( m_Pcb );

    if( affiche )
        Affiche_1_Parametre( this, -1, wxEmptyString, wxT( "Conn Pads" ), a_color );

603
    /* Reset variables and flags used in computation */
604 605 606 607
    pt_piste = m_Pcb->m_Track;
    for( ; pt_piste != NULL; pt_piste = (TRACK*) pt_piste->Pnext )
    {
        pt_piste->SetState( BUSY | EDIT | BEGIN_ONPAD | END_ONPAD, OFF );
608
        pt_piste->SetNet( 0 );  // net code = 0 means not connected
609 610
    }

611
    /* First pass: search connection between a track segment and a pad.
612
     * if found, set the track net code to the pad netcode
613
     */
614 615 616 617 618 619
    pt_piste = m_Pcb->m_Track;
    for( ; pt_piste != NULL; pt_piste = (TRACK*) pt_piste->Pnext )
    {
        flag = 0;
        masque_layer = g_TabOneLayerMask[pt_piste->GetLayer()];

620
        /* Search for a pad on the segment starting point */
621 622 623 624 625 626 627 628
        pt_piste->start = SuperFast_Locate_Pad_Connecte( m_Pcb,
                                                         pt_mem,
                                                         pt_piste->m_Start.x,
                                                         pt_piste->m_Start.y,
                                                         masque_layer );
        if( pt_piste->start != NULL )
        {
            pt_piste->SetState( BEGIN_ONPAD, ON );
629
            pt_piste->SetNet( ( (D_PAD*) (pt_piste->start) )->GetNet() );
630 631
        }

632
        /* Search for a pad on the segment ending point */
633 634 635 636 637 638 639 640 641
        pt_piste->end = SuperFast_Locate_Pad_Connecte( m_Pcb,
                                                       pt_mem,
                                                       pt_piste->m_End.x,
                                                       pt_piste->m_End.y,
                                                       masque_layer );

        if( pt_piste->end != NULL )
        {
            pt_piste->SetState( END_ONPAD, ON );
642
            pt_piste->SetNet( ( (D_PAD*) (pt_piste->end) )->GetNet() );
643 644 645 646 647
        }
    }

    MyFree( pt_mem );

648 649 650
    /*****************************************************/
    /* Pass 2: search the connections between track ends */
    /*****************************************************/
651

652 653 654
    /*  the .start et .end member pointers are updated, only if NULLs
     * (if not nuls, the end is already connected to a pad).
     * the connection (if found) is between segments
655
     * when a track has a net code and the other has a null net code, the null net code is changed
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
     */
    if( affiche )
        Affiche_1_Parametre( this, POS_AFF_CHREF, wxEmptyString, wxT( "Conn Segm" ), a_color );

    for( pt_piste = m_Pcb->m_Track; pt_piste != NULL; pt_piste = pt_piste->Next() )
    {
        if( pt_piste->start == NULL )
        {
            pt_piste->start = Locate_Piste_Connectee( pt_piste, m_Pcb->m_Track, NULL, START );
        }

        if( pt_piste->end == NULL )
        {
            pt_piste->end = Locate_Piste_Connectee( pt_piste, m_Pcb->m_Track, NULL, END );
        }
    }

673 674 675
    /**********************************************************/
    /* Propagate net codes from a segment to an other segment */
    /**********************************************************/
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691

    a_color = YELLOW;

    while( new_passe_request )
    {
        bool reset_flag = FALSE;
        new_passe_request = 0;
        if( affiche )
        {
            msg.Printf( wxT( "Net->Segm pass %d  " ), new_passe_request + 1 );
            Affiche_1_Parametre( this, POS_AFF_CHREF, wxEmptyString, msg, a_color );
        }

        /* look for vias which could be connect many tracks */
        for( TRACK* via = m_Pcb->m_Track; via != NULL; via = via->Next() )
        {
692
            if( via->Type() != TYPEVIA )
693
                continue;
694

695
            if( via->GetNet() > 0 )
696 697
                continue; // Netcode already known

698 699 700 701
            // Lock for a connection to a track with a known netcode
            pt_next = m_Pcb->m_Track;
            while( ( pt_next = Locate_Piste_Connectee( via, pt_next, NULL, START ) ) != NULL )
            {
702
                if( pt_next->GetNet() )
703
                {
704
                    via->SetNet( pt_next->GetNet() );
705 706 707 708 709 710 711 712 713 714 715 716 717
                    break;
                }
                pt_next->SetState( BUSY, ON );
                reset_flag = TRUE;
            }
        }

        if( reset_flag )
            for( pt_piste = m_Pcb->m_Track; pt_piste != NULL; pt_piste = pt_piste->Next() )
            {
                pt_piste->SetState( BUSY, OFF );
            }

718 719 720 721 722 723 724
        /* set the netcode of connected tracks: if at track is connected to a pad, its net code is already set.
         * if the current track is connected to an other track:
         * if a track has a net code, it is used for the other track.
         * Thus there is a propagation of the netcode from a track to an other.
         * if none of the 2 track has a net code we do nothing
         * the iteration is stopped when no new change occurs
         */
725 726
        for( pt_piste = m_Pcb->m_Track; pt_piste != NULL; pt_piste = pt_piste->Next() )
        {
727
            /* look for the connection to the current segment starting point */
728
            PtStruct = (BOARD_ITEM*) pt_piste->start;
729
            if( PtStruct && (PtStruct->Type() != TYPEPAD) )
730
            {
731
                // Begin on an other track segment
732
                pt_next = (TRACK*) PtStruct;
733
                if( pt_piste->GetNet() )
734
                {
735
                    if( pt_next->GetNet() == 0 )    // the current track has a netcode, we use it for the other track
736
                    {
737
                        new_passe_request = 1;      // A change is made: a new iteration is requested.
738
                        pt_next->SetNet( pt_piste->GetNet() );
739 740 741 742
                    }
                }
                else
                {
743
                    if( pt_next->GetNet() != 0 )    // the other track has a netcode, we use it for the current track
744
                    {
745
                        pt_piste->SetNet( pt_next->GetNet() );
746
                        new_passe_request = 1;
747 748 749 750
                    }
                }
            }

751
            /* look for the connection to the current segment ending point */
752
            PtStruct = pt_piste->end;
753
            if( PtStruct &&(PtStruct->Type() != TYPEPAD) )
754
            {
755
                pt_next = (TRACK*) PtStruct;
756 757

                // End on an other track: propagate netcode if possible
758
                if( pt_piste->GetNet() )
759
                {
760
                    if( pt_next->GetNet() == 0 )
761
                    {
762
                        new_passe_request = 1;
763
                        pt_next->SetNet( pt_piste->GetNet() );
764 765 766 767
                    }
                }
                else
                {
768
                    if( pt_next->GetNet() != 0 )
769
                    {
770
                        pt_piste->SetNet( pt_next->GetNet() );
771
                        new_passe_request = 1;
772 773 774 775 776 777
                    }
                }
            }
        }
    }

778
    /* Sort the track list by net codes: */
779 780 781 782 783 784
    if( affiche )
        Affiche_1_Parametre( this, -1, wxEmptyString, wxT( "Reorder " ), a_color );
    RebuildTrackChain( m_Pcb );

    if( affiche )
        Affiche_1_Parametre( this, -1, wxEmptyString, wxT( "         " ), a_color );
785 786
}

787

788
/*
789 790
 *  Sort function for track segments used in RebuildTrackChain() (for the qsort C function)
 *  The sorting is made by net code
791
 */
792
int Sort_By_NetCode( TRACK** pt_ref, TRACK** pt_compare )
793
{
794
    int ii;
795

796
    ii = (*pt_ref)->GetNet() - (*pt_compare)->GetNet();
797 798
    return ii;
}
799 800 801


/*****************************************/
802
static void RebuildTrackChain( BOARD* pcb )
803
/*****************************************/
804

805 806 807
/** Function RebuildTrackChain()
 * @param pcb = board to rebuild
 * Rebuild the track segment linked list in order to have a chain sorted by increasing netcodes
808
 */
809
{
810 811 812 813 814 815 816 817
    TRACK* Track, ** Liste;
    int    ii, nbsegm;

    /* Count segments */
    nbsegm = pcb->GetNumSegmTrack();
    if( pcb->m_Track == NULL )
        return;

818
    Liste = (TRACK**) MyZMalloc( (nbsegm + 1) * sizeof(TRACK*) );
819 820 821 822 823 824 825

    ii = 0; Track = pcb->m_Track;
    for( ; Track != NULL; ii++, Track = (TRACK*) Track->Pnext )
    {
        Liste[ii] = Track;
    }

826 827
    qsort( Liste, nbsegm, sizeof(TRACK*),
           ( int( * ) ( const void*, const void* ) )Sort_By_NetCode );
828 829 830 831 832 833 834 835 836 837 838 839 840 841

    /* Update the linked list pointers */

    Track = Liste[0];
    Track->Pback = pcb; Track->Pnext = Liste[1];
    pcb->m_Track = Track;
    for( ii = 1; ii < nbsegm; ii++ )
    {
        Track = Liste[ii];
        Track->Pback = Liste[ii - 1];
        Track->Pnext = Liste[ii + 1];
    }

    MyFree( Liste );
842
}