kicad/pcbnew/xchgmod.cpp

509 lines
14 KiB
C++
Raw Normal View History

/**
* @file xchgmod.cpp
*/
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
* Copyright (C) 2013 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
* Copyright (C) 2013-2016 Wayne Stambaugh <stambaughw@verizon.net>
* Copyright (C) 1992-2016 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <fctsys.h>
#include <class_drawpanel.h>
#include <class_draw_panel_gal.h>
#include <confirm.h>
#include <kicad_string.h>
#include <wxPcbStruct.h>
#include <macros.h>
#include <board_commit.h>
#include <class_board.h>
#include <class_module.h>
#include <project.h>
#include <pcbnew.h>
#include <dialog_exchange_modules_base.h>
#include <wildcards_and_files_ext.h>
#include <kiway.h>
static bool RecreateCmpFile( BOARD * aBrd, const wxString& aFullCmpFileName );
2009-06-20 19:09:43 +00:00
class DIALOG_EXCHANGE_MODULE : public DIALOG_EXCHANGE_MODULE_BASE
{
private:
PCB_EDIT_FRAME* m_parent;
MODULE* m_currentModule;
static int m_selectionMode; // Remember the last exchange option
2007-08-23 04:28:46 +00:00
public:
DIALOG_EXCHANGE_MODULE( PCB_EDIT_FRAME* aParent, MODULE* aModule );
2009-06-20 19:09:43 +00:00
~DIALOG_EXCHANGE_MODULE() { };
private:
2016-09-24 18:53:15 +00:00
void OnSelectionClicked( wxCommandEvent& event ) override;
void OnOkClick( wxCommandEvent& event ) override;
void OnQuit( wxCommandEvent& event ) override;
void BrowseAndSelectFootprint( wxCommandEvent& event ) override;
void ViewAndSelectFootprint( wxCommandEvent& event ) override;
void RebuildCmpList( wxCommandEvent& event ) override;
void init();
bool changeCurrentFootprint();
bool changeSameFootprints( bool aUseValue);
bool changeAllFootprints();
bool change_1_Module( MODULE* aModule,
const LIB_ID& aNewFootprintFPID,
bool eShowError );
BOARD_COMMIT m_commit;
};
int DIALOG_EXCHANGE_MODULE::m_selectionMode = 0;
2007-08-23 04:28:46 +00:00
DIALOG_EXCHANGE_MODULE::DIALOG_EXCHANGE_MODULE( PCB_EDIT_FRAME* parent, MODULE* Module ) :
DIALOG_EXCHANGE_MODULE_BASE( parent ), m_commit( parent )
2009-06-20 19:09:43 +00:00
{
m_parent = parent;
m_currentModule = Module;
init();
2009-06-20 19:09:43 +00:00
GetSizer()->Fit( this );
GetSizer()->SetSizeHints( this );
Center();
2009-06-20 19:09:43 +00:00
}
2007-08-23 04:28:46 +00:00
int PCB_EDIT_FRAME::InstallExchangeModuleFrame( MODULE* Module )
2009-06-20 19:09:43 +00:00
{
DIALOG_EXCHANGE_MODULE dialog( this, Module );
2007-08-23 04:28:46 +00:00
return dialog.ShowQuasiModal();
2009-06-20 19:09:43 +00:00
}
2007-08-23 04:28:46 +00:00
void DIALOG_EXCHANGE_MODULE::OnQuit( wxCommandEvent& event )
2009-06-20 19:09:43 +00:00
{
m_selectionMode = m_Selection->GetSelection();
Show( false );
EndQuasiModal( wxID_CANCEL );
2009-06-20 19:09:43 +00:00
}
2007-08-23 04:28:46 +00:00
void DIALOG_EXCHANGE_MODULE::init()
2009-06-20 19:09:43 +00:00
{
SetFocus();
2007-08-23 04:28:46 +00:00
m_CurrentFootprintFPID->AppendText( FROM_UTF8( m_currentModule->GetFPID().Format().c_str() ) );
m_NewFootprintFPID->AppendText( FROM_UTF8( m_currentModule->GetFPID().Format().c_str() ) );
m_CmpValue->AppendText( m_currentModule->GetValue() );
m_CmpReference->AppendText( m_currentModule->GetReference() );
m_Selection->SetString( 0, wxString::Format(
_( "Change footprint of '%s'" ),
GetChars( m_currentModule->GetReference() ) ) );
wxString fpname = m_CurrentFootprintFPID->GetValue().AfterLast( ':' );
if( fpname.IsEmpty() ) // Happens for old fp names
fpname = m_CurrentFootprintFPID->GetValue();
m_Selection->SetString( 1, wxString::Format(
_( "Change footprints '%s'" ),
GetChars( fpname.Left( 12 ) ) ) );
m_Selection->SetSelection( m_selectionMode );
2007-08-23 04:28:46 +00:00
2009-06-20 19:09:43 +00:00
// Enable/disable widgets:
wxCommandEvent event;
OnSelectionClicked( event );
}
2009-06-20 19:09:43 +00:00
void DIALOG_EXCHANGE_MODULE::OnOkClick( wxCommandEvent& event )
{
m_selectionMode = m_Selection->GetSelection();
bool result = false;
2009-06-20 19:09:43 +00:00
switch( m_Selection->GetSelection() )
{
case 0:
result = changeCurrentFootprint();
2009-06-20 19:09:43 +00:00
break;
2007-08-23 04:28:46 +00:00
2009-06-20 19:09:43 +00:00
case 1:
result = changeSameFootprints( false );
2009-06-20 19:09:43 +00:00
break;
case 2:
result = changeSameFootprints( true );
2009-06-20 19:09:43 +00:00
break;
case 3:
result = changeAllFootprints();
2009-06-20 19:09:43 +00:00
break;
}
if( result )
{
if( m_parent->GetBoard()->IsElementVisible( RATSNEST_VISIBLE ) )
m_parent->Compile_Ratsnest( NULL, true );
m_parent->GetCanvas()->Refresh();
}
m_commit.Push( wxT( "Changed footprint" ) );
}
2009-06-20 19:09:43 +00:00
void DIALOG_EXCHANGE_MODULE::OnSelectionClicked( wxCommandEvent& event )
{
bool enable = true;
2009-06-20 19:09:43 +00:00
switch( m_Selection->GetSelection() )
{
case 0:
case 1:
case 2:
break;
case 3:
enable = false;
2009-06-20 19:09:43 +00:00
break;
}
m_NewFootprintFPID->Enable( enable );
m_Browsebutton->Enable( enable );
}
void DIALOG_EXCHANGE_MODULE::RebuildCmpList( wxCommandEvent& event )
{
wxFileName fn;
wxString msg;
2007-08-23 04:28:46 +00:00
// Build the .cmp file name from the board name
fn = m_parent->GetBoard()->GetFileName();
fn.SetExt( ComponentFileExtension );
2007-08-23 04:28:46 +00:00
if( RecreateCmpFile( m_parent->GetBoard(), fn.GetFullPath() ) )
2007-08-23 04:28:46 +00:00
{
msg.Printf( _( "File '%s' created\n" ),
GetChars( fn.GetFullPath() ) );
2007-08-23 04:28:46 +00:00
}
else
2007-08-23 04:28:46 +00:00
{
msg.Printf( _( "** Could not create file '%s' ***\n" ),
GetChars( fn.GetFullPath() ) );
2007-08-23 04:28:46 +00:00
}
m_WinMessages->AppendText( msg );
}
bool DIALOG_EXCHANGE_MODULE::changeCurrentFootprint()
{
wxString newmodulename = m_NewFootprintFPID->GetValue();
2007-08-23 04:28:46 +00:00
if( newmodulename == wxEmptyString )
return false;
2009-08-06 07:11:04 +00:00
return change_1_Module( m_currentModule, newmodulename, true );
}
2007-08-23 04:28:46 +00:00
bool DIALOG_EXCHANGE_MODULE::changeSameFootprints( bool aUseValue )
{
2007-08-23 04:28:46 +00:00
wxString msg;
MODULE* Module;
MODULE* PtBack;
2009-08-06 07:11:04 +00:00
bool change = false;
wxString newmodulename = m_NewFootprintFPID->GetValue();
wxString value;
LIB_ID lib_reference;
2009-08-06 07:11:04 +00:00
bool check_module_value = false;
int ShowErr = 3; // Post 3 error messages max.
2007-08-23 04:28:46 +00:00
if( m_parent->GetBoard()->m_Modules == NULL )
return false;
2007-08-23 04:28:46 +00:00
if( newmodulename == wxEmptyString )
return false;
2007-08-23 04:28:46 +00:00
lib_reference = m_currentModule->GetFPID();
2009-06-20 19:09:43 +00:00
if( aUseValue )
2007-08-23 04:28:46 +00:00
{
check_module_value = true;
value = m_currentModule->GetValue();
msg.Printf( _( "Change footprint %s -> %s (for value = %s)?" ),
GetChars( FROM_UTF8( m_currentModule->GetFPID().Format().c_str() ) ),
GetChars( newmodulename ),
GetChars( m_currentModule->GetValue() ) );
2007-08-23 04:28:46 +00:00
}
else
{
msg.Printf( _( "Change footprint %s -> %s ?" ),
GetChars( FROM_UTF8( lib_reference.Format().c_str() ) ),
GetChars( newmodulename ) );
2007-08-23 04:28:46 +00:00
}
if( !IsOK( this, msg ) )
return false;
2007-08-23 04:28:46 +00:00
/* The change is done from the last module because
* change_1_Module () modifies the last item in the list.
*
* note: for the first module in chain (the last here), Module->Back()
* points the board or is NULL
2009-06-20 19:09:43 +00:00
*/
Module = m_parent->GetBoard()->m_Modules.GetLast();
for( ; Module && ( Module->Type() == PCB_MODULE_T ); Module = PtBack )
2007-08-23 04:28:46 +00:00
{
PtBack = Module->Back();
if( lib_reference != Module->GetFPID() )
2007-08-23 04:28:46 +00:00
continue;
2007-08-23 04:28:46 +00:00
if( check_module_value )
{
if( value.CmpNoCase( Module->GetValue() ) != 0 )
2007-08-23 04:28:46 +00:00
continue;
}
if( change_1_Module( Module, newmodulename, ShowErr ) )
change = true;
2007-08-23 04:28:46 +00:00
else if( ShowErr )
ShowErr--;
}
return change;
}
2007-08-23 04:28:46 +00:00
bool DIALOG_EXCHANGE_MODULE::changeAllFootprints()
{
MODULE* Module, * PtBack;
2009-08-06 07:11:04 +00:00
bool change = false;
int ShowErr = 3; // Post 3 error max.
2007-08-23 04:28:46 +00:00
if( m_parent->GetBoard()->m_Modules == NULL )
return false;
2007-08-23 04:28:46 +00:00
if( !IsOK( this, _( "Are you sure you want to change all footprints?" ) ) )
return false;
2007-08-23 04:28:46 +00:00
/* The change is done from the last module because the function
* change_1_Module () modifies the last module in the list
*
* note: for the first module in chain (the last here), Module->Back()
* points the board or is NULL
2009-06-20 19:09:43 +00:00
*/
Module = m_parent->GetBoard()->m_Modules.GetLast();
for( ; Module && ( Module->Type() == PCB_MODULE_T ); Module = PtBack )
2007-08-23 04:28:46 +00:00
{
PtBack = Module->Back();
if( change_1_Module( Module, Module->GetFPID(), ShowErr ) )
change = true;
2007-08-23 04:28:46 +00:00
else if( ShowErr )
ShowErr--;
}
return change;
}
2007-08-23 04:28:46 +00:00
bool DIALOG_EXCHANGE_MODULE::change_1_Module( MODULE* aModule,
const LIB_ID& aNewFootprintFPID,
bool aShowError )
{
MODULE* newModule;
wxString line;
if( aModule == NULL )
2009-08-06 07:11:04 +00:00
return false;
2007-08-23 04:28:46 +00:00
wxBusyCursor dummy;
// Copy parameters from the old module.
LIB_ID oldFootprintFPID = aModule->GetFPID();
// Load module.
line.Printf( _( "Change footprint '%s' (from '%s') to '%s'" ),
GetChars( aModule->GetReference() ),
oldFootprintFPID.Format().c_str(),
aNewFootprintFPID.Format().c_str() );
m_WinMessages->AppendText( line );
newModule = m_parent->LoadFootprint( aNewFootprintFPID );
if( newModule == NULL ) // New module not found, redraw the old one.
2007-08-23 04:28:46 +00:00
{
m_WinMessages->AppendText( wxT( " No\n" ) );
2009-08-06 07:11:04 +00:00
return false;
2007-08-23 04:28:46 +00:00
}
m_parent->Exchange_Module( aModule, newModule, m_commit );
if( aModule == m_currentModule )
m_currentModule = newModule;
m_WinMessages->AppendText( wxT( " OK\n" ) );
2009-08-06 07:11:04 +00:00
return true;
}
2007-08-23 04:28:46 +00:00
void PCB_EDIT_FRAME::Exchange_Module( MODULE* aOldModule,
MODULE* aNewModule,
BOARD_COMMIT& aCommit )
{
2009-08-06 07:11:04 +00:00
aNewModule->SetParent( GetBoard() );
2007-08-23 04:28:46 +00:00
/* place module without ratsnest refresh: this will be made later
* when all modules are on board */
PlaceModule( aNewModule, NULL, true );
2007-08-23 04:28:46 +00:00
// Copy full placement and pad net names (when possible)
// but not local settings like clearances (use library values)
aOldModule->CopyNetlistSettings( aNewModule, false );
2007-08-23 04:28:46 +00:00
// Copy reference and value
aNewModule->SetReference( aOldModule->GetReference() );
aNewModule->SetValue( aOldModule->GetValue() );
2007-08-23 04:28:46 +00:00
// Updating other parameters
2011-12-12 08:37:05 +00:00
aNewModule->SetTimeStamp( aOldModule->GetTimeStamp() );
aNewModule->SetPath( aOldModule->GetPath() );
2007-08-23 04:28:46 +00:00
aCommit.Remove( aOldModule );
aCommit.Add( aNewModule );
2007-08-23 04:28:46 +00:00
// @todo LEGACY should be unnecessary
GetBoard()->m_Status_Pcb = 0;
aNewModule->ClearFlags();
}
// Displays the list of available footprints in library name and select a footprint.
2009-06-20 19:09:43 +00:00
void DIALOG_EXCHANGE_MODULE::BrowseAndSelectFootprint( wxCommandEvent& event )
{
2007-08-23 04:28:46 +00:00
wxString newname;
newname = m_parent->SelectFootprint( m_parent, wxEmptyString, wxEmptyString, wxEmptyString,
Prj().PcbFootprintLibs() );
2007-08-23 04:28:46 +00:00
if( newname != wxEmptyString )
m_NewFootprintFPID->SetValue( newname );
}
void DIALOG_EXCHANGE_MODULE::ViewAndSelectFootprint( wxCommandEvent& event )
{
wxString newname;
KIWAY_PLAYER* frame = Kiway().Player( FRAME_PCB_MODULE_VIEWER_MODAL, true );
if( frame->ShowModal( &newname, this ) )
{
m_NewFootprintFPID->SetValue( newname );
}
frame->Destroy();
}
void PCB_EDIT_FRAME::RecreateCmpFileFromBoard( wxCommandEvent& aEvent )
{
wxFileName fn;
MODULE* module = GetBoard()->m_Modules;
wxString msg;
wxString wildcard;
2007-08-23 04:28:46 +00:00
if( module == NULL )
2007-08-23 04:28:46 +00:00
{
DisplayError( this, _( "No footprints!" ) );
return;
2007-08-23 04:28:46 +00:00
}
// Build the .cmp file name from the board name
2012-08-29 16:59:50 +00:00
fn = GetBoard()->GetFileName();
fn.SetExt( ComponentFileExtension );
wildcard = wxGetTranslation( ComponentFileWildcard );
wxString pro_dir = wxPathOnly( Prj().GetProjectFullName() );
wxFileDialog dlg( this, _( "Save Footprint Association File" ), pro_dir,
fn.GetFullName(), wildcard,
wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
if( dlg.ShowModal() == wxID_CANCEL )
return;
2007-08-23 04:28:46 +00:00
fn = dlg.GetPath();
2007-08-23 04:28:46 +00:00
if( ! RecreateCmpFile( GetBoard(), fn.GetFullPath() ) )
2007-08-23 04:28:46 +00:00
{
msg.Printf( _( "Could not create file '%s'" ), GetChars(fn.GetFullPath() ) );
2007-08-23 04:28:46 +00:00
DisplayError( this, msg );
return;
2007-08-23 04:28:46 +00:00
}
}
2007-08-23 04:28:46 +00:00
bool RecreateCmpFile( BOARD * aBrd, const wxString& aFullCmpFileName )
{
FILE* cmpFile;
cmpFile = wxFopen( aFullCmpFileName, wxT( "wt" ) );
2007-08-23 04:28:46 +00:00
if( cmpFile == NULL )
return false;
fprintf( cmpFile, "Cmp-Mod V01 Created by PcbNew date = %s\n", TO_UTF8( DateAndTime() ) );
MODULE* module = aBrd->m_Modules;
for( ; module != NULL; module = module->Next() )
2007-08-23 04:28:46 +00:00
{
fprintf( cmpFile, "\nBeginCmp\n" );
fprintf( cmpFile, "TimeStamp = %8.8lX\n", (unsigned long)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" );
2007-08-23 04:28:46 +00:00
}
fprintf( cmpFile, "\nEndListe\n" );
fclose( cmpFile );
return true;
}