netlist.cpp 27.3 KB
Newer Older
1 2 3
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
4 5 6
 * Copyright (C) 2013 Jean-Pierre Charras, jp.charras at wanadoo.fr
 * Copyright (C) 2013 Wayne Stambaugh <stambaughw@verizon.net>
 * Copyright (C) 1992-2013 KiCad Developers, see AUTHORS.txt for contributors.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

26 27 28
/**
 * @file eeschema/netlist.cpp
 */
plyatov's avatar
plyatov committed
29

30
#include <fctsys.h>
31
#include <schframe.h>
32
#include <confirm.h>
33

34
#include <netlist.h>
35
#include <class_netlist_object.h>
36 37 38 39 40 41 42 43 44
#include <class_library.h>
#include <lib_pin.h>
#include <sch_junction.h>
#include <sch_component.h>
#include <sch_line.h>
#include <sch_no_connect.h>
#include <sch_text.h>
#include <sch_sheet.h>
#include <algorithm>
45

46 47
#include <boost/foreach.hpp>

48 49
#define IS_WIRE false
#define IS_BUS true
50

51 52 53 54 55 56 57
//Imported function:
int TestDuplicateSheetNames( bool aCreateMarker );

bool SCH_EDIT_FRAME::CreateNetlist( int aFormat, const wxString& aFullFileName,
                                    unsigned aNetlistOptions )
{
    SCH_SHEET_LIST sheets;
58 59

    sheets.AnnotatePowerSymbols( Prj().SchLibs() );
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

    // Performs some controls:
    if( CheckAnnotate( NULL, 0 ) )
    {
        if( !IsOK( NULL, _( "Some items are not annotated\n"
                            "Do you want to annotate schematic?" ) ) )
            return false;

        // Schematic must be annotated: call Annotate dialog:
        wxCommandEvent event;
        OnAnnotate( event );

        if( CheckAnnotate( NULL, 0 ) )
            return false;
    }

    // Test duplicate sheet names:
    if( TestDuplicateSheetNames( false ) > 0 )
    {
        if( !IsOK( NULL, _( "Error: duplicate sheet names. Continue?" ) ) )
            return false;
    }

    // Cleanup the entire hierarchy
    SCH_SCREENS screens;
85

86 87
    screens.SchematicCleanUp();

88 89
    NETLIST_OBJECT_LIST* connectedItemsList = BuildNetListBase();

90 91 92 93 94 95 96
    bool success = WriteNetListFile( connectedItemsList, aFormat,
                                     aFullFileName, aNetlistOptions );

    return success;
}


97
// Buffer to build the list of items used in netlist and erc calculations
98
NETLIST_OBJECT_LIST s_NetObjectslist( true );
99

100
//#define NETLIST_DEBUG
plyatov's avatar
plyatov committed
101

102 103 104
NETLIST_OBJECT_LIST::~NETLIST_OBJECT_LIST()
{
    if( m_isOwner )
105 106 107
        FreeList();
    else
        Clear();
108
}
dickelbeck's avatar
dickelbeck committed
109

110

111
void NETLIST_OBJECT_LIST::FreeList()
dickelbeck's avatar
dickelbeck committed
112
{
113 114 115
    std::vector<NETLIST_OBJECT*>::iterator iter;

    for( iter = begin(); iter != end(); iter++ )
dickelbeck's avatar
dickelbeck committed
116
    {
117 118
        NETLIST_OBJECT* item = *iter;
        delete item;
dickelbeck's avatar
dickelbeck committed
119 120
    }

121 122
    clear();
}
123

124

125
void NETLIST_OBJECT_LIST::SortListbyNetcode()
126
{
127
    sort( this->begin(), this->end(), NETLIST_OBJECT_LIST::sortItemsbyNetcode );
128 129
}

130

131
void NETLIST_OBJECT_LIST::SortListbySheet()
plyatov's avatar
plyatov committed
132
{
133
    sort( this->begin(), this->end(), NETLIST_OBJECT_LIST::sortItemsBySheet );
plyatov's avatar
plyatov committed
134 135
}

136

137
NETLIST_OBJECT_LIST * SCH_EDIT_FRAME::BuildNetListBase()
plyatov's avatar
plyatov committed
138
{
139 140
    // Creates the flattened sheet list:
    SCH_SHEET_LIST aSheets;
141

142
    // Build netlist info
143
    bool success = s_NetObjectslist.BuildNetListInfo( aSheets );
144

145
    if( !success )
146
    {
147
        SetStatusText( _("No Objects" ) );
148
        return &s_NetObjectslist;
149
    }
150

151 152
    /* The new %zu specification is needed to properly format a size_t
     * value (returned by size(), here) */
153
    wxString msg;
154

155
    msg.Printf( _( "Net count = %zu" ), s_NetObjectslist.size() );
156
    SetStatusText( msg );
157 158

    return &s_NetObjectslist;
159
}
160

161

162 163
bool NETLIST_OBJECT_LIST::BuildNetListInfo( SCH_SHEET_LIST& aSheets )
{
164 165
    s_NetObjectslist.SetOwner( true );
    s_NetObjectslist.FreeList();
166

167 168 169 170 171 172 173 174 175 176 177 178 179 180
    SCH_SHEET_PATH* sheet;

    // Fill list with connected items from the flattened sheet list
    for( sheet = aSheets.GetFirst(); sheet != NULL;
         sheet = aSheets.GetNext() )
    {
        for( SCH_ITEM* item = sheet->LastScreen()->GetDrawItems(); item; item = item->Next() )
        {
            item->GetNetListItem( *this, sheet );
        }
    }

    if( size() == 0 )
        return false;
181

182 183 184
    // Sort objects by Sheet
    SortListbySheet();

185
    sheet = &(GetItem( 0 )->m_SheetPath);
186
    m_lastNetCode = m_lastBusNetCode = 1;
187

188
    for( unsigned ii = 0, istart = 0; ii < size(); ii++ )
189
    {
190
        NETLIST_OBJECT* net_item = GetItem( ii );
191

192
        if( net_item->m_SheetPath != *sheet )   // Sheet change
193
        {
194
            sheet  = &(net_item->m_SheetPath);
195
            istart = ii;
196 197
        }

198
        switch( net_item->m_Type )
199
        {
200
        case NET_ITEM_UNSPECIFIED:
201
            wxMessageBox( wxT( "BuildNetListBase() error" ) );
202
            break;
203

204 205 206 207
        case NET_PIN:
        case NET_PINLABEL:
        case NET_SHEETLABEL:
        case NET_NOCONNECT:
208
            if( net_item->GetNet() != 0 )
209
                break;
210 211

        case NET_SEGMENT:
212
            // Test connections point to point type without bus.
213
            if( net_item->GetNet() == 0 )
214
            {
215 216
                net_item->SetNet( m_lastNetCode );
                m_lastNetCode++;
217
            }
218

219
            pointToPointConnect( net_item, IS_WIRE, istart );
220 221
            break;

222
        case NET_JUNCTION:
223
            // Control of the junction outside BUS.
224
            if( net_item->GetNet() == 0 )
225
            {
226 227
                net_item->SetNet( m_lastNetCode );
                m_lastNetCode++;
228
            }
229

230
            segmentToPointConnect( net_item, IS_WIRE, istart );
231

232
            // Control of the junction, on BUS.
233
            if( net_item->m_BusNetCode == 0 )
234
            {
235 236
                net_item->m_BusNetCode = m_lastBusNetCode;
                m_lastBusNetCode++;
237
            }
238

239
            segmentToPointConnect( net_item, IS_BUS, istart );
240 241 242
            break;

        case NET_LABEL:
243
        case NET_HIERLABEL:
244
        case NET_GLOBLABEL:
245
            // Test connections type junction without bus.
246
            if( net_item->GetNet() == 0 )
247
            {
248 249
                net_item->SetNet( m_lastNetCode );
                m_lastNetCode++;
250
            }
251

252
            segmentToPointConnect( net_item, IS_WIRE, istart );
253 254 255
            break;

        case NET_SHEETBUSLABELMEMBER:
256
            if( net_item->m_BusNetCode != 0 )
257
                break;
258 259

        case NET_BUS:
260
            // Control type connections point to point mode bus
261
            if( net_item->m_BusNetCode == 0 )
262
            {
263 264
                net_item->m_BusNetCode = m_lastBusNetCode;
                m_lastBusNetCode++;
265
            }
266

267
            pointToPointConnect( net_item, IS_BUS, istart );
268 269 270
            break;

        case NET_BUSLABELMEMBER:
271
        case NET_HIERBUSLABELMEMBER:
272
        case NET_GLOBBUSLABELMEMBER:
273
            // Control connections similar has on BUS
274
            if( net_item->GetNet() == 0 )
275
            {
276 277
                net_item->m_BusNetCode = m_lastBusNetCode;
                m_lastBusNetCode++;
278
            }
279

280
            segmentToPointConnect( net_item, IS_BUS, istart );
281 282 283 284
            break;
        }
    }

285
#if defined(NETLIST_DEBUG) && defined(DEBUG)
286
    std::cout << "\n\nafter sheet local\n\n";
287
    DumpNetTable();
288
#endif
dickelbeck's avatar
dickelbeck committed
289

290
    // Updating the Bus Labels Netcode connected by Bus
291
    connectBusLabels();
292

293
    // Group objects by label.
294
    for( unsigned ii = 0; ii < size(); ii++ )
295
    {
296
        switch( GetItem( ii )->m_Type )
297 298 299 300
        {
        case NET_PIN:
        case NET_SHEETLABEL:
        case NET_SEGMENT:
301
        case NET_JUNCTION:
302 303 304 305 306
        case NET_BUS:
        case NET_NOCONNECT:
            break;

        case NET_LABEL:
307
        case NET_GLOBLABEL:
308 309
        case NET_PINLABEL:
        case NET_BUSLABELMEMBER:
310
        case NET_GLOBBUSLABELMEMBER:
311
            labelConnect( GetItem( ii ) );
312 313 314
            break;

        case NET_SHEETBUSLABELMEMBER:
315 316
        case NET_HIERLABEL:
        case NET_HIERBUSLABELMEMBER:
317
            break;
318

319 320
        case NET_ITEM_UNSPECIFIED:
            break;
321 322 323
        }
    }

324
#if defined(NETLIST_DEBUG) && defined(DEBUG)
325
    std::cout << "\n\nafter sheet global\n\n";
326
    DumpNetTable();
327 328
#endif

329
    // Connection between hierarchy sheets
330
    for( unsigned ii = 0; ii < size(); ii++ )
331
    {
332 333
        if( GetItem( ii )->m_Type == NET_SHEETLABEL
            || GetItem( ii )->m_Type == NET_SHEETBUSLABELMEMBER )
334
            sheetLabelConnect( GetItem( ii ) );
335
    }
336

337 338
    // Sort objects by NetCode
    SortListbyNetcode();
339

340
#if defined(NETLIST_DEBUG) && defined(DEBUG)
341
    std::cout << "\n\nafter qsort()\n";
342
    DumpNetTable();
343 344
#endif

345
    // Compress numbers of Netcode having consecutive values.
346
    int NetCode = 0;
347
    m_lastNetCode = 0;
348

349
    for( unsigned ii = 0; ii < size(); ii++ )
350
    {
351
        if( GetItem( ii )->GetNet() != m_lastNetCode )
352
        {
353
            NetCode++;
354
            m_lastNetCode = GetItem( ii )->GetNet();
355
        }
356

357
        GetItem( ii )->SetNet( NetCode );
358 359
    }

360
    // Set the minimal connection info:
361
    setUnconnectedFlag();
jean-pierre charras's avatar
jean-pierre charras committed
362

363
    // find the best label object to give the best net name to each net
364
    findBestNetNameForEachNet();
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386

    return true;
}

// Helper function to give a priority to sort labels:
// NET_PINLABEL and NET_GLOBLABEL are global labels
// and the priority is hight
static int getPriority( const NETLIST_OBJECT* Objet )
{
    switch( Objet->m_Type )
    {
        case NET_PIN: return 1;
        case NET_LABEL: return 2;
        case NET_HIERLABEL: return 3;
        case NET_PINLABEL: return 4;
        case NET_GLOBLABEL: return 5;
        default: break;
    }

    return 0;
}

387

388
/* function evalLabelsPriority used by findBestNetNameForEachNet()
389
 * evalLabelsPriority calculates the priority of alabel1 and aLabel2
390
 * return true if alabel1 has a highter priority than aLabel2
391 392 393 394 395 396 397 398
 */
static bool evalLabelsPriority( const NETLIST_OBJECT* aLabel1,
                                 const NETLIST_OBJECT* aLabel2 )
{
    int priority1 = getPriority( aLabel1 );
    int priority2 = getPriority( aLabel2 );

    if( priority1 != priority2 )
399
        return priority1 > priority2;
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415

    // Objects have here the same priority, therefore they have the same type.

    // for global labels, we select the best candidate by alphabetic order
    // because they have no sheetpath as prefix name
    // for other labels, we select them before by sheet deep order
    // because the actual name is /sheetpath/label
    // and for a given path length, by alphabetic order

    if( aLabel1->m_Type == NET_PINLABEL || aLabel1->m_Type == NET_GLOBLABEL )
        return aLabel1->m_Label.Cmp( aLabel2->m_Label );

    // not global: names are prefixed by their sheetpath
    // use name defined in higher hierarchical sheet
    // (i.e. shorter path because paths are /<timestamp1>/<timestamp2>/...
    // and timestamp = 8 letters.
416 417
    if( aLabel1->m_SheetPath.Path().Length() != aLabel2->m_SheetPath.Path().Length() )
        return aLabel1->m_SheetPath.Path().Length() < aLabel2->m_SheetPath.Path().Length();
418 419 420 421 422

    // Sheet paths have the same length: use alphabetic label name order
    // For labels on sheets having an equivalent deep in hierarchy, use
    // alphabetic label name order:
    if( aLabel1->m_Label.Cmp( aLabel2->m_Label ) != 0 )
423
        return aLabel1->m_Label.Cmp( aLabel2->m_Label ) < 0;
424

425 426
    return aLabel1->m_SheetPath.PathHumanReadable().Cmp(
                aLabel2->m_SheetPath.PathHumanReadable() ) < 0;
jean-pierre charras's avatar
jean-pierre charras committed
427 428 429
}


430
void NETLIST_OBJECT_LIST::findBestNetNameForEachNet()
jean-pierre charras's avatar
jean-pierre charras committed
431 432
{
    int netcode = 0;            // current netcode for tested items
433 434 435 436 437 438 439 440
    unsigned idxstart = 0;      // index of the first item of this net
    NETLIST_OBJECT* item;
    NETLIST_OBJECT* candidate;

    // Pass 1: find the best name for labelled nets:
    item = NULL;
    candidate = NULL;
    for( unsigned ii = 0; ii <= size(); ii++ )
jean-pierre charras's avatar
jean-pierre charras committed
441
    {
442
        if( ii == size() ) // last item already found
jean-pierre charras's avatar
jean-pierre charras committed
443 444
            netcode = -2;
        else
445
            item = GetItem( ii );
446

447
        if( netcode != item->GetNet() )     // End of net found
jean-pierre charras's avatar
jean-pierre charras committed
448
        {
449
            if( candidate )         // One or more labels exists, find the best
jean-pierre charras's avatar
jean-pierre charras committed
450 451
            {
                for (unsigned jj = idxstart; jj < ii; jj++ )
452
                    GetItem( jj )->SetNetNameCandidate( candidate );
jean-pierre charras's avatar
jean-pierre charras committed
453
            }
454

jean-pierre charras's avatar
jean-pierre charras committed
455 456
            if( netcode == -2 )
                break;
457

jean-pierre charras's avatar
jean-pierre charras committed
458
            netcode = item->GetNet();
459
            candidate = NULL;
jean-pierre charras's avatar
jean-pierre charras committed
460 461
            idxstart = ii;
        }
462

jean-pierre charras's avatar
jean-pierre charras committed
463 464 465 466 467
        switch( item->m_Type )
        {
        case NET_HIERLABEL:
        case NET_LABEL:
        case NET_PINLABEL:
468
        case NET_GLOBLABEL:
469 470 471 472 473 474 475
            // A candidate is found: select the better between the previous
            // and this one
            if( candidate == NULL )
                candidate = item;
            else
            {
                if( evalLabelsPriority( item, candidate ) )
476 477
                    // item has a highter priority than candidate
                    // so update the best candidate
478 479
                    candidate = item;
            }
jean-pierre charras's avatar
jean-pierre charras committed
480 481 482 483 484 485 486
            break;

        default:
            break;
        }
    }

487 488 489 490 491 492 493
    // Pass 2: find the best name for not labelled nets:
    // The "default" net name is Net-<<Ref cmp>_Pad<num pad>>
    // (see NETLIST_OBJECT::GetShortNetName())
    // therefore the "best" is the short net name alphabetically classed first
    // (to avoid net names changes when the net is not modified,
    // even if components are moved or deleted and undelete or replaced, as long
    // the reference is kept)
494 495 496 497 498 499 500 501 502 503 504 505 506

    // Build the list of items with no net names
    NETLIST_OBJECT_LIST list;
    for( unsigned ii = 0; ii < size(); ii++ )
    {
        item = GetItem( ii );
        if( !item->HasNetNameCandidate() )
            list.push_back( item );
    }

    if( list.size() == 0 )
        return;

507 508
    idxstart = 0;
    candidate = NULL;
509 510 511
    netcode = list.GetItemNet( 0 );

    for( unsigned ii = 0; ii <= list.size(); ii++ )
512
    {
513 514
        if( ii < list.size() )
            item = list.GetItem( ii );
515

516
        if( netcode != item->GetNet() || ii >= list.size() )     // End of net found
jean-pierre charras's avatar
jean-pierre charras committed
517
        {
518
            if( candidate )
519
            {
520
                for (unsigned jj = idxstart; jj < ii; jj++ )
521 522 523 524
                {
                    NETLIST_OBJECT* obj = list.GetItem( jj );
                    obj->SetNetNameCandidate( candidate );
                }
525
            }
526

527
            if( ii >= list.size() )
528 529 530 531 532
                break;

            netcode = item->GetNet();
            candidate = NULL;
            idxstart = ii;
533
        }
534

535 536 537 538 539 540
        // Examine all pins of the net to find the best candidate,
        // i.e. the first net name candidate, by alphabetic order
        // the net names are names bu_ilt by GetShortNetName
        // (Net-<{reference}-Pad{pad number}> like Net-<U3-Pad5>
        // Not named nets do not have usually a lot of members.
        // Many have only 2 members(a pad and a non connection symbol)
541
        if( item->m_Type == NET_PIN )
542
        {
543 544
            // A candidate is found, however components which are not in
            // netlist are not candidate because some have their reference
545 546 547 548
            // changed each time the netlist is built (power components)
            // and anyway obviously they are not a good candidate
            SCH_COMPONENT* link = item->GetComponentParent();
            if( link && link->IsInNetlist() )
549
            {
550 551
                // select the better between the previous and this one
                item->SetNetNameCandidate( item );  // Needed to calculate GetShortNetName
552

553
                if( candidate == NULL )
554
                    candidate = item;
555 556 557 558 559
                else
                {
                    if( item->GetShortNetName().Cmp( candidate->GetShortNetName() ) < 0 )
                        candidate = item;
                }
560
            }
561
        }
jean-pierre charras's avatar
jean-pierre charras committed
562 563
    }
}
564

565

566
void NETLIST_OBJECT_LIST::sheetLabelConnect( NETLIST_OBJECT* SheetLabel )
plyatov's avatar
plyatov committed
567
{
568
    if( SheetLabel->GetNet() == 0 )
569 570
        return;

571
    for( unsigned ii = 0; ii < size(); ii++ )
572
    {
573
        NETLIST_OBJECT* ObjetNet = GetItem( ii );
574

575
        if( ObjetNet->m_SheetPath != SheetLabel->m_SheetPathInclude )
576
            continue;  //use SheetInclude, not the sheet!!
577

578
        if( (ObjetNet->m_Type != NET_HIERLABEL ) && (ObjetNet->m_Type != NET_HIERBUSLABELMEMBER ) )
579
            continue;
580

581
        if( ObjetNet->GetNet() == SheetLabel->GetNet() )
582
            continue;  //already connected.
583

584
        if( ObjetNet->m_Label.CmpNoCase( SheetLabel->m_Label ) != 0 )
585
            continue;  //different names.
586

587
        // Propagate Netcode having all the objects of the same Netcode.
588
        if( ObjetNet->GetNet() )
589
            propageNetCode( ObjetNet->GetNet(), SheetLabel->GetNet(), IS_WIRE );
590
        else
591
            ObjetNet->SetNet( SheetLabel->GetNet() );
592
    }
plyatov's avatar
plyatov committed
593 594
}

595

596
void NETLIST_OBJECT_LIST::connectBusLabels()
plyatov's avatar
plyatov committed
597
{
598
    for( unsigned ii = 0; ii < size(); ii++ )
599
    {
600
        NETLIST_OBJECT* Label = GetItem( ii );
601 602 603 604

        if(  (Label->m_Type == NET_SHEETBUSLABELMEMBER)
          || (Label->m_Type == NET_BUSLABELMEMBER)
          || (Label->m_Type == NET_HIERBUSLABELMEMBER) )
605
        {
606
            if( Label->GetNet() == 0 )
607
            {
608 609
                Label->SetNet( m_lastNetCode );
                m_lastNetCode++;
610
            }
611

612
            for( unsigned jj = ii + 1; jj < size(); jj++ )
613
            {
614
                NETLIST_OBJECT* LabelInTst =  GetItem( jj );
615 616
                if( (LabelInTst->m_Type == NET_SHEETBUSLABELMEMBER)
                   || (LabelInTst->m_Type == NET_BUSLABELMEMBER)
617
                   || (LabelInTst->m_Type == NET_HIERBUSLABELMEMBER) )
618 619 620
                {
                    if( LabelInTst->m_BusNetCode != Label->m_BusNetCode )
                        continue;
621

622 623
                    if( LabelInTst->m_Member != Label->m_Member )
                        continue;
624

625 626
                    if( LabelInTst->GetNet() == 0 )
                        LabelInTst->SetNet( Label->GetNet() );
627
                    else
628
                        propageNetCode( LabelInTst->GetNet(), Label->GetNet(), IS_WIRE );
629 630 631 632
                }
            }
        }
    }
plyatov's avatar
plyatov committed
633
}
634 635


636
void NETLIST_OBJECT_LIST::propageNetCode( int aOldNetCode, int aNewNetCode, bool aIsBus )
plyatov's avatar
plyatov committed
637
{
638
    if( aOldNetCode == aNewNetCode )
639 640
        return;

641
    if( aIsBus == false )    // Propagate NetCode
642
    {
643
        for( unsigned jj = 0; jj < size(); jj++ )
644
        {
645
            NETLIST_OBJECT* object = GetItem( jj );
646

647 648
            if( object->GetNet() == aOldNetCode )
                object->SetNet( aNewNetCode );
649 650
        }
    }
651
    else               // Propagate BusNetCode
652
    {
653
        for( unsigned jj = 0; jj < size(); jj++ )
654
        {
655
            NETLIST_OBJECT* object = GetItem( jj );
656

657 658
            if( object->m_BusNetCode == aOldNetCode )
                object->m_BusNetCode = aNewNetCode;
659 660
        }
    }
plyatov's avatar
plyatov committed
661 662
}

663

664
void NETLIST_OBJECT_LIST::pointToPointConnect( NETLIST_OBJECT* aRef, bool aIsBus,
665
                                               int start )
plyatov's avatar
plyatov committed
666
{
667
    int netCode;
668

669
    if( aIsBus == false )    // Objects other than BUS and BUSLABELS
670
    {
671
        netCode = aRef->GetNet();
672

673
        for( unsigned i = start; i < size(); i++ )
674
        {
675
            NETLIST_OBJECT* item = GetItem( i );
676

677
            if( item->m_SheetPath != aRef->m_SheetPath )  //used to be > (why?)
678
                continue;
679

680
            switch( item->m_Type )
681 682 683 684
            {
            case NET_SEGMENT:
            case NET_PIN:
            case NET_LABEL:
685
            case NET_HIERLABEL:
686
            case NET_GLOBLABEL:
687 688
            case NET_SHEETLABEL:
            case NET_PINLABEL:
689
            case NET_JUNCTION:
690
            case NET_NOCONNECT:
691 692 693 694
                if( aRef->m_Start == item->m_Start
                    || aRef->m_Start == item->m_End
                    || aRef->m_End   == item->m_Start
                    || aRef->m_End   == item->m_End )
695
                {
696 697
                    if( item->GetNet() == 0 )
                        item->SetNet( netCode );
698
                    else
699
                        propageNetCode( item->GetNet(), netCode, IS_WIRE );
700 701 702 703 704 705
                }
                break;

            case NET_BUS:
            case NET_BUSLABELMEMBER:
            case NET_SHEETBUSLABELMEMBER:
706
            case NET_HIERBUSLABELMEMBER:
707
            case NET_GLOBBUSLABELMEMBER:
708
            case NET_ITEM_UNSPECIFIED:
709 710 711 712
                break;
            }
        }
    }
713
    else    // Object type BUS, BUSLABELS, and junctions.
714
    {
715
        netCode = aRef->m_BusNetCode;
716

717
        for( unsigned i = start; i < size(); i++ )
718
        {
719
            NETLIST_OBJECT* item = GetItem( i );
720

721
            if( item->m_SheetPath != aRef->m_SheetPath )
722
                continue;
723

724
            switch( item->m_Type )
725
            {
726
            case NET_ITEM_UNSPECIFIED:
727 728 729
            case NET_SEGMENT:
            case NET_PIN:
            case NET_LABEL:
730
            case NET_HIERLABEL:
731
            case NET_GLOBLABEL:
732 733 734 735 736 737 738 739
            case NET_SHEETLABEL:
            case NET_PINLABEL:
            case NET_NOCONNECT:
                break;

            case NET_BUS:
            case NET_BUSLABELMEMBER:
            case NET_SHEETBUSLABELMEMBER:
740
            case NET_HIERBUSLABELMEMBER:
741
            case NET_GLOBBUSLABELMEMBER:
742
            case NET_JUNCTION:
743 744 745 746
                if(  aRef->m_Start == item->m_Start
                  || aRef->m_Start == item->m_End
                  || aRef->m_End   == item->m_Start
                  || aRef->m_End   == item->m_End )
747
                {
748 749
                    if( item->m_BusNetCode == 0 )
                        item->m_BusNetCode = netCode;
750
                    else
751
                        propageNetCode( item->m_BusNetCode, netCode, IS_BUS );
752 753 754 755 756
                }
                break;
            }
        }
    }
plyatov's avatar
plyatov committed
757 758 759
}


760
void NETLIST_OBJECT_LIST::segmentToPointConnect( NETLIST_OBJECT* aJonction,
761
                                                bool aIsBus, int aIdxStart )
plyatov's avatar
plyatov committed
762
{
763
    for( unsigned i = aIdxStart; i < size(); i++ )
764
    {
765
        NETLIST_OBJECT* segment = GetItem( i );
766

767
        // if different sheets, obviously no physical connection between elements.
768
        if( segment->m_SheetPath != aJonction->m_SheetPath )
769
            continue;
770

771
        if( aIsBus == IS_WIRE )
772
        {
773
            if( segment->m_Type != NET_SEGMENT )
774 775 776 777
                continue;
        }
        else
        {
778
            if( segment->m_Type != NET_BUS )
779 780 781
                continue;
        }

782
        if( IsPointOnSegment( segment->m_Start, segment->m_End, aJonction->m_Start ) )
783
        {
784
            // Propagation Netcode has all the objects of the same Netcode.
785
            if( aIsBus == IS_WIRE )
786
            {
787 788
                if( segment->GetNet() )
                    propageNetCode( segment->GetNet(), aJonction->GetNet(), aIsBus );
789
                else
790
                    segment->SetNet( aJonction->GetNet() );
791 792 793
            }
            else
            {
794 795
                if( segment->m_BusNetCode )
                    propageNetCode( segment->m_BusNetCode, aJonction->m_BusNetCode, aIsBus );
796
                else
797
                    segment->m_BusNetCode = aJonction->m_BusNetCode;
798 799 800
            }
        }
    }
plyatov's avatar
plyatov committed
801 802
}

803

804
void NETLIST_OBJECT_LIST::labelConnect( NETLIST_OBJECT* aLabelRef )
plyatov's avatar
plyatov committed
805
{
806
    if( aLabelRef->GetNet() == 0 )
807 808
        return;

809
    for( unsigned i = 0; i < size(); i++ )
810
    {
811 812 813
        NETLIST_OBJECT* item = GetItem( i );

        if( item->GetNet() == aLabelRef->GetNet() )
814
            continue;
815

816
        if( item->m_SheetPath != aLabelRef->m_SheetPath )
817
        {
818 819
            if( item->m_Type != NET_PINLABEL && item->m_Type != NET_GLOBLABEL
                && item->m_Type != NET_GLOBBUSLABELMEMBER )
820
                continue;
821

822 823 824
            if( (item->m_Type == NET_GLOBLABEL
                 || item->m_Type == NET_GLOBBUSLABELMEMBER)
               && item->m_Type != aLabelRef->m_Type )
825
                //global labels only connect other global labels.
826 827
                continue;
        }
828

829
        // NET_HIERLABEL are used to connect sheets.
830 831
        // NET_LABEL are local to a sheet
        // NET_GLOBLABEL are global.
832
        // NET_PINLABEL is a kind of global label (generated by a power pin invisible)
833
        if( item->IsLabelType() )
834
        {
835
            if( item->m_Label.CmpNoCase( aLabelRef->m_Label ) != 0 )
836
                continue;
837

838 839
            if( item->GetNet() )
                propageNetCode( item->GetNet(), aLabelRef->GetNet(), IS_WIRE );
840
            else
841
                item->SetNet( aLabelRef->GetNet() );
842 843
        }
    }
plyatov's avatar
plyatov committed
844 845
}

846

847
void NETLIST_OBJECT_LIST::setUnconnectedFlag()
plyatov's avatar
plyatov committed
848
{
849 850
    NETLIST_OBJECT* NetItemRef;
    unsigned NetStart, NetEnd;
851
    NET_CONNECTION_T StateFlag;
852

853
    NetStart  = NetEnd = 0;
854
    StateFlag = UNCONNECTED;
855
    for( unsigned ii = 0; ii < size(); ii++ )
856
    {
857
        NetItemRef = GetItem( ii );
858 859
        if( NetItemRef->m_Type == NET_NOCONNECT && StateFlag != PAD_CONNECT )
            StateFlag = NOCONNECT_SYMBOL_PRESENT;
860

861
        // Analysis of current net.
862
        unsigned idxtoTest = ii + 1;
863

864 865
        if( ( idxtoTest >= size() )
           || ( NetItemRef->GetNet() != GetItem( idxtoTest )->GetNet() ) )
866
        {
867
            // Net analysis to update m_ConnectionType
868
            NetEnd = idxtoTest;
869

870
            /* set m_ConnectionType member to StateFlag for all items of
871
             * this net: */
872
            for( unsigned kk = NetStart; kk < NetEnd; kk++ )
873
                GetItem( kk )->m_ConnectionType = StateFlag;
874

875
            if( idxtoTest >= size() )
876 877
                return;

878
            // Start Analysis next Net
879
            StateFlag = UNCONNECTED;
880
            NetStart  = idxtoTest;
881 882 883
            continue;
        }

884 885 886 887 888 889 890
        /* test the current item: if this is a pin and if the reference item
         * is also a pin, then 2 pins are connected, so set StateFlag to
         * PAD_CONNECT (can be already done)  Of course, if the current
         * item is a no connect symbol, set StateFlag to
         * NOCONNECT_SYMBOL_PRESENT to inhibit error diags. However if
         * StateFlag is already set to PAD_CONNECT this state is kept (the
         * no connect symbol was surely an error and an ERC will report this)
891
         */
892
       for( ; ; idxtoTest++ )
893
        {
894 895
            if( ( idxtoTest >= size() )
               || ( NetItemRef->GetNet() != GetItem( idxtoTest )->GetNet() ) )
896 897
                break;

898
            switch( GetItem( idxtoTest )->m_Type )
899
            {
900
            case NET_ITEM_UNSPECIFIED:
901
                wxMessageBox( wxT( "BuildNetListBase() error" ) );
902
                break;
903

904 905
            case NET_SEGMENT:
            case NET_LABEL:
906
            case NET_HIERLABEL:
907
            case NET_GLOBLABEL:
908 909 910 911 912
            case NET_SHEETLABEL:
            case NET_PINLABEL:
            case NET_BUS:
            case NET_BUSLABELMEMBER:
            case NET_SHEETBUSLABELMEMBER:
913
            case NET_HIERBUSLABELMEMBER:
914
            case NET_GLOBBUSLABELMEMBER:
915
            case NET_JUNCTION:
916 917 918 919
                break;

            case NET_PIN:
                if( NetItemRef->m_Type == NET_PIN )
dickelbeck's avatar
dickelbeck committed
920
                    StateFlag = PAD_CONNECT;
921

922 923 924
                break;

            case NET_NOCONNECT:
dickelbeck's avatar
dickelbeck committed
925
                if( StateFlag != PAD_CONNECT )
926
                    StateFlag = NOCONNECT_SYMBOL_PRESENT;
927

928 929 930 931
                break;
            }
        }
    }
plyatov's avatar
plyatov committed
932
}