summaryrefslogtreecommitdiff
path: root/pcbnew/dialogs/dialog_design_rules.cpp
blob: 80fe0f8cdb954de9841818052058fa83d8dfb050 (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
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
/**
 * @file dialog_design_rules.cpp
 */

/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2004-2009 Jean-Pierre Charras, jp.charras at wanadoo.fr
 * Copyright (C) 2009 Dick Hollenbeck, dick@softplc.com
 * Copyright (C) 2009-2015 KiCad Developers, see change_log.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
 */


/* functions relative to the design rules editor
 */
#include <fctsys.h>
#include <class_drawpanel.h>
#include <base_units.h>
#include <confirm.h>
#include <pcbnew.h>
#include <wxPcbStruct.h>
#include <class_board_design_settings.h>

#include <pcbnew_id.h>
#include <class_track.h>
#include <macros.h>
#include <html_messagebox.h>

#include <dialog_design_rules.h>
#include <wx/generic/gridctrl.h>
#include <dialog_design_rules_aux_helper_class.h>

#include <boost/make_shared.hpp>

// Column labels for net lists
#define NET_TITLE       _( "Net" )
#define CLASS_TITLE     _( "Class" )

// Field Positions on rules grid
enum {
    GRID_CLEARANCE,
    GRID_TRACKSIZE,
    GRID_VIASIZE,
    GRID_VIADRILL,
    GRID_uVIASIZE,
    GRID_uVIADRILL
};

const wxString DIALOG_DESIGN_RULES::wildCard = _( "* (Any)" );

// dialog should remember its previously selected tab
int DIALOG_DESIGN_RULES::s_LastTabSelection = -1;

// methods for the helper class NETS_LIST_CTRL

wxString NETS_LIST_CTRL::OnGetItemText( long item, long column ) const
{
    if( column == 0 )
    {
        if( item < (long) m_Netnames.GetCount() )
            return m_Netnames[item];
        else
            return wxEmptyString;
    }
    else if( item < (long) m_Classnames.GetCount() )
        return m_Classnames[item];

    return wxEmptyString;
}


void NETS_LIST_CTRL::SetRowItems( unsigned        aRow,
                                  const wxString& aNetname,
                                  const wxString& aNetclassName )
{
    // insert blanks if aRow is larger than existing row count
    unsigned cnt = m_Netnames.GetCount();

    if( cnt <= aRow )
        m_Netnames.Add( wxEmptyString, aRow - cnt + 1 );

    cnt = m_Classnames.GetCount();

    if( cnt <= aRow )
        m_Classnames.Add( wxEmptyString, aRow - cnt + 1 );

    if( (int)aRow <= GetItemCount() )
        SetItemCount( aRow + 1 );

    m_Netnames[aRow]   = aNetname;
    m_Classnames[aRow] = aNetclassName;
}


/**
 * Function EnsureGridColumnWidths
 * resizes all the columns in a wxGrid based only on the requirements of the
 * column titles and not on the grid cell requirements, assuming that the grid
 * cell width requirements are narrower than the column title requirements.
 */

// @todo: maybe move this to common.cpp if it works.
void EnsureGridColumnWidths( wxGrid* aGrid )
{
    wxScreenDC sDC;

    sDC.SetFont( aGrid->GetLabelFont() );

    int colCount = aGrid->GetNumberCols();

    for( int col = 0; col<colCount;  ++col )
    {
        // add two spaces to the text and size it.
        wxString colText = aGrid->GetColLabelValue( col ) + wxT( "  " );

        wxSize   needed = sDC.GetTextExtent( colText );

        // set the width of this column
        aGrid->SetColSize( col, needed.x );
    }
}


DIALOG_DESIGN_RULES::DIALOG_DESIGN_RULES( PCB_EDIT_FRAME* parent ) :
    DIALOG_DESIGN_RULES_BASE( parent )
{
    m_Parent = parent;
    SetAutoLayout( true );

    EnsureGridColumnWidths( m_grid );   // override any column widths set by wxformbuilder.

    wxListItem column0;
    wxListItem column1;

    column0.Clear();
    column1.Clear();

    column0.SetMask( wxLIST_MASK_TEXT );
    column1.SetMask( wxLIST_MASK_TEXT );

    column0.SetText( NET_TITLE );
    column1.SetText( CLASS_TITLE );

    m_leftListCtrl->InsertColumn( 0, column0 );
    m_leftListCtrl->InsertColumn( 1, column1 );
    m_leftListCtrl->SetColumnWidth( 0, wxLIST_AUTOSIZE );
    m_leftListCtrl->SetColumnWidth( 1, wxLIST_AUTOSIZE );

    m_rightListCtrl->InsertColumn( 0, column0 );
    m_rightListCtrl->InsertColumn( 1, column1 );
    m_rightListCtrl->SetColumnWidth( 0, wxLIST_AUTOSIZE );
    m_rightListCtrl->SetColumnWidth( 1, wxLIST_AUTOSIZE );

    // if user has been into the dialog before, go back to same tab
    if( s_LastTabSelection != -1 )
    {
        m_DRnotebook->SetSelection( s_LastTabSelection );
    }

    InitDialogRules();
    m_sdbSizer1OK->SetDefault();

    // Allow tabbing out of grid controls.
    m_grid->SetTabBehaviour( wxGrid::Tab_Leave );
    m_gridViaSizeList->SetTabBehaviour( wxGrid::Tab_Leave );
    m_gridTrackWidthList->SetTabBehaviour( wxGrid::Tab_Leave );

    Layout();

    FixOSXCancelButtonIssue();

    // Now all widgets have the size fixed, call FinishDialogSettings
    FinishDialogSettings();
}


void DIALOG_DESIGN_RULES::InitDialogRules()
{
    // @todo:  Move the initialization code into TransferDataToWindow() to follow wxWidgets
    //         dialog data transfer convention.
    SetFocus();
    SetReturnCode( 0 );

    m_Pcb = m_Parent->GetBoard();
    m_BrdSettings = &m_Pcb->GetDesignSettings();

    // Initialize the Rules List
    InitRulesList();

    // copy all NETs into m_AllNets by adding them as NETCUPs.

    // @todo go fix m_Pcb->SynchronizeNetsAndNetClasses() so that the netcode==0 is not
    //       present in the BOARD::m_NetClasses
    NETCLASSES& netclasses = m_BrdSettings->m_NetClasses;
    NETCLASSPTR netclass = netclasses.GetDefault();

    // Initialize list of nets for Default Net Class
    for( NETCLASS::iterator name = netclass->begin();  name != netclass->end();  ++name )
    {
        m_AllNets.push_back( NETCUP( *name, netclass->GetName() ) );
    }

    // Initialize list of nets for others (custom) Net Classes
    for( NETCLASSES::const_iterator nc = netclasses.begin();  nc != netclasses.end();  ++nc )
    {
        netclass = nc->second;

        for( NETCLASS::const_iterator name = netclass->begin();  name != netclass->end();  ++name )
        {
            m_AllNets.push_back( NETCUP( *name, netclass->GetName() ) );
        }
    }

    InitializeRulesSelectionBoxes();
    InitGlobalRules();
}


void DIALOG_DESIGN_RULES::InitGlobalRules()
{
    AddUnitSymbol( *m_ViaMinTitle );
    AddUnitSymbol( *m_ViaMinDrillTitle );
    AddUnitSymbol( *m_MicroViaMinSizeTitle );
    AddUnitSymbol( *m_MicroViaMinDrillTitle );
    AddUnitSymbol( *m_TrackMinWidthTitle );

    PutValueInLocalUnits( *m_SetViasMinSizeCtrl, m_BrdSettings->m_ViasMinSize );
    PutValueInLocalUnits( *m_SetViasMinDrillCtrl, m_BrdSettings->m_ViasMinDrill );

    if( m_BrdSettings->m_BlindBuriedViaAllowed )
        m_OptViaType->SetSelection( 1 );

    m_AllowMicroViaCtrl->SetSelection( m_BrdSettings->m_MicroViasAllowed ? 1 : 0 );
    PutValueInLocalUnits( *m_SetMicroViasMinSizeCtrl, m_BrdSettings->m_MicroViasMinSize );
    PutValueInLocalUnits( *m_SetMicroViasMinDrillCtrl, m_BrdSettings->m_MicroViasMinDrill );
    PutValueInLocalUnits( *m_SetTrackMinWidthCtrl, m_BrdSettings->m_TrackMinWidth );

    // Initialize Vias and Tracks sizes lists.
    // note we display only extra values, never the current netclass value.
    // (the first value in history list)
    m_TracksWidthList = m_BrdSettings->m_TrackWidthList;
    m_TracksWidthList.erase( m_TracksWidthList.begin() );       // remove the netclass value
    m_ViasDimensionsList = m_BrdSettings->m_ViasDimensionsList;
    m_ViasDimensionsList.erase( m_ViasDimensionsList.begin() ); // remove the netclass value
    InitDimensionsLists();
}


void DIALOG_DESIGN_RULES::InitDimensionsLists()
{
    wxString msg;

    // Compute the column widths here, after setting texts
    msg = wxT("000000.000000"); // This is a very long text to display values.
                                // Actual values are shorter.
    m_gridViaSizeList->SetCellValue( 0, 0, msg );
    m_gridViaSizeList->SetCellValue( 0, 1, msg );
    m_gridTrackWidthList->SetCellValue( 0, 0, msg );
    m_gridViaSizeList->SetColMinimalWidth( 0, 150 );
    m_gridViaSizeList->SetColMinimalWidth( 1, 150 );
    m_gridViaSizeList->AutoSizeColumns( true );
    m_gridTrackWidthList->SetColMinimalWidth( 0, 150 );
    m_gridTrackWidthList->AutoSizeColumns( true );
    m_gridViaSizeList->SetColMinimalWidth( 1, 150 );

    // Fill cells with actual values:
    m_gridViaSizeList->SetCellValue( 0, 0, wxEmptyString );
    m_gridViaSizeList->SetCellValue( 0, 1, wxEmptyString );
    m_gridTrackWidthList->SetCellValue( 0, 0, wxEmptyString );

    // Give a correct size to row labels column
    m_gridViaSizeList->SetRowLabelSize( wxGRID_AUTOSIZE );
    m_gridTrackWidthList->SetRowLabelSize( wxGRID_AUTOSIZE );

    for( unsigned ii = 0; ii < m_TracksWidthList.size(); ii++ )
    {
        msg = StringFromValue( g_UserUnit, m_TracksWidthList[ii], false );
        m_gridTrackWidthList->SetCellValue( ii, 0, msg  );
    }

    for( unsigned ii = 0; ii < m_ViasDimensionsList.size(); ii++ )
    {
        msg = StringFromValue( g_UserUnit, m_ViasDimensionsList[ii].m_Diameter, false );
        m_gridViaSizeList->SetCellValue( ii, 0, msg );

        if( m_ViasDimensionsList[ii].m_Drill > 0 )
        {
            msg = StringFromValue( g_UserUnit, m_ViasDimensionsList[ii].m_Drill, false );
            m_gridViaSizeList->SetCellValue( ii, 1, msg );
        }
    }
}


// Sort comparison function (helper for makePointers() )
static bool sortByClassThenName( NETCUP* a, NETCUP* b )
{
    // return a < b
    if( a->clazz < b->clazz )
        return true;

    // inside the same class, sort by net name:
    if( a->clazz == b->clazz )
    {
        if( a->net < b->net )
            return true;
    }

    return false;
}


void DIALOG_DESIGN_RULES::makePointers( PNETCUPS* aList, const wxString& aNetClassName )
{
    aList->clear();

    if( wildCard == aNetClassName )
    {
        for( NETCUPS::iterator n = m_AllNets.begin();  n != m_AllNets.end();  ++n )
        {
            aList->push_back( &*n );
        }

        sort( aList->begin(), aList->end(), sortByClassThenName );

        // could use a different sort order for wildCard case.
    }
    else
    {
        for( NETCUPS::iterator n = m_AllNets.begin();  n != m_AllNets.end();  ++n )
        {
            if( n->clazz == aNetClassName )
                aList->push_back( &*n );
        }

        sort( aList->begin(), aList->end(), sortByClassThenName );
    }
}


void DIALOG_DESIGN_RULES::FillListBoxWithNetNames( NETS_LIST_CTRL* aListCtrl,
                                                   const wxString& aNetClass )
{
    aListCtrl->ClearList();

    PNETCUPS ptrList;

    // get a subset of m_AllNets in pointer form, sorted as desired.
    makePointers( &ptrList, aNetClass );

#if 0 && defined(DEBUG)
    int r = 0;
    for( PNETCUPS::iterator i = ptrList.begin();  i!=ptrList.end();  ++i, ++r )
    {
        printf( "[%d]: %s  %s\n", r, TO_UTF8( (*i)->net ), TO_UTF8( (*i)->clazz ) );
    }

#endif

    // Add netclass info to m_Netnames and m_Classnames wxArrayString buffers
    // aListCtrl uses wxLC_VIRTUAL option, so this is fast
    wxClientDC sDC( aListCtrl );
    int row = 0;
    // recompute the column widths here, after setting texts
    int net_colsize = sDC.GetTextExtent( NET_TITLE ).x;
    int class_colsize = sDC.GetTextExtent( CLASS_TITLE ).x;

    for( PNETCUPS::iterator i = ptrList.begin();  i!=ptrList.end();  ++i, ++row )
    {
        wxSize   net_needed = sDC.GetTextExtent( (*i)->net );
        wxSize   class_needed = sDC.GetTextExtent( (*i)->clazz );
        net_colsize = std::max( net_colsize, net_needed.x );
        class_colsize = std::max( class_colsize, class_needed.x );
        aListCtrl->SetRowItems( row, (*i)->net, (*i)->clazz );
    }

    int margin = sDC.GetTextExtent( wxT( "XX" ) ).x;
    aListCtrl->SetColumnWidth( 0, net_colsize + margin );
    aListCtrl->SetColumnWidth( 1, class_colsize + margin );
    aListCtrl->Refresh();
}


/* Populates combo boxes with the list of existing net classes
 */
void DIALOG_DESIGN_RULES::InitializeRulesSelectionBoxes()
{
    m_rightClassChoice->Clear();
    m_leftClassChoice->Clear();

    m_rightClassChoice->Append( wildCard );
    m_leftClassChoice->Append( wildCard );

    for( int ii = 0; ii < m_grid->GetNumberRows(); ii++ )
    {
        m_rightClassChoice->Append( m_grid->GetRowLabelValue( ii ) );
        m_leftClassChoice->Append( m_grid->GetRowLabelValue( ii ) );
    }

    m_rightClassChoice->Select( 0 );
    m_leftClassChoice->Select( 0 );

    m_buttonRightToLeft->Enable( false );
    m_buttonLeftToRight->Enable( false );

    FillListBoxWithNetNames( m_leftListCtrl, m_leftClassChoice->GetStringSelection() );
    FillListBoxWithNetNames( m_rightListCtrl, m_rightClassChoice->GetStringSelection() );
}


/* Initialize the rules list from board
 */

static void class2gridRow( wxGrid* grid, int row, NETCLASSPTR nc )
{
    wxString msg;

    // label is netclass name
    grid->SetRowLabelValue( row, nc->GetName() );

    msg = StringFromValue( g_UserUnit, nc->GetClearance() );
    grid->SetCellValue( row, GRID_CLEARANCE, msg );

    msg = StringFromValue( g_UserUnit, nc->GetTrackWidth() );
    grid->SetCellValue( row, GRID_TRACKSIZE, msg );

    msg = StringFromValue( g_UserUnit, nc->GetViaDiameter() );
    grid->SetCellValue( row, GRID_VIASIZE, msg );

    msg = StringFromValue( g_UserUnit, nc->GetViaDrill() );
    grid->SetCellValue( row, GRID_VIADRILL, msg );

    msg = StringFromValue( g_UserUnit, nc->GetuViaDiameter() );
    grid->SetCellValue( row, GRID_uVIASIZE, msg );

    msg = StringFromValue( g_UserUnit, nc->GetuViaDrill() );
    grid->SetCellValue( row, GRID_uVIADRILL, msg );
}


void DIALOG_DESIGN_RULES::InitRulesList()
{
    NETCLASSES& netclasses = m_BrdSettings->m_NetClasses;

    // the +1 is for the Default NETCLASS.
    if( netclasses.GetCount() + 1 > (unsigned) m_grid->GetNumberRows() )
    {
        m_grid->AppendRows( netclasses.GetCount() + 1 - m_grid->GetNumberRows() );
    }

    // enter the Default NETCLASS.
    class2gridRow( m_grid, 0, netclasses.GetDefault() );

    // enter others netclasses
    int row = 1;

    for( NETCLASSES::iterator i = netclasses.begin();  i!=netclasses.end();  ++i, ++row )
    {
        NETCLASSPTR netclass = i->second;

        class2gridRow( m_grid, row, netclass );
    }
}


static void gridRow2class( wxGrid* grid, int row, NETCLASSPTR nc )
{
#define MYCELL( col )   \
    ValueFromString( g_UserUnit, grid->GetCellValue( row, col ) )

    nc->SetClearance( MYCELL( GRID_CLEARANCE ) );
    nc->SetTrackWidth( MYCELL( GRID_TRACKSIZE ) );
    nc->SetViaDiameter( MYCELL( GRID_VIASIZE ) );
    nc->SetViaDrill( MYCELL( GRID_VIADRILL ) );
    nc->SetuViaDiameter( MYCELL( GRID_uVIASIZE ) );
    nc->SetuViaDrill( MYCELL( GRID_uVIADRILL ) );
}


void DIALOG_DESIGN_RULES::CopyRulesListToBoard()
{
    NETCLASSES& netclasses = m_BrdSettings->m_NetClasses;

    // Remove all netclasses from board. We'll copy new list after
    netclasses.Clear();

    // Copy the default NetClass:
    gridRow2class( m_grid, 0, netclasses.GetDefault() );

    // Copy other NetClasses :
    for( int row = 1; row < m_grid->GetNumberRows();  ++row )
    {
        NETCLASSPTR nc = boost::make_shared<NETCLASS>( m_grid->GetRowLabelValue( row ) );

        if( !m_BrdSettings->m_NetClasses.Add( nc ) )
        {
            // this netclass cannot be added because an other netclass with the same name exists
            // Should not occur because OnAddNetclassClick() tests for existing NetClass names
            wxString msg;
            msg.Printf( wxT( "CopyRulesListToBoard(): The NetClass \"%s\" already exists. Skip" ),
                        GetChars( m_grid->GetRowLabelValue( row ) ) );
            wxMessageBox( msg );

            continue;
        }

        gridRow2class( m_grid, row, nc );
    }

    // Now read all nets and push them in the corresponding netclass net buffer
    for( NETCUPS::const_iterator netcup = m_AllNets.begin(); netcup != m_AllNets.end(); ++netcup )
    {
        NETCLASSPTR nc = netclasses.Find( netcup->clazz );
        wxASSERT( nc );
        nc->Add( netcup->net );
    }

    m_Pcb->SynchronizeNetsAndNetClasses();
}


void DIALOG_DESIGN_RULES::CopyGlobalRulesToBoard()
{
    m_BrdSettings->m_BlindBuriedViaAllowed = m_OptViaType->GetSelection() > 0;

    // Update vias minimum values for DRC
    m_BrdSettings->m_ViasMinSize = ValueFromTextCtrl( *m_SetViasMinSizeCtrl );
    m_BrdSettings->m_ViasMinDrill = ValueFromTextCtrl( *m_SetViasMinDrillCtrl );

    m_BrdSettings->m_MicroViasAllowed = m_AllowMicroViaCtrl->GetSelection() == 1;

    // Update microvias minimum values for DRC
    m_BrdSettings->m_MicroViasMinSize = ValueFromTextCtrl( *m_SetMicroViasMinSizeCtrl );
    m_BrdSettings->m_MicroViasMinDrill = ValueFromTextCtrl( *m_SetMicroViasMinDrillCtrl );

    // Update tracks minimum values for DRC
    m_BrdSettings->m_TrackMinWidth = ValueFromTextCtrl( *m_SetTrackMinWidthCtrl );
}


void DIALOG_DESIGN_RULES::CopyDimensionsListsToBoard()
{
    wxString msg;

    // Reinitialize m_TrackWidthList
    m_TracksWidthList.clear();

    for( int row = 0; row < m_gridTrackWidthList->GetNumberRows();  ++row )
    {
        msg = m_gridTrackWidthList->GetCellValue( row, 0 );

        if( msg.IsEmpty() )
            continue;

        int value = ValueFromString( g_UserUnit, msg );
        m_TracksWidthList.push_back( value );
    }

    // Sort new list by by increasing value
    sort( m_TracksWidthList.begin(), m_TracksWidthList.end() );

    // Reinitialize m_TrackWidthList
    m_ViasDimensionsList.clear();

    for( int row = 0; row < m_gridViaSizeList->GetNumberRows();  ++row )
    {
        msg = m_gridViaSizeList->GetCellValue( row, 0 );

        if( msg.IsEmpty() )
            continue;

        int           value = ValueFromString( g_UserUnit, msg );
        VIA_DIMENSION via_dim;
        via_dim.m_Diameter = value;
        msg = m_gridViaSizeList->GetCellValue( row, 1 );

        if( !msg.IsEmpty() )
        {
            value = ValueFromString( g_UserUnit, msg );
            via_dim.m_Drill = value;
        }

        m_ViasDimensionsList.push_back( via_dim );
    }

    // Sort new list by by increasing value
    sort( m_ViasDimensionsList.begin(), m_ViasDimensionsList.end() );

    std::vector<int>* tlist = &m_BrdSettings->m_TrackWidthList;

    // Remove old "custom" sizes
    tlist->erase( tlist->begin() + 1, tlist->end() );

    // Add new "custom" sizes
    tlist->insert( tlist->end(), m_TracksWidthList.begin(), m_TracksWidthList.end() );

    // Reinitialize m_ViaSizeList
    std::vector<VIA_DIMENSION>* vialist = &m_BrdSettings->m_ViasDimensionsList;
    vialist->erase( vialist->begin() + 1, vialist->end() );
    vialist->insert( vialist->end(), m_ViasDimensionsList.begin(), m_ViasDimensionsList.end() );
}


void DIALOG_DESIGN_RULES::OnNotebookPageChanged( wxNotebookEvent& event )
{
    s_LastTabSelection = event.GetSelection();

    // Skip() allows OSX to properly refresh controls.
    event.Skip();
}


bool DIALOG_DESIGN_RULES::TransferDataFromWindow()
{
    if( !wxDialog::TransferDataFromWindow() )
        return false;

    wxString errorMsg;

    if( !TestDataValidity( &errorMsg ) )
    {
        HTML_MESSAGE_BOX dlg( this, _( "Design Rule Setting Error" ) );
        dlg.MessageSet( errorMsg );
        dlg.ShowModal();
        return false;
    }

    CopyRulesListToBoard();
    CopyGlobalRulesToBoard();
    CopyDimensionsListsToBoard();
    m_BrdSettings->SetCurrentNetClass( NETCLASS::Default );
    return true;
}


void DIALOG_DESIGN_RULES::OnAddNetclassClick( wxCommandEvent& event )
{
    wxString          class_name;

    // @todo set validator to ensure net class name is valid rather than all of the checks
    //       after the OK button has been selected.
    wxTextEntryDialog dlg( this, _( "New Net Class Name:" ), wxEmptyString, class_name );

    if( dlg.ShowModal() != wxID_OK )
        return;         // canceled by user

    class_name = dlg.GetValue();
    class_name.Trim( true );
    class_name.Trim( false );

    if( class_name.IsEmpty() )
        return;         // empty name not allowed

    // The name must dot exists:
    for( int ii = 0; ii < m_grid->GetNumberRows(); ii++ )
    {
        wxString value;
        value = m_grid->GetRowLabelValue( ii );

        if( class_name.CmpNoCase( value ) == 0 )       // Already exists!
        {
            DisplayError( this, _( "Duplicate net class names are not allowed." ) );
            return;
        }
    }

    m_grid->AppendRows();
    m_grid->SetRowLabelValue( m_grid->GetNumberRows() - 1, class_name );

    // Copy values of the default class:
    int irow = m_grid->GetNumberRows() - 1;

    for( int icol = 0; icol < m_grid->GetNumberCols(); icol++ )
    {
        wxString value;
        value = m_grid->GetCellValue( 0, icol );
        m_grid->SetCellValue( irow, icol, value );
    }

    InitializeRulesSelectionBoxes();
}


// Sort function for wxArrayInt. Items (ints) are sorted by decreasing value
// used in DIALOG_DESIGN_RULES::OnRemoveNetclassClick
int sort_int( int* first, int* second )
{
    return *second - *first;
}


void DIALOG_DESIGN_RULES::OnRemoveNetclassClick( wxCommandEvent& event )
{
    wxArrayInt select = m_grid->GetSelectedRows();

    // Sort selection by decreasing index order:
    select.Sort( sort_int );
    bool reinit = false;

    // rows labels are not removed when deleting rows: they are not deleted.
    // So we must store them, remove corresponding labels and reinit them
    wxArrayString labels;

    for( int ii = 0; ii < m_grid->GetNumberRows(); ii++ )
        labels.Add( m_grid->GetRowLabelValue( ii ) );

    // Delete rows from last to first (this is the order wxArrayInt select after sorting) )
    // This order is Ok when removing rows
    for( unsigned ii = 0; ii < select.GetCount(); ii++ )
    {
        int grid_row = select[ii];

        if( grid_row != 0 )   // Do not remove the default class
        {
            wxString classname = m_grid->GetRowLabelValue( grid_row );
            m_grid->DeleteRows( grid_row );
            labels.RemoveAt( grid_row );  // Remove corresponding row label
            reinit = true;

            // reset the net class to default for members of the removed class
            swapNetClass( classname, NETCLASS::Default );
        }
        else
            wxMessageBox( _( "The default net class cannot be removed" ) );
    }

    if( reinit )
    {
        // Reinit labels :
        for( unsigned ii = 1; ii < labels.GetCount(); ii++ )
            m_grid->SetRowLabelValue( ii, labels[ii] );

        InitializeRulesSelectionBoxes();
    }
}


void DIALOG_DESIGN_RULES::OnMoveUpSelectedNetClass( wxCommandEvent& event )
{
    // Cannot move up rules if we have 1 or 2 rules only
    if( m_grid->GetNumberRows() < 3 )
        return;

    wxArrayInt select = m_grid->GetSelectedRows();
    bool       reinit = false;

    for( unsigned irow = 0; irow < select.GetCount(); irow++ )
    {
        int ii = select[irow];
        if( ii < 2 )            // The default netclass *must* be the first netclass
            continue;           // so we cannot move up line 0 and 1

        // Swap the rule and the previous rule
        wxString curr_value, previous_value;

        for( int icol = 0; icol < m_grid->GetNumberCols(); icol++ )
        {
            reinit         = true;
            curr_value     = m_grid->GetCellValue( ii, icol );
            previous_value = m_grid->GetCellValue( ii - 1, icol );
            m_grid->SetCellValue( ii, icol, previous_value );
            m_grid->SetCellValue( ii - 1, icol, curr_value );
        }

        curr_value     = m_grid->GetRowLabelValue( ii );
        previous_value = m_grid->GetRowLabelValue( ii - 1 );
        m_grid->SetRowLabelValue( ii, previous_value );
        m_grid->SetRowLabelValue( ii - 1, curr_value );
    }

    if( reinit )
        InitializeRulesSelectionBoxes();
}


void DIALOG_DESIGN_RULES::OnLeftCBSelection( wxCommandEvent& event )
{
    FillListBoxWithNetNames( m_leftListCtrl, m_leftClassChoice->GetStringSelection() );

    if( m_leftClassChoice->GetStringSelection() ==  m_rightClassChoice->GetStringSelection() )
    {
        m_buttonRightToLeft->Enable( false );
        m_buttonLeftToRight->Enable( false );
    }
    else
    {
        m_buttonRightToLeft->Enable( true );
        m_buttonLeftToRight->Enable( true );
    }
}


void DIALOG_DESIGN_RULES::OnRightCBSelection( wxCommandEvent& event )
{
    FillListBoxWithNetNames( m_rightListCtrl, m_rightClassChoice->GetStringSelection() );

    if( m_leftClassChoice->GetStringSelection() ==  m_rightClassChoice->GetStringSelection() )
    {
        m_buttonRightToLeft->Enable( false );
        m_buttonLeftToRight->Enable( false );
    }
    else
    {
        m_buttonRightToLeft->Enable( true );
        m_buttonLeftToRight->Enable( true );
    }
}


void DIALOG_DESIGN_RULES::moveSelectedItems( NETS_LIST_CTRL* src, const wxString& newClassName )
{
    wxListItem item;
    wxString   netName;

    item.m_mask |= wxLIST_MASK_TEXT;       // Validate the member m_text of the wxListItem item

    for( int row = 0;  row < src->GetItemCount();  ++row )
    {
        if( !src->GetItemState( row, wxLIST_STATE_SELECTED ) )
            continue;

        item.SetColumn( 0 );
        item.SetId( row );

        src->GetItem( item );
        netName = item.GetText();

        setNetClass( netName, newClassName == wildCard ? NETCLASS::Default : newClassName );
    }
}


void DIALOG_DESIGN_RULES::OnRightToLeftCopyButton( wxCommandEvent& event )
{
    wxString newClassName = m_leftClassChoice->GetStringSelection();

    moveSelectedItems( m_rightListCtrl, newClassName );

    FillListBoxWithNetNames( m_leftListCtrl, m_leftClassChoice->GetStringSelection() );
    FillListBoxWithNetNames( m_rightListCtrl, m_rightClassChoice->GetStringSelection() );
}


void DIALOG_DESIGN_RULES::OnLeftToRightCopyButton( wxCommandEvent& event )
{
    wxString newClassName = m_rightClassChoice->GetStringSelection();

    moveSelectedItems( m_leftListCtrl, newClassName );

    FillListBoxWithNetNames( m_leftListCtrl, m_leftClassChoice->GetStringSelection() );
    FillListBoxWithNetNames( m_rightListCtrl, m_rightClassChoice->GetStringSelection() );
}


void DIALOG_DESIGN_RULES::OnLeftSelectAllButton( wxCommandEvent& event )
{
    for( int ii = 0; ii < m_leftListCtrl->GetItemCount(); ii++ )
        m_leftListCtrl->SetItemState( ii, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
}


void DIALOG_DESIGN_RULES::OnRightSelectAllButton( wxCommandEvent& event )
{
    for( int ii = 0; ii < m_rightListCtrl->GetItemCount(); ii++ )
        m_rightListCtrl->SetItemState( ii, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
}


void DIALOG_DESIGN_RULES::setNetClass( const wxString& aNetName, const wxString& aClassName )
{
    for( NETCUPS::iterator i = m_AllNets.begin();  i != m_AllNets.end();  ++i )
    {
        if( i->net == aNetName )
        {
            i->clazz = aClassName;
            break;
        }
    }
}


bool DIALOG_DESIGN_RULES::TestDataValidity( wxString* aErrorMsg )
{
    bool     result = true;

    wxString msg;
    wxString errorMsg;

    int      minViaDia = ValueFromTextCtrl( *m_SetViasMinSizeCtrl );
    int      minViaDrill = ValueFromTextCtrl( *m_SetViasMinDrillCtrl );
    int      minUViaDia = ValueFromTextCtrl( *m_SetMicroViasMinSizeCtrl );
    int      minUViaDrill = ValueFromTextCtrl( *m_SetMicroViasMinDrillCtrl );
    int      minTrackWidth = ValueFromTextCtrl( *m_SetTrackMinWidthCtrl );
    int      maxval = 1000 * IU_PER_MILS;   // a max value for tracks and vias sizes (1 inch)

    // @todo Change this code to set the focus to the control where the first error occurs
    //       so the user doesn't have to figure out where the issue is.

    // Test net class parameters.
    for( int row = 0; row < m_grid->GetNumberRows(); row++ )
    {
        int tracksize = ValueFromString( g_UserUnit,
                                         m_grid->GetCellValue( row, GRID_TRACKSIZE ) );
        if( tracksize < minTrackWidth )
        {
            result = false;
            msg.Printf( _( "%s: <b>Track Size</b> &lt; <b>Min Track Size</b><br>" ),
                        GetChars( m_grid->GetRowLabelValue( row ) ) );
            errorMsg += msg;
        }

        // Test vias
        int viadia = ValueFromString( g_UserUnit,
                                      m_grid->GetCellValue( row, GRID_VIASIZE ) );

        if( viadia < minViaDia )
        {
            result = false;
            msg.Printf( _( "%s: <b>Via Diameter</b> &lt; <b>Minimum Via Diameter</b><br>" ),
                        GetChars( m_grid->GetRowLabelValue( row ) ) );
            errorMsg += msg;
        }

        int viadrill = ValueFromString( g_UserUnit,
                                        m_grid->GetCellValue( row, GRID_VIADRILL ) );

        if( viadrill >= viadia )
        {
            result = false;
            msg.Printf( _( "%s: <b>Via Drill</b> &ge; <b>Via Dia</b><br>" ),
                        GetChars( m_grid->GetRowLabelValue( row ) ) );
            errorMsg += msg;
        }

        if( viadrill < minViaDrill )
        {
            result = false;
            msg.Printf( _( "%s: <b>Via Drill</b> &lt; <b>Min Via Drill</b><br>" ),
                        GetChars( m_grid->GetRowLabelValue( row ) ) );
            errorMsg += msg;
        }

        // Test Micro vias
        int muviadia = ValueFromString( g_UserUnit,
                                        m_grid->GetCellValue( row, GRID_uVIASIZE ) );

        if( muviadia < minUViaDia )
        {
            result = false;
            msg.Printf( _( "%s: <b>MicroVia Diameter</b> &lt; <b>MicroVia Min Diameter</b><br>" ),
                        GetChars( m_grid->GetRowLabelValue( row ) ) );
            errorMsg += msg;
        }

        int muviadrill = ValueFromString( g_UserUnit,
                                          m_grid->GetCellValue( row, GRID_uVIADRILL ) );

        if( muviadrill >= muviadia )
        {
            result = false;
            msg.Printf( _( "%s: <b>MicroVia Drill</b> &ge; <b>MicroVia Dia</b><br>" ),
                        GetChars( m_grid->GetRowLabelValue( row ) ) );
            errorMsg += msg;
        }

        if( muviadrill < minUViaDrill )
        {
            result = false;
            msg.Printf( _( "%s: <b>MicroVia Drill</b> &lt; <b>MicroVia Min Drill</b><br>" ),
                        GetChars( m_grid->GetRowLabelValue( row ) ) );
            errorMsg += msg;
        }
    }

    // Test custom tracks
    for( int row = 0; row < m_gridTrackWidthList->GetNumberRows();  ++row )
    {
        wxString tvalue = m_gridTrackWidthList->GetCellValue( row, 0 );

        if( tvalue.IsEmpty() )
            continue;

        int tracksize = ValueFromString( g_UserUnit, tvalue );

        if( tracksize < minTrackWidth )
        {
            result = false;
            msg.Printf( _( "<b>Extra Track %d Size</b> %s &lt; <b>Min Track Size</b><br>" ),
                        row + 1, GetChars( tvalue ) );

            errorMsg += msg;
        }

        if( tracksize > maxval )
        {
            result = false;
            msg.Printf( _( "<b>Extra Track %d Size</b> %s &gt; <b>1 inch!</b><br>" ),
                        row + 1, GetChars( tvalue ) );

            errorMsg += msg;
        }
    }

    // Test custom vias
    for( int row = 0; row < m_gridViaSizeList->GetNumberRows();  ++row )
    {
        wxString tvalue = m_gridViaSizeList->GetCellValue( row, 0 );

        if( tvalue.IsEmpty() )
            continue;

        int viadia = ValueFromString( g_UserUnit, tvalue );

        if( viadia < minViaDia )
        {
            result = false;
            msg.Printf( _( "<b>Extra Via %d Size</b> %s &lt; <b>Min Via Size</b><br>" ),
                        row + 1, GetChars( tvalue ) );
            errorMsg += msg;
        }

        wxString drlvalue = m_gridViaSizeList->GetCellValue( row, 1 );

        if( drlvalue.IsEmpty() )
        {
            result =  false;
            msg.Printf( _( "<b>No via drill size define in row %d</b><br>" ), row + 1 );
            errorMsg += msg;
            continue;
        }

        int viadrill = ValueFromString( g_UserUnit, drlvalue );

        if( viadrill < minViaDrill )
        {
            result = false;
            msg.Printf( _( "<b>Extra Via %d Drill</b> %s &lt; <b>Min Via Drill %s</b><br>" ),
                        row + 1, GetChars( drlvalue ),
                        GetChars( m_SetViasMinDrillCtrl->GetValue() ) );
            errorMsg += msg;
        }

        if( viadia <= viadrill )
        {
            result = false;
            msg.Printf( _( "<b>Extra Via %d Size</b> %s &le; <b> Drill Size</b> %s<br>" ),
                        row + 1, GetChars( tvalue ), GetChars( drlvalue ) );
            errorMsg += msg;
        }

        // Test for a reasonable via size:
        if( viadia > maxval )    // 1 inch!
        {
            result = false;
            msg.Printf( _( "<b>Extra Via %d Size</b>%s &gt; <b>1 inch!</b><br>" ),
                        row + 1, GetChars( tvalue ) );
            errorMsg += msg;
        }
    }

    if( !result && aErrorMsg )
        *aErrorMsg = errorMsg;

    return result;
}