2017-03-09 23:33:39 +00:00
|
|
|
/**
|
|
|
|
* @file dialog_symbol_remap.cpp
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2021-02-24 13:48:02 +00:00
|
|
|
* Copyright (C) 2017 Wayne Stambaugh <stambaughw@gmail.com>
|
2022-08-14 17:36:40 +00:00
|
|
|
* Copyright (C) 2017-2022 KiCad Developers, see AUTHORS.txt for contributors.
|
2017-03-09 23:33:39 +00:00
|
|
|
*
|
|
|
|
* 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 3 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <macros.h>
|
|
|
|
#include <pgm_base.h>
|
2021-09-14 22:45:14 +00:00
|
|
|
#include <kiface_base.h>
|
2017-03-09 23:33:39 +00:00
|
|
|
#include <project.h>
|
|
|
|
#include <confirm.h>
|
|
|
|
#include <reporter.h>
|
2017-11-24 16:43:16 +00:00
|
|
|
#include <wildcards_and_files_ext.h>
|
2022-08-14 17:36:40 +00:00
|
|
|
#include <wx_filename.h>
|
2022-09-03 18:29:02 +00:00
|
|
|
#include "widgets/wx_html_report_panel.h"
|
2017-03-09 23:33:39 +00:00
|
|
|
|
2021-06-15 13:24:55 +00:00
|
|
|
#include <symbol_library.h>
|
2020-10-24 12:53:11 +00:00
|
|
|
#include <core/kicad_algo.h>
|
2020-12-25 23:37:01 +00:00
|
|
|
#include <symbol_viewer_frame.h>
|
2019-02-05 17:14:27 +00:00
|
|
|
#include <project_rescue.h>
|
2017-03-09 23:33:39 +00:00
|
|
|
#include <sch_io_mgr.h>
|
2021-02-24 13:48:02 +00:00
|
|
|
#include <sch_symbol.h>
|
2018-01-30 08:56:43 +00:00
|
|
|
#include <sch_screen.h>
|
2018-01-30 10:49:51 +00:00
|
|
|
#include <sch_edit_frame.h>
|
2020-05-13 02:00:37 +00:00
|
|
|
#include <schematic.h>
|
2020-06-08 02:19:46 +00:00
|
|
|
#include <settings/settings_manager.h>
|
2017-03-09 23:33:39 +00:00
|
|
|
#include <symbol_lib_table.h>
|
2017-11-14 10:17:16 +00:00
|
|
|
#include <env_paths.h>
|
2023-09-28 03:04:53 +00:00
|
|
|
#include <project_sch.h>
|
2017-03-09 23:33:39 +00:00
|
|
|
|
|
|
|
#include <dialog_symbol_remap.h>
|
|
|
|
|
|
|
|
|
2017-11-01 00:57:57 +00:00
|
|
|
DIALOG_SYMBOL_REMAP::DIALOG_SYMBOL_REMAP( SCH_EDIT_FRAME* aParent ) :
|
2020-05-13 02:00:37 +00:00
|
|
|
DIALOG_SYMBOL_REMAP_BASE( aParent ),
|
|
|
|
m_frame( aParent )
|
2017-03-09 23:33:39 +00:00
|
|
|
{
|
2017-12-18 23:25:34 +00:00
|
|
|
m_remapped = false;
|
|
|
|
|
2018-01-03 16:25:19 +00:00
|
|
|
if( !wxFileName::IsDirWritable( Prj().GetProjectPath() ) )
|
|
|
|
{
|
2021-06-28 23:44:07 +00:00
|
|
|
DisplayInfoMessage( this, _( "Remapping is not possible because you have insufficient "
|
|
|
|
"privileges to the project folder '%s'." ) );
|
2018-01-03 16:25:19 +00:00
|
|
|
|
|
|
|
// Disable the remap button.
|
|
|
|
m_remapped = true;
|
|
|
|
}
|
|
|
|
|
2017-11-24 16:43:16 +00:00
|
|
|
wxString text;
|
|
|
|
|
2018-01-03 16:25:19 +00:00
|
|
|
text = _( "This schematic currently uses the project symbol library list look up method "
|
|
|
|
"for loading library symbols. KiCad will attempt to map the existing symbols "
|
|
|
|
"to use the new symbol library table. Remapping will change some project files "
|
|
|
|
"and schematics may not be compatible with older versions of KiCad. All files "
|
2023-09-14 23:40:57 +00:00
|
|
|
"that are changed will be backed up to the \"rescue-backup\" folder in the project "
|
2018-01-03 16:25:19 +00:00
|
|
|
"folder should you need to revert any changes. If you choose to skip this step, "
|
|
|
|
"you will be responsible for manually remapping the symbols." );
|
2017-11-24 16:43:16 +00:00
|
|
|
|
|
|
|
m_htmlCtrl->AppendToPage( text );
|
2021-02-25 15:05:26 +00:00
|
|
|
|
|
|
|
m_messagePanel->SetFileName( Prj().GetProjectPath() + wxT( "report.txt" ) );
|
2017-03-09 23:33:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DIALOG_SYMBOL_REMAP::OnRemapSymbols( wxCommandEvent& aEvent )
|
|
|
|
{
|
2018-01-03 13:37:54 +00:00
|
|
|
SCH_EDIT_FRAME* parent = dynamic_cast< SCH_EDIT_FRAME* >( GetParent() );
|
|
|
|
|
|
|
|
wxCHECK_RET( parent != nullptr, "Parent window is not type SCH_EDIT_FRAME." );
|
|
|
|
|
2018-01-12 15:13:41 +00:00
|
|
|
if( !backupProject( m_messagePanel->Reporter() ) )
|
|
|
|
return;
|
2017-11-24 16:43:16 +00:00
|
|
|
|
2018-01-03 13:37:54 +00:00
|
|
|
// Ignore the never show rescue setting for one last rescue of legacy symbol
|
|
|
|
// libraries before remapping to the symbol library table. This ensures the
|
|
|
|
// best remapping results.
|
2022-09-10 08:30:35 +00:00
|
|
|
|
2020-05-13 02:00:37 +00:00
|
|
|
LEGACY_RESCUER rescuer( Prj(), &parent->Schematic(), &parent->GetCurrentSheet(),
|
2022-09-10 08:30:35 +00:00
|
|
|
parent->GetCanvas()->GetBackend() );
|
2019-02-05 17:14:27 +00:00
|
|
|
|
|
|
|
if( RESCUER::RescueProject( this, rescuer, false ) )
|
|
|
|
{
|
2022-09-10 08:30:35 +00:00
|
|
|
wxBusyCursor busy;
|
|
|
|
|
2020-12-25 23:37:01 +00:00
|
|
|
auto viewer = (SYMBOL_VIEWER_FRAME*) parent->Kiway().Player( FRAME_SCH_VIEWER, false );
|
2019-02-05 17:14:27 +00:00
|
|
|
|
|
|
|
if( viewer )
|
2020-12-17 23:56:47 +00:00
|
|
|
viewer->ReCreateLibList();
|
2019-02-05 17:14:27 +00:00
|
|
|
|
2020-07-13 13:50:35 +00:00
|
|
|
parent->ClearUndoORRedoList( EDA_BASE_FRAME::UNDO_LIST, 1 );
|
2019-02-05 17:14:27 +00:00
|
|
|
parent->SyncView();
|
|
|
|
parent->GetCanvas()->Refresh();
|
|
|
|
parent->OnModify();
|
|
|
|
}
|
2018-01-03 13:37:54 +00:00
|
|
|
|
2017-03-09 23:33:39 +00:00
|
|
|
// The schematic is fully loaded, any legacy library symbols have been rescued. Now
|
|
|
|
// check to see if the schematic has not been converted to the symbol library table
|
|
|
|
// method for looking up symbols.
|
2022-09-10 08:30:35 +00:00
|
|
|
|
2017-03-09 23:33:39 +00:00
|
|
|
wxFileName prjSymLibTableFileName( Prj().GetProjectPath(),
|
|
|
|
SYMBOL_LIB_TABLE::GetSymbolLibTableFileName() );
|
|
|
|
|
2018-01-12 15:13:41 +00:00
|
|
|
// Delete the existing project symbol library table.
|
|
|
|
if( prjSymLibTableFileName.FileExists() )
|
|
|
|
wxRemoveFile( prjSymLibTableFileName.GetFullPath() );
|
2017-03-09 23:33:39 +00:00
|
|
|
|
2018-01-12 15:13:41 +00:00
|
|
|
createProjectSymbolLibTable( m_messagePanel->Reporter() );
|
2021-07-16 20:13:26 +00:00
|
|
|
Prj().SetElem( PROJECT::ELEM_SYMBOL_LIB_TABLE, nullptr );
|
2023-09-28 03:04:53 +00:00
|
|
|
PROJECT_SCH::SchSymbolLibTable( &Prj() );
|
2018-01-12 15:13:41 +00:00
|
|
|
|
2017-03-09 23:33:39 +00:00
|
|
|
remapSymbolsToLibTable( m_messagePanel->Reporter() );
|
2017-11-01 00:57:57 +00:00
|
|
|
|
|
|
|
// Remove all of the libraries from the legacy library list.
|
2017-11-08 00:15:09 +00:00
|
|
|
wxString paths;
|
|
|
|
wxArrayString libNames;
|
|
|
|
|
2022-10-11 10:21:42 +00:00
|
|
|
SYMBOL_LIBS::SetLibNamesAndPaths( &Prj(), paths, libNames );
|
2017-11-08 00:15:09 +00:00
|
|
|
|
2021-06-09 19:32:58 +00:00
|
|
|
// Reload the cache symbol library.
|
2021-07-16 20:13:26 +00:00
|
|
|
Prj().SetElem( PROJECT::ELEM_SCH_SYMBOL_LIBS, nullptr );
|
2023-09-28 03:04:53 +00:00
|
|
|
PROJECT_SCH::SchLibs( &Prj() );
|
2017-12-18 23:25:34 +00:00
|
|
|
|
2018-01-06 21:51:53 +00:00
|
|
|
Raise();
|
2017-12-18 23:25:34 +00:00
|
|
|
m_remapped = true;
|
2017-03-09 23:33:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
size_t DIALOG_SYMBOL_REMAP::getLibsNotInGlobalSymbolLibTable( std::vector< SYMBOL_LIB* >& aLibs )
|
2017-03-09 23:33:39 +00:00
|
|
|
{
|
2023-09-28 03:04:53 +00:00
|
|
|
SYMBOL_LIBS* libs = PROJECT_SCH::SchLibs( &Prj() );
|
2017-03-09 23:33:39 +00:00
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
for( SYMBOL_LIBS_BASE::iterator it = libs->begin(); it != libs->end(); ++it )
|
2017-03-09 23:33:39 +00:00
|
|
|
{
|
|
|
|
// Ignore the cache library.
|
|
|
|
if( it->IsCache() )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Check for the obvious library name.
|
|
|
|
wxString libFileName = it->GetFullFileName();
|
|
|
|
|
|
|
|
if( !SYMBOL_LIB_TABLE::GetGlobalLibTable().FindRowByURI( libFileName ) )
|
|
|
|
aLibs.push_back( &(*it) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return aLibs.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DIALOG_SYMBOL_REMAP::createProjectSymbolLibTable( REPORTER& aReporter )
|
|
|
|
{
|
2022-10-11 10:01:33 +00:00
|
|
|
std::vector<SYMBOL_LIB*> libs;
|
2017-03-09 23:33:39 +00:00
|
|
|
|
|
|
|
if( getLibsNotInGlobalSymbolLibTable( libs ) )
|
|
|
|
{
|
2022-10-11 10:21:42 +00:00
|
|
|
wxBusyCursor busy;
|
2022-10-11 10:01:33 +00:00
|
|
|
SYMBOL_LIB_TABLE libTable;
|
|
|
|
std::vector<wxString> libNames = SYMBOL_LIB_TABLE::GetGlobalLibTable().GetLogicalLibs();
|
|
|
|
wxString msg;
|
2017-03-09 23:33:39 +00:00
|
|
|
|
2022-10-11 10:01:33 +00:00
|
|
|
for( SYMBOL_LIB* lib : libs )
|
2017-03-09 23:33:39 +00:00
|
|
|
{
|
|
|
|
wxString libName = lib->GetName();
|
|
|
|
int libNameInc = 1;
|
|
|
|
int libNameLen = libName.Length();
|
|
|
|
|
2018-01-10 21:04:32 +00:00
|
|
|
// Spaces in the file name will break the symbol name because they are not
|
|
|
|
// quoted in the symbol library file format.
|
2023-01-17 04:14:38 +00:00
|
|
|
libName.Replace( wxS( " " ), wxS( "-" ) );
|
2018-01-10 21:04:32 +00:00
|
|
|
|
2017-03-09 23:33:39 +00:00
|
|
|
// Don't create duplicate table entries.
|
2020-09-26 13:42:40 +00:00
|
|
|
while( alg::contains( libNames, libName ) )
|
2017-03-09 23:33:39 +00:00
|
|
|
{
|
|
|
|
libName = libName.Left( libNameLen );
|
|
|
|
libName << libNameInc;
|
|
|
|
libNameInc++;
|
|
|
|
}
|
|
|
|
|
2022-10-11 10:01:33 +00:00
|
|
|
wxString type = SCH_IO_MGR::ShowType( SCH_IO_MGR::SCH_LEGACY );
|
|
|
|
wxFileName fn( lib->GetFullFileName() );
|
2017-03-09 23:33:39 +00:00
|
|
|
|
|
|
|
// Use environment variable substitution where possible. This is based solely
|
|
|
|
// on the internal user environment variable list. Checking against all of the
|
|
|
|
// system wide environment variables is probably not a good idea.
|
2022-10-11 10:01:33 +00:00
|
|
|
wxString normalizedPath = NormalizePath( fn, &Pgm().GetLocalEnvVariables(), &Prj() );
|
|
|
|
wxFileName normalizedFn( normalizedPath );
|
2017-03-09 23:33:39 +00:00
|
|
|
|
2017-09-06 13:21:50 +00:00
|
|
|
// Don't add symbol libraries that do not exist.
|
2022-10-11 10:01:33 +00:00
|
|
|
if( normalizedFn.Normalize(FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS )
|
|
|
|
&& normalizedFn.FileExists() )
|
2017-09-06 13:21:50 +00:00
|
|
|
{
|
2021-06-16 22:35:00 +00:00
|
|
|
msg.Printf( _( "Adding library '%s', file '%s' to project symbol library table." ),
|
|
|
|
libName,
|
2022-10-11 10:01:33 +00:00
|
|
|
normalizedPath );
|
2020-03-04 09:48:18 +00:00
|
|
|
aReporter.Report( msg, RPT_SEVERITY_INFO );
|
2017-03-09 23:33:39 +00:00
|
|
|
|
2022-10-11 10:01:33 +00:00
|
|
|
libTable.InsertRow( new SYMBOL_LIB_TABLE_ROW( libName, normalizedPath, type ) );
|
2017-09-06 13:21:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-10-11 10:01:33 +00:00
|
|
|
msg.Printf( _( "Library '%s' not found." ), normalizedPath );
|
2020-03-04 09:48:18 +00:00
|
|
|
aReporter.Report( msg, RPT_SEVERITY_WARNING );
|
2017-09-06 13:21:50 +00:00
|
|
|
}
|
2017-03-09 23:33:39 +00:00
|
|
|
}
|
2017-09-06 13:21:50 +00:00
|
|
|
|
|
|
|
// Don't save empty project symbol library table.
|
2022-10-11 10:01:33 +00:00
|
|
|
if( !libTable.IsEmpty() )
|
2017-03-09 23:33:39 +00:00
|
|
|
{
|
2017-09-06 13:21:50 +00:00
|
|
|
wxFileName fn( Prj().GetProjectPath(), SYMBOL_LIB_TABLE::GetSymbolLibTableFileName() );
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
FILE_OUTPUTFORMATTER formatter( fn.GetFullPath() );
|
2022-10-11 10:01:33 +00:00
|
|
|
libTable.Format( &formatter, 0 );
|
2017-09-06 13:21:50 +00:00
|
|
|
}
|
|
|
|
catch( const IO_ERROR& ioe )
|
|
|
|
{
|
2022-10-11 10:01:33 +00:00
|
|
|
msg.Printf( _( "Error writing project symbol library table.\n %s" ), ioe.What() );
|
2020-03-04 09:48:18 +00:00
|
|
|
aReporter.ReportTail( msg, RPT_SEVERITY_ERROR );
|
2017-09-06 13:21:50 +00:00
|
|
|
}
|
|
|
|
|
2021-06-10 14:10:55 +00:00
|
|
|
aReporter.ReportTail( _( "Created project symbol library table.\n" ),
|
|
|
|
RPT_SEVERITY_INFO );
|
2017-03-09 23:33:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DIALOG_SYMBOL_REMAP::remapSymbolsToLibTable( REPORTER& aReporter )
|
|
|
|
{
|
2022-10-11 10:21:42 +00:00
|
|
|
wxBusyCursor busy;
|
|
|
|
wxString msg;
|
|
|
|
SCH_SCREENS schematic( m_frame->Schematic().Root() );
|
|
|
|
SCH_SYMBOL* symbol;
|
|
|
|
SCH_SCREEN* screen;
|
2017-03-09 23:33:39 +00:00
|
|
|
|
|
|
|
for( screen = schematic.GetFirst(); screen; screen = schematic.GetNext() )
|
|
|
|
{
|
2022-07-29 21:02:35 +00:00
|
|
|
for( EDA_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
|
2017-03-09 23:33:39 +00:00
|
|
|
{
|
2021-06-10 14:10:55 +00:00
|
|
|
symbol = dynamic_cast<SCH_SYMBOL*>( item );
|
2017-03-09 23:33:39 +00:00
|
|
|
|
2022-10-17 13:19:39 +00:00
|
|
|
wxCHECK2( symbol, continue );
|
|
|
|
|
2017-03-09 23:33:39 +00:00
|
|
|
if( !remapSymbolToLibTable( symbol ) )
|
|
|
|
{
|
2021-06-16 22:35:00 +00:00
|
|
|
msg.Printf( _( "No symbol %s found in symbol library table." ),
|
2020-02-28 00:05:40 +00:00
|
|
|
symbol->GetLibId().GetLibItemName().wx_str() );
|
2020-03-04 09:48:18 +00:00
|
|
|
aReporter.Report( msg, RPT_SEVERITY_WARNING );
|
2017-03-09 23:33:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-06-16 22:35:00 +00:00
|
|
|
msg.Printf( _( "Symbol %s mapped to symbol library '%s'." ),
|
2020-02-28 00:05:40 +00:00
|
|
|
symbol->GetLibId().GetLibItemName().wx_str(),
|
|
|
|
symbol->GetLibId().GetLibNickname().wx_str() );
|
2020-03-04 09:48:18 +00:00
|
|
|
aReporter.Report( msg, RPT_SEVERITY_ACTION );
|
2021-05-28 19:07:04 +00:00
|
|
|
screen->SetContentModified();
|
2017-03-09 23:33:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-01 20:42:20 +00:00
|
|
|
|
2020-03-04 09:48:18 +00:00
|
|
|
aReporter.Report( _( "Symbol library table mapping complete!" ), RPT_SEVERITY_INFO );
|
2020-04-16 16:43:50 +00:00
|
|
|
schematic.UpdateSymbolLinks();
|
2017-03-09 23:33:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-10 14:10:55 +00:00
|
|
|
bool DIALOG_SYMBOL_REMAP::remapSymbolToLibTable( SCH_SYMBOL* aSymbol )
|
2017-03-09 23:33:39 +00:00
|
|
|
{
|
2021-07-16 20:13:26 +00:00
|
|
|
wxCHECK_MSG( aSymbol != nullptr, false, "Null pointer passed to remapSymbolToLibTable." );
|
2017-04-05 12:43:05 +00:00
|
|
|
wxCHECK_MSG( aSymbol->GetLibId().GetLibNickname().empty(), false,
|
2017-03-09 23:33:39 +00:00
|
|
|
"Cannot remap symbol that is already mapped." );
|
2017-04-05 12:43:05 +00:00
|
|
|
wxCHECK_MSG( !aSymbol->GetLibId().GetLibItemName().empty(), false,
|
2017-03-09 23:33:39 +00:00
|
|
|
"The symbol LIB_ID name is empty." );
|
|
|
|
|
2023-09-28 03:04:53 +00:00
|
|
|
SYMBOL_LIBS* libs = PROJECT_SCH::SchLibs( &Prj() );
|
2017-03-09 23:33:39 +00:00
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
for( SYMBOL_LIBS_BASE::iterator it = libs->begin(); it != libs->end(); ++it )
|
2017-03-09 23:33:39 +00:00
|
|
|
{
|
|
|
|
// Ignore the cache library.
|
|
|
|
if( it->IsCache() )
|
|
|
|
continue;
|
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
LIB_SYMBOL* alias = it->FindSymbol( aSymbol->GetLibId().GetLibItemName().wx_str() );
|
2017-03-09 23:33:39 +00:00
|
|
|
|
|
|
|
// Found in the same library as the old look up method assuming the user didn't
|
|
|
|
// change the libraries or library ordering since the last time the schematic was
|
|
|
|
// loaded.
|
|
|
|
if( alias )
|
|
|
|
{
|
|
|
|
// Find the same library in the symbol library table using the full path and file name.
|
|
|
|
wxString libFileName = it->GetFullFileName();
|
|
|
|
|
2023-09-28 03:04:53 +00:00
|
|
|
const LIB_TABLE_ROW* row = PROJECT_SCH::SchSymbolLibTable( &Prj() )->FindRowByURI( libFileName );
|
2017-03-09 23:33:39 +00:00
|
|
|
|
|
|
|
if( row )
|
|
|
|
{
|
|
|
|
LIB_ID id = aSymbol->GetLibId();
|
|
|
|
|
|
|
|
id.SetLibNickname( row->GetNickName() );
|
2017-09-01 20:42:20 +00:00
|
|
|
|
|
|
|
// Don't resolve symbol library links now.
|
2020-04-16 16:43:50 +00:00
|
|
|
aSymbol->SetLibId( id );
|
2017-03-09 23:33:39 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2017-11-24 16:43:16 +00:00
|
|
|
|
|
|
|
|
2018-01-12 15:13:41 +00:00
|
|
|
bool DIALOG_SYMBOL_REMAP::backupProject( REPORTER& aReporter )
|
2017-11-24 16:43:16 +00:00
|
|
|
{
|
2018-02-28 16:41:24 +00:00
|
|
|
static wxString backupFolder = "rescue-backup";
|
2017-11-24 16:43:16 +00:00
|
|
|
|
2018-01-03 16:25:19 +00:00
|
|
|
wxString tmp;
|
2017-11-24 16:43:16 +00:00
|
|
|
wxString errorMsg;
|
2018-01-12 15:13:41 +00:00
|
|
|
wxFileName srcFileName;
|
2017-11-24 16:43:16 +00:00
|
|
|
wxFileName destFileName;
|
2018-02-28 16:41:24 +00:00
|
|
|
wxFileName backupPath;
|
2020-05-13 02:00:37 +00:00
|
|
|
SCH_SCREENS schematic( m_frame->Schematic().Root() );
|
2017-11-24 16:43:16 +00:00
|
|
|
|
2018-01-03 16:25:19 +00:00
|
|
|
// Copy backup files to different folder so as not to pollute the project folder.
|
|
|
|
destFileName.SetPath( Prj().GetProjectPath() );
|
2018-02-28 16:41:24 +00:00
|
|
|
destFileName.AppendDir( backupFolder );
|
|
|
|
backupPath = destFileName;
|
2018-01-03 16:25:19 +00:00
|
|
|
|
|
|
|
if( !destFileName.DirExists() )
|
|
|
|
{
|
|
|
|
if( !destFileName.Mkdir() )
|
|
|
|
{
|
2021-06-16 22:35:00 +00:00
|
|
|
errorMsg.Printf( _( "Cannot create project remap back up folder '%s'." ),
|
2018-01-03 16:25:19 +00:00
|
|
|
destFileName.GetPath() );
|
2018-01-12 15:13:41 +00:00
|
|
|
|
|
|
|
wxMessageDialog dlg( this, errorMsg, _( "Backup Error" ),
|
|
|
|
wxYES_NO | wxCENTRE | wxRESIZE_BORDER | wxICON_QUESTION );
|
|
|
|
dlg.SetYesNoLabels( wxMessageDialog::ButtonLabel( _( "Continue with Rescue" ) ),
|
|
|
|
wxMessageDialog::ButtonLabel( _( "Abort Rescue" ) ) );
|
|
|
|
|
|
|
|
if( dlg.ShowModal() == wxID_NO )
|
|
|
|
return false;
|
2018-01-03 16:25:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-12 15:13:41 +00:00
|
|
|
{
|
2022-09-10 08:30:35 +00:00
|
|
|
wxBusyCursor busy;
|
2018-02-28 16:41:24 +00:00
|
|
|
|
2022-09-10 08:30:35 +00:00
|
|
|
// Time stamp to append to file name in case multiple remappings are performed.
|
|
|
|
wxString timeStamp = wxDateTime::Now().Format( "-%Y-%m-%d-%H-%M-%S" );
|
2018-02-28 16:41:24 +00:00
|
|
|
|
2022-09-10 08:30:35 +00:00
|
|
|
// Back up symbol library table.
|
|
|
|
srcFileName.SetPath( Prj().GetProjectPath() );
|
|
|
|
srcFileName.SetName( SYMBOL_LIB_TABLE::GetSymbolLibTableFileName() );
|
|
|
|
destFileName = srcFileName;
|
|
|
|
destFileName.AppendDir( backupFolder );
|
|
|
|
destFileName.SetName( destFileName.GetName() + timeStamp );
|
2017-11-24 16:43:16 +00:00
|
|
|
|
2021-06-16 22:35:00 +00:00
|
|
|
tmp.Printf( _( "Backing up file '%s' to '%s'." ),
|
2022-09-10 08:30:35 +00:00
|
|
|
srcFileName.GetFullPath(),
|
2021-06-16 22:35:00 +00:00
|
|
|
destFileName.GetFullPath() );
|
2020-03-04 09:48:18 +00:00
|
|
|
aReporter.Report( tmp, RPT_SEVERITY_INFO );
|
2017-11-24 16:43:16 +00:00
|
|
|
|
2022-09-10 08:30:35 +00:00
|
|
|
if( wxFileName::Exists( srcFileName.GetFullPath() )
|
|
|
|
&& !wxCopyFile( srcFileName.GetFullPath(), destFileName.GetFullPath() ) )
|
2018-02-28 16:41:24 +00:00
|
|
|
{
|
2022-09-10 08:30:35 +00:00
|
|
|
tmp.Printf( _( "Failed to back up file '%s'.\n" ),
|
|
|
|
srcFileName.GetFullPath() );
|
2018-02-28 16:41:24 +00:00
|
|
|
errorMsg += tmp;
|
|
|
|
}
|
|
|
|
|
2022-09-10 08:30:35 +00:00
|
|
|
// Back up the schematic files.
|
|
|
|
for( SCH_SCREEN* screen = schematic.GetFirst(); screen; screen = schematic.GetNext() )
|
2017-11-24 16:43:16 +00:00
|
|
|
{
|
2022-09-10 08:30:35 +00:00
|
|
|
destFileName = screen->GetFileName();
|
|
|
|
destFileName.SetName( destFileName.GetName() + timeStamp );
|
|
|
|
|
|
|
|
// Check for nest hierarchical schematic paths.
|
|
|
|
if( destFileName.GetPath() != backupPath.GetPath() )
|
|
|
|
{
|
|
|
|
destFileName.SetPath( backupPath.GetPath() );
|
|
|
|
|
|
|
|
wxArrayString srcDirs = wxFileName( screen->GetFileName() ).GetDirs();
|
|
|
|
wxArrayString destDirs = wxFileName( Prj().GetProjectPath() ).GetDirs();
|
|
|
|
|
|
|
|
for( size_t i = destDirs.GetCount(); i < srcDirs.GetCount(); i++ )
|
|
|
|
destFileName.AppendDir( srcDirs[i] );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
destFileName.AppendDir( backupFolder );
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp.Printf( _( "Backing up file '%s' to '%s'." ),
|
|
|
|
screen->GetFileName(),
|
|
|
|
destFileName.GetFullPath() );
|
|
|
|
aReporter.Report( tmp, RPT_SEVERITY_INFO );
|
|
|
|
|
|
|
|
if( !destFileName.DirExists() && !destFileName.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
|
|
|
|
{
|
|
|
|
tmp.Printf( _( "Failed to create backup folder '%s'.\n" ), destFileName.GetPath() );
|
|
|
|
errorMsg += tmp;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( wxFileName::Exists( screen->GetFileName() )
|
|
|
|
&& !wxCopyFile( screen->GetFileName(), destFileName.GetFullPath() ) )
|
|
|
|
{
|
|
|
|
tmp.Printf( _( "Failed to back up file '%s'.\n" ), screen->GetFileName() );
|
|
|
|
errorMsg += tmp;
|
|
|
|
}
|
2017-11-24 16:43:16 +00:00
|
|
|
}
|
|
|
|
|
2022-09-10 08:30:35 +00:00
|
|
|
// Back up the project file.
|
|
|
|
destFileName = Prj().GetProjectFullName();
|
|
|
|
destFileName.SetName( destFileName.GetName() + timeStamp );
|
|
|
|
destFileName.AppendDir( backupFolder );
|
2017-11-24 16:43:16 +00:00
|
|
|
|
2022-09-10 08:30:35 +00:00
|
|
|
tmp.Printf( _( "Backing up file '%s' to '%s'." ),
|
|
|
|
Prj().GetProjectFullName(),
|
|
|
|
destFileName.GetFullPath() );
|
|
|
|
aReporter.Report( tmp, RPT_SEVERITY_INFO );
|
2017-11-24 16:43:16 +00:00
|
|
|
|
2022-09-10 08:30:35 +00:00
|
|
|
if( wxFileName::Exists( Prj().GetProjectFullName() )
|
|
|
|
&& !wxCopyFile( Prj().GetProjectFullName(), destFileName.GetFullPath() ) )
|
|
|
|
{
|
|
|
|
tmp.Printf( _( "Failed to back up file '%s'.\n" ), Prj().GetProjectFullName() );
|
|
|
|
errorMsg += tmp;
|
|
|
|
}
|
2017-11-24 16:43:16 +00:00
|
|
|
|
2022-09-10 08:30:35 +00:00
|
|
|
// Back up the cache library.
|
|
|
|
srcFileName.SetPath( Prj().GetProjectPath() );
|
2023-01-17 04:14:38 +00:00
|
|
|
srcFileName.SetName( Prj().GetProjectName() + wxS( "-cache" ) );
|
2022-09-10 08:30:35 +00:00
|
|
|
srcFileName.SetExt( LegacySymbolLibFileExtension );
|
2017-11-24 16:43:16 +00:00
|
|
|
|
2022-09-10 08:30:35 +00:00
|
|
|
destFileName = srcFileName;
|
|
|
|
destFileName.SetName( destFileName.GetName() + timeStamp );
|
|
|
|
destFileName.AppendDir( backupFolder );
|
2017-11-24 16:43:16 +00:00
|
|
|
|
2022-09-10 08:30:35 +00:00
|
|
|
tmp.Printf( _( "Backing up file '%s' to '%s'." ),
|
|
|
|
srcFileName.GetFullPath(),
|
|
|
|
destFileName.GetFullPath() );
|
|
|
|
aReporter.Report( tmp, RPT_SEVERITY_INFO );
|
2017-11-24 16:43:16 +00:00
|
|
|
|
2022-09-10 08:30:35 +00:00
|
|
|
if( srcFileName.Exists()
|
|
|
|
&& !wxCopyFile( srcFileName.GetFullPath(), destFileName.GetFullPath() ) )
|
|
|
|
{
|
|
|
|
tmp.Printf( _( "Failed to back up file '%s'.\n" ), srcFileName.GetFullPath() );
|
|
|
|
errorMsg += tmp;
|
|
|
|
}
|
2017-11-24 16:43:16 +00:00
|
|
|
|
2022-09-10 08:30:35 +00:00
|
|
|
// Back up the rescue symbol library if it exists.
|
2023-01-17 04:14:38 +00:00
|
|
|
srcFileName.SetName( Prj().GetProjectName() + wxS( "-rescue" ) );
|
2022-09-10 08:30:35 +00:00
|
|
|
destFileName.SetName( srcFileName.GetName() + timeStamp );
|
2017-11-24 16:43:16 +00:00
|
|
|
|
2022-09-10 08:30:35 +00:00
|
|
|
tmp.Printf( _( "Backing up file '%s' to '%s'." ),
|
|
|
|
srcFileName.GetFullPath(),
|
|
|
|
destFileName.GetFullPath() );
|
|
|
|
aReporter.Report( tmp, RPT_SEVERITY_INFO );
|
2017-11-24 16:43:16 +00:00
|
|
|
|
2022-09-10 08:30:35 +00:00
|
|
|
if( srcFileName.Exists()
|
|
|
|
&& !wxCopyFile( srcFileName.GetFullPath(), destFileName.GetFullPath() ) )
|
|
|
|
{
|
|
|
|
tmp.Printf( _( "Failed to back up file '%s'.\n" ), srcFileName.GetFullPath() );
|
|
|
|
errorMsg += tmp;
|
|
|
|
}
|
2017-11-24 16:43:16 +00:00
|
|
|
|
2022-09-10 08:30:35 +00:00
|
|
|
// Back up the rescue symbol library document file if it exists.
|
|
|
|
srcFileName.SetExt( LegacySymbolDocumentFileExtension );
|
|
|
|
destFileName.SetExt( srcFileName.GetExt() );
|
2017-11-24 16:43:16 +00:00
|
|
|
|
2022-09-10 08:30:35 +00:00
|
|
|
tmp.Printf( _( "Backing up file '%s' to '%s'." ),
|
|
|
|
srcFileName.GetFullPath(),
|
|
|
|
destFileName.GetFullPath() );
|
|
|
|
aReporter.Report( tmp, RPT_SEVERITY_INFO );
|
2017-11-24 16:43:16 +00:00
|
|
|
|
2022-09-10 08:30:35 +00:00
|
|
|
if( srcFileName.Exists()
|
|
|
|
&& !wxCopyFile( srcFileName.GetFullPath(), destFileName.GetFullPath() ) )
|
|
|
|
{
|
|
|
|
tmp.Printf( _( "Failed to back up file '%s'.\n" ), srcFileName.GetFullPath() );
|
|
|
|
errorMsg += tmp;
|
|
|
|
}
|
2017-11-24 16:43:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( !errorMsg.IsEmpty() )
|
|
|
|
{
|
2018-01-12 15:13:41 +00:00
|
|
|
wxMessageDialog dlg( this, _( "Some of the project files could not be backed up." ),
|
|
|
|
_( "Backup Error" ),
|
|
|
|
wxYES_NO | wxCENTRE | wxRESIZE_BORDER | wxICON_QUESTION );
|
2017-11-24 16:43:16 +00:00
|
|
|
errorMsg.Trim();
|
2018-01-12 15:13:41 +00:00
|
|
|
dlg.SetExtendedMessage( errorMsg );
|
|
|
|
dlg.SetYesNoLabels( wxMessageDialog::ButtonLabel( _( "Continue with Rescue" ) ),
|
|
|
|
wxMessageDialog::ButtonLabel( _( "Abort Rescue" ) ) );
|
|
|
|
|
|
|
|
if( dlg.ShowModal() == wxID_NO )
|
|
|
|
return false;
|
2017-11-24 16:43:16 +00:00
|
|
|
}
|
2018-01-12 15:13:41 +00:00
|
|
|
|
|
|
|
return true;
|
2017-11-24 16:43:16 +00:00
|
|
|
}
|
|
|
|
|
2017-12-18 23:25:34 +00:00
|
|
|
|
|
|
|
void DIALOG_SYMBOL_REMAP::OnUpdateUIRemapButton( wxUpdateUIEvent& aEvent )
|
|
|
|
{
|
|
|
|
aEvent.Enable( !m_remapped );
|
|
|
|
}
|