dialog_design_rules.cpp 33.1 KB
Newer Older
1 2 3
/**
 * @file dialog_design_rules.cpp
 */
4

charras's avatar
charras committed
5
/*
6
 * This program source code file is part of KiCad, a free EDA CAD application.
charras's avatar
charras committed
7
 *
8
 * Copyright (C) 2004-2009 Jean-Pierre Charras, jean-pierre.charras@gpisa-lab.inpg.fr
charras's avatar
charras committed
9
 * Copyright (C) 2009 Dick Hollenbeck, dick@softplc.com
10
 * Copyright (C) 2009 KiCad Developers, see change_log.txt for contributors.
charras's avatar
charras committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 *
 * 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
 */


31
/* functions relative to the design rules editor
32
 */
33 34
#include <fctsys.h>
#include <class_drawpanel.h>
35
#include <base_units.h>
36 37 38 39
#include <confirm.h>
#include <pcbnew.h>
#include <wxPcbStruct.h>
#include <class_board_design_settings.h>
40

41 42
#include <pcbnew_id.h>
#include <class_track.h>
43
#include <macros.h>
44

45 46 47
#include <dialog_design_rules.h>
#include <wx/generic/gridctrl.h>
#include <dialog_design_rules_aux_helper_class.h>
48

49 50
#include <boost/make_shared.hpp>

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

55 56 57 58 59 60 61
// Field Positions on rules grid
enum {
    GRID_CLEARANCE,
    GRID_TRACKSIZE,
    GRID_VIASIZE,
    GRID_VIADRILL,
    GRID_uVIASIZE,
jean-pierre charras's avatar
jean-pierre charras committed
62
    GRID_uVIADRILL
63 64
};

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

dickelbeck's avatar
dickelbeck committed
67
// dialog should remember its previously selected tab
68
int DIALOG_DESIGN_RULES::s_LastTabSelection = -1;
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

// 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;
}


88
void NETS_LIST_CTRL::SetRowItems( unsigned        aRow,
89 90 91 92 93 94 95 96 97 98
                                  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();
99

100 101
    if( cnt <= aRow )
        m_Classnames.Add( wxEmptyString, aRow - cnt + 1 );
dickelbeck's avatar
dickelbeck committed
102

103 104 105 106 107 108
    if( (int)aRow <= GetItemCount() )
        SetItemCount( aRow + 1 );

    m_Netnames[aRow]   = aNetname;
    m_Classnames[aRow] = aNetclassName;
}
dickelbeck's avatar
dickelbeck committed
109 110 111 112 113 114 115 116


/**
 * 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.
 */
117

dickelbeck's avatar
dickelbeck committed
118 119 120 121 122 123 124 125
// @todo: maybe move this to common.cpp if it works.
void EnsureGridColumnWidths( wxGrid* aGrid )
{
    wxScreenDC sDC;

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

    int colCount = aGrid->GetNumberCols();
126

127
    for( int col = 0; col<colCount;  ++col )
dickelbeck's avatar
dickelbeck committed
128 129
    {
        // add two spaces to the text and size it.
faa's avatar
faa committed
130
        wxString colText = aGrid->GetColLabelValue( col ) + wxT( "  " );
dickelbeck's avatar
dickelbeck committed
131

132
        wxSize   needed = sDC.GetTextExtent( colText );
dickelbeck's avatar
dickelbeck committed
133 134 135 136 137 138

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

139

140
DIALOG_DESIGN_RULES::DIALOG_DESIGN_RULES( PCB_EDIT_FRAME* parent ) :
141 142 143
    DIALOG_DESIGN_RULES_BASE( parent )
{
    m_Parent = parent;
144
    SetAutoLayout( true );
145

dickelbeck's avatar
dickelbeck committed
146 147
    EnsureGridColumnWidths( m_grid );   // override any column widths set by wxformbuilder.

148 149
    wxListItem column0;
    wxListItem column1;
150 151 152 153

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

154 155
    column0.SetMask( wxLIST_MASK_TEXT );
    column1.SetMask( wxLIST_MASK_TEXT );
156

157 158
    column0.SetText( NET_TITLE );
    column1.SetText( CLASS_TITLE );
159 160 161 162 163 164 165 166 167 168 169

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

dickelbeck's avatar
dickelbeck committed
170 171 172 173 174
    // if user has been into the dialog before, go back to same tab
    if( s_LastTabSelection != -1 )
    {
        m_DRnotebook->SetSelection( s_LastTabSelection );
    }
175

176
    InitDialogRules();
177
    Layout();
178 179
    GetSizer()->Fit( this );
    GetSizer()->SetSizeHints( this );
dickelbeck's avatar
dickelbeck committed
180

181 182 183 184 185 186 187
    // Allow tabbing out of grid controls.  Only available on wxWidgets 2.9.5 or later.
#if wxCHECK_VERSION( 2, 9, 5 )
    m_grid->SetTabBehaviour( wxGrid::Tab_Leave );
    m_gridViaSizeList->SetTabBehaviour( wxGrid::Tab_Leave );
    m_gridTrackWidthList->SetTabBehaviour( wxGrid::Tab_Leave );
#endif

188
    Center();
189 190 191
}


192
void DIALOG_DESIGN_RULES::PrintCurrentSettings()
193 194 195
{
    wxString msg, value;

196
    m_MessagesList->AppendToPage( _( "<b>Current general settings:</b><br>" ) );
197 198

    // Display min values:
199
    value = StringFromValue( g_UserUnit, m_BrdSettings->m_TrackMinWidth, true );
200 201 202
    msg.Printf( _( "Minimum value for tracks width: <b>%s</b><br>\n" ), GetChars( value ) );
    m_MessagesList->AppendToPage( msg );

203
    value = StringFromValue( g_UserUnit, m_BrdSettings->m_ViasMinSize, true );
204 205 206
    msg.Printf( _( "Minimum value for vias diameter: <b>%s</b><br>\n" ), GetChars( value ) );
    m_MessagesList->AppendToPage( msg );

207
    value = StringFromValue( g_UserUnit, m_BrdSettings->m_MicroViasMinSize, true );
208 209
    msg.Printf( _( "Minimum value for microvias diameter: <b>%s</b><br>\n" ), GetChars( value ) );
    m_MessagesList->AppendToPage( msg );
210 211 212
}


213
void DIALOG_DESIGN_RULES::InitDialogRules()
214 215
{
    SetFocus();
216
    SetReturnCode( 0 );
217

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

221 222
    // Initialize the Rules List
    InitRulesList();
223

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

dickelbeck's avatar
dickelbeck committed
226
    // @todo go fix m_Pcb->SynchronizeNetsAndNetClasses() so that the netcode==0 is not present in the BOARD::m_NetClasses
227
    NETCLASSES& netclasses = m_BrdSettings->m_NetClasses;
228
    NETCLASSPTR netclass = netclasses.GetDefault();
229

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

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

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

247
    InitializeRulesSelectionBoxes();
248
    InitGlobalRules();
249

250
    PrintCurrentSettings();
251
}
252

253

254 255 256 257 258 259 260 261
void DIALOG_DESIGN_RULES::InitGlobalRules()
{
    AddUnitSymbol( *m_ViaMinTitle );
    AddUnitSymbol( *m_ViaMinDrillTitle );
    AddUnitSymbol( *m_MicroViaMinSizeTitle );
    AddUnitSymbol( *m_MicroViaMinDrillTitle );
    AddUnitSymbol( *m_TrackMinWidthTitle );

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

265
    if( m_BrdSettings->m_BlindBuriedViaAllowed )
266 267
        m_OptViaType->SetSelection( 1 );

268 269 270 271
    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 );
272 273 274

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

283

284 285 286 287
void DIALOG_DESIGN_RULES::InitDimensionsLists()
{
    wxString msg;

288 289 290 291 292 293
    // 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 );
294 295 296 297 298
    m_gridViaSizeList->SetColMinimalWidth( 0, 150 );
    m_gridViaSizeList->SetColMinimalWidth( 1, 150 );
    m_gridViaSizeList->AutoSizeColumns( true );
    m_gridTrackWidthList->SetColMinimalWidth( 0, 150 );
    m_gridTrackWidthList->AutoSizeColumns( true );
299 300 301 302 303 304

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

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

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

316
        if( m_ViasDimensionsList[ii].m_Drill > 0 )
317
        {
318
            msg = StringFromValue( g_UserUnit, m_ViasDimensionsList[ii].m_Drill, false );
319 320
            m_gridViaSizeList->SetCellValue( ii, 1, msg );
        }
321 322
    }
}
323

324

325
// Sort comparison function (helper for makePointers() )
326 327 328 329 330 331
static bool sortByClassThenName( NETCUP* a, NETCUP* b )
{
    // return a < b
    if( a->clazz < b->clazz )
        return true;

332 333 334 335 336 337
    // inside the same class, sort by net name:
    if( a->clazz == b->clazz )
    {
        if( a->net < b->net )
            return true;
    }
338

339 340 341
    return false;
}

342

343 344 345 346 347
void DIALOG_DESIGN_RULES::makePointers( PNETCUPS* aList, const wxString& aNetClassName )
{
    aList->clear();

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

354 355 356 357 358 359 360
        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 )
361
        {
362 363
            if( n->clazz == aNetClassName )
                aList->push_back( &*n );
364
        }
365

366
        sort( aList->begin(), aList->end(), sortByClassThenName );
367
    }
368
}
369

370

371 372
void DIALOG_DESIGN_RULES::FillListBoxWithNetNames( NETS_LIST_CTRL* aListCtrl,
                                                   const wxString& aNetClass )
373
{
374
    aListCtrl->ClearList();
375

376
    PNETCUPS ptrList;
377

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

dickelbeck's avatar
dickelbeck committed
381
#if 0 && defined(DEBUG)
382 383
    int r = 0;
    for( PNETCUPS::iterator i = ptrList.begin();  i!=ptrList.end();  ++i, ++r )
384
    {
385
        printf( "[%d]: %s  %s\n", r, TO_UTF8( (*i)->net ), TO_UTF8( (*i)->clazz ) );
386
    }
387

388 389
#endif

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

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

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

413

414
/* Populates combo boxes with the list of existing net classes
415 416 417
 */
void DIALOG_DESIGN_RULES::InitializeRulesSelectionBoxes()
{
418 419 420 421 422 423 424
    m_rightClassChoice->Clear();
    m_leftClassChoice->Clear();

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

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

430 431 432
    m_rightClassChoice->Select( 0 );
    m_leftClassChoice->Select( 0 );

433
    m_buttonRightToLeft->Enable( false );
434
    m_buttonLeftToRight->Enable( false );
435 436 437

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


441
/* Initialize the rules list from board
442
 */
443

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

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

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

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

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

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

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

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

470

471 472
void DIALOG_DESIGN_RULES::InitRulesList()
{
473
    NETCLASSES& netclasses = m_BrdSettings->m_NetClasses;
dickelbeck's avatar
dickelbeck committed
474

475
    // the +1 is for the Default NETCLASS.
476
    if( netclasses.GetCount() + 1 > (unsigned) m_grid->GetNumberRows() )
477
    {
478
        m_grid->AppendRows( netclasses.GetCount() + 1 - m_grid->GetNumberRows() );
479 480
    }

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

484
    // enter others netclasses
485
    int row = 1;
486

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

491
        class2gridRow( m_grid, row, netclass );
492 493 494
    }
}

495

496
static void gridRow2class( wxGrid* grid, int row, NETCLASSPTR nc )
497
{
498
#define MYCELL( col )   \
499
    ValueFromString( g_UserUnit, grid->GetCellValue( row, col ) )
500 501 502 503 504 505 506

    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 ) );
507 508 509
}


510 511
void DIALOG_DESIGN_RULES::CopyRulesListToBoard()
{
512
    NETCLASSES& netclasses = m_BrdSettings->m_NetClasses;
dickelbeck's avatar
dickelbeck committed
513

514
    // Remove all netclasses from board. We'll copy new list after
dickelbeck's avatar
dickelbeck committed
515 516
    netclasses.Clear();

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

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

525
        if( !m_BrdSettings->m_NetClasses.Add( nc ) )
526
        {
527 528 529
            // 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;
530
            msg.Printf( wxT( "CopyRulesListToBoard(): The NetClass \"%s\" already exists. Skip" ),
531
                        GetChars( m_grid->GetRowLabelValue( row ) ) );
532
            wxMessageBox( msg );
533

534
            continue;
535
        }
dickelbeck's avatar
dickelbeck committed
536

537
        gridRow2class( m_grid, row, nc );
538 539
    }

540 541
    // 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 )
542
    {
543
        NETCLASSPTR nc = netclasses.Find( netcup->clazz );
544 545
        wxASSERT( nc );
        nc->Add( netcup->net );
546 547
    }

dickelbeck's avatar
dickelbeck committed
548
    m_Pcb->SynchronizeNetsAndNetClasses();
549 550
}

551

552
void DIALOG_DESIGN_RULES::CopyGlobalRulesToBoard()
553
{
554
    m_BrdSettings->m_BlindBuriedViaAllowed = m_OptViaType->GetSelection() > 0;
555 556

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

560
    m_BrdSettings->m_MicroViasAllowed = m_AllowMicroViaCtrl->GetSelection() == 1;
561 562

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

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

570 571

void DIALOG_DESIGN_RULES::CopyDimensionsListsToBoard()
572 573 574 575 576
{
    wxString msg;

    // Reinitialize m_TrackWidthList
    m_TracksWidthList.clear();
577

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

582 583
        if( msg.IsEmpty() )
            continue;
584

585
        int value = ValueFromString( g_UserUnit, msg );
586
        m_TracksWidthList.push_back( value );
587
    }
588

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

592
    // Reinitialize m_TrackWidthList
593
    m_ViasDimensionsList.clear();
594

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

599 600
        if( msg.IsEmpty() )
            continue;
601

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

607
        if( !msg.IsEmpty() )
608
        {
609
            value = ValueFromString( g_UserUnit, msg );
610
            via_dim.m_Drill = value;
611
        }
612

613
        m_ViasDimensionsList.push_back( via_dim );
614
    }
615

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

619
    std::vector<int>* tlist = &m_BrdSettings->m_TrackWidthList;
620 621 622 623 624 625

    // 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() );
626 627

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

633

634 635
void DIALOG_DESIGN_RULES::OnCancelButtonClick( wxCommandEvent& event )
{
dickelbeck's avatar
dickelbeck committed
636 637
    s_LastTabSelection = m_DRnotebook->GetSelection();

638
    EndModal( wxID_CANCEL );
639 640 641 642 643
}


void DIALOG_DESIGN_RULES::OnOkButtonClick( wxCommandEvent& event )
{
dickelbeck's avatar
dickelbeck committed
644 645
    s_LastTabSelection = m_DRnotebook->GetSelection();

646 647 648 649 650 651
    if( !TestDataValidity() )
    {
        DisplayError( this, _( "Errors detected, Abort" ) );
        return;
    }

652
    CopyRulesListToBoard();
653
    CopyGlobalRulesToBoard();
654
    CopyDimensionsListsToBoard();
655

656
    EndModal( wxID_OK );
657

658
    m_BrdSettings->SetCurrentNetClass( NETCLASS::Default );
659 660 661 662 663
}


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

    wxTextEntryDialog dlg( this, _( "New Net Class Name:" ), wxEmptyString, class_name );
667

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

671
    class_name = dlg.GetValue();
672 673
    class_name.Trim( true );
    class_name.Trim( false );
674

675 676
    if( class_name.IsEmpty() )
        return;         // empty name not allowed
677 678

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

684
        if( class_name.CmpNoCase( value ) == 0 )       // Already exists!
685
        {
686
            DisplayError( this, _( "This NetClass is already existing, cannot add it; Aborted" ) );
687 688 689 690
            return;
        }
    }

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

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

697
    for( int icol = 0; icol < m_grid->GetNumberCols(); icol++ )
698 699
    {
        wxString value;
700
        value = m_grid->GetCellValue( 0, icol );
701
        m_grid->SetCellValue( irow, icol, value );
702
    }
703

704 705 706
    InitializeRulesSelectionBoxes();
}

707

708
// Sort function for wxArrayInt. Items (ints) are sorted by decreasing value
709
// used in DIALOG_DESIGN_RULES::OnRemoveNetclassClick
710
int sort_int( int* first, int* second )
711
{
712
    return *second - *first;
713
}
714 715


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

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

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

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

731 732 733
    // 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++ )
734
    {
735
        int grid_row = select[ii];
736

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

744 745
            // reset the net class to default for members of the removed class
            swapNetClass( classname, NETCLASS::Default );
746
        }
747
        else
748
            wxMessageBox( _( "The default Netclass cannot be removed" ) );
749
    }
750

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

757 758
        InitializeRulesSelectionBoxes();
    }
759 760
}

761

762 763 764 765 766 767
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;

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

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

777 778
        // Swap the rule and the previous rule
        wxString curr_value, previous_value;
779

780 781
        for( int icol = 0; icol < m_grid->GetNumberCols(); icol++ )
        {
782 783 784
            reinit         = true;
            curr_value     = m_grid->GetCellValue( ii, icol );
            previous_value = m_grid->GetCellValue( ii - 1, icol );
785
            m_grid->SetCellValue( ii, icol, previous_value );
786
            m_grid->SetCellValue( ii - 1, icol, curr_value );
787
        }
788 789 790 791 792

        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 );
793
    }
794

795 796 797 798
    if( reinit )
        InitializeRulesSelectionBoxes();
}

799

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

804
    if( m_leftClassChoice->GetStringSelection() ==  m_rightClassChoice->GetStringSelection() )
805
    {
806 807
        m_buttonRightToLeft->Enable( false );
        m_buttonLeftToRight->Enable( false );
808 809 810
    }
    else
    {
811 812
        m_buttonRightToLeft->Enable( true );
        m_buttonLeftToRight->Enable( true );
813 814 815
    }
}

816

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

821
    if( m_leftClassChoice->GetStringSelection() ==  m_rightClassChoice->GetStringSelection() )
822
    {
823
        m_buttonRightToLeft->Enable( false );
824
        m_buttonLeftToRight->Enable( false );
825 826 827
    }
    else
    {
828 829
        m_buttonRightToLeft->Enable( true );
        m_buttonLeftToRight->Enable( true );
830 831 832 833
    }
}


834
void DIALOG_DESIGN_RULES::moveSelectedItems( NETS_LIST_CTRL* src, const wxString& newClassName )
835
{
836 837 838 839
    wxListItem item;
    wxString   netName;

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

dickelbeck's avatar
dickelbeck committed
841
    for( int row = 0;  row < src->GetItemCount();  ++row )
842
    {
dickelbeck's avatar
dickelbeck committed
843
        if( !src->GetItemState( row, wxLIST_STATE_SELECTED ) )
844 845
            continue;

dickelbeck's avatar
dickelbeck committed
846 847 848 849 850
        item.SetColumn( 0 );
        item.SetId( row );

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

        setNetClass( netName, newClassName == wildCard ? NETCLASS::Default : newClassName );
853
    }
dickelbeck's avatar
dickelbeck committed
854 855 856 857 858 859 860 861
}


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

    moveSelectedItems( m_rightListCtrl, newClassName );
862

863 864
    FillListBoxWithNetNames( m_leftListCtrl, m_leftClassChoice->GetStringSelection() );
    FillListBoxWithNetNames( m_rightListCtrl, m_rightClassChoice->GetStringSelection() );
865 866
}

867

868 869
void DIALOG_DESIGN_RULES::OnLeftToRightCopyButton( wxCommandEvent& event )
{
dickelbeck's avatar
dickelbeck committed
870 871 872
    wxString newClassName = m_rightClassChoice->GetStringSelection();

    moveSelectedItems( m_leftListCtrl, newClassName );
873

874 875
    FillListBoxWithNetNames( m_leftListCtrl, m_leftClassChoice->GetStringSelection() );
    FillListBoxWithNetNames( m_rightListCtrl, m_rightClassChoice->GetStringSelection() );
876 877
}

878

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

885

886 887
void DIALOG_DESIGN_RULES::OnRightSelectAllButton( wxCommandEvent& event )
{
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
    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;
        }
    }
903 904
}

905 906 907

bool DIALOG_DESIGN_RULES::TestDataValidity()
{
908
    bool result = true;
909

910
    m_MessagesList->SetPage( wxEmptyString );     // Clear message list
911

912
    wxString msg;
913

914 915 916 917 918
    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 );
919 920
    int      maxval = 1000 * IU_PER_MILS;   // a max value for tracks and vias sizes (1 inch)

921 922


923
    for( int row = 0; row < m_grid->GetNumberRows(); row++ )
924
    {
925
        int tracksize = ValueFromString( g_UserUnit,
926
                                         m_grid->GetCellValue( row, GRID_TRACKSIZE ) );
927
        if( tracksize < minTrackWidth )
928 929 930
        {
            result = false;
            msg.Printf( _( "%s: <b>Track Size</b> &lt; <b>Min Track Size</b><br>" ),
931
                        GetChars( m_grid->GetRowLabelValue( row ) ) );
932 933 934

            m_MessagesList->AppendToPage( msg );
        }
935

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

940
        if( viadia < minViaDia )
941 942 943
        {
            result = false;
            msg.Printf( _( "%s: <b>Via Diameter</b> &lt; <b>Minimun Via Diameter</b><br>" ),
944
                        GetChars( m_grid->GetRowLabelValue( row ) ) );
945 946 947 948

            m_MessagesList->AppendToPage( msg );
        }

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

952
        if( viadrill >= viadia )
953
        {
954 955
            result = false;
            msg.Printf( _( "%s: <b>Via Drill</b> &ge; <b>Via Dia</b><br>" ),
956
                        GetChars( m_grid->GetRowLabelValue( row ) ) );
957 958

            m_MessagesList->AppendToPage( msg );
959
        }
960

961 962 963 964
        if( viadrill < minViaDrill )
        {
            result = false;
            msg.Printf( _( "%s: <b>Via Drill</b> &lt; <b>Min Via Drill</b><br>" ),
965
                        GetChars( m_grid->GetRowLabelValue( row ) ) );
966 967 968 969

            m_MessagesList->AppendToPage( msg );
        }

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

974
        if( muviadia < minUViaDia )
975 976
        {
            result = false;
977
            msg.Printf( _( "%s: <b>MicroVia Diameter</b> &lt; <b>MicroVia Min Diameter</b><br>" ),
978
                        GetChars( m_grid->GetRowLabelValue( row ) ) );
979 980 981 982

            m_MessagesList->AppendToPage( msg );
        }

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

986
        if( muviadrill >= muviadia )
987 988 989
        {
            result = false;
            msg.Printf( _( "%s: <b>MicroVia Drill</b> &ge; <b>MicroVia Dia</b><br>" ),
990
                        GetChars( m_grid->GetRowLabelValue( row ) ) );
991 992 993

            m_MessagesList->AppendToPage( msg );
        }
994 995 996 997

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

            m_MessagesList->AppendToPage( msg );
        }
1003 1004
    }

1005
    // Test list of values for specific vias and tracks
1006 1007
    // Test tracks
    for( int row = 0; row < m_gridTrackWidthList->GetNumberRows();  ++row )
1008
    {
1009
        wxString tvalue = m_gridTrackWidthList->GetCellValue( row, 0 );
1010

1011 1012 1013
        if( tvalue.IsEmpty() )
            continue;

1014
        int tracksize = ValueFromString( g_UserUnit, tvalue );
1015

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

            m_MessagesList->AppendToPage( msg );
        }
1024

1025
        if( tracksize > maxval )
1026 1027
        {
            result = false;
1028
            msg.Printf( _( "<b>Extra Track %d Size</b> %s &gt; <b>1 inch!</b><br>" ),
1029
                        row + 1, GetChars( tvalue ) );
1030 1031 1032 1033 1034

            m_MessagesList->AppendToPage( msg );
        }
    }

1035 1036
    // Test vias
    for( int row = 0; row < m_gridViaSizeList->GetNumberRows();  ++row )
1037
    {
1038
        wxString tvalue = m_gridViaSizeList->GetCellValue( row, 0 );
1039

1040 1041 1042
        if( tvalue.IsEmpty() )
            continue;

1043
        int viadia = ValueFromString( g_UserUnit, tvalue );
1044 1045
        int viadrill = 0;
        wxString drlvalue = m_gridViaSizeList->GetCellValue( row, 1 );
1046

1047
        if( !drlvalue.IsEmpty() )
1048
            viadrill = ValueFromString( g_UserUnit, drlvalue );
1049

1050 1051 1052 1053
        if( viadia < minViaDia )
        {
            result = false;
            msg.Printf( _( "<b>Extra Via %d Size</b> %s &lt; <b>Min Via Size</b><br>" ),
1054
                        row + 1, GetChars( tvalue ) );
1055 1056 1057

            m_MessagesList->AppendToPage( msg );
        }
1058

1059
        if( viadia <= viadrill )
1060 1061
        {
            result = false;
1062
            msg.Printf( _( "<b>Extra Via %d Size</b> %s &le; <b> Drill Size</b> %s<br>" ),
1063
                        row + 1, GetChars( tvalue ), GetChars( drlvalue ) );
1064 1065 1066 1067

            m_MessagesList->AppendToPage( msg );
        }

1068
        // Test for a reasonable via size:
1069
        if( viadia > maxval )    // 1 inch!
1070 1071 1072
        {
            result = false;
            msg.Printf( _( "<b>Extra Via %d Size</b>%s &gt; <b>1 inch!</b><br>" ),
1073
                        row + 1, GetChars( tvalue ) );
1074 1075 1076 1077 1078

            m_MessagesList->AppendToPage( msg );
        }
    }

1079
    return result;
1080
}