xchgmod.cpp 16.5 KB
Newer Older
1 2 3
/**
 * @file xchgmod.cpp
 */
4

5 6 7
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
8
 * Copyright (C) 2013 Jean-Pierre Charras, jp.charras at wanadoo.fr
9 10 11
 * Copyright (C) 2013 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
 * Copyright (C) 2013 Wayne Stambaugh <stambaughw@verizon.net>
 * Copyright (C) 1992-2013 KiCad Developers, see AUTHORS.txt for contributors.
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 32
#include <fctsys.h>
#include <class_drawpanel.h>
33
#include <class_draw_panel_gal.h>
34 35 36 37
#include <confirm.h>
#include <kicad_string.h>
#include <wxPcbStruct.h>
#include <macros.h>
38

39 40
#include <class_board.h>
#include <class_module.h>
41
#include <project.h>
42

43 44
#include <pcbnew.h>
#include <dialog_exchange_modules_base.h>
45
#include <wildcards_and_files_ext.h>
46

47
#include <boost/bind.hpp>
48 49
#include <tool/tool_manager.h>
#include <tools/common_actions.h>
50

51
static bool RecreateCmpFile( BOARD * aBrd, const wxString& aFullCmpFileName );
52

charras's avatar
charras committed
53
class DIALOG_EXCHANGE_MODULE : public DIALOG_EXCHANGE_MODULE_BASE
54 55
{
private:
56 57 58
    PCB_EDIT_FRAME* m_parent;
    MODULE*         m_currentModule;
    static int      m_selectionMode;    // Remember the last exchange option
59

60 61
public:
    DIALOG_EXCHANGE_MODULE( PCB_EDIT_FRAME* aParent, MODULE* aModule );
charras's avatar
charras committed
62
    ~DIALOG_EXCHANGE_MODULE() { };
63

64
private:
65 66 67 68
    void OnSelectionClicked( wxCommandEvent& event );
    void OnOkClick( wxCommandEvent& event );
    void OnQuit( wxCommandEvent& event );
    void BrowseAndSelectFootprint( wxCommandEvent& event );
69
    void RebuildCmpList( wxCommandEvent& event );
70 71 72
    void init();

    void ChangeCurrentFootprint();
73
    void ChangeSameFootprints( bool aUseValue);
74 75 76
    void ChangeAllFootprints();
    bool Change_1_Module( MODULE*            aModule,
                          const FPID&        aNewFootprintFPID,
77
                          PICKED_ITEMS_LIST* aUndoPickList,
78
                          bool               eShowError );
79 80
};

81
int DIALOG_EXCHANGE_MODULE::m_selectionMode = 0;
82

83
DIALOG_EXCHANGE_MODULE::DIALOG_EXCHANGE_MODULE( PCB_EDIT_FRAME* parent, MODULE* Module ) :
charras's avatar
charras committed
84 85
    DIALOG_EXCHANGE_MODULE_BASE( parent )
{
86 87 88
    m_parent = parent;
    m_currentModule = Module;
    init();
charras's avatar
charras committed
89 90 91
    GetSizer()->Fit( this );
    GetSizer()->SetSizeHints( this );
}
92 93


94
void PCB_EDIT_FRAME::InstallExchangeModuleFrame( MODULE* Module )
charras's avatar
charras committed
95 96
{
    DIALOG_EXCHANGE_MODULE dialog( this, Module );
97

charras's avatar
charras committed
98 99
    dialog.ShowModal();
}
100 101


102
void DIALOG_EXCHANGE_MODULE::OnQuit( wxCommandEvent& event )
charras's avatar
charras committed
103
{
104
    m_selectionMode = m_Selection->GetSelection();
105
    EndModal( 0 );
charras's avatar
charras committed
106
}
107 108


109
void DIALOG_EXCHANGE_MODULE::init()
charras's avatar
charras committed
110 111
{
    SetFocus();
112

113 114 115 116
    m_OldModule->AppendText( FROM_UTF8( m_currentModule->GetFPID().Format().c_str() ) );
    m_NewModule->AppendText( FROM_UTF8( m_currentModule->GetFPID().Format().c_str() ) );
    m_OldValue->AppendText( m_currentModule->GetValue() );
    m_Selection->SetSelection( m_selectionMode );
117

charras's avatar
charras committed
118 119 120
    // Enable/disable widgets:
    wxCommandEvent event;
    OnSelectionClicked( event );
121 122 123
}


charras's avatar
charras committed
124
void DIALOG_EXCHANGE_MODULE::OnOkClick( wxCommandEvent& event )
125
{
126
    m_selectionMode = m_Selection->GetSelection();
127

charras's avatar
charras committed
128 129 130
    switch( m_Selection->GetSelection() )
    {
    case 0:
131
        ChangeCurrentFootprint();
charras's avatar
charras committed
132
        break;
133

charras's avatar
charras committed
134
    case 1:
135
        ChangeSameFootprints( false );
charras's avatar
charras committed
136 137 138
        break;

    case 2:
139
        ChangeSameFootprints( true );
charras's avatar
charras committed
140 141 142
        break;

    case 3:
143
        ChangeAllFootprints();
charras's avatar
charras committed
144 145
        break;
    }
146 147 148
}


charras's avatar
charras committed
149
void DIALOG_EXCHANGE_MODULE::OnSelectionClicked( wxCommandEvent& event )
150
{
151 152
    bool enable = true;

charras's avatar
charras committed
153 154 155 156 157 158 159 160
    switch( m_Selection->GetSelection() )
    {
    case 0:
    case 1:
    case 2:
        break;

    case 3:
161
        enable = false;
charras's avatar
charras committed
162 163
        break;
    }
164 165 166

    m_NewModule->Enable( enable );
    m_Browsebutton->Enable( enable );
167 168 169
}


170
/*
171 172 173
 * Rebuild the file name.CMP (if any) after exchanging footprints
 * if the footprint are managed by this file
 * Return false if error
174
 */
175
void DIALOG_EXCHANGE_MODULE::RebuildCmpList( wxCommandEvent& event )
176
{
177 178
    wxFileName  fn;
    wxString    msg;
179

180
    // Build CMP file name by changing the extension of NetList filename
181
    fn = m_parent->GetBoard()->GetFileName();
182
    fn.SetExt( ComponentFileExtension );
183

184
    if( RecreateCmpFile( m_parent->GetBoard(), fn.GetFullPath() ) )
185
    {
186 187
        msg.Printf( _( "File '%s' created\n" ),
                    GetChars( fn.GetFullPath() ) );
188
    }
189
    else
190
    {
191 192
        msg.Printf( _( "** Could not create file '%s' ***\n" ),
                    GetChars( fn.GetFullPath() ) );
193 194
    }

195
    m_WinMessages->AppendText( msg );
196 197 198
}


199
/* Change the current footprint at the current cursor position.
200
 * Retains the following:
201 202 203
 * - position, orientation and side
 * - value and ref
 * - pads net names
204
 */
205
void DIALOG_EXCHANGE_MODULE::ChangeCurrentFootprint()
206
{
207
    wxString newmodulename = m_NewModule->GetValue();
208

209 210
    if( newmodulename == wxEmptyString )
        return;
211

charras's avatar
charras committed
212 213
    PICKED_ITEMS_LIST pickList;

214
    if( Change_1_Module( m_currentModule, newmodulename, &pickList, true ) )
215
    {
216 217
        if( m_parent->GetBoard()->IsElementVisible( RATSNEST_VISIBLE ) )
            m_parent->Compile_Ratsnest( NULL, true );
218

219
        m_parent->GetCanvas()->Refresh();
220
    }
charras's avatar
charras committed
221 222

    if( pickList.GetCount() )
223
        m_parent->SaveCopyInUndoList( pickList, UR_UNSPECIFIED );
224 225
}

226

227
/*
228
 * Change all footprints having the same fpid by a new one from lib
229
 * Retains:
230 231 232 233
 * - direction, position, side
 * - value and ref
 * - pads net names
 * Note: m_currentModule is no longer the current footprint
234
 * since it has been changed!
235 236
 * if aUseValue is true, footprints having the same fpid should
 * also have the same value
237
 */
238
void DIALOG_EXCHANGE_MODULE::ChangeSameFootprints( bool aUseValue )
239
{
240
    wxString msg;
241
    MODULE*  Module, * PtBack;
charras's avatar
charras committed
242
    bool     change = false;
243
    wxString newmodulename = m_NewModule->GetValue();
244 245
    wxString value;
    FPID     lib_reference;
charras's avatar
charras committed
246
    bool     check_module_value = false;
247
    int      ShowErr = 3;           // Post 3 error messages max.
248

249
    if( m_parent->GetBoard()->m_Modules == NULL )
250
        return;
251

252 253 254
    if( newmodulename == wxEmptyString )
        return;

255
    lib_reference = m_currentModule->GetFPID();
256

charras's avatar
charras committed
257
    if( aUseValue )
258
    {
259
        check_module_value = true;
260
        value = m_currentModule->GetValue();
261
        msg.Printf( _( "Change modules %s -> %s (for value = %s)?" ),
262
                    GetChars( FROM_UTF8( m_currentModule->GetFPID().Format().c_str() ) ),
263
                    GetChars( newmodulename ),
264
                    GetChars( m_currentModule->GetValue() ) );
265 266 267
    }
    else
    {
268
        msg.Printf( _( "Change modules %s -> %s ?" ),
269 270
                    GetChars( FROM_UTF8( lib_reference.Format().c_str() ) ),
                    GetChars( newmodulename ) );
271 272 273 274 275
    }

    if( !IsOK( this, msg ) )
        return;

276 277
    /* The change is done from the last module because
     * Change_1_Module () modifies the last item in the list.
278
     */
charras's avatar
charras committed
279
    PICKED_ITEMS_LIST pickList;
280 281 282

    /* note: for the first module in chain (the last here), Module->Back()
     * points the board or is NULL
charras's avatar
charras committed
283
     */
284
    Module = m_parent->GetBoard()->m_Modules.GetLast();
285

286
    for( ; Module && ( Module->Type() == PCB_MODULE_T ); Module = PtBack )
287
    {
288
        PtBack = Module->Back();
289

290
        if( lib_reference != Module->GetFPID() )
291
            continue;
292

293 294
        if( check_module_value )
        {
295
            if( value.CmpNoCase( Module->GetValue() ) != 0 )
296 297
                continue;
        }
298

299
        if( Change_1_Module( Module, newmodulename, &pickList, ShowErr ) )
300
            change = true;
301 302 303 304 305 306
        else if( ShowErr )
            ShowErr--;
    }

    if( change )
    {
307 308
        if( m_parent->GetBoard()->IsElementVisible( RATSNEST_VISIBLE ) )
            m_parent->Compile_Ratsnest( NULL, true );
309

310
        m_parent->GetCanvas()->Refresh();
311
    }
charras's avatar
charras committed
312 313

    if( pickList.GetCount() )
314
        m_parent->SaveCopyInUndoList( pickList, UR_UNSPECIFIED );
315 316
}

317

318
/*
319 320
 * Change all modules with module of the same name in library.
 * Maintains:
321 322 323
 * - direction, position, side
 * - value and ref
 * - pads net names
324
 */
325
void DIALOG_EXCHANGE_MODULE::ChangeAllFootprints()
326
{
327
    MODULE* Module, * PtBack;
charras's avatar
charras committed
328
    bool    change  = false;
329
    int     ShowErr = 3;              // Post 3 error max.
330

331
    if( m_parent->GetBoard()->m_Modules == NULL )
332 333 334 335 336
        return;

    if( !IsOK( this, _( "Change ALL modules ?" ) ) )
        return;

337
    /* The change is done from the last module because the function
338
     * Change_1_Module () modifies the last module in the list
339
     */
340
    PICKED_ITEMS_LIST pickList;
341

342 343
    /* note: for the first module in chain (the last here), Module->Back()
     * points the board or is NULL
charras's avatar
charras committed
344
     */
345
    Module = m_parent->GetBoard()->m_Modules.GetLast();
346

347
    for( ; Module && ( Module->Type() == PCB_MODULE_T ); Module = PtBack )
348
    {
349
        PtBack = Module->Back();
350

351
        if( Change_1_Module( Module, Module->GetFPID(), &pickList, ShowErr ) )
352
            change = true;
353 354 355 356 357 358
        else if( ShowErr )
            ShowErr--;
    }

    if( change )
    {
359 360
        if( m_parent->GetBoard()->IsElementVisible( RATSNEST_VISIBLE ) )
            m_parent->Compile_Ratsnest( NULL, true );
361

362
        m_parent->GetCanvas()->Refresh();
363
    }
364

charras's avatar
charras committed
365
    if( pickList.GetCount() )
366
        m_parent->SaveCopyInUndoList( pickList, UR_UNSPECIFIED );
367 368
}

369

370
/*
371 372 373 374 375 376 377
 * Change aModule to a new, fresh one from lib
 * Retains
 * - direction, position, side
 * - value and ref
 * - pads net names
 * Returns: false if no change (if the new module is not found)
 * true if OK
378
 */
379 380
bool DIALOG_EXCHANGE_MODULE::Change_1_Module( MODULE*            aModule,
                                              const FPID&        aNewFootprintFPID,
381
                                              PICKED_ITEMS_LIST* aUndoPickList,
382
                                              bool               aShowError )
383
{
384
    MODULE*  newModule;
385
    wxString line;
386

387
    if( aModule == NULL )
charras's avatar
charras committed
388
        return false;
389

390
    wxBusyCursor dummy;
391

392
    // Copy parameters from the old module.
393
    FPID  oldFootprintFPID = aModule->GetFPID();
394

395
    // Load module.
396
    line.Printf( _( "Change footprint '%s' (from '%s') to '%s'" ),
397
                 GetChars( aModule->GetReference() ),
398 399
                 oldFootprintFPID.Format().c_str(),
                 aNewFootprintFPID.Format().c_str() );
400
    m_WinMessages->AppendText( line );
401

402 403
    wxString moduleName = aNewFootprintFPID.GetFootprintName();
    wxString libName    = aNewFootprintFPID.GetLibNickname();
404 405

    newModule = m_parent->LoadFootprint( aNewFootprintFPID );
406

407
    if( newModule == NULL )  // New module not found, redraw the old one.
408
    {
409
        m_WinMessages->AppendText( wxT( " No\n" ) );
charras's avatar
charras committed
410
        return false;
411
    }
412

413
    m_parent->Exchange_Module( aModule, newModule, aUndoPickList );
414
    m_parent->GetBoard()->Add( newModule, ADD_APPEND );
415

416 417
    if( aModule == m_currentModule )
        m_currentModule = newModule;
418

419
    m_WinMessages->AppendText( wxT( " OK\n" ) );
420

charras's avatar
charras committed
421
    return true;
422 423
}

424

425 426 427
void PCB_EDIT_FRAME::Exchange_Module( MODULE*            aOldModule,
                                      MODULE*            aNewModule,
                                      PICKED_ITEMS_LIST* aUndoPickList )
428
{
charras's avatar
charras committed
429
    aNewModule->SetParent( GetBoard() );
430

431
    /* place module without ratsnest refresh: this will be made later
charras's avatar
charras committed
432 433
     * when all modules are on board
     */
434
    PlaceModule( aNewModule, NULL, true );
435
    aNewModule->SetPosition( aOldModule->GetPosition() );
436

437
    // Flip footprint if needed
charras's avatar
charras committed
438
    if( aOldModule->GetLayer() != aNewModule->GetLayer() )
439
    {
440
        aNewModule->Flip( aNewModule->GetPosition() );
441 442
    }

443
    // Rotate footprint if needed
444
    if( aOldModule->GetOrientation() != aNewModule->GetOrientation() )
445
    {
446
        Rotate_Module( NULL, aNewModule, aOldModule->GetOrientation(), false );
447 448
    }

449
    // Update reference and value
450 451
    aNewModule->SetReference( aOldModule->GetReference() );
    aNewModule->SetValue( aOldModule->GetValue() );
452

453
    // Updating other parameters
Dick Hollenbeck's avatar
Dick Hollenbeck committed
454
    aNewModule->SetTimeStamp( aOldModule->GetTimeStamp() );
455
    aNewModule->SetPath( aOldModule->GetPath() );
456

457
    // Update pad netnames ( when possible)
458
    for( D_PAD* pad = aNewModule->Pads(); pad != NULL; pad = pad->Next() )
459
    {
460
        pad->SetNetCode( NETINFO_LIST::UNCONNECTED );
461

462
        for( D_PAD* old_pad = aOldModule->Pads(); old_pad != NULL; old_pad = old_pad->Next() )
463
        {
464
            if( pad->PadNameEqual( old_pad ) )
465
                pad->SetNetCode( old_pad->GetNetCode() );
466 467 468
        }
    }

charras's avatar
charras committed
469 470
    if( aUndoPickList )
    {
471 472 473 474 475
        GetBoard()->Remove( aOldModule );
        ITEM_PICKER picker_old( aOldModule, UR_DELETED );
        ITEM_PICKER picker_new( aNewModule, UR_NEW );
        aUndoPickList->PushItem( picker_old );
        aUndoPickList->PushItem( picker_new );
476 477 478 479 480 481 482 483 484 485

        if( IsGalCanvasActive() )
        {
            KIGFX::VIEW* view = GetGalCanvas()->GetView();

            aOldModule->RunOnChildren( boost::bind( &KIGFX::VIEW::Remove, view, _1 ) );
            view->Remove( aOldModule );

            aNewModule->RunOnChildren( boost::bind( &KIGFX::VIEW::Add, view, _1 ) );
            view->Add( aNewModule );
486 487 488

            m_toolManager->RunAction( COMMON_ACTIONS::selectionClear, true );
            GetGalCanvas()->ForceRefresh();
489
        }
charras's avatar
charras committed
490
    }
491
    else
492
    {
493
        GetGalCanvas()->GetView()->Remove( aOldModule );
charras's avatar
charras committed
494
        aOldModule->DeleteStructure();
495
    }
496

497
    GetBoard()->m_Status_Pcb = 0;
498
    aNewModule->ClearFlags();
499
    OnModify();
500 501 502
}


503
/*
504
 * Displays the list of modules in library name and select 1 name.
505
 */
charras's avatar
charras committed
506
void DIALOG_EXCHANGE_MODULE::BrowseAndSelectFootprint( wxCommandEvent& event )
507
{
508 509
    wxString newname;

510
    newname = m_parent->SelectFootprint( m_parent, wxEmptyString, wxEmptyString, wxEmptyString,
511
                                         Prj().PcbFootprintLibs() );
512

513 514
    if( newname != wxEmptyString )
        m_NewModule->SetValue( newname );
515 516 517
}


518
void PCB_EDIT_FRAME::RecreateCmpFileFromBoard( wxCommandEvent& aEvent )
519
{
520
    wxFileName  fn;
521
    MODULE*     module = GetBoard()->m_Modules;
522 523
    wxString    msg;
    wxString    wildcard;
524

525
    if( module == NULL )
526
    {
527
        DisplayError( this, _( "No footprints!" ) );
528
        return;
529 530
    }

531
    // Calculation file name by changing the extension name to NetList
532
    fn = GetBoard()->GetFileName();
533 534
    fn.SetExt( ComponentFileExtension );
    wildcard = wxGetTranslation( ComponentFileWildcard );
535

536 537 538
    wxString pro_dir = wxPathOnly( Prj().GetProjectFullName() );

    wxFileDialog dlg( this, _( "Save Component Files" ), pro_dir,
539 540 541 542
                      fn.GetFullName(), wildcard,
                      wxFD_SAVE | wxFD_OVERWRITE_PROMPT );

    if( dlg.ShowModal() == wxID_CANCEL )
543
        return;
544

545
    fn = dlg.GetPath();
546

547
    if( ! RecreateCmpFile( GetBoard(), fn.GetFullPath() ) )
548
    {
549
        msg.Printf( _( "Could not create file '%s'" ), GetChars(fn.GetFullPath() ) );
550
        DisplayError( this, msg );
551
        return;
552
    }
553
}
554

555 556 557 558 559
bool RecreateCmpFile( BOARD * aBrd, const wxString& aFullCmpFileName )
{
    FILE* cmpFile;

    cmpFile = wxFopen( aFullCmpFileName, wxT( "wt" ) );
560

561 562 563 564 565 566 567
    if( cmpFile == NULL )
        return false;

    fprintf( cmpFile, "Cmp-Mod V01 Genere par PcbNew le %s\n", TO_UTF8( DateAndTime() ) );

    MODULE* module = aBrd->m_Modules;
    for( ; module != NULL; module = module->Next() )
568
    {
569 570 571 572 573 574 575 576 577 578 579
        fprintf( cmpFile, "\nBeginCmp\n" );
        fprintf( cmpFile, "TimeStamp = %8.8lX\n", module->GetTimeStamp() );
        fprintf( cmpFile, "Path = %s\n", TO_UTF8( module->GetPath() ) );
        fprintf( cmpFile, "Reference = %s;\n",
                 !module->GetReference().IsEmpty() ?
                 TO_UTF8( module->GetReference() ) : "[NoRef]" );
        fprintf( cmpFile, "ValeurCmp = %s;\n",
                 !module->GetValue().IsEmpty() ?
                 TO_UTF8( module->GetValue() ) : "[NoVal]" );
        fprintf( cmpFile, "IdModule  = %s;\n", module->GetFPID().Format().c_str() );
        fprintf( cmpFile, "EndCmp\n" );
580 581
    }

582 583 584 585
    fprintf( cmpFile, "\nEndListe\n" );
    fclose( cmpFile );

    return true;
586
}