2015-06-22 15:38:54 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2017-07-24 19:02:59 +00:00
|
|
|
* Copyright (C) 2015 Chris Pavlina <pavlina.chris@gmail.com>
|
2023-01-21 15:35:14 +00:00
|
|
|
* Copyright (C) 2015-2023 KiCad Developers, see change_log.txt for contributors.
|
2015-06-22 15:38:54 +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 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
|
|
|
|
*/
|
|
|
|
|
2018-08-03 12:18:26 +00:00
|
|
|
#include <sch_draw_panel.h>
|
2021-06-15 13:24:55 +00:00
|
|
|
#include <symbol_library.h>
|
2015-06-22 15:38:54 +00:00
|
|
|
#include <confirm.h>
|
2019-12-09 19:47:29 +00:00
|
|
|
#include <connection_graph.h>
|
2015-06-22 15:38:54 +00:00
|
|
|
#include <invoke_sch_dialog.h>
|
2017-10-22 00:48:25 +00:00
|
|
|
#include <kiway.h>
|
2020-12-25 23:37:01 +00:00
|
|
|
#include <symbol_viewer_frame.h>
|
2015-06-22 15:38:54 +00:00
|
|
|
#include <project_rescue.h>
|
2023-09-28 03:04:53 +00:00
|
|
|
#include <project_sch.h>
|
2021-02-24 13:48:02 +00:00
|
|
|
#include <sch_symbol.h>
|
2015-06-22 15:38:54 +00:00
|
|
|
#include <sch_sheet.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>
|
2022-05-06 12:49:34 +00:00
|
|
|
#include <string_utils.h>
|
2017-10-22 00:48:25 +00:00
|
|
|
#include <symbol_lib_table.h>
|
2015-06-22 15:38:54 +00:00
|
|
|
#include <wildcards_and_files_ext.h>
|
2023-09-28 03:04:53 +00:00
|
|
|
#include <project_sch.h>
|
2015-06-22 15:38:54 +00:00
|
|
|
|
2015-08-17 23:12:34 +00:00
|
|
|
#include <cctype>
|
2015-06-22 15:38:54 +00:00
|
|
|
#include <map>
|
|
|
|
|
|
|
|
|
2021-06-10 14:10:55 +00:00
|
|
|
typedef std::pair<SCH_SYMBOL*, wxString> SYMBOL_NAME_PAIR;
|
2015-06-22 15:38:54 +00:00
|
|
|
|
|
|
|
|
2021-06-10 14:10:55 +00:00
|
|
|
// Helper sort function, used in getSymbols, to sort a symbol list by lib_id
|
|
|
|
static bool sort_by_libid( const SCH_SYMBOL* ref, SCH_SYMBOL* cmp )
|
2017-06-21 07:32:19 +00:00
|
|
|
{
|
|
|
|
return ref->GetLibId() < cmp->GetLibId();
|
|
|
|
}
|
|
|
|
|
2017-10-22 00:48:25 +00:00
|
|
|
|
2015-06-22 15:38:54 +00:00
|
|
|
/**
|
2017-11-09 23:07:16 +00:00
|
|
|
* Fill a vector with all of the project's symbols, to ease iterating over them.
|
2017-10-22 00:48:25 +00:00
|
|
|
*
|
2021-06-10 14:10:55 +00:00
|
|
|
* The list is sorted by #LIB_ID, therefore symbols using the same library
|
2017-03-09 17:13:03 +00:00
|
|
|
* symbol are grouped, allowing later faster calculations (one library search by group
|
|
|
|
* of symbols)
|
2015-06-22 15:38:54 +00:00
|
|
|
*
|
2021-06-10 14:10:55 +00:00
|
|
|
* @param aSymbols is a vector that will take the symbols.
|
2015-06-22 15:38:54 +00:00
|
|
|
*/
|
2021-06-10 14:10:55 +00:00
|
|
|
static void getSymbols( SCHEMATIC* aSchematic, std::vector<SCH_SYMBOL*>& aSymbols )
|
2015-06-22 15:38:54 +00:00
|
|
|
{
|
2020-05-13 02:00:37 +00:00
|
|
|
SCH_SCREENS screens( aSchematic->Root() );
|
2017-03-09 17:13:03 +00:00
|
|
|
|
|
|
|
// Get the full list
|
2015-06-22 15:38:54 +00:00
|
|
|
for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
|
|
|
|
{
|
2022-07-29 21:02:35 +00:00
|
|
|
for( EDA_ITEM* aItem : screen->Items().OfType( SCH_SYMBOL_T ) )
|
2021-06-10 14:10:55 +00:00
|
|
|
aSymbols.push_back( static_cast<SCH_SYMBOL*>( aItem ) );
|
2015-06-22 15:38:54 +00:00
|
|
|
}
|
2017-03-09 17:13:03 +00:00
|
|
|
|
2021-06-10 14:10:55 +00:00
|
|
|
if( aSymbols.empty() )
|
2017-10-22 00:48:25 +00:00
|
|
|
return;
|
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
// sort aSymbols by lib symbol. symbols will be grouped by same lib symbol.
|
2021-06-10 14:10:55 +00:00
|
|
|
std::sort( aSymbols.begin(), aSymbols.end(), sort_by_libid );
|
2015-06-22 15:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-06-10 14:10:55 +00:00
|
|
|
* Search the libraries for the first symbol with a given name.
|
2015-06-22 15:38:54 +00:00
|
|
|
*
|
|
|
|
* @param aName - name to search for
|
2021-06-15 12:31:28 +00:00
|
|
|
* @param aLibs - the loaded SYMBOL_LIBS
|
2021-06-10 18:51:46 +00:00
|
|
|
* @param aCached - whether we are looking for the cached symbol
|
2015-06-22 15:38:54 +00:00
|
|
|
*/
|
2021-06-15 12:31:28 +00:00
|
|
|
static LIB_SYMBOL* findSymbol( const wxString& aName, SYMBOL_LIBS* aLibs, bool aCached )
|
2015-06-22 15:38:54 +00:00
|
|
|
{
|
2021-07-16 20:13:26 +00:00
|
|
|
LIB_SYMBOL *symbol = nullptr;
|
2015-06-22 15:38:54 +00:00
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
for( SYMBOL_LIB& each_lib : *aLibs )
|
2015-06-22 15:38:54 +00:00
|
|
|
{
|
|
|
|
if( aCached && !each_lib.IsCache() )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if( !aCached && each_lib.IsCache() )
|
|
|
|
continue;
|
|
|
|
|
2021-09-09 14:19:25 +00:00
|
|
|
symbol = each_lib.FindSymbol( aName );
|
2017-10-22 00:48:25 +00:00
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
if( symbol )
|
2015-06-22 15:38:54 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
return symbol;
|
2015-06-22 15:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-13 02:00:37 +00:00
|
|
|
static wxFileName GetRescueLibraryFileName( SCHEMATIC* aSchematic )
|
2015-06-22 15:38:54 +00:00
|
|
|
{
|
2020-05-13 02:00:37 +00:00
|
|
|
wxFileName fn = aSchematic->GetFileName();
|
2017-10-22 00:48:25 +00:00
|
|
|
fn.SetName( fn.GetName() + wxT( "-rescue" ) );
|
2020-05-15 13:25:11 +00:00
|
|
|
fn.SetExt( LegacySymbolLibFileExtension );
|
2017-10-22 00:48:25 +00:00
|
|
|
return fn;
|
|
|
|
}
|
2015-06-22 15:38:54 +00:00
|
|
|
|
|
|
|
|
2017-10-22 00:48:25 +00:00
|
|
|
RESCUE_CASE_CANDIDATE::RESCUE_CASE_CANDIDATE( const wxString& aRequestedName,
|
|
|
|
const wxString& aNewName,
|
2021-06-10 18:51:46 +00:00
|
|
|
LIB_SYMBOL* aLibCandidate,
|
2020-04-02 00:17:25 +00:00
|
|
|
int aUnit,
|
|
|
|
int aConvert )
|
2017-10-22 00:48:25 +00:00
|
|
|
{
|
|
|
|
m_requested_name = aRequestedName;
|
|
|
|
m_new_name = aNewName;
|
|
|
|
m_lib_candidate = aLibCandidate;
|
2020-04-02 00:17:25 +00:00
|
|
|
m_unit = aUnit;
|
|
|
|
m_convert = aConvert;
|
2015-06-22 15:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-22 00:48:25 +00:00
|
|
|
void RESCUE_CASE_CANDIDATE::FindRescues( RESCUER& aRescuer,
|
|
|
|
boost::ptr_vector<RESCUE_CANDIDATE>& aCandidates )
|
2015-06-22 15:38:54 +00:00
|
|
|
{
|
2017-10-22 00:48:25 +00:00
|
|
|
typedef std::map<wxString, RESCUE_CASE_CANDIDATE> candidate_map_t;
|
|
|
|
candidate_map_t candidate_map;
|
2021-06-10 14:10:55 +00:00
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
// Remember the list of symbols is sorted by symbol name.
|
2017-10-22 00:48:25 +00:00
|
|
|
// So a search in libraries is made only once by group
|
2021-06-10 18:51:46 +00:00
|
|
|
LIB_SYMBOL* case_sensitive_match = nullptr;
|
|
|
|
std::vector<LIB_SYMBOL*> case_insensitive_matches;
|
2015-06-22 15:38:54 +00:00
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
wxString symbol_name;
|
|
|
|
wxString last_symbol_name;
|
2017-03-09 17:13:03 +00:00
|
|
|
|
2021-06-10 14:10:55 +00:00
|
|
|
for( SCH_SYMBOL* eachSymbol : *( aRescuer.GetSymbols() ) )
|
2017-10-22 00:48:25 +00:00
|
|
|
{
|
2023-09-14 15:35:19 +00:00
|
|
|
symbol_name = eachSymbol->GetLibId().GetUniStringLibItemName();
|
2017-03-09 17:13:03 +00:00
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
if( last_symbol_name != symbol_name )
|
2015-06-22 15:38:54 +00:00
|
|
|
{
|
2021-06-10 18:51:46 +00:00
|
|
|
// A new symbol name is found (a new group starts here).
|
2017-10-22 00:48:25 +00:00
|
|
|
// Search the symbol names candidates only once for this group:
|
2021-06-10 18:51:46 +00:00
|
|
|
last_symbol_name = symbol_name;
|
2017-10-22 00:48:25 +00:00
|
|
|
case_insensitive_matches.clear();
|
2017-03-09 17:13:03 +00:00
|
|
|
|
2022-10-11 20:16:11 +00:00
|
|
|
LIB_ID id( wxEmptyString, symbol_name );
|
2017-03-09 17:13:03 +00:00
|
|
|
|
2023-09-28 03:04:53 +00:00
|
|
|
case_sensitive_match = PROJECT_SCH::SchLibs( aRescuer.GetPrj() )->FindLibSymbol( id );
|
2017-03-10 09:31:50 +00:00
|
|
|
|
2022-10-11 20:16:11 +00:00
|
|
|
if( case_sensitive_match )
|
|
|
|
continue;
|
|
|
|
|
2020-10-28 13:30:39 +00:00
|
|
|
// If the case sensitive match failed, try a case insensitive match.
|
2023-09-28 03:04:53 +00:00
|
|
|
PROJECT_SCH::SchLibs( aRescuer.GetPrj() )
|
|
|
|
->FindLibraryNearEntries( case_insensitive_matches,
|
2023-01-21 15:35:14 +00:00
|
|
|
symbol_name );
|
2015-06-22 15:38:54 +00:00
|
|
|
|
2022-10-11 20:16:11 +00:00
|
|
|
// If there are not case insensitive matches either, the symbol cannot be rescued.
|
|
|
|
if( !case_insensitive_matches.size() )
|
|
|
|
continue;
|
2017-03-10 09:31:50 +00:00
|
|
|
|
2022-10-11 20:16:11 +00:00
|
|
|
RESCUE_CASE_CANDIDATE candidate( symbol_name, case_insensitive_matches[0]->GetName(),
|
|
|
|
case_insensitive_matches[0],
|
|
|
|
eachSymbol->GetUnit(),
|
|
|
|
eachSymbol->GetConvert() );
|
2015-06-22 15:38:54 +00:00
|
|
|
|
2022-10-11 20:16:11 +00:00
|
|
|
candidate_map[symbol_name] = candidate;
|
|
|
|
}
|
2015-06-22 15:38:54 +00:00
|
|
|
}
|
|
|
|
|
2017-10-22 00:48:25 +00:00
|
|
|
// Now, dump the map into aCandidates
|
|
|
|
for( const candidate_map_t::value_type& each_pair : candidate_map )
|
2015-06-22 15:38:54 +00:00
|
|
|
{
|
2017-10-22 00:48:25 +00:00
|
|
|
aCandidates.push_back( new RESCUE_CASE_CANDIDATE( each_pair.second ) );
|
2015-06-22 15:38:54 +00:00
|
|
|
}
|
2017-10-22 00:48:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wxString RESCUE_CASE_CANDIDATE::GetActionDescription() const
|
|
|
|
{
|
|
|
|
wxString action;
|
2020-10-28 13:30:39 +00:00
|
|
|
action.Printf( _( "Rename %s to %s" ), m_requested_name, m_new_name );
|
2017-10-22 00:48:25 +00:00
|
|
|
return action;
|
|
|
|
}
|
|
|
|
|
2015-06-22 15:38:54 +00:00
|
|
|
|
2017-10-22 00:48:25 +00:00
|
|
|
bool RESCUE_CASE_CANDIDATE::PerformAction( RESCUER* aRescuer )
|
|
|
|
{
|
2023-01-21 15:35:14 +00:00
|
|
|
wxCHECK( m_lib_candidate, true );
|
|
|
|
|
|
|
|
std::unique_ptr<LIB_SYMBOL> new_symbol = m_lib_candidate->Flatten();
|
|
|
|
new_symbol->SetName( m_new_name );
|
|
|
|
aRescuer->AddSymbol( new_symbol.get() );
|
|
|
|
|
2021-06-10 14:10:55 +00:00
|
|
|
for( SCH_SYMBOL* eachSymbol : *aRescuer->GetSymbols() )
|
2015-06-22 15:38:54 +00:00
|
|
|
{
|
2021-06-10 14:10:55 +00:00
|
|
|
if( eachSymbol->GetLibId().GetLibItemName() != UTF8( m_requested_name ) )
|
2017-10-22 00:48:25 +00:00
|
|
|
continue;
|
2017-01-31 01:31:19 +00:00
|
|
|
|
2017-10-22 00:48:25 +00:00
|
|
|
LIB_ID libId;
|
2017-01-31 01:31:19 +00:00
|
|
|
|
2021-06-30 11:00:06 +00:00
|
|
|
libId.SetLibItemName( m_new_name );
|
2021-06-10 14:10:55 +00:00
|
|
|
eachSymbol->SetLibId( libId );
|
|
|
|
eachSymbol->ClearFlags();
|
|
|
|
aRescuer->LogRescue( eachSymbol, m_requested_name, m_new_name );
|
2015-06-22 15:38:54 +00:00
|
|
|
}
|
2017-10-22 00:48:25 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2015-06-22 15:38:54 +00:00
|
|
|
|
|
|
|
|
2017-10-22 00:48:25 +00:00
|
|
|
RESCUE_CACHE_CANDIDATE::RESCUE_CACHE_CANDIDATE( const wxString& aRequestedName,
|
|
|
|
const wxString& aNewName,
|
2021-06-10 18:51:46 +00:00
|
|
|
LIB_SYMBOL* aCacheCandidate,
|
|
|
|
LIB_SYMBOL* aLibCandidate,
|
2020-04-02 00:17:25 +00:00
|
|
|
int aUnit,
|
|
|
|
int aConvert )
|
2015-06-22 15:38:54 +00:00
|
|
|
{
|
2017-10-22 00:48:25 +00:00
|
|
|
m_requested_name = aRequestedName;
|
|
|
|
m_new_name = aNewName;
|
|
|
|
m_cache_candidate = aCacheCandidate;
|
|
|
|
m_lib_candidate = aLibCandidate;
|
2020-04-02 00:17:25 +00:00
|
|
|
m_unit = aUnit;
|
|
|
|
m_convert = aConvert;
|
2017-10-22 00:48:25 +00:00
|
|
|
}
|
2015-06-22 15:38:54 +00:00
|
|
|
|
|
|
|
|
2017-10-22 00:48:25 +00:00
|
|
|
RESCUE_CACHE_CANDIDATE::RESCUE_CACHE_CANDIDATE()
|
|
|
|
{
|
2021-07-16 20:13:26 +00:00
|
|
|
m_cache_candidate = nullptr;
|
|
|
|
m_lib_candidate = nullptr;
|
2017-10-22 00:48:25 +00:00
|
|
|
}
|
2015-06-22 15:38:54 +00:00
|
|
|
|
2017-03-09 17:13:03 +00:00
|
|
|
|
2017-10-22 00:48:25 +00:00
|
|
|
void RESCUE_CACHE_CANDIDATE::FindRescues( RESCUER& aRescuer,
|
|
|
|
boost::ptr_vector<RESCUE_CANDIDATE>& aCandidates )
|
|
|
|
{
|
|
|
|
typedef std::map<wxString, RESCUE_CACHE_CANDIDATE> candidate_map_t;
|
|
|
|
candidate_map_t candidate_map;
|
2015-06-22 15:38:54 +00:00
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
// Remember the list of symbols is sorted by symbol name.
|
2017-10-22 00:48:25 +00:00
|
|
|
// So a search in libraries is made only once by group
|
2021-06-10 18:51:46 +00:00
|
|
|
LIB_SYMBOL* cache_match = nullptr;
|
|
|
|
LIB_SYMBOL* lib_match = nullptr;
|
|
|
|
wxString symbol_name;
|
|
|
|
wxString old_symbol_name;
|
2015-06-22 15:38:54 +00:00
|
|
|
|
2021-06-10 14:10:55 +00:00
|
|
|
for( SCH_SYMBOL* eachSymbol : *( aRescuer.GetSymbols() ) )
|
2017-10-22 00:48:25 +00:00
|
|
|
{
|
2022-10-11 20:16:11 +00:00
|
|
|
symbol_name = eachSymbol->GetLibId().GetUniStringLibItemName();
|
2017-10-22 00:48:25 +00:00
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
if( old_symbol_name != symbol_name )
|
2015-06-22 15:38:54 +00:00
|
|
|
{
|
2021-06-10 18:51:46 +00:00
|
|
|
// A new symbol name is found (a new group starts here).
|
2017-10-22 00:48:25 +00:00
|
|
|
// Search the symbol names candidates only once for this group:
|
2021-06-10 18:51:46 +00:00
|
|
|
old_symbol_name = symbol_name;
|
2023-09-28 03:04:53 +00:00
|
|
|
cache_match = findSymbol( symbol_name, PROJECT_SCH::SchLibs( aRescuer.GetPrj() ), true );
|
|
|
|
lib_match = findSymbol( symbol_name, PROJECT_SCH::SchLibs( aRescuer.GetPrj() ), false );
|
2017-10-22 00:48:25 +00:00
|
|
|
|
2022-10-11 20:16:11 +00:00
|
|
|
// At some point during V5 development, the LIB_ID delimiter character ':' was
|
|
|
|
// replaced by '_' when writing the symbol cache library so we have to test for
|
|
|
|
// the LIB_NICKNAME_LIB_SYMBOL_NAME case.
|
|
|
|
if( !cache_match && eachSymbol->GetLibId().IsValid() )
|
|
|
|
{
|
|
|
|
wxString tmp;
|
|
|
|
|
|
|
|
tmp = eachSymbol->GetLibId().GetLibNickname().wx_str() + wxT( "_" ) +
|
|
|
|
eachSymbol->GetLibId().GetLibItemName().wx_str();
|
2023-09-28 03:04:53 +00:00
|
|
|
cache_match = findSymbol( tmp, PROJECT_SCH::SchLibs( aRescuer.GetPrj() ), true );
|
2022-10-11 20:16:11 +00:00
|
|
|
}
|
2017-11-10 02:24:43 +00:00
|
|
|
|
2017-11-23 15:52:28 +00:00
|
|
|
// Test whether there is a conflict or if the symbol can only be found in the cache
|
|
|
|
// and the symbol name does not have any illegal characters.
|
2020-10-28 13:30:39 +00:00
|
|
|
if( cache_match && lib_match &&
|
|
|
|
!cache_match->PinsConflictWith( *lib_match, true, true, true, true, false ) )
|
|
|
|
continue;
|
2018-07-26 14:38:30 +00:00
|
|
|
|
2020-10-28 13:30:39 +00:00
|
|
|
if( !cache_match && lib_match )
|
|
|
|
continue;
|
2017-10-22 00:48:25 +00:00
|
|
|
|
2017-11-09 23:07:16 +00:00
|
|
|
// Check if the symbol has already been rescued.
|
2022-10-11 20:16:11 +00:00
|
|
|
RESCUE_CACHE_CANDIDATE candidate( symbol_name, symbol_name, cache_match, lib_match,
|
2021-06-10 14:10:55 +00:00
|
|
|
eachSymbol->GetUnit(),
|
|
|
|
eachSymbol->GetConvert() );
|
2017-10-22 00:48:25 +00:00
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
candidate_map[symbol_name] = candidate;
|
2015-06-22 15:38:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-22 00:48:25 +00:00
|
|
|
// Now, dump the map into aCandidates
|
|
|
|
for( const candidate_map_t::value_type& each_pair : candidate_map )
|
2015-06-22 15:38:54 +00:00
|
|
|
{
|
2017-10-22 00:48:25 +00:00
|
|
|
aCandidates.push_back( new RESCUE_CACHE_CANDIDATE( each_pair.second ) );
|
2015-06-22 15:38:54 +00:00
|
|
|
}
|
2017-10-22 00:48:25 +00:00
|
|
|
}
|
|
|
|
|
2015-06-22 15:38:54 +00:00
|
|
|
|
2017-10-22 00:48:25 +00:00
|
|
|
wxString RESCUE_CACHE_CANDIDATE::GetActionDescription() const
|
|
|
|
{
|
|
|
|
wxString action;
|
2018-01-09 20:41:26 +00:00
|
|
|
|
|
|
|
if( !m_cache_candidate && !m_lib_candidate )
|
2018-01-10 13:48:00 +00:00
|
|
|
action.Printf( _( "Cannot rescue symbol %s which is not available in any library or "
|
2018-01-09 20:41:26 +00:00
|
|
|
"the cache." ), m_requested_name );
|
|
|
|
else if( m_cache_candidate && !m_lib_candidate )
|
|
|
|
action.Printf( _( "Rescue symbol %s found only in cache library to %s." ),
|
|
|
|
m_requested_name, m_new_name );
|
|
|
|
else
|
|
|
|
action.Printf( _( "Rescue modified symbol %s to %s" ),
|
|
|
|
m_requested_name, m_new_name );
|
|
|
|
|
2017-10-22 00:48:25 +00:00
|
|
|
return action;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool RESCUE_CACHE_CANDIDATE::PerformAction( RESCUER* aRescuer )
|
|
|
|
{
|
2021-06-10 18:51:46 +00:00
|
|
|
LIB_SYMBOL* tmp = ( m_cache_candidate ) ? m_cache_candidate : m_lib_candidate;
|
2018-01-08 22:33:01 +00:00
|
|
|
|
2023-01-21 15:35:14 +00:00
|
|
|
// A symbol that cannot be rescued is a valid condition so just bail out here.
|
|
|
|
if( !tmp )
|
|
|
|
return true;
|
2018-01-08 22:33:01 +00:00
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
std::unique_ptr<LIB_SYMBOL> new_symbol = tmp->Flatten();
|
|
|
|
new_symbol->SetName( m_new_name );
|
2021-06-15 12:31:28 +00:00
|
|
|
aRescuer->AddSymbol( new_symbol.get() );
|
2017-10-22 00:48:25 +00:00
|
|
|
|
2021-06-10 14:10:55 +00:00
|
|
|
for( SCH_SYMBOL* eachSymbol : *aRescuer->GetSymbols() )
|
2015-06-22 15:38:54 +00:00
|
|
|
{
|
2021-06-10 14:10:55 +00:00
|
|
|
if( eachSymbol->GetLibId().GetLibItemName() != UTF8( m_requested_name ) )
|
2017-10-22 00:48:25 +00:00
|
|
|
continue;
|
2015-06-22 15:38:54 +00:00
|
|
|
|
2017-10-22 00:48:25 +00:00
|
|
|
LIB_ID libId;
|
2015-06-22 15:38:54 +00:00
|
|
|
|
2021-06-30 11:00:06 +00:00
|
|
|
libId.SetLibItemName( m_new_name );
|
2021-06-10 14:10:55 +00:00
|
|
|
eachSymbol->SetLibId( libId );
|
|
|
|
eachSymbol->ClearFlags();
|
|
|
|
aRescuer->LogRescue( eachSymbol, m_requested_name, m_new_name );
|
2015-06-22 15:38:54 +00:00
|
|
|
}
|
|
|
|
|
2017-10-22 00:48:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RESCUE_SYMBOL_LIB_TABLE_CANDIDATE::RESCUE_SYMBOL_LIB_TABLE_CANDIDATE(
|
|
|
|
const LIB_ID& aRequestedId,
|
|
|
|
const LIB_ID& aNewId,
|
2021-06-10 18:51:46 +00:00
|
|
|
LIB_SYMBOL* aCacheCandidate,
|
|
|
|
LIB_SYMBOL* aLibCandidate,
|
2020-04-02 00:17:25 +00:00
|
|
|
int aUnit,
|
|
|
|
int aConvert ) : RESCUE_CANDIDATE()
|
2017-10-22 00:48:25 +00:00
|
|
|
{
|
|
|
|
m_requested_id = aRequestedId;
|
2023-09-14 15:35:19 +00:00
|
|
|
m_requested_name = aRequestedId.Format().wx_str();
|
2017-10-22 00:48:25 +00:00
|
|
|
m_new_id = aNewId;
|
|
|
|
m_lib_candidate = aLibCandidate;
|
|
|
|
m_cache_candidate = aCacheCandidate;
|
2020-04-02 00:17:25 +00:00
|
|
|
m_unit = aUnit;
|
|
|
|
m_convert = aConvert;
|
2017-10-22 00:48:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RESCUE_SYMBOL_LIB_TABLE_CANDIDATE::RESCUE_SYMBOL_LIB_TABLE_CANDIDATE()
|
|
|
|
{
|
2021-07-16 20:13:26 +00:00
|
|
|
m_cache_candidate = nullptr;
|
|
|
|
m_lib_candidate = nullptr;
|
2017-10-22 00:48:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RESCUE_SYMBOL_LIB_TABLE_CANDIDATE::FindRescues(
|
|
|
|
RESCUER& aRescuer,
|
|
|
|
boost::ptr_vector<RESCUE_CANDIDATE>& aCandidates )
|
|
|
|
{
|
|
|
|
typedef std::map<LIB_ID, RESCUE_SYMBOL_LIB_TABLE_CANDIDATE> candidate_map_t;
|
|
|
|
|
|
|
|
candidate_map_t candidate_map;
|
|
|
|
|
2021-06-10 14:10:55 +00:00
|
|
|
// Remember the list of symbols is sorted by LIB_ID.
|
2017-10-22 00:48:25 +00:00
|
|
|
// So a search in libraries is made only once by group
|
2021-06-10 18:51:46 +00:00
|
|
|
LIB_SYMBOL* cache_match = nullptr;
|
|
|
|
LIB_SYMBOL* lib_match = nullptr;
|
|
|
|
LIB_ID old_symbol_id;
|
2017-10-22 00:48:25 +00:00
|
|
|
|
2022-10-11 20:16:11 +00:00
|
|
|
wxString symbolName;
|
2022-05-06 12:49:34 +00:00
|
|
|
|
2021-06-10 14:10:55 +00:00
|
|
|
for( SCH_SYMBOL* eachSymbol : *( aRescuer.GetSymbols() ) )
|
2015-06-22 15:38:54 +00:00
|
|
|
{
|
2021-06-10 18:51:46 +00:00
|
|
|
const LIB_ID& symbol_id = eachSymbol->GetLibId();
|
2015-06-22 15:38:54 +00:00
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
if( old_symbol_id != symbol_id )
|
2015-06-22 15:38:54 +00:00
|
|
|
{
|
2021-06-10 18:51:46 +00:00
|
|
|
// A new symbol name is found (a new group starts here).
|
2017-10-22 00:48:25 +00:00
|
|
|
// Search the symbol names candidates only once for this group:
|
2021-06-10 18:51:46 +00:00
|
|
|
old_symbol_id = symbol_id;
|
2019-12-09 19:47:29 +00:00
|
|
|
|
2022-10-11 20:16:11 +00:00
|
|
|
symbolName = symbol_id.Format().wx_str();
|
2022-05-06 12:49:34 +00:00
|
|
|
|
2019-12-09 19:47:29 +00:00
|
|
|
// Get the library symbol from the cache library. It will be a flattened
|
|
|
|
// symbol by default (no inheritance).
|
2023-09-28 03:04:53 +00:00
|
|
|
cache_match = findSymbol( symbolName, PROJECT_SCH::SchLibs( aRescuer.GetPrj() ), true );
|
2017-10-22 00:48:25 +00:00
|
|
|
|
2022-05-06 12:49:34 +00:00
|
|
|
// At some point during V5 development, the LIB_ID delimiter character ':' was
|
|
|
|
// replaced by '_' when writing the symbol cache library so we have to test for
|
|
|
|
// the LIB_NICKNAME_LIB_SYMBOL_NAME case.
|
|
|
|
if( !cache_match )
|
|
|
|
{
|
2022-10-11 20:16:11 +00:00
|
|
|
symbolName = symbol_id.GetLibNickname().wx_str() + wxT( "_" ) +
|
|
|
|
symbol_id.GetLibItemName().wx_str();
|
2023-09-28 03:04:53 +00:00
|
|
|
cache_match = findSymbol( symbolName, PROJECT_SCH::SchLibs( aRescuer.GetPrj() ), true );
|
2022-05-06 12:49:34 +00:00
|
|
|
}
|
|
|
|
|
2019-12-09 19:47:29 +00:00
|
|
|
// Get the library symbol from the symbol library table.
|
2023-09-28 03:04:53 +00:00
|
|
|
lib_match = SchGetLibSymbol( symbol_id, PROJECT_SCH::SchSymbolLibTable( aRescuer.GetPrj() ) );
|
2017-10-22 00:48:25 +00:00
|
|
|
|
2017-11-10 02:24:43 +00:00
|
|
|
if( !cache_match && !lib_match )
|
|
|
|
continue;
|
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
LIB_SYMBOL_SPTR lib_match_parent;
|
2019-12-09 19:47:29 +00:00
|
|
|
|
|
|
|
// If it's a derive symbol, use the parent symbol to perform the pin test.
|
|
|
|
if( lib_match && lib_match->IsAlias() )
|
|
|
|
{
|
2023-10-28 17:15:45 +00:00
|
|
|
lib_match_parent = lib_match->GetRootSymbol();
|
2019-12-09 19:47:29 +00:00
|
|
|
|
|
|
|
if( !lib_match_parent )
|
|
|
|
lib_match = nullptr;
|
|
|
|
else
|
|
|
|
lib_match = lib_match_parent.get();
|
|
|
|
}
|
|
|
|
|
2017-11-09 23:07:16 +00:00
|
|
|
// Test whether there is a conflict or if the symbol can only be found in the cache.
|
2021-06-10 18:51:46 +00:00
|
|
|
if( LIB_ID::HasIllegalChars( symbol_id.GetLibItemName() ) == -1 )
|
2018-04-30 09:44:35 +00:00
|
|
|
{
|
2018-07-26 14:38:30 +00:00
|
|
|
if( cache_match && lib_match &&
|
|
|
|
!cache_match->PinsConflictWith( *lib_match, true, true, true, true, false ) )
|
2021-06-17 21:22:10 +00:00
|
|
|
{
|
2018-07-26 14:38:30 +00:00
|
|
|
continue;
|
2021-06-17 21:22:10 +00:00
|
|
|
}
|
2018-07-26 14:38:30 +00:00
|
|
|
|
|
|
|
if( !cache_match && lib_match )
|
|
|
|
continue;
|
2018-04-30 09:44:35 +00:00
|
|
|
}
|
2017-01-31 01:31:19 +00:00
|
|
|
|
2018-01-09 20:41:26 +00:00
|
|
|
// Fix illegal LIB_ID name characters.
|
2022-10-11 20:16:11 +00:00
|
|
|
wxString new_name = EscapeString( symbol_id.GetLibItemName(), CTX_LIBID );
|
2017-10-22 00:48:25 +00:00
|
|
|
|
2022-10-11 20:16:11 +00:00
|
|
|
// Differentiate symbol name in the rescue library by appending the original symbol
|
|
|
|
// library table nickname to the symbol name to prevent name clashes in the rescue
|
|
|
|
// library.
|
2020-05-13 02:00:37 +00:00
|
|
|
wxString libNickname = GetRescueLibraryFileName( aRescuer.Schematic() ).GetName();
|
2018-01-10 21:04:32 +00:00
|
|
|
|
2023-01-17 04:14:38 +00:00
|
|
|
LIB_ID new_id( libNickname, new_name + wxS( "-" ) + symbol_id.GetLibNickname().wx_str() );
|
2017-10-22 00:48:25 +00:00
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
RESCUE_SYMBOL_LIB_TABLE_CANDIDATE candidate( symbol_id, new_id, cache_match, lib_match,
|
2021-06-10 14:10:55 +00:00
|
|
|
eachSymbol->GetUnit(),
|
|
|
|
eachSymbol->GetConvert() );
|
2017-01-31 01:31:19 +00:00
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
candidate_map[symbol_id] = candidate;
|
2015-06-22 15:38:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-22 00:48:25 +00:00
|
|
|
// Now, dump the map into aCandidates
|
|
|
|
for( const candidate_map_t::value_type& each_pair : candidate_map )
|
2015-06-22 15:38:54 +00:00
|
|
|
{
|
2017-10-22 00:48:25 +00:00
|
|
|
aCandidates.push_back( new RESCUE_SYMBOL_LIB_TABLE_CANDIDATE( each_pair.second ) );
|
2015-06-22 15:38:54 +00:00
|
|
|
}
|
2017-10-22 00:48:25 +00:00
|
|
|
}
|
2017-01-27 15:56:36 +00:00
|
|
|
|
2015-06-22 15:38:54 +00:00
|
|
|
|
2017-10-22 00:48:25 +00:00
|
|
|
wxString RESCUE_SYMBOL_LIB_TABLE_CANDIDATE::GetActionDescription() const
|
2015-06-22 15:38:54 +00:00
|
|
|
{
|
2017-10-22 00:48:25 +00:00
|
|
|
wxString action;
|
2018-01-09 20:41:26 +00:00
|
|
|
|
|
|
|
if( !m_cache_candidate && !m_lib_candidate )
|
2021-06-17 21:22:10 +00:00
|
|
|
{
|
2018-01-10 13:48:00 +00:00
|
|
|
action.Printf( _( "Cannot rescue symbol %s which is not available in any library or "
|
2021-06-17 21:22:10 +00:00
|
|
|
"the cache." ),
|
2022-10-11 20:16:11 +00:00
|
|
|
UnescapeString( m_requested_id.GetLibItemName().wx_str() ) );
|
2021-06-17 21:22:10 +00:00
|
|
|
}
|
2018-01-09 20:41:26 +00:00
|
|
|
else if( m_cache_candidate && !m_lib_candidate )
|
2021-06-17 21:22:10 +00:00
|
|
|
{
|
2018-01-09 20:41:26 +00:00
|
|
|
action.Printf( _( "Rescue symbol %s found only in cache library to %s." ),
|
2022-10-11 20:16:11 +00:00
|
|
|
UnescapeString( m_requested_id.Format().wx_str() ),
|
|
|
|
UnescapeString( m_new_id.Format().wx_str() ) );
|
2021-06-17 21:22:10 +00:00
|
|
|
}
|
2018-01-09 20:41:26 +00:00
|
|
|
else
|
2021-06-17 21:22:10 +00:00
|
|
|
{
|
2018-01-09 20:41:26 +00:00
|
|
|
action.Printf( _( "Rescue modified symbol %s to %s" ),
|
2022-10-11 20:16:11 +00:00
|
|
|
UnescapeString( m_requested_id.Format().wx_str() ),
|
|
|
|
UnescapeString( m_new_id.Format().wx_str() ) );
|
2021-06-17 21:22:10 +00:00
|
|
|
}
|
2018-01-09 20:41:26 +00:00
|
|
|
|
2017-10-22 00:48:25 +00:00
|
|
|
return action;
|
2015-06-22 15:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-22 00:48:25 +00:00
|
|
|
bool RESCUE_SYMBOL_LIB_TABLE_CANDIDATE::PerformAction( RESCUER* aRescuer )
|
2015-06-22 15:38:54 +00:00
|
|
|
{
|
2021-06-10 18:51:46 +00:00
|
|
|
LIB_SYMBOL* tmp = ( m_cache_candidate ) ? m_cache_candidate : m_lib_candidate;
|
2018-01-08 22:33:01 +00:00
|
|
|
|
2023-01-17 04:14:38 +00:00
|
|
|
wxCHECK_MSG( tmp, false, wxS( "Both cache and library symbols undefined." ) );
|
2018-01-08 22:33:01 +00:00
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
std::unique_ptr<LIB_SYMBOL> new_symbol = tmp->Flatten();
|
|
|
|
new_symbol->SetLibId( m_new_id );
|
|
|
|
new_symbol->SetName( m_new_id.GetLibItemName() );
|
2021-06-15 12:31:28 +00:00
|
|
|
aRescuer->AddSymbol( new_symbol.get() );
|
2017-10-22 00:48:25 +00:00
|
|
|
|
2021-06-10 14:10:55 +00:00
|
|
|
for( SCH_SYMBOL* eachSymbol : *aRescuer->GetSymbols() )
|
2017-10-22 00:48:25 +00:00
|
|
|
{
|
2021-06-10 14:10:55 +00:00
|
|
|
if( eachSymbol->GetLibId() != m_requested_id )
|
2017-10-22 00:48:25 +00:00
|
|
|
continue;
|
|
|
|
|
2021-06-10 14:10:55 +00:00
|
|
|
eachSymbol->SetLibId( m_new_id );
|
|
|
|
eachSymbol->ClearFlags();
|
|
|
|
aRescuer->LogRescue( eachSymbol, m_requested_id.Format(), m_new_id.Format() );
|
2017-10-22 00:48:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2015-06-22 15:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-13 02:00:37 +00:00
|
|
|
RESCUER::RESCUER( PROJECT& aProject, SCHEMATIC* aSchematic, SCH_SHEET_PATH* aCurrentSheet,
|
2019-02-05 17:14:27 +00:00
|
|
|
EDA_DRAW_PANEL_GAL::GAL_TYPE aGalBackEndType )
|
2015-06-22 15:38:54 +00:00
|
|
|
{
|
2020-05-13 02:00:37 +00:00
|
|
|
m_schematic = aSchematic ? aSchematic : aCurrentSheet->LastScreen()->Schematic();
|
|
|
|
|
|
|
|
wxASSERT( m_schematic );
|
|
|
|
|
2020-05-19 02:58:28 +00:00
|
|
|
if( m_schematic )
|
2021-06-10 14:10:55 +00:00
|
|
|
getSymbols( m_schematic, m_symbols );
|
2020-05-19 02:58:28 +00:00
|
|
|
|
2017-10-22 00:48:25 +00:00
|
|
|
m_prj = &aProject;
|
2019-02-05 17:14:27 +00:00
|
|
|
m_currentSheet = aCurrentSheet;
|
|
|
|
m_galBackEndType = aGalBackEndType;
|
2015-06-22 15:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-10 14:10:55 +00:00
|
|
|
void RESCUER::LogRescue( SCH_SYMBOL* aSymbol, const wxString &aOldName,
|
2017-10-22 00:48:25 +00:00
|
|
|
const wxString &aNewName )
|
2015-06-22 15:38:54 +00:00
|
|
|
{
|
|
|
|
RESCUE_LOG logitem;
|
2021-06-10 14:10:55 +00:00
|
|
|
logitem.symbol = aSymbol;
|
2015-06-22 15:38:54 +00:00
|
|
|
logitem.old_name = aOldName;
|
|
|
|
logitem.new_name = aNewName;
|
|
|
|
m_rescue_log.push_back( logitem );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool RESCUER::DoRescues()
|
|
|
|
{
|
2016-06-29 20:07:55 +00:00
|
|
|
for( RESCUE_CANDIDATE* each_candidate : m_chosen_candidates )
|
2015-06-22 15:38:54 +00:00
|
|
|
{
|
|
|
|
if( ! each_candidate->PerformAction( this ) )
|
|
|
|
return false;
|
|
|
|
}
|
2017-10-22 00:48:25 +00:00
|
|
|
|
2015-06-22 15:38:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RESCUER::UndoRescues()
|
|
|
|
{
|
2016-06-29 20:07:55 +00:00
|
|
|
for( RESCUE_LOG& each_logitem : m_rescue_log )
|
2015-06-22 15:38:54 +00:00
|
|
|
{
|
2017-01-31 01:31:19 +00:00
|
|
|
LIB_ID libId;
|
|
|
|
|
2021-06-30 11:00:06 +00:00
|
|
|
libId.SetLibItemName( each_logitem.old_name );
|
2021-06-10 14:10:55 +00:00
|
|
|
each_logitem.symbol->SetLibId( libId );
|
|
|
|
each_logitem.symbol->ClearFlags();
|
2015-06-22 15:38:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-05 17:14:27 +00:00
|
|
|
bool RESCUER::RescueProject( wxWindow* aParent, RESCUER& aRescuer, bool aRunningOnDemand )
|
2017-10-22 00:48:25 +00:00
|
|
|
{
|
|
|
|
aRescuer.FindCandidates();
|
|
|
|
|
2019-02-05 17:14:27 +00:00
|
|
|
if( !aRescuer.GetCandidateCount() )
|
2015-06-22 15:38:54 +00:00
|
|
|
{
|
|
|
|
if( aRunningOnDemand )
|
|
|
|
{
|
2019-02-05 17:14:27 +00:00
|
|
|
wxMessageDialog dlg( aParent, _( "This project has nothing to rescue." ),
|
2017-10-22 00:48:25 +00:00
|
|
|
_( "Project Rescue Helper" ) );
|
2015-06-22 15:38:54 +00:00
|
|
|
dlg.ShowModal();
|
|
|
|
}
|
2017-10-22 00:48:25 +00:00
|
|
|
|
2015-06-22 15:38:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-22 00:48:25 +00:00
|
|
|
aRescuer.RemoveDuplicates();
|
2019-02-05 17:14:27 +00:00
|
|
|
aRescuer.InvokeDialog( aParent, !aRunningOnDemand );
|
2015-06-22 15:38:54 +00:00
|
|
|
|
2017-10-22 00:48:25 +00:00
|
|
|
// If no symbols were rescued, let the user know what's going on. He might
|
2015-06-22 15:38:54 +00:00
|
|
|
// have clicked cancel by mistake, and should have some indication of that.
|
2017-10-22 00:48:25 +00:00
|
|
|
if( !aRescuer.GetChosenCandidateCount() )
|
2015-06-22 15:38:54 +00:00
|
|
|
{
|
2019-02-05 17:14:27 +00:00
|
|
|
wxMessageDialog dlg( aParent, _( "No symbols were rescued." ),
|
2017-10-22 00:48:25 +00:00
|
|
|
_( "Project Rescue Helper" ) );
|
2015-06-22 15:38:54 +00:00
|
|
|
dlg.ShowModal();
|
2015-06-29 16:31:00 +00:00
|
|
|
|
|
|
|
// Set the modified flag even on Cancel. Many users seem to instinctively want to Save at
|
|
|
|
// this point, due to the reloading of the symbols, so we'll make the save button active.
|
2015-06-22 15:38:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-22 00:48:25 +00:00
|
|
|
aRescuer.OpenRescueLibrary();
|
2015-06-22 15:38:54 +00:00
|
|
|
|
2017-10-22 00:48:25 +00:00
|
|
|
if( !aRescuer.DoRescues() )
|
2015-06-22 15:38:54 +00:00
|
|
|
{
|
2017-10-22 00:48:25 +00:00
|
|
|
aRescuer.UndoRescues();
|
2015-06-22 15:38:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-05 17:14:27 +00:00
|
|
|
aRescuer.WriteRescueLibrary( aParent );
|
2015-06-22 15:38:54 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2017-10-22 00:48:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
void RESCUER::RemoveDuplicates()
|
|
|
|
{
|
|
|
|
std::vector<wxString> names_seen;
|
|
|
|
|
|
|
|
for( boost::ptr_vector<RESCUE_CANDIDATE>::iterator it = m_all_candidates.begin();
|
|
|
|
it != m_all_candidates.end(); )
|
|
|
|
{
|
|
|
|
bool seen_already = false;
|
|
|
|
|
|
|
|
for( wxString& name_seen : names_seen )
|
|
|
|
{
|
|
|
|
if( name_seen == it->GetRequestedName() )
|
|
|
|
{
|
|
|
|
seen_already = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( seen_already )
|
|
|
|
{
|
|
|
|
it = m_all_candidates.erase( it );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
names_seen.push_back( it->GetRequestedName() );
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LEGACY_RESCUER::FindCandidates()
|
|
|
|
{
|
|
|
|
RESCUE_CASE_CANDIDATE::FindRescues( *this, m_all_candidates );
|
|
|
|
RESCUE_CACHE_CANDIDATE::FindRescues( *this, m_all_candidates );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-05 17:14:27 +00:00
|
|
|
void LEGACY_RESCUER::InvokeDialog( wxWindow* aParent, bool aAskShowAgain )
|
2017-10-22 00:48:25 +00:00
|
|
|
{
|
2019-02-05 17:14:27 +00:00
|
|
|
InvokeDialogRescueEach( aParent, static_cast< RESCUER& >( *this ), m_currentSheet,
|
|
|
|
m_galBackEndType, aAskShowAgain );
|
2017-10-22 00:48:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LEGACY_RESCUER::OpenRescueLibrary()
|
|
|
|
{
|
2020-05-13 02:00:37 +00:00
|
|
|
wxFileName fn = GetRescueLibraryFileName( m_schematic );
|
2017-10-22 00:48:25 +00:00
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
std::unique_ptr<SYMBOL_LIB> rescue_lib = std::make_unique<SYMBOL_LIB>( SCH_LIB_TYPE::LT_EESCHEMA,
|
2022-06-28 19:04:09 +00:00
|
|
|
fn.GetFullPath() );
|
2017-10-22 00:48:25 +00:00
|
|
|
|
|
|
|
m_rescue_lib = std::move( rescue_lib );
|
|
|
|
m_rescue_lib->EnableBuffering();
|
2017-11-10 02:24:43 +00:00
|
|
|
|
|
|
|
// If a rescue library already exists copy the contents of that library so we do not
|
2022-06-28 19:04:09 +00:00
|
|
|
// lose any previous rescues.
|
2023-09-28 03:04:53 +00:00
|
|
|
SYMBOL_LIB* rescueLib = PROJECT_SCH::SchLibs( m_prj )->FindLibrary( fn.GetName() );
|
2017-11-10 02:24:43 +00:00
|
|
|
|
|
|
|
if( rescueLib )
|
|
|
|
{
|
|
|
|
// For items in the rescue library, aliases are the root symbol.
|
2021-06-10 18:51:46 +00:00
|
|
|
std::vector< LIB_SYMBOL* > symbols;
|
2017-11-10 02:24:43 +00:00
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
rescueLib->GetSymbols( symbols );
|
2017-11-10 02:24:43 +00:00
|
|
|
|
2022-07-29 21:02:35 +00:00
|
|
|
for( LIB_SYMBOL* symbol : symbols )
|
2017-11-10 02:24:43 +00:00
|
|
|
{
|
2021-06-10 18:51:46 +00:00
|
|
|
// The LIB_SYMBOL copy constructor flattens derived symbols (formerly known as aliases).
|
2021-06-15 12:31:28 +00:00
|
|
|
m_rescue_lib->AddSymbol( new LIB_SYMBOL( *symbol, m_rescue_lib.get() ) );
|
2017-11-10 02:24:43 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-22 00:48:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-05 17:14:27 +00:00
|
|
|
bool LEGACY_RESCUER::WriteRescueLibrary( wxWindow *aParent )
|
2017-10-22 00:48:25 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
m_rescue_lib->Save( false );
|
|
|
|
}
|
|
|
|
catch( ... /* IO_ERROR ioe */ )
|
|
|
|
{
|
|
|
|
wxString msg;
|
|
|
|
|
2021-06-16 22:35:00 +00:00
|
|
|
msg.Printf( _( "Failed to create symbol library file '%s'." ),
|
2017-10-22 00:48:25 +00:00
|
|
|
m_rescue_lib->GetFullFileName() );
|
2019-02-05 17:14:27 +00:00
|
|
|
DisplayError( aParent, msg );
|
2017-10-22 00:48:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxArrayString libNames;
|
|
|
|
wxString libPaths;
|
|
|
|
|
|
|
|
wxString libName = m_rescue_lib->GetName();
|
2021-06-15 12:31:28 +00:00
|
|
|
SYMBOL_LIBS *libs = dynamic_cast<SYMBOL_LIBS*>( m_prj->GetElem( PROJECT::ELEM_SCH_SYMBOL_LIBS ) );
|
2017-10-22 00:48:25 +00:00
|
|
|
|
|
|
|
if( !libs )
|
|
|
|
{
|
2021-06-15 12:31:28 +00:00
|
|
|
libs = new SYMBOL_LIBS();
|
|
|
|
m_prj->SetElem( PROJECT::ELEM_SCH_SYMBOL_LIBS, libs );
|
2017-10-22 00:48:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2022-10-11 10:22:09 +00:00
|
|
|
SYMBOL_LIBS::GetLibNamesAndPaths( m_prj, &libPaths, &libNames );
|
2017-10-22 00:48:25 +00:00
|
|
|
|
|
|
|
// Make sure the library is not already in the list
|
|
|
|
while( libNames.Index( libName ) != wxNOT_FOUND )
|
|
|
|
libNames.Remove( libName );
|
|
|
|
|
|
|
|
// Add the library to the top of the list and save.
|
|
|
|
libNames.Insert( libName, 0 );
|
2022-10-11 10:22:09 +00:00
|
|
|
SYMBOL_LIBS::SetLibNamesAndPaths( m_prj, libPaths, libNames );
|
2017-10-22 00:48:25 +00:00
|
|
|
}
|
|
|
|
catch( const IO_ERROR& )
|
|
|
|
{
|
|
|
|
// Could not get or save the current libraries.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the old libraries in case there is a problem after clear(). We'll
|
|
|
|
// put them back in.
|
2021-06-15 12:31:28 +00:00
|
|
|
boost::ptr_vector<SYMBOL_LIB> libsSave;
|
2017-10-22 00:48:25 +00:00
|
|
|
libsSave.transfer( libsSave.end(), libs->begin(), libs->end(), *libs );
|
|
|
|
|
2021-07-16 20:13:26 +00:00
|
|
|
m_prj->SetElem( PROJECT::ELEM_SCH_SYMBOL_LIBS, nullptr );
|
2017-10-22 00:48:25 +00:00
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
libs = new SYMBOL_LIBS();
|
2017-10-22 00:48:25 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
libs->LoadAllLibraries( m_prj );
|
|
|
|
}
|
|
|
|
catch( const PARSE_ERROR& )
|
|
|
|
{
|
|
|
|
// Some libraries were not found. There's no point in showing the error,
|
|
|
|
// because it was already shown. Just don't do anything.
|
|
|
|
}
|
|
|
|
catch( const IO_ERROR& )
|
|
|
|
{
|
|
|
|
// Restore the old list
|
|
|
|
libs->clear();
|
|
|
|
libs->transfer( libs->end(), libsSave.begin(), libsSave.end(), libsSave );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
m_prj->SetElem( PROJECT::ELEM_SCH_SYMBOL_LIBS, libs );
|
2017-10-22 00:48:25 +00:00
|
|
|
|
|
|
|
// Update the schematic symbol library links since the library list has changed.
|
2020-05-13 02:00:37 +00:00
|
|
|
SCH_SCREENS schematic( m_schematic->Root() );
|
2017-10-22 00:48:25 +00:00
|
|
|
schematic.UpdateSymbolLinks();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
void LEGACY_RESCUER::AddSymbol( LIB_SYMBOL* aNewSymbol )
|
2017-10-22 00:48:25 +00:00
|
|
|
{
|
2023-01-17 04:14:38 +00:00
|
|
|
wxCHECK_RET( aNewSymbol, wxS( "Invalid LIB_SYMBOL pointer." ) );
|
2017-10-22 00:48:25 +00:00
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
aNewSymbol->SetLib( m_rescue_lib.get() );
|
2021-06-15 12:31:28 +00:00
|
|
|
m_rescue_lib->AddSymbol( aNewSymbol );
|
2017-10-22 00:48:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-13 02:00:37 +00:00
|
|
|
SYMBOL_LIB_TABLE_RESCUER::SYMBOL_LIB_TABLE_RESCUER( PROJECT& aProject, SCHEMATIC* aSchematic,
|
2019-02-05 17:14:27 +00:00
|
|
|
SCH_SHEET_PATH* aCurrentSheet,
|
|
|
|
EDA_DRAW_PANEL_GAL::GAL_TYPE aGalBackEndType ) :
|
2020-05-13 02:00:37 +00:00
|
|
|
RESCUER( aProject, aSchematic, aCurrentSheet, aGalBackEndType )
|
2017-10-22 00:48:25 +00:00
|
|
|
{
|
2022-11-06 16:51:52 +00:00
|
|
|
m_properties = std::make_unique<STRING_UTF8_MAP>();
|
2017-10-22 00:48:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SYMBOL_LIB_TABLE_RESCUER::FindCandidates()
|
|
|
|
{
|
|
|
|
RESCUE_SYMBOL_LIB_TABLE_CANDIDATE::FindRescues( *this, m_all_candidates );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-05 17:14:27 +00:00
|
|
|
void SYMBOL_LIB_TABLE_RESCUER::InvokeDialog( wxWindow* aParent, bool aAskShowAgain )
|
2017-10-22 00:48:25 +00:00
|
|
|
{
|
2019-02-05 17:14:27 +00:00
|
|
|
InvokeDialogRescueEach( aParent, static_cast< RESCUER& >( *this ), m_currentSheet,
|
|
|
|
m_galBackEndType, aAskShowAgain );
|
2017-10-22 00:48:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SYMBOL_LIB_TABLE_RESCUER::OpenRescueLibrary()
|
|
|
|
{
|
|
|
|
(*m_properties)[ SCH_LEGACY_PLUGIN::PropBuffering ] = "";
|
2022-06-28 19:04:09 +00:00
|
|
|
|
|
|
|
wxFileName fn = GetRescueLibraryFileName( m_schematic );
|
|
|
|
|
2023-09-28 03:04:53 +00:00
|
|
|
SYMBOL_LIB_TABLE_ROW* row = PROJECT_SCH::SchSymbolLibTable( m_prj )->FindRow( fn.GetName() );
|
2022-10-11 20:16:11 +00:00
|
|
|
|
2022-06-28 19:04:09 +00:00
|
|
|
// If a rescue library already exists copy the contents of that library so we do not
|
|
|
|
// lose any previous rescues.
|
2022-10-11 20:16:11 +00:00
|
|
|
if( row )
|
2022-06-28 19:04:09 +00:00
|
|
|
{
|
2022-10-11 20:16:11 +00:00
|
|
|
if( SCH_IO_MGR::EnumFromStr( row->GetType() ) == SCH_IO_MGR::SCH_KICAD )
|
|
|
|
fn.SetExt( KiCadSymbolLibFileExtension );
|
|
|
|
|
2022-06-28 19:04:09 +00:00
|
|
|
std::vector<LIB_SYMBOL*> symbols;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2023-09-28 03:04:53 +00:00
|
|
|
PROJECT_SCH::SchSymbolLibTable( m_prj )->LoadSymbolLib( symbols, fn.GetName() );
|
2022-06-28 19:04:09 +00:00
|
|
|
}
|
|
|
|
catch( ... /* IO_ERROR */ )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( LIB_SYMBOL* symbol : symbols )
|
2022-10-11 20:16:11 +00:00
|
|
|
m_rescueLibSymbols.emplace_back( std::make_unique<LIB_SYMBOL>( *symbol ) );
|
2022-06-28 19:04:09 +00:00
|
|
|
}
|
2017-10-22 00:48:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-05 17:14:27 +00:00
|
|
|
bool SYMBOL_LIB_TABLE_RESCUER::WriteRescueLibrary( wxWindow *aParent )
|
2017-10-22 00:48:25 +00:00
|
|
|
{
|
|
|
|
wxString msg;
|
2020-05-13 02:00:37 +00:00
|
|
|
wxFileName fn = GetRescueLibraryFileName( m_schematic );
|
2023-09-28 03:04:53 +00:00
|
|
|
SYMBOL_LIB_TABLE_ROW* row = PROJECT_SCH::SchSymbolLibTable( m_prj )->FindRow( fn.GetName() );
|
2022-10-11 20:16:11 +00:00
|
|
|
|
|
|
|
fn.SetExt( KiCadSymbolLibFileExtension );
|
2017-10-22 00:48:25 +00:00
|
|
|
|
2022-06-28 19:04:09 +00:00
|
|
|
try
|
|
|
|
{
|
2022-10-12 20:56:56 +00:00
|
|
|
SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD ) );
|
|
|
|
|
2022-10-11 20:16:11 +00:00
|
|
|
for( const std::unique_ptr<LIB_SYMBOL>& symbol : m_rescueLibSymbols )
|
|
|
|
pi->SaveSymbol( fn.GetFullPath(), new LIB_SYMBOL( *symbol.get() ), m_properties.get() );
|
|
|
|
|
|
|
|
pi->SaveLibrary( fn.GetFullPath() );
|
2022-06-28 19:04:09 +00:00
|
|
|
}
|
|
|
|
catch( const IO_ERROR& ioe )
|
|
|
|
{
|
|
|
|
msg.Printf( _( "Failed to save rescue library %s." ), fn.GetFullPath() );
|
|
|
|
DisplayErrorMessage( aParent, msg, ioe.What() );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-22 00:48:25 +00:00
|
|
|
// If the rescue library already exists in the symbol library table no need save it to add
|
|
|
|
// it to the table.
|
2022-10-11 20:16:11 +00:00
|
|
|
if( !row || ( SCH_IO_MGR::EnumFromStr( row->GetType() ) == SCH_IO_MGR::SCH_LEGACY ) )
|
2017-10-22 00:48:25 +00:00
|
|
|
{
|
2023-01-17 04:14:38 +00:00
|
|
|
wxString uri = wxS( "${KIPRJMOD}/" ) + fn.GetFullName();
|
2018-01-10 21:04:32 +00:00
|
|
|
wxString libNickname = fn.GetName();
|
|
|
|
|
2022-10-11 20:16:11 +00:00
|
|
|
row = new SYMBOL_LIB_TABLE_ROW( libNickname, uri, wxT( "KiCad" ) );
|
2023-09-28 03:04:53 +00:00
|
|
|
PROJECT_SCH::SchSymbolLibTable( m_prj )->InsertRow( row, true );
|
2017-10-22 00:48:25 +00:00
|
|
|
|
|
|
|
fn = wxFileName( m_prj->GetProjectPath(), SYMBOL_LIB_TABLE::GetSymbolLibTableFileName() );
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2023-09-28 03:04:53 +00:00
|
|
|
PROJECT_SCH::SchSymbolLibTable( m_prj )->Save( fn.GetFullPath() );
|
2017-10-22 00:48:25 +00:00
|
|
|
}
|
|
|
|
catch( const IO_ERROR& ioe )
|
|
|
|
{
|
|
|
|
msg.Printf( _( "Error occurred saving project specific symbol library table." ) );
|
2019-02-05 17:14:27 +00:00
|
|
|
DisplayErrorMessage( aParent, msg, ioe.What() );
|
2017-10-22 00:48:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 20:13:26 +00:00
|
|
|
m_prj->SetElem( PROJECT::ELEM_SYMBOL_LIB_TABLE, nullptr );
|
2017-10-22 00:48:25 +00:00
|
|
|
|
2021-06-09 19:32:58 +00:00
|
|
|
// This can only happen if the symbol library table file was corrupted on write.
|
2023-09-28 03:04:53 +00:00
|
|
|
if( !PROJECT_SCH::SchSymbolLibTable( m_prj ) )
|
2017-10-22 00:48:25 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Update the schematic symbol library links since the library list has changed.
|
2020-05-13 02:00:37 +00:00
|
|
|
SCH_SCREENS schematic( m_schematic->Root() );
|
2020-04-16 16:43:50 +00:00
|
|
|
schematic.UpdateSymbolLinks();
|
2017-10-22 00:48:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
void SYMBOL_LIB_TABLE_RESCUER::AddSymbol( LIB_SYMBOL* aNewSymbol )
|
2017-10-22 00:48:25 +00:00
|
|
|
{
|
2023-01-17 04:14:38 +00:00
|
|
|
wxCHECK_RET( aNewSymbol, wxS( "Invalid LIB_SYMBOL pointer." ) );
|
2017-10-22 00:48:25 +00:00
|
|
|
|
2022-10-11 20:16:11 +00:00
|
|
|
m_rescueLibSymbols.emplace_back( std::make_unique<LIB_SYMBOL>( *aNewSymbol ) );
|
2017-10-22 00:48:25 +00:00
|
|
|
}
|