summaryrefslogtreecommitdiff
path: root/eeschema/netlist.cpp
blob: 10a1ddddd873ee730d2f9df3f2e62d0cfd679d16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
 * Copyright (C) 2013 Wayne Stambaugh <stambaughw@verizon.net>
 * Copyright (C) 1992-2015 KiCad Developers, see AUTHORS.txt for contributors.
 *
 * 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
 */

/**
 * @file eeschema/netlist.cpp
 */

#include <fctsys.h>
#include <schframe.h>
#include <confirm.h>
#include <netlist_exporter_kicad.h>
#include <kiway.h>

#include <netlist.h>
#include <class_netlist_object.h>
#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>
#include <invoke_sch_dialog.h>
#include <boost/foreach.hpp>

#define IS_WIRE false
#define IS_BUS true

//Imported function:
int TestDuplicateSheetNames( bool aCreateMarker );


bool SCH_EDIT_FRAME::prepareForNetlist()
{
    SCH_SHEET_LIST sheets;

    sheets.AnnotatePowerSymbols( Prj().SchLibs() );

    // Performs some controls:
    if( CheckAnnotate( NULL, 0 ) )
    {
        // Schematic must be annotated: call Annotate dialog and tell
        // the user why that is.
        InvokeDialogAnnotate( this,  _( "Exporting the netlist requires a "
                                        "completely\nannotated schematic." ) );

        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;

    screens.SchematicCleanUp();

    return true;
}


void SCH_EDIT_FRAME::sendNetlist()
{
    NETLIST_OBJECT_LIST* net_atoms = BuildNetListBase();

    NETLIST_EXPORTER_KICAD exporter( net_atoms, Prj().SchLibs() );

    STRING_FORMATTER    formatter;

    // @todo : trim GNL_ALL down to minimum for CVPCB
    exporter.Format( &formatter, GNL_ALL );

    Kiway().ExpressMail( FRAME_CVPCB,
        MAIL_EESCHEMA_NETLIST,
        formatter.GetString(),  // an abbreviated "kicad" (s-expr) netlist
        this
        );
}


bool SCH_EDIT_FRAME::CreateNetlist( int aFormat, const wxString& aFullFileName,
                                    unsigned aNetlistOptions, REPORTER* aReporter )
{
    if( !prepareForNetlist() )
        return false;

    std::auto_ptr<NETLIST_OBJECT_LIST> connectedItemsList( BuildNetListBase() );

    bool success = WriteNetListFile( connectedItemsList.release(), aFormat,
                                     aFullFileName, aNetlistOptions, aReporter );

    return success;
}


//#define NETLIST_DEBUG

NETLIST_OBJECT_LIST::~NETLIST_OBJECT_LIST()
{
    Clear();
}


void NETLIST_OBJECT_LIST::Clear()
{
    NETLIST_OBJECTS::iterator iter;

    for( iter = begin(); iter != end(); iter++ )
    {
        NETLIST_OBJECT* item = *iter;
        delete item;
    }

    clear();
}


void NETLIST_OBJECT_LIST::SortListbyNetcode()
{
    sort( this->begin(), this->end(), NETLIST_OBJECT_LIST::sortItemsbyNetcode );
}


void NETLIST_OBJECT_LIST::SortListbySheet()
{
    sort( this->begin(), this->end(), NETLIST_OBJECT_LIST::sortItemsBySheet );
}


NETLIST_OBJECT_LIST* SCH_EDIT_FRAME::BuildNetListBase()
{
    // I own this list until I return it to the new owner.
    std::auto_ptr<NETLIST_OBJECT_LIST> ret( new NETLIST_OBJECT_LIST() );

    // Creates the flattened sheet list:
    SCH_SHEET_LIST aSheets;

    // Build netlist info
    bool success = ret->BuildNetListInfo( aSheets );

    if( !success )
    {
        SetStatusText( _( "No Objects" ) );
        return ret.release();
    }

    wxString msg = wxString::Format( _( "Net count = %d" ), int( ret->size() ) );

    SetStatusText( msg );

    return ret.release();
}


bool NETLIST_OBJECT_LIST::BuildNetListInfo( SCH_SHEET_LIST& aSheets )
{
    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;

    // Sort objects by Sheet
    SortListbySheet();

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

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

        if( net_item->m_SheetPath != *sheet )   // Sheet change
        {
            sheet  = &(net_item->m_SheetPath);
            istart = ii;
        }

        switch( net_item->m_Type )
        {
        case NET_ITEM_UNSPECIFIED:
            wxMessageBox( wxT( "BuildNetListInfo() error" ) );
            break;

        case NET_PIN:
        case NET_PINLABEL:
        case NET_SHEETLABEL:
        case NET_NOCONNECT:
            if( net_item->GetNet() != 0 )
                break;

        case NET_SEGMENT:
            // Test connections point to point type without bus.
            if( net_item->GetNet() == 0 )
            {
                net_item->SetNet( m_lastNetCode );
                m_lastNetCode++;
            }

            pointToPointConnect( net_item, IS_WIRE, istart );
            break;

        case NET_JUNCTION:
            // Control of the junction outside BUS.
            if( net_item->GetNet() == 0 )
            {
                net_item->SetNet( m_lastNetCode );
                m_lastNetCode++;
            }

            segmentToPointConnect( net_item, IS_WIRE, istart );

            // Control of the junction, on BUS.
            if( net_item->m_BusNetCode == 0 )
            {
                net_item->m_BusNetCode = m_lastBusNetCode;
                m_lastBusNetCode++;
            }

            segmentToPointConnect( net_item, IS_BUS, istart );
            break;

        case NET_LABEL:
        case NET_HIERLABEL:
        case NET_GLOBLABEL:
            // Test connections type junction without bus.
            if( net_item->GetNet() == 0 )
            {
                net_item->SetNet( m_lastNetCode );
                m_lastNetCode++;
            }

            segmentToPointConnect( net_item, IS_WIRE, istart );
            break;

        case NET_SHEETBUSLABELMEMBER:
            if( net_item->m_BusNetCode != 0 )
                break;

        case NET_BUS:
            // Control type connections point to point mode bus
            if( net_item->m_BusNetCode == 0 )
            {
                net_item->m_BusNetCode = m_lastBusNetCode;
                m_lastBusNetCode++;
            }

            pointToPointConnect( net_item, IS_BUS, istart );
            break;

        case NET_BUSLABELMEMBER:
        case NET_HIERBUSLABELMEMBER:
        case NET_GLOBBUSLABELMEMBER:
            // Control connections similar has on BUS
            if( net_item->GetNet() == 0 )
            {
                net_item->m_BusNetCode = m_lastBusNetCode;
                m_lastBusNetCode++;
            }

            segmentToPointConnect( net_item, IS_BUS, istart );
            break;
        }
    }

#if defined(NETLIST_DEBUG) && defined(DEBUG)
    std::cout << "\n\nafter sheet local\n\n";
    DumpNetTable();
#endif

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

    // Group objects by label.
    for( unsigned ii = 0; ii < size(); ii++ )
    {
        switch( GetItem( ii )->m_Type )
        {
        case NET_PIN:
        case NET_SHEETLABEL:
        case NET_SEGMENT:
        case NET_JUNCTION:
        case NET_BUS:
        case NET_NOCONNECT:
            break;

        case NET_LABEL:
        case NET_GLOBLABEL:
        case NET_PINLABEL:
        case NET_BUSLABELMEMBER:
        case NET_GLOBBUSLABELMEMBER:
            labelConnect( GetItem( ii ) );
            break;

        case NET_SHEETBUSLABELMEMBER:
        case NET_HIERLABEL:
        case NET_HIERBUSLABELMEMBER:
            break;

        case NET_ITEM_UNSPECIFIED:
            break;
        }
    }

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

    // Connection between hierarchy sheets
    for( unsigned ii = 0; ii < size(); ii++ )
    {
        if( GetItem( ii )->m_Type == NET_SHEETLABEL
            || GetItem( ii )->m_Type == NET_SHEETBUSLABELMEMBER )
            sheetLabelConnect( GetItem( ii ) );
    }

    // Sort objects by NetCode
    SortListbyNetcode();

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

    // Compress numbers of Netcode having consecutive values.
    int NetCode = 0;
    m_lastNetCode = 0;

    for( unsigned ii = 0; ii < size(); ii++ )
    {
        if( GetItem( ii )->GetNet() != m_lastNetCode )
        {
            NetCode++;
            m_lastNetCode = GetItem( ii )->GetNet();
        }

        GetItem( ii )->SetNet( NetCode );
    }

    // Set the minimal connection info:
    setUnconnectedFlag();

    // find the best label object to give the best net name to each net
    findBestNetNameForEachNet();

    return true;
}

// Helper function to give a priority to sort labels:
// NET_PINLABEL, NET_GLOBBUSLABELMEMBER and NET_GLOBLABEL are global labels
// and the priority is high
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_GLOBBUSLABELMEMBER: return 5;
        case NET_GLOBLABEL: return 6;
        default: break;
    }

    return 0;
}


/* function evalLabelsPriority used by findBestNetNameForEachNet()
 * evalLabelsPriority calculates the priority of alabel1 and aLabel2
 * return true if alabel1 has a higher priority than aLabel2
 */
static bool evalLabelsPriority( const NETLIST_OBJECT* aLabel1,
                                 const NETLIST_OBJECT* aLabel2 )
{
    // Global labels have the highest prioriy.
    // For local labels: names are prefixed by their sheetpath
    // use name defined in the more top level hierarchical sheet
    // (i.e. shorter timestamp path because paths are /<timestamp1>/<timestamp2>/...
    // and timestamp = 8 letters.
    // Note: the final net name uses human sheetpath name, not timestamp sheetpath name
    // They are equivalent, but not for human readers.
    if( ! aLabel1->IsLabelGlobal() && ! aLabel2->IsLabelGlobal() )
    {
        if( aLabel1->m_SheetPath.Path().Length() != aLabel2->m_SheetPath.Path().Length() )
            return aLabel1->m_SheetPath.Path().Length() < aLabel2->m_SheetPath.Path().Length();
    }

    int priority1 = getPriority( aLabel1 );
    int priority2 = getPriority( aLabel2 );

    if( priority1 != priority2 )
        return priority1 > priority2;

    // 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->IsLabelGlobal() )
        return aLabel1->m_Label.Cmp( aLabel2->m_Label ) < 0;

    // Sheet paths have here 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 )
        return aLabel1->m_Label.Cmp( aLabel2->m_Label ) < 0;

    // For identical labels having the same priority: choose the
    // alphabetic label full name order
    return aLabel1->m_SheetPath.PathHumanReadable().Cmp(
                aLabel2->m_SheetPath.PathHumanReadable() ) < 0;
}


void NETLIST_OBJECT_LIST::findBestNetNameForEachNet()
{
    // Important note: NET_SHEETLABEL items of sheet items should *NOT* be considered,
    // because they live in a sheet but their names are actually used in the subsheet.
    // Moreover, in the parent sheet, the name of NET_SHEETLABEL can be not unique,
    // ( for instance when 2 different sheets share the same schematic in complex hierarchies
    // and 2 identical NET_SHEETLABEL labels can be connected to 2 different nets

    int netcode = 0;            // current netcode for tested items
    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:
    candidate = NULL;
    for( unsigned ii = 0; ii <= size(); ii++ )
    {
        if( ii == size() ) // last item already tested
            item = NULL;
        else
            item = GetItem( ii );

        if( !item || netcode != item->GetNet() )     // End of net found
        {
            if( candidate )         // One or more labels exists, find the best
            {
                for (unsigned jj = idxstart; jj < ii; jj++ )
                    GetItem( jj )->SetNetNameCandidate( candidate );
            }

            if( item == NULL )  // End of list
                break;

            // Prepare next net analysis:
            netcode = item->GetNet();
            candidate = NULL;
            idxstart = ii;
        }

        switch( item->m_Type )
        {
        case NET_HIERLABEL:
        case NET_LABEL:
        case NET_PINLABEL:
        case NET_GLOBLABEL:
        case NET_GLOBBUSLABELMEMBER:
            // A candidate is found: select the better between the previous
            // and this one
            if( candidate == NULL )
                candidate = item;
            else
            {
                if( evalLabelsPriority( item, candidate ) )
                    // item has a highter priority than candidate
                    // so update the best candidate
                    candidate = item;
            }
            break;

        default:
            break;
        }
    }

    // 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)

    // Build a list of items with no net names
    NETLIST_OBJECTS    list;   // no ownership of elements being pointed at

    for( unsigned ii = 0; ii < size(); ii++ )
    {
        item = GetItem( ii );
        if( !item->HasNetNameCandidate() )
            list.push_back( item );
    }

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

    idxstart = 0;
    candidate = NULL;
    netcode = list[0]->GetNet();

    for( unsigned ii = 0; ii <= list.size(); ii++ )
    {
        if( ii < list.size() )
            item = list[ii];
        else
            item = NULL;

        if( !item || netcode != item->GetNet() )     // End of net found
        {
            if( candidate )
            {
                for (unsigned jj = idxstart; jj < ii; jj++ )
                {
                    NETLIST_OBJECT* obj = list[jj];
                    obj->SetNetNameCandidate( candidate );
                }
            }

            if( !item )
                break;

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

        // 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 built 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)
        if( item->m_Type == NET_PIN )
        {
            // A candidate is found, however components which are not in
            // netlist are not candidate because some have their reference
            // 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() )
            {
                // select the better between the previous and this one
                item->SetNetNameCandidate( item );  // Needed to calculate GetShortNetName

                if( candidate == NULL )
                    candidate = item;
                else
                {
                    if( item->GetShortNetName().Cmp( candidate->GetShortNetName() ) < 0 )
                        candidate = item;
                }
            }
        }
    }
}


void NETLIST_OBJECT_LIST::sheetLabelConnect( NETLIST_OBJECT* SheetLabel )
{
    if( SheetLabel->GetNet() == 0 )
        return;

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

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

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

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

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

        // Propagate Netcode having all the objects of the same Netcode.
        if( ObjetNet->GetNet() )
            propageNetCode( ObjetNet->GetNet(), SheetLabel->GetNet(), IS_WIRE );
        else
            ObjetNet->SetNet( SheetLabel->GetNet() );
    }
}


void NETLIST_OBJECT_LIST::connectBusLabels()
{
    // Propagate the net code between all bus label member objects connected by they name.
    // If the net code is not yet existing, a new one is created
    // Search is done in the entire list
    for( unsigned ii = 0; ii < size(); ii++ )
    {
        NETLIST_OBJECT* Label = GetItem( ii );

        if( Label->IsLabelBusMemberType() )
        {
            if( Label->GetNet() == 0 )
            {
                // Not yet existiing net code: create a new one.
                Label->SetNet( m_lastNetCode );
                m_lastNetCode++;
            }

            for( unsigned jj = ii + 1; jj < size(); jj++ )
            {
                NETLIST_OBJECT* LabelInTst =  GetItem( jj );

                if( LabelInTst->IsLabelBusMemberType() )
                {
                    if( LabelInTst->m_BusNetCode != Label->m_BusNetCode )
                        continue;

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

                    if( LabelInTst->GetNet() == 0 )
                        // Append this object to the current net
                        LabelInTst->SetNet( Label->GetNet() );
                    else
                        // Merge the 2 net codes, they are connected.
                        propageNetCode( LabelInTst->GetNet(), Label->GetNet(), IS_WIRE );
                }
            }
        }
    }
}


void NETLIST_OBJECT_LIST::propageNetCode( int aOldNetCode, int aNewNetCode, bool aIsBus )
{
    if( aOldNetCode == aNewNetCode )
        return;

    if( aIsBus == false )    // Propagate NetCode
    {
        for( unsigned jj = 0; jj < size(); jj++ )
        {
            NETLIST_OBJECT* object = GetItem( jj );

            if( object->GetNet() == aOldNetCode )
                object->SetNet( aNewNetCode );
        }
    }
    else               // Propagate BusNetCode
    {
        for( unsigned jj = 0; jj < size(); jj++ )
        {
            NETLIST_OBJECT* object = GetItem( jj );

            if( object->m_BusNetCode == aOldNetCode )
                object->m_BusNetCode = aNewNetCode;
        }
    }
}


void NETLIST_OBJECT_LIST::pointToPointConnect( NETLIST_OBJECT* aRef, bool aIsBus,
                                               int start )
{
    int netCode;

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

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

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

            switch( item->m_Type )
            {
            case NET_SEGMENT:
            case NET_PIN:
            case NET_LABEL:
            case NET_HIERLABEL:
            case NET_GLOBLABEL:
            case NET_SHEETLABEL:
            case NET_PINLABEL:
            case NET_JUNCTION:
            case NET_NOCONNECT:
                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 )
                {
                    if( item->GetNet() == 0 )
                        item->SetNet( netCode );
                    else
                        propageNetCode( item->GetNet(), netCode, IS_WIRE );
                }
                break;

            case NET_BUS:
            case NET_BUSLABELMEMBER:
            case NET_SHEETBUSLABELMEMBER:
            case NET_HIERBUSLABELMEMBER:
            case NET_GLOBBUSLABELMEMBER:
            case NET_ITEM_UNSPECIFIED:
                break;
            }
        }
    }
    else    // Object type BUS, BUSLABELS, and junctions.
    {
        netCode = aRef->m_BusNetCode;

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

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

            switch( item->m_Type )
            {
            case NET_ITEM_UNSPECIFIED:
            case NET_SEGMENT:
            case NET_PIN:
            case NET_LABEL:
            case NET_HIERLABEL:
            case NET_GLOBLABEL:
            case NET_SHEETLABEL:
            case NET_PINLABEL:
            case NET_NOCONNECT:
                break;

            case NET_BUS:
            case NET_BUSLABELMEMBER:
            case NET_SHEETBUSLABELMEMBER:
            case NET_HIERBUSLABELMEMBER:
            case NET_GLOBBUSLABELMEMBER:
            case NET_JUNCTION:
                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 )
                {
                    if( item->m_BusNetCode == 0 )
                        item->m_BusNetCode = netCode;
                    else
                        propageNetCode( item->m_BusNetCode, netCode, IS_BUS );
                }
                break;
            }
        }
    }
}


void NETLIST_OBJECT_LIST::segmentToPointConnect( NETLIST_OBJECT* aJonction,
                                                bool aIsBus, int aIdxStart )
{
    for( unsigned i = aIdxStart; i < size(); i++ )
    {
        NETLIST_OBJECT* segment = GetItem( i );

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

        if( aIsBus == IS_WIRE )
        {
            if( segment->m_Type != NET_SEGMENT )
                continue;
        }
        else
        {
            if( segment->m_Type != NET_BUS )
                continue;
        }

        if( IsPointOnSegment( segment->m_Start, segment->m_End, aJonction->m_Start ) )
        {
            // Propagation Netcode has all the objects of the same Netcode.
            if( aIsBus == IS_WIRE )
            {
                if( segment->GetNet() )
                    propageNetCode( segment->GetNet(), aJonction->GetNet(), aIsBus );
                else
                    segment->SetNet( aJonction->GetNet() );
            }
            else
            {
                if( segment->m_BusNetCode )
                    propageNetCode( segment->m_BusNetCode, aJonction->m_BusNetCode, aIsBus );
                else
                    segment->m_BusNetCode = aJonction->m_BusNetCode;
            }
        }
    }
}


void NETLIST_OBJECT_LIST::labelConnect( NETLIST_OBJECT* aLabelRef )
{
    if( aLabelRef->GetNet() == 0 )
        return;

    for( unsigned i = 0; i < size(); i++ )
    {
        NETLIST_OBJECT* item = GetItem( i );

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

        if( item->m_SheetPath != aLabelRef->m_SheetPath )
        {
            if( item->m_Type != NET_PINLABEL && item->m_Type != NET_GLOBLABEL
                && item->m_Type != NET_GLOBBUSLABELMEMBER )
                continue;

            if( (item->m_Type == NET_GLOBLABEL
                 || item->m_Type == NET_GLOBBUSLABELMEMBER)
               && item->m_Type != aLabelRef->m_Type )
                //global labels only connect other global labels.
                continue;
        }

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

            if( item->GetNet() )
                propageNetCode( item->GetNet(), aLabelRef->GetNet(), IS_WIRE );
            else
                item->SetNet( aLabelRef->GetNet() );
        }
    }
}


void NETLIST_OBJECT_LIST::setUnconnectedFlag()
{
    NETLIST_OBJECT* NetItemRef;
    unsigned NetStart, NetEnd;
    NET_CONNECTION_T StateFlag;

    NetStart  = NetEnd = 0;
    StateFlag = UNCONNECTED;
    for( unsigned ii = 0; ii < size(); ii++ )
    {
        NetItemRef = GetItem( ii );
        if( NetItemRef->m_Type == NET_NOCONNECT && StateFlag != PAD_CONNECT )
            StateFlag = NOCONNECT_SYMBOL_PRESENT;

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

        if( ( idxtoTest >= size() )
           || ( NetItemRef->GetNet() != GetItem( idxtoTest )->GetNet() ) )
        {
            // Net analysis to update m_ConnectionType
            NetEnd = idxtoTest;

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

            if( idxtoTest >= size() )
                return;

            // Start Analysis next Net
            StateFlag = UNCONNECTED;
            NetStart  = idxtoTest;
            continue;
        }

        /* 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)
         */
       for( ; ; idxtoTest++ )
        {
            if( ( idxtoTest >= size() )
               || ( NetItemRef->GetNet() != GetItem( idxtoTest )->GetNet() ) )
                break;

            switch( GetItem( idxtoTest )->m_Type )
            {
            case NET_ITEM_UNSPECIFIED:
                wxMessageBox( wxT( "BuildNetListBase() error" ) );
                break;

            case NET_SEGMENT:
            case NET_LABEL:
            case NET_HIERLABEL:
            case NET_GLOBLABEL:
            case NET_SHEETLABEL:
            case NET_PINLABEL:
            case NET_BUS:
            case NET_BUSLABELMEMBER:
            case NET_SHEETBUSLABELMEMBER:
            case NET_HIERBUSLABELMEMBER:
            case NET_GLOBBUSLABELMEMBER:
            case NET_JUNCTION:
                break;

            case NET_PIN:
                if( NetItemRef->m_Type == NET_PIN )
                    StateFlag = PAD_CONNECT;

                break;

            case NET_NOCONNECT:
                if( StateFlag != PAD_CONNECT )
                    StateFlag = NOCONNECT_SYMBOL_PRESENT;

                break;
            }
        }
    }
}