2017-11-12 17:55:20 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2017 CERN
|
2022-04-21 10:57:15 +00:00
|
|
|
* Copyright (C) 2019-2022 KiCad Developers, see AUTHORS.txt for contributors.
|
2017-11-12 17:55:20 +00:00
|
|
|
* @author Maciej Suminski <maciej.suminski@cern.ch>
|
|
|
|
*
|
|
|
|
* 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, you may find one here:
|
|
|
|
* https://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 3 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2020-10-31 01:27:16 +00:00
|
|
|
#include <symbol_library_manager.h>
|
2021-06-15 13:24:55 +00:00
|
|
|
#include <symbol_library.h>
|
2021-09-14 18:26:03 +00:00
|
|
|
#include <dialogs/html_message_box.h>
|
2020-10-31 01:27:16 +00:00
|
|
|
#include <symbol_edit_frame.h>
|
2018-09-15 00:37:40 +00:00
|
|
|
#include <env_paths.h>
|
|
|
|
#include <pgm_base.h>
|
2017-11-12 17:55:20 +00:00
|
|
|
#include <kiway.h>
|
|
|
|
#include <profile.h>
|
2020-02-13 13:39:52 +00:00
|
|
|
#include <sch_io_mgr.h>
|
2020-10-05 18:38:40 +00:00
|
|
|
#include <sch_plugins/legacy/sch_legacy_plugin.h>
|
2020-02-13 13:39:52 +00:00
|
|
|
#include <symbol_lib_table.h>
|
2021-03-20 00:23:54 +00:00
|
|
|
#include <symbol_async_loader.h>
|
2021-08-14 20:05:21 +00:00
|
|
|
#include <progress_reporter.h>
|
2017-11-12 17:55:20 +00:00
|
|
|
#include <list>
|
2021-03-20 00:23:54 +00:00
|
|
|
#include <locale_io.h>
|
2021-06-01 05:17:57 +00:00
|
|
|
#include <wx/log.h>
|
2021-07-29 09:56:22 +00:00
|
|
|
#include <string_utils.h>
|
2021-06-01 05:17:57 +00:00
|
|
|
#include "lib_logger.h"
|
2017-11-12 17:55:20 +00:00
|
|
|
|
|
|
|
|
2020-10-31 01:27:16 +00:00
|
|
|
SYMBOL_LIBRARY_MANAGER::SYMBOL_LIBRARY_MANAGER( SYMBOL_EDIT_FRAME& aFrame ) :
|
2019-06-13 11:44:12 +00:00
|
|
|
m_frame( aFrame ),
|
2019-06-05 19:15:57 +00:00
|
|
|
m_syncHash( 0 )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2019-06-05 19:15:57 +00:00
|
|
|
m_adapter = SYMBOL_TREE_SYNCHRONIZING_ADAPTER::Create( &m_frame, this );
|
2017-11-15 10:13:58 +00:00
|
|
|
m_adapter->ShowUnits( false );
|
2021-06-01 05:17:57 +00:00
|
|
|
m_logger = new LIB_LOGGER();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SYMBOL_LIBRARY_MANAGER::~SYMBOL_LIBRARY_MANAGER()
|
|
|
|
{
|
|
|
|
delete m_logger;
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-31 22:58:34 +00:00
|
|
|
void SYMBOL_LIBRARY_MANAGER::Sync( const wxString& aForceRefresh,
|
2021-03-13 13:28:52 +00:00
|
|
|
std::function<void( int, int,
|
|
|
|
const wxString& )> aProgressCallback )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2021-06-01 05:17:57 +00:00
|
|
|
m_logger->Activate();
|
2017-11-12 13:24:55 +00:00
|
|
|
{
|
2021-01-31 22:58:34 +00:00
|
|
|
getAdapter()->Sync( aForceRefresh, aProgressCallback );
|
|
|
|
m_syncHash = symTable()->GetModifyHash();
|
2017-11-12 13:24:55 +00:00
|
|
|
}
|
2021-06-01 05:17:57 +00:00
|
|
|
m_logger->Deactivate();
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-20 00:23:54 +00:00
|
|
|
void SYMBOL_LIBRARY_MANAGER::Preload( PROGRESS_REPORTER& aReporter )
|
|
|
|
{
|
|
|
|
SYMBOL_ASYNC_LOADER loader( symTable()->GetLogicalLibs(), symTable(), false, nullptr,
|
|
|
|
&aReporter );
|
|
|
|
|
|
|
|
LOCALE_IO toggle;
|
|
|
|
|
|
|
|
loader.Start();
|
|
|
|
|
|
|
|
while( !loader.Done() )
|
|
|
|
{
|
2022-02-27 11:54:12 +00:00
|
|
|
if( !aReporter.KeepRefreshing() )
|
|
|
|
break;
|
2021-03-20 00:23:54 +00:00
|
|
|
|
2022-02-27 11:54:12 +00:00
|
|
|
wxMilliSleep( 33 /* 30 FPS refresh rate */ );
|
2021-03-20 00:23:54 +00:00
|
|
|
}
|
|
|
|
|
2022-02-27 11:54:12 +00:00
|
|
|
loader.Join();
|
2021-03-20 00:23:54 +00:00
|
|
|
|
|
|
|
if( !loader.GetErrors().IsEmpty() )
|
|
|
|
{
|
2021-04-02 23:50:50 +00:00
|
|
|
HTML_MESSAGE_BOX dlg( &m_frame, _( "Load Error" ) );
|
|
|
|
|
2021-06-26 11:33:37 +00:00
|
|
|
dlg.MessageSet( _( "Errors loading symbols:" ) );
|
2021-04-02 23:50:50 +00:00
|
|
|
|
|
|
|
wxString msg = loader.GetErrors();
|
|
|
|
msg.Replace( "\n", "<BR>" );
|
|
|
|
|
|
|
|
dlg.AddHTML_Text( msg );
|
|
|
|
dlg.ShowModal();
|
2021-03-20 00:23:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-31 01:27:16 +00:00
|
|
|
bool SYMBOL_LIBRARY_MANAGER::HasModifications() const
|
2019-06-13 11:44:12 +00:00
|
|
|
{
|
2022-02-27 11:54:12 +00:00
|
|
|
for( const std::pair<const wxString, LIB_BUFFER>& lib : m_libs )
|
2019-06-13 11:44:12 +00:00
|
|
|
{
|
2020-12-11 22:44:10 +00:00
|
|
|
if( lib.second.IsModified() )
|
2019-06-13 11:44:12 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-31 01:27:16 +00:00
|
|
|
int SYMBOL_LIBRARY_MANAGER::GetHash() const
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2018-01-08 14:18:08 +00:00
|
|
|
int hash = symTable()->GetModifyHash();
|
2017-11-12 17:55:20 +00:00
|
|
|
|
2022-02-27 11:54:12 +00:00
|
|
|
for( const std::pair<const wxString, LIB_BUFFER>& lib : m_libs )
|
2019-07-10 17:59:39 +00:00
|
|
|
hash += lib.second.GetHash();
|
2017-11-12 17:55:20 +00:00
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-31 01:27:16 +00:00
|
|
|
int SYMBOL_LIBRARY_MANAGER::GetLibraryHash( const wxString& aLibrary ) const
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
|
|
|
const auto libBufIt = m_libs.find( aLibrary );
|
2017-11-12 13:24:55 +00:00
|
|
|
|
|
|
|
if( libBufIt != m_libs.end() )
|
|
|
|
return libBufIt->second.GetHash();
|
|
|
|
|
2022-02-27 11:54:12 +00:00
|
|
|
SYMBOL_LIB_TABLE_ROW* row = GetLibrary( aLibrary );
|
2017-11-12 13:24:55 +00:00
|
|
|
|
|
|
|
// return -1 if library does not exist or 0 if not modified
|
2019-11-06 19:15:42 +00:00
|
|
|
return row ? std::hash<std::string>{}( aLibrary.ToStdString() +
|
|
|
|
row->GetFullURI( true ).ToStdString() ) : -1;
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-31 01:27:16 +00:00
|
|
|
wxArrayString SYMBOL_LIBRARY_MANAGER::GetLibraryNames() const
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
|
|
|
wxArrayString res;
|
|
|
|
|
2022-02-27 11:54:12 +00:00
|
|
|
for( const wxString& libName : symTable()->GetLogicalLibs() )
|
2017-11-12 17:55:20 +00:00
|
|
|
res.Add( libName );
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-31 01:27:16 +00:00
|
|
|
SYMBOL_LIB_TABLE_ROW* SYMBOL_LIBRARY_MANAGER::GetLibrary( const wxString& aLibrary ) const
|
2018-02-06 15:14:01 +00:00
|
|
|
{
|
2018-02-16 10:59:22 +00:00
|
|
|
SYMBOL_LIB_TABLE_ROW* row = nullptr;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2021-01-10 22:55:30 +00:00
|
|
|
row = symTable()->FindRow( aLibrary, true );
|
2018-02-16 10:59:22 +00:00
|
|
|
}
|
|
|
|
catch( const IO_ERROR& e )
|
|
|
|
{
|
2021-06-28 23:44:07 +00:00
|
|
|
wxLogMessage( _( "Library '%s' not found in the Symbol Library Table." ) + e.What(),
|
|
|
|
aLibrary );
|
2018-02-16 10:59:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return row;
|
2018-02-06 15:14:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-31 01:27:16 +00:00
|
|
|
bool SYMBOL_LIBRARY_MANAGER::SaveLibrary( const wxString& aLibrary, const wxString& aFileName,
|
|
|
|
SCH_IO_MGR::SCH_FILE_T aFileType )
|
2017-11-13 13:58:39 +00:00
|
|
|
{
|
2020-05-15 13:25:11 +00:00
|
|
|
wxCHECK( aFileType != SCH_IO_MGR::SCH_FILE_T::SCH_LEGACY && LibraryExists( aLibrary ), false );
|
2017-11-13 13:58:39 +00:00
|
|
|
wxFileName fn( aFileName );
|
|
|
|
wxCHECK( !fn.FileExists() || fn.IsFileWritable(), false );
|
2020-02-13 13:39:52 +00:00
|
|
|
SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( SCH_IO_MGR::FindPlugin( aFileType ) );
|
2017-11-13 13:58:39 +00:00
|
|
|
bool res = true; // assume all libraries are successfully saved
|
|
|
|
|
2021-03-03 00:19:08 +00:00
|
|
|
PROPERTIES properties;
|
|
|
|
properties.emplace( SCH_LEGACY_PLUGIN::PropBuffering, "" );
|
|
|
|
|
2017-11-13 13:58:39 +00:00
|
|
|
auto it = m_libs.find( aLibrary );
|
|
|
|
|
|
|
|
if( it != m_libs.end() )
|
|
|
|
{
|
|
|
|
// Handle buffered library
|
|
|
|
LIB_BUFFER& libBuf = it->second;
|
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
const auto& symbolBuffers = libBuf.GetBuffers();
|
2017-11-13 13:58:39 +00:00
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
for( const auto& symbolBuf : symbolBuffers )
|
2017-11-13 13:58:39 +00:00
|
|
|
{
|
2021-06-15 12:31:28 +00:00
|
|
|
if( !libBuf.SaveBuffer( symbolBuf, aFileName, &*pi, true ) )
|
2017-11-13 13:58:39 +00:00
|
|
|
{
|
|
|
|
// Something went wrong, but try to save other libraries
|
|
|
|
res = false;
|
|
|
|
}
|
|
|
|
}
|
2017-11-14 13:40:37 +00:00
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
// clear the deleted symbols buffer only if data is saved to the original file
|
2018-02-05 15:51:42 +00:00
|
|
|
wxFileName original, destination( aFileName );
|
2018-02-16 10:59:22 +00:00
|
|
|
auto row = GetLibrary( aLibrary );
|
2017-11-14 13:40:37 +00:00
|
|
|
|
2018-02-05 15:51:42 +00:00
|
|
|
if( row )
|
|
|
|
{
|
|
|
|
original = row->GetFullURI( true );
|
|
|
|
original.Normalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
destination.Normalize();
|
|
|
|
|
|
|
|
if( res && original == destination )
|
2017-11-14 13:40:37 +00:00
|
|
|
libBuf.ClearDeletedBuffer();
|
2017-11-13 13:58:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Handle original library
|
2021-06-15 12:31:28 +00:00
|
|
|
for( LIB_SYMBOL* symbol : getOriginalSymbols( aLibrary ) )
|
2019-11-06 19:15:42 +00:00
|
|
|
{
|
2021-06-10 18:51:46 +00:00
|
|
|
LIB_SYMBOL* newSymbol;
|
2019-11-06 19:15:42 +00:00
|
|
|
|
2022-05-06 16:47:06 +00:00
|
|
|
try
|
2019-11-06 19:15:42 +00:00
|
|
|
{
|
2022-05-06 16:47:06 +00:00
|
|
|
if( symbol->IsAlias() )
|
|
|
|
{
|
|
|
|
std::shared_ptr< LIB_SYMBOL > oldParent = symbol->GetParent().lock();
|
|
|
|
|
|
|
|
wxCHECK_MSG( oldParent, false,
|
|
|
|
wxString::Format( wxT( "Derived symbol '%s' found with undefined parent." ),
|
|
|
|
symbol->GetName() ) );
|
2019-11-06 19:15:42 +00:00
|
|
|
|
2022-05-06 16:47:06 +00:00
|
|
|
LIB_SYMBOL* libParent = pi->LoadSymbol( aLibrary, oldParent->GetName(),
|
|
|
|
&properties );
|
2019-11-06 19:15:42 +00:00
|
|
|
|
2022-05-06 16:47:06 +00:00
|
|
|
if( !libParent )
|
|
|
|
{
|
|
|
|
libParent = new LIB_SYMBOL( *oldParent.get() );
|
|
|
|
pi->SaveSymbol( aLibrary, libParent, &properties );
|
|
|
|
}
|
2019-11-06 19:15:42 +00:00
|
|
|
|
2022-05-06 16:47:06 +00:00
|
|
|
newSymbol = new LIB_SYMBOL( *symbol );
|
|
|
|
newSymbol->SetParent( libParent );
|
|
|
|
pi->SaveSymbol( aLibrary, newSymbol, &properties );
|
|
|
|
}
|
|
|
|
else if( !pi->LoadSymbol( aLibrary, symbol->GetName(), &properties ) )
|
2019-11-06 19:15:42 +00:00
|
|
|
{
|
2022-05-06 16:47:06 +00:00
|
|
|
pi->SaveSymbol( aLibrary, new LIB_SYMBOL( *symbol ), &properties );
|
2019-11-06 19:15:42 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-06 16:47:06 +00:00
|
|
|
catch( ... )
|
2019-11-06 19:15:42 +00:00
|
|
|
{
|
2022-05-06 16:47:06 +00:00
|
|
|
res = false;
|
|
|
|
break;
|
2019-11-06 19:15:42 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-13 13:58:39 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 16:27:19 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
pi->SaveLibrary( aFileName );
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
// return false because something happens.
|
|
|
|
// The library is not successfully saved
|
|
|
|
res = false;
|
|
|
|
}
|
|
|
|
|
2017-11-13 13:58:39 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-31 01:27:16 +00:00
|
|
|
bool SYMBOL_LIBRARY_MANAGER::IsLibraryModified( const wxString& aLibrary ) const
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
|
|
|
auto it = m_libs.find( aLibrary );
|
|
|
|
return it != m_libs.end() ? it->second.IsModified() : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
bool SYMBOL_LIBRARY_MANAGER::IsSymbolModified( const wxString& aAlias,
|
|
|
|
const wxString& aLibrary ) const
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
|
|
|
auto libIt = m_libs.find( aLibrary );
|
|
|
|
|
|
|
|
if( libIt == m_libs.end() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const LIB_BUFFER& buf = libIt->second;
|
2022-04-21 10:57:15 +00:00
|
|
|
const std::shared_ptr<SYMBOL_LIBRARY_MANAGER::SYMBOL_BUFFER> symbolBuf = buf.GetBuffer( aAlias );
|
2021-06-10 18:51:46 +00:00
|
|
|
return symbolBuf ? symbolBuf->IsModified() : false;
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-21 10:57:15 +00:00
|
|
|
void SYMBOL_LIBRARY_MANAGER::SetSymbolModified( const wxString& aAlias,
|
|
|
|
const wxString& aLibrary )
|
|
|
|
{
|
|
|
|
auto libIt = m_libs.find( aLibrary );
|
|
|
|
|
|
|
|
if( libIt == m_libs.end() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
const LIB_BUFFER& buf = libIt->second;
|
|
|
|
std::shared_ptr<SYMBOL_LIBRARY_MANAGER::SYMBOL_BUFFER> symbolBuf = buf.GetBuffer( aAlias );
|
|
|
|
symbolBuf->GetScreen()->SetContentModified();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-31 01:27:16 +00:00
|
|
|
bool SYMBOL_LIBRARY_MANAGER::ClearLibraryModified( const wxString& aLibrary ) const
|
2017-11-13 22:13:10 +00:00
|
|
|
{
|
|
|
|
auto libIt = m_libs.find( aLibrary );
|
|
|
|
|
|
|
|
if( libIt == m_libs.end() )
|
|
|
|
return false;
|
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
for( auto& symbolBuf : libIt->second.GetBuffers() )
|
2017-11-13 22:13:10 +00:00
|
|
|
{
|
2021-06-10 18:51:46 +00:00
|
|
|
SCH_SCREEN* screen = symbolBuf->GetScreen();
|
2017-11-13 22:13:10 +00:00
|
|
|
|
|
|
|
if( screen )
|
2021-05-28 19:07:04 +00:00
|
|
|
screen->SetContentModified( false );
|
2017-11-13 22:13:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
bool SYMBOL_LIBRARY_MANAGER::ClearSymbolModified( const wxString& aAlias,
|
|
|
|
const wxString& aLibrary ) const
|
2017-11-13 22:13:10 +00:00
|
|
|
{
|
|
|
|
auto libI = m_libs.find( aLibrary );
|
|
|
|
|
|
|
|
if( libI == m_libs.end() )
|
|
|
|
return false;
|
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
auto symbolBuf = libI->second.GetBuffer( aAlias );
|
|
|
|
wxCHECK( symbolBuf, false );
|
2017-11-13 22:13:10 +00:00
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
symbolBuf->GetScreen()->SetContentModified( false );
|
2017-11-13 22:13:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-31 01:27:16 +00:00
|
|
|
bool SYMBOL_LIBRARY_MANAGER::IsLibraryReadOnly( const wxString& aLibrary ) const
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
|
|
|
wxCHECK( LibraryExists( aLibrary ), true );
|
2020-05-15 13:25:11 +00:00
|
|
|
|
2020-05-22 14:12:44 +00:00
|
|
|
return !symTable()->IsSymbolLibWritable( aLibrary );
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-31 01:27:16 +00:00
|
|
|
bool SYMBOL_LIBRARY_MANAGER::IsLibraryLoaded( const wxString& aLibrary ) const
|
2020-08-29 23:19:05 +00:00
|
|
|
{
|
|
|
|
wxCHECK( LibraryExists( aLibrary ), false );
|
|
|
|
|
|
|
|
return symTable()->IsSymbolLibLoaded( aLibrary );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
std::list<LIB_SYMBOL*> SYMBOL_LIBRARY_MANAGER::GetAliases( const wxString& aLibrary ) const
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2021-06-10 18:51:46 +00:00
|
|
|
std::list<LIB_SYMBOL*> ret;
|
2017-11-12 17:55:20 +00:00
|
|
|
wxCHECK( LibraryExists( aLibrary ), ret );
|
|
|
|
|
|
|
|
auto libIt = m_libs.find( aLibrary );
|
|
|
|
|
|
|
|
if( libIt != m_libs.end() )
|
|
|
|
{
|
2021-06-10 18:51:46 +00:00
|
|
|
for( auto& symbolBuf : libIt->second.GetBuffers() )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2021-06-15 12:31:28 +00:00
|
|
|
ret.push_back( symbolBuf->GetSymbol() );
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-06-10 18:51:46 +00:00
|
|
|
std::vector<LIB_SYMBOL*> aliases;
|
2017-11-12 17:55:20 +00:00
|
|
|
|
2018-01-25 23:49:04 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
symTable()->LoadSymbolLib( aliases, aLibrary );
|
|
|
|
}
|
2018-02-13 07:50:51 +00:00
|
|
|
catch( const IO_ERROR& e )
|
|
|
|
{
|
2019-07-10 17:59:39 +00:00
|
|
|
wxLogWarning( e.Problem() );
|
2018-02-13 07:50:51 +00:00
|
|
|
}
|
2017-11-12 17:55:20 +00:00
|
|
|
|
2018-01-25 23:49:04 +00:00
|
|
|
std::copy( aliases.begin(), aliases.end(), std::back_inserter( ret ) );
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
LIB_SYMBOL* SYMBOL_LIBRARY_MANAGER::GetBufferedSymbol( const wxString& aAlias,
|
|
|
|
const wxString& aLibrary )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
|
|
|
wxCHECK( LibraryExists( aLibrary ), nullptr );
|
|
|
|
|
|
|
|
// try the library buffers first
|
|
|
|
LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
|
2021-06-15 12:31:28 +00:00
|
|
|
LIB_SYMBOL* bufferedSymbol = libBuf.GetSymbol( aAlias );
|
2017-11-12 17:55:20 +00:00
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
if( !bufferedSymbol ) // no buffer symbol found
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2021-06-10 18:51:46 +00:00
|
|
|
// create a copy of the symbol
|
2018-02-13 07:50:51 +00:00
|
|
|
try
|
|
|
|
{
|
2021-06-10 18:51:46 +00:00
|
|
|
LIB_SYMBOL* symbol = symTable()->LoadSymbol( aLibrary, aAlias );
|
2018-11-21 19:50:26 +00:00
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
if( symbol == nullptr )
|
2018-11-21 19:50:26 +00:00
|
|
|
THROW_IO_ERROR( _( "Symbol not found." ) );
|
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
LIB_SYMBOL* bufferedParent = nullptr;
|
2019-11-06 19:15:42 +00:00
|
|
|
|
|
|
|
// Create parent symbols on demand so parent symbol can be set.
|
2021-06-10 18:51:46 +00:00
|
|
|
if( symbol->IsAlias() )
|
2019-11-06 19:15:42 +00:00
|
|
|
{
|
2021-06-10 18:51:46 +00:00
|
|
|
std::shared_ptr< LIB_SYMBOL > parent = symbol->GetParent().lock();
|
2019-11-06 19:15:42 +00:00
|
|
|
wxCHECK_MSG( parent, nullptr,
|
|
|
|
wxString::Format( "Derived symbol '%s' found with undefined parent.",
|
2021-06-10 18:51:46 +00:00
|
|
|
symbol->GetName() ) );
|
2019-11-06 19:15:42 +00:00
|
|
|
|
|
|
|
// Check if the parent symbol buffer has already be created.
|
2021-06-15 12:31:28 +00:00
|
|
|
bufferedParent = libBuf.GetSymbol( parent->GetName() );
|
2019-11-06 19:15:42 +00:00
|
|
|
|
|
|
|
if( !bufferedParent )
|
|
|
|
{
|
2021-06-10 18:51:46 +00:00
|
|
|
bufferedParent = new LIB_SYMBOL( *parent.get() );
|
2020-05-21 02:27:48 +00:00
|
|
|
libBuf.CreateBuffer( bufferedParent, new SCH_SCREEN );
|
2019-11-06 19:15:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
bufferedSymbol = new LIB_SYMBOL( *symbol );
|
2019-11-06 19:15:42 +00:00
|
|
|
|
|
|
|
if( bufferedParent )
|
2021-06-15 12:31:28 +00:00
|
|
|
bufferedSymbol->SetParent( bufferedParent );
|
2019-11-06 19:15:42 +00:00
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
libBuf.CreateBuffer( bufferedSymbol, new SCH_SCREEN );
|
2018-01-10 11:04:34 +00:00
|
|
|
}
|
2018-02-13 07:50:51 +00:00
|
|
|
catch( const IO_ERROR& e )
|
2018-01-10 11:04:34 +00:00
|
|
|
{
|
2021-06-26 11:33:37 +00:00
|
|
|
wxLogMessage( _( "Error loading symbol %s from library '%s'. (%s)" ),
|
2019-07-02 15:15:26 +00:00
|
|
|
aAlias, aLibrary, e.What() );
|
2021-06-15 12:31:28 +00:00
|
|
|
bufferedSymbol = nullptr;
|
2018-01-10 11:04:34 +00:00
|
|
|
}
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
return bufferedSymbol;
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-31 01:27:16 +00:00
|
|
|
SCH_SCREEN* SYMBOL_LIBRARY_MANAGER::GetScreen( const wxString& aAlias, const wxString& aLibrary )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
|
|
|
wxCHECK( LibraryExists( aLibrary ), nullptr );
|
|
|
|
wxCHECK( !aAlias.IsEmpty(), nullptr );
|
|
|
|
auto it = m_libs.find( aLibrary );
|
|
|
|
wxCHECK( it != m_libs.end(), nullptr );
|
|
|
|
|
|
|
|
LIB_BUFFER& buf = it->second;
|
2021-06-10 18:51:46 +00:00
|
|
|
auto symbolBuf = buf.GetBuffer( aAlias );
|
|
|
|
return symbolBuf ? symbolBuf->GetScreen() : nullptr;
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
bool SYMBOL_LIBRARY_MANAGER::UpdateSymbol( LIB_SYMBOL* aSymbol, const wxString& aLibrary )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
|
|
|
wxCHECK( LibraryExists( aLibrary ), false );
|
2021-06-10 18:51:46 +00:00
|
|
|
wxCHECK( aSymbol, false );
|
2017-11-12 17:55:20 +00:00
|
|
|
LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
|
2021-06-10 18:51:46 +00:00
|
|
|
auto symbolBuf = libBuf.GetBuffer( aSymbol->GetName() );
|
2017-11-12 17:55:20 +00:00
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
if( symbolBuf ) // Existing symbol.
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2021-06-15 12:31:28 +00:00
|
|
|
LIB_SYMBOL* bufferedSymbol = const_cast< LIB_SYMBOL* >( symbolBuf->GetSymbol() );
|
2019-12-13 21:51:59 +00:00
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
wxCHECK( bufferedSymbol, false );
|
2019-12-13 21:51:59 +00:00
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
*bufferedSymbol = *aSymbol;
|
2021-06-10 18:51:46 +00:00
|
|
|
symbolBuf->GetScreen()->SetContentModified();
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
2019-12-26 13:00:59 +00:00
|
|
|
else // New symbol
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2021-06-10 18:51:46 +00:00
|
|
|
LIB_SYMBOL* symbolCopy = new LIB_SYMBOL( *aSymbol, nullptr );
|
2019-12-26 13:00:59 +00:00
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
symbolCopy->SetLibId( LIB_ID( aLibrary, aSymbol->GetLibId().GetLibItemName() ) );
|
2019-12-26 13:00:59 +00:00
|
|
|
|
2020-05-21 02:27:48 +00:00
|
|
|
SCH_SCREEN* screen = new SCH_SCREEN;
|
2021-06-10 18:51:46 +00:00
|
|
|
libBuf.CreateBuffer( symbolCopy, screen );
|
2021-05-28 19:07:04 +00:00
|
|
|
screen->SetContentModified();
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
bool SYMBOL_LIBRARY_MANAGER::UpdateSymbolAfterRename( LIB_SYMBOL* aSymbol, const wxString& aOldName,
|
|
|
|
const wxString& aLibrary )
|
2018-01-19 18:56:01 +00:00
|
|
|
{
|
|
|
|
LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
|
2021-06-10 18:51:46 +00:00
|
|
|
auto symbolBuf = libBuf.GetBuffer( aOldName );
|
2019-11-06 19:15:42 +00:00
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
wxCHECK( symbolBuf, false );
|
2018-01-19 18:56:01 +00:00
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
libBuf.UpdateBuffer( symbolBuf, aSymbol );
|
2018-08-25 12:21:09 +00:00
|
|
|
m_frame.SyncLibraries( false );
|
|
|
|
|
2018-01-19 18:56:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
bool SYMBOL_LIBRARY_MANAGER::FlushSymbol( const wxString& aAlias, const wxString& aLibrary )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
|
|
|
auto it = m_libs.find( aLibrary );
|
|
|
|
|
|
|
|
if( it == m_libs.end() ) // no items to flush
|
|
|
|
return true;
|
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
auto symbolBuf = it->second.GetBuffer( aAlias );
|
|
|
|
wxCHECK( symbolBuf, false );
|
2017-11-12 17:55:20 +00:00
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
return it->second.SaveBuffer( symbolBuf, symTable() );
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
LIB_ID SYMBOL_LIBRARY_MANAGER::RevertSymbol( const wxString& aAlias, const wxString& aLibrary )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
|
|
|
auto it = m_libs.find( aLibrary );
|
|
|
|
|
|
|
|
if( it == m_libs.end() ) // no items to flush
|
2018-01-19 18:56:01 +00:00
|
|
|
return LIB_ID( aLibrary, aAlias );
|
2017-11-12 17:55:20 +00:00
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
auto symbolBuf = it->second.GetBuffer( aAlias );
|
|
|
|
wxCHECK( symbolBuf, LIB_ID( aLibrary, aAlias ) );
|
|
|
|
LIB_SYMBOL original( *symbolBuf->GetOriginal() );
|
2017-11-12 17:55:20 +00:00
|
|
|
|
2018-01-19 18:56:01 +00:00
|
|
|
if( original.GetName() != aAlias )
|
|
|
|
{
|
2021-06-15 12:31:28 +00:00
|
|
|
UpdateSymbolAfterRename( &original, aAlias, aLibrary );
|
2018-01-19 18:56:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-06-15 12:31:28 +00:00
|
|
|
symbolBuf->SetSymbol( new LIB_SYMBOL( original ) );
|
2018-01-19 18:56:01 +00:00
|
|
|
m_frame.SyncLibraries( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
return LIB_ID( aLibrary, original.GetName() );
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-31 01:27:16 +00:00
|
|
|
bool SYMBOL_LIBRARY_MANAGER::RevertLibrary( const wxString& aLibrary )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
|
|
|
auto it = m_libs.find( aLibrary );
|
|
|
|
|
|
|
|
if( it == m_libs.end() ) // nothing to reverse
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_libs.erase( it );
|
2017-11-30 11:31:36 +00:00
|
|
|
m_frame.SyncLibraries( false );
|
2017-11-12 17:55:20 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-31 01:27:16 +00:00
|
|
|
bool SYMBOL_LIBRARY_MANAGER::RevertAll()
|
2019-06-13 11:44:12 +00:00
|
|
|
{
|
|
|
|
bool retv = true;
|
|
|
|
|
|
|
|
// Nothing to revert.
|
|
|
|
if( GetHash() == 0 )
|
|
|
|
return true;
|
|
|
|
|
2019-12-05 15:20:59 +00:00
|
|
|
for( const auto& lib : m_libs )
|
2019-06-13 11:44:12 +00:00
|
|
|
{
|
|
|
|
if( !lib.second.IsModified() )
|
|
|
|
continue;
|
|
|
|
|
2019-12-05 15:20:59 +00:00
|
|
|
for( const auto& buffer : lib.second.GetBuffers() )
|
2019-06-13 11:44:12 +00:00
|
|
|
{
|
|
|
|
if( !buffer->IsModified() )
|
|
|
|
continue;
|
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
RevertSymbol( lib.first, buffer->GetOriginal()->GetName() );
|
2019-06-13 11:44:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return retv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
bool SYMBOL_LIBRARY_MANAGER::RemoveSymbol( const wxString& aAlias, const wxString& aLibrary )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
|
|
|
LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
|
2021-06-10 18:51:46 +00:00
|
|
|
auto symbolBuf = libBuf.GetBuffer( aAlias );
|
|
|
|
wxCHECK( symbolBuf, false );
|
2017-11-12 17:55:20 +00:00
|
|
|
|
2019-11-06 19:15:42 +00:00
|
|
|
bool retv = true;
|
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
retv &= libBuf.DeleteBuffer( symbolBuf );
|
2017-11-30 11:31:36 +00:00
|
|
|
m_frame.SyncLibraries( false );
|
2017-11-12 17:55:20 +00:00
|
|
|
|
2019-11-06 19:15:42 +00:00
|
|
|
return retv;
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
LIB_SYMBOL* SYMBOL_LIBRARY_MANAGER::GetAlias( const wxString& aAlias,
|
2021-06-15 12:31:28 +00:00
|
|
|
const wxString& aLibrary ) const
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
|
|
|
// Try the library buffers first
|
|
|
|
auto libIt = m_libs.find( aLibrary );
|
|
|
|
|
|
|
|
if( libIt != m_libs.end() )
|
|
|
|
{
|
2021-06-15 12:31:28 +00:00
|
|
|
LIB_SYMBOL* symbol = libIt->second.GetSymbol( aAlias );
|
2017-11-12 17:55:20 +00:00
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
if( symbol )
|
|
|
|
return symbol;
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
// Get the original symbol
|
|
|
|
LIB_SYMBOL* alias = nullptr;
|
2017-11-13 14:25:55 +00:00
|
|
|
|
2018-02-13 07:50:51 +00:00
|
|
|
try
|
|
|
|
{
|
2018-01-08 14:18:08 +00:00
|
|
|
alias = symTable()->LoadSymbol( aLibrary, aAlias );
|
2018-02-13 07:50:51 +00:00
|
|
|
}
|
|
|
|
catch( const IO_ERROR& e )
|
|
|
|
{
|
2021-06-28 23:44:07 +00:00
|
|
|
wxLogMessage( _( "Cannot load symbol '%s' from library '%s'." ) + e.What(),
|
|
|
|
aAlias,
|
|
|
|
aLibrary );
|
2018-02-13 07:50:51 +00:00
|
|
|
}
|
2017-11-13 14:25:55 +00:00
|
|
|
|
|
|
|
return alias;
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
bool SYMBOL_LIBRARY_MANAGER::SymbolExists( const wxString& aAlias, const wxString& aLibrary ) const
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
|
|
|
auto libBufIt = m_libs.find( aLibrary );
|
2021-06-10 18:51:46 +00:00
|
|
|
LIB_SYMBOL* alias = nullptr;
|
2017-11-12 17:55:20 +00:00
|
|
|
|
|
|
|
if( libBufIt != m_libs.end() )
|
|
|
|
return !!libBufIt->second.GetBuffer( aAlias );
|
|
|
|
|
2018-02-13 07:50:51 +00:00
|
|
|
try
|
|
|
|
{
|
2018-01-08 14:18:08 +00:00
|
|
|
alias = symTable()->LoadSymbol( aLibrary, aAlias );
|
2019-06-13 11:44:12 +00:00
|
|
|
}
|
2019-06-05 19:15:57 +00:00
|
|
|
catch( IO_ERROR& )
|
2018-02-13 07:50:51 +00:00
|
|
|
{
|
|
|
|
// checking if certain symbol exists, so its absence is perfectly fine
|
|
|
|
}
|
2017-11-13 14:25:55 +00:00
|
|
|
|
|
|
|
return alias != nullptr;
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-31 01:27:16 +00:00
|
|
|
bool SYMBOL_LIBRARY_MANAGER::LibraryExists( const wxString& aLibrary, bool aCheckEnabled ) const
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2017-11-12 13:24:55 +00:00
|
|
|
if( aLibrary.IsEmpty() )
|
|
|
|
return false;
|
|
|
|
|
2017-11-12 17:55:20 +00:00
|
|
|
if( m_libs.count( aLibrary ) > 0 )
|
|
|
|
return true;
|
|
|
|
|
2018-04-28 09:43:41 +00:00
|
|
|
return symTable()->HasLibrary( aLibrary, aCheckEnabled );
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 11:44:12 +00:00
|
|
|
|
2020-10-31 01:27:16 +00:00
|
|
|
wxString SYMBOL_LIBRARY_MANAGER::GetUniqueLibraryName() const
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
|
|
|
wxString name = "New_Library";
|
|
|
|
|
|
|
|
if( !LibraryExists( name ) )
|
|
|
|
return name;
|
|
|
|
|
|
|
|
name += "_";
|
|
|
|
|
|
|
|
for( unsigned int i = 0; i < std::numeric_limits<unsigned int>::max(); ++i )
|
|
|
|
{
|
|
|
|
if( !LibraryExists( name + wxString::Format( "%u", i ) ) )
|
|
|
|
return name + wxString::Format( "%u", i );
|
|
|
|
}
|
|
|
|
|
|
|
|
wxFAIL;
|
|
|
|
return wxEmptyString;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-31 01:27:16 +00:00
|
|
|
void SYMBOL_LIBRARY_MANAGER::GetRootSymbolNames( const wxString& aLibraryName,
|
|
|
|
wxArrayString& aRootSymbolNames )
|
2019-11-06 19:15:42 +00:00
|
|
|
{
|
|
|
|
LIB_BUFFER& libBuf = getLibraryBuffer( aLibraryName );
|
|
|
|
|
|
|
|
libBuf.GetRootSymbolNames( aRootSymbolNames );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-31 01:27:16 +00:00
|
|
|
bool SYMBOL_LIBRARY_MANAGER:: HasDerivedSymbols( const wxString& aSymbolName,
|
|
|
|
const wxString& aLibraryName )
|
2019-12-12 15:44:53 +00:00
|
|
|
{
|
|
|
|
LIB_BUFFER& libBuf = getLibraryBuffer( aLibraryName );
|
|
|
|
|
|
|
|
return libBuf.HasDerivedSymbols( aSymbolName );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-20 00:23:54 +00:00
|
|
|
size_t SYMBOL_LIBRARY_MANAGER::GetLibraryCount() const
|
|
|
|
{
|
|
|
|
return symTable()->GetLogicalLibs().size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-31 01:27:16 +00:00
|
|
|
wxString SYMBOL_LIBRARY_MANAGER::getLibraryName( const wxString& aFilePath )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
|
|
|
wxFileName fn( aFilePath );
|
|
|
|
return fn.GetName();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-31 01:27:16 +00:00
|
|
|
bool SYMBOL_LIBRARY_MANAGER::addLibrary( const wxString& aFilePath, bool aCreate,
|
|
|
|
SYMBOL_LIB_TABLE* aTable )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2017-11-22 13:02:45 +00:00
|
|
|
wxCHECK( aTable, false );
|
2017-11-12 17:55:20 +00:00
|
|
|
wxString libName = getLibraryName( aFilePath );
|
|
|
|
wxCHECK( !LibraryExists( libName ), false ); // either create or add an existing one
|
|
|
|
|
2018-09-15 00:37:40 +00:00
|
|
|
// try to use path normalized to an environmental variable or project path
|
|
|
|
wxString relPath = NormalizePath( aFilePath, &Pgm().GetLocalEnvVariables(), &m_frame.Prj() );
|
|
|
|
|
2020-05-07 15:25:48 +00:00
|
|
|
SCH_IO_MGR::SCH_FILE_T schFileType = SCH_IO_MGR::GuessPluginTypeFromLibPath( aFilePath );
|
|
|
|
wxString typeName = SCH_IO_MGR::ShowType( schFileType );
|
2019-08-15 07:19:15 +00:00
|
|
|
SYMBOL_LIB_TABLE_ROW* libRow = new SYMBOL_LIB_TABLE_ROW( libName, relPath, typeName );
|
2017-11-22 13:02:45 +00:00
|
|
|
aTable->InsertRow( libRow );
|
2017-11-12 17:55:20 +00:00
|
|
|
|
|
|
|
if( aCreate )
|
2017-11-22 12:20:26 +00:00
|
|
|
{
|
2020-05-15 13:25:11 +00:00
|
|
|
wxCHECK( schFileType != SCH_IO_MGR::SCH_FILE_T::SCH_LEGACY, false );
|
|
|
|
|
2019-04-18 21:00:04 +00:00
|
|
|
try
|
2017-11-22 12:20:26 +00:00
|
|
|
{
|
2019-04-18 21:00:04 +00:00
|
|
|
aTable->CreateSymbolLib( libName );
|
|
|
|
}
|
2020-02-29 21:53:51 +00:00
|
|
|
catch( const IO_ERROR& )
|
2019-04-18 21:00:04 +00:00
|
|
|
{
|
|
|
|
aTable->RemoveRow( libRow );
|
|
|
|
return false;
|
2017-11-22 12:20:26 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-12 17:55:20 +00:00
|
|
|
|
2017-11-30 11:31:36 +00:00
|
|
|
m_frame.SyncLibraries( false );
|
2017-11-12 17:55:20 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-31 01:27:16 +00:00
|
|
|
SYMBOL_LIB_TABLE* SYMBOL_LIBRARY_MANAGER::symTable() const
|
2018-01-08 14:18:08 +00:00
|
|
|
{
|
|
|
|
return m_frame.Prj().SchSymbolLibTable();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
std::set<LIB_SYMBOL*> SYMBOL_LIBRARY_MANAGER::getOriginalSymbols( const wxString& aLibrary )
|
2017-11-13 13:58:39 +00:00
|
|
|
{
|
2021-06-10 18:51:46 +00:00
|
|
|
std::set<LIB_SYMBOL*> symbols;
|
|
|
|
wxCHECK( LibraryExists( aLibrary ), symbols );
|
2017-11-13 13:58:39 +00:00
|
|
|
|
2018-02-13 07:50:51 +00:00
|
|
|
try
|
|
|
|
{
|
2017-11-13 14:25:55 +00:00
|
|
|
wxArrayString aliases;
|
2018-01-08 14:18:08 +00:00
|
|
|
symTable()->EnumerateSymbolLib( aLibrary, aliases );
|
2017-11-13 14:25:55 +00:00
|
|
|
|
|
|
|
for( const auto& aliasName : aliases )
|
|
|
|
{
|
2021-06-10 18:51:46 +00:00
|
|
|
LIB_SYMBOL* alias = symTable()->LoadSymbol( aLibrary, aliasName );
|
|
|
|
symbols.insert( alias );
|
2017-11-13 14:25:55 +00:00
|
|
|
}
|
2019-04-18 21:00:04 +00:00
|
|
|
}
|
|
|
|
catch( const IO_ERROR& e )
|
2018-02-13 07:50:51 +00:00
|
|
|
{
|
2021-06-28 23:44:07 +00:00
|
|
|
wxLogMessage( _( "Cannot enumerate library '%s'." ) + e.What(), aLibrary );
|
2018-02-13 07:50:51 +00:00
|
|
|
}
|
2017-11-13 13:58:39 +00:00
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
return symbols;
|
2017-11-13 13:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-13 13:28:52 +00:00
|
|
|
SYMBOL_LIBRARY_MANAGER::LIB_BUFFER& SYMBOL_LIBRARY_MANAGER::getLibraryBuffer(
|
|
|
|
const wxString& aLibrary )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
|
|
|
auto it = m_libs.find( aLibrary );
|
|
|
|
|
|
|
|
if( it != m_libs.end() )
|
|
|
|
return it->second;
|
|
|
|
|
2018-01-10 11:04:34 +00:00
|
|
|
// The requested buffer does not exist yet, so create one
|
2017-11-12 17:55:20 +00:00
|
|
|
auto ret = m_libs.emplace( aLibrary, LIB_BUFFER( aLibrary ) );
|
|
|
|
LIB_BUFFER& buf = ret.first->second;
|
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
for( auto symbol : getOriginalSymbols( aLibrary ) )
|
2019-11-06 19:15:42 +00:00
|
|
|
{
|
2021-06-10 18:51:46 +00:00
|
|
|
LIB_SYMBOL* newSymbol;
|
2019-11-06 19:15:42 +00:00
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
if( symbol->IsAlias() )
|
2019-11-06 19:15:42 +00:00
|
|
|
{
|
2021-06-10 18:51:46 +00:00
|
|
|
std::shared_ptr< LIB_SYMBOL > oldParent = symbol->GetParent().lock();
|
2019-11-06 19:15:42 +00:00
|
|
|
|
|
|
|
wxCHECK_MSG( oldParent, buf,
|
|
|
|
wxString::Format( "Derived symbol '%s' found with undefined parent.",
|
2021-06-10 18:51:46 +00:00
|
|
|
symbol->GetName() ) );
|
2019-11-06 19:15:42 +00:00
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
LIB_SYMBOL* libParent = buf.GetSymbol( oldParent->GetName() );
|
2019-11-06 19:15:42 +00:00
|
|
|
|
|
|
|
if( !libParent )
|
|
|
|
{
|
2021-06-10 18:51:46 +00:00
|
|
|
libParent = new LIB_SYMBOL( *oldParent.get() );
|
2020-05-21 02:27:48 +00:00
|
|
|
buf.CreateBuffer( libParent, new SCH_SCREEN );
|
2019-11-06 19:15:42 +00:00
|
|
|
}
|
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
newSymbol = new LIB_SYMBOL( *symbol );
|
2019-11-06 19:15:42 +00:00
|
|
|
newSymbol->SetParent( libParent );
|
2020-05-21 02:27:48 +00:00
|
|
|
buf.CreateBuffer( newSymbol, new SCH_SCREEN );
|
2019-11-06 19:15:42 +00:00
|
|
|
}
|
2021-06-15 12:31:28 +00:00
|
|
|
else if( !buf.GetSymbol( symbol->GetName() ) )
|
2019-11-06 19:15:42 +00:00
|
|
|
{
|
2021-06-10 18:51:46 +00:00
|
|
|
buf.CreateBuffer( new LIB_SYMBOL( *symbol ), new SCH_SCREEN );
|
2019-11-06 19:15:42 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-12 17:55:20 +00:00
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
SYMBOL_LIBRARY_MANAGER::SYMBOL_BUFFER::SYMBOL_BUFFER( LIB_SYMBOL* aSymbol,
|
|
|
|
std::unique_ptr<SCH_SCREEN> aScreen ) :
|
2019-06-13 11:44:12 +00:00
|
|
|
m_screen( std::move( aScreen ) ),
|
2021-06-15 12:31:28 +00:00
|
|
|
m_symbol( aSymbol )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2021-06-10 18:51:46 +00:00
|
|
|
m_original = new LIB_SYMBOL( *aSymbol );
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
SYMBOL_LIBRARY_MANAGER::SYMBOL_BUFFER::~SYMBOL_BUFFER()
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2021-06-15 12:31:28 +00:00
|
|
|
delete m_symbol;
|
2017-11-12 17:55:20 +00:00
|
|
|
delete m_original;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
void SYMBOL_LIBRARY_MANAGER::SYMBOL_BUFFER::SetSymbol( LIB_SYMBOL* aSymbol )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2021-06-15 12:31:28 +00:00
|
|
|
wxCHECK( m_symbol != aSymbol, /* void */ );
|
2021-06-10 18:51:46 +00:00
|
|
|
wxASSERT( aSymbol );
|
2021-06-15 12:31:28 +00:00
|
|
|
delete m_symbol;
|
|
|
|
m_symbol = aSymbol;
|
2018-11-21 19:50:26 +00:00
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
// If the symbol moves libraries then the original moves with it
|
|
|
|
if( m_original->GetLibId().GetLibNickname() != m_symbol->GetLibId().GetLibNickname() )
|
2018-11-21 19:50:26 +00:00
|
|
|
{
|
2021-06-15 12:31:28 +00:00
|
|
|
m_original->SetLibId( LIB_ID( m_symbol->GetLibId().GetLibNickname(),
|
2018-11-21 19:50:26 +00:00
|
|
|
m_original->GetLibId().GetLibItemName() ) );
|
|
|
|
}
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
void SYMBOL_LIBRARY_MANAGER::SYMBOL_BUFFER::SetOriginal( LIB_SYMBOL* aSymbol )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2021-06-10 18:51:46 +00:00
|
|
|
wxCHECK( m_original != aSymbol, /* void */ );
|
|
|
|
wxASSERT( aSymbol );
|
2017-11-12 17:55:20 +00:00
|
|
|
delete m_original;
|
2021-06-10 18:51:46 +00:00
|
|
|
m_original = aSymbol;
|
2018-11-21 19:50:26 +00:00
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
// The original is not allowed to have a different library than its symbol
|
|
|
|
if( m_original->GetLibId().GetLibNickname() != m_symbol->GetLibId().GetLibNickname() )
|
2018-11-21 19:50:26 +00:00
|
|
|
{
|
2021-06-15 12:31:28 +00:00
|
|
|
m_original->SetLibId( LIB_ID( m_symbol->GetLibId().GetLibNickname(),
|
2018-11-21 19:50:26 +00:00
|
|
|
m_original->GetLibId().GetLibItemName() ) );
|
|
|
|
}
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
bool SYMBOL_LIBRARY_MANAGER::SYMBOL_BUFFER::IsModified() const
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2021-05-28 19:07:04 +00:00
|
|
|
return m_screen && m_screen->IsContentModified();
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
LIB_SYMBOL* SYMBOL_LIBRARY_MANAGER::LIB_BUFFER::GetSymbol( const wxString& aAlias ) const
|
2019-11-06 19:15:42 +00:00
|
|
|
{
|
|
|
|
auto buf = GetBuffer( aAlias );
|
|
|
|
|
|
|
|
if( !buf )
|
|
|
|
return nullptr;
|
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
LIB_SYMBOL* symbol = buf->GetSymbol();
|
2019-11-06 19:15:42 +00:00
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
wxCHECK( symbol, nullptr );
|
2019-11-06 19:15:42 +00:00
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
return symbol;
|
2019-11-06 19:15:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
bool SYMBOL_LIBRARY_MANAGER::LIB_BUFFER::CreateBuffer( LIB_SYMBOL* aCopy, SCH_SCREEN* aScreen )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2019-11-06 19:15:42 +00:00
|
|
|
wxASSERT( aCopy );
|
2017-11-12 17:55:20 +00:00
|
|
|
wxASSERT( aCopy->GetLib() == nullptr );
|
2018-01-22 16:46:28 +00:00
|
|
|
std::unique_ptr<SCH_SCREEN> screen( aScreen );
|
2021-06-15 12:31:28 +00:00
|
|
|
auto symbolBuf = std::make_shared<SYMBOL_BUFFER>( aCopy, std::move( screen ) );
|
|
|
|
m_symbols.push_back( symbolBuf );
|
2017-11-12 17:55:20 +00:00
|
|
|
|
|
|
|
// Set the parent library name,
|
|
|
|
// otherwise it is empty as no library has been given as the owner during object construction
|
2019-11-06 19:15:42 +00:00
|
|
|
LIB_ID libId = aCopy->GetLibId();
|
2017-11-12 17:55:20 +00:00
|
|
|
libId.SetLibNickname( m_libName );
|
2019-11-06 19:15:42 +00:00
|
|
|
aCopy->SetLibId( libId );
|
2017-11-12 17:55:20 +00:00
|
|
|
++m_hash;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-28 20:04:53 +00:00
|
|
|
bool SYMBOL_LIBRARY_MANAGER::LIB_BUFFER::UpdateBuffer( std::shared_ptr<SYMBOL_BUFFER> aSymbolBuf,
|
|
|
|
LIB_SYMBOL* aCopy )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2021-06-10 18:51:46 +00:00
|
|
|
wxCHECK( aCopy && aSymbolBuf, false );
|
2019-12-26 13:00:59 +00:00
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
LIB_SYMBOL* bufferedSymbol = aSymbolBuf->GetSymbol();
|
2017-11-12 17:55:20 +00:00
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
wxCHECK( bufferedSymbol, false );
|
2019-12-26 13:00:59 +00:00
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
*bufferedSymbol = *aCopy;
|
2017-11-12 17:55:20 +00:00
|
|
|
++m_hash;
|
|
|
|
|
2019-12-26 13:00:59 +00:00
|
|
|
return true;
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-28 20:04:53 +00:00
|
|
|
bool SYMBOL_LIBRARY_MANAGER::LIB_BUFFER::DeleteBuffer( std::shared_ptr<SYMBOL_BUFFER> aSymbolBuf )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2021-06-15 12:31:28 +00:00
|
|
|
auto symbolBufIt = std::find( m_symbols.begin(), m_symbols.end(), aSymbolBuf );
|
|
|
|
wxCHECK( symbolBufIt != m_symbols.end(), false );
|
2019-11-06 19:15:42 +00:00
|
|
|
|
|
|
|
bool retv = true;
|
|
|
|
|
|
|
|
// Remove all derived symbols to prevent broken inheritance.
|
2021-06-15 12:31:28 +00:00
|
|
|
if( aSymbolBuf->GetSymbol()->IsRoot() && HasDerivedSymbols( aSymbolBuf->GetSymbol()->GetName() )
|
|
|
|
&& removeChildSymbols( aSymbolBuf ) == 0 )
|
2020-09-26 11:40:32 +00:00
|
|
|
{
|
2019-12-12 19:36:29 +00:00
|
|
|
retv = false;
|
2020-09-26 11:40:32 +00:00
|
|
|
}
|
2019-11-06 19:15:42 +00:00
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
m_deleted.emplace_back( *symbolBufIt );
|
|
|
|
m_symbols.erase( symbolBufIt );
|
2017-11-12 17:55:20 +00:00
|
|
|
++m_hash;
|
|
|
|
|
2019-11-06 19:15:42 +00:00
|
|
|
return retv;
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-28 20:04:53 +00:00
|
|
|
bool SYMBOL_LIBRARY_MANAGER::LIB_BUFFER::SaveBuffer( std::shared_ptr<SYMBOL_BUFFER> aSymbolBuf,
|
|
|
|
SYMBOL_LIB_TABLE* aLibTable )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2021-06-10 18:51:46 +00:00
|
|
|
wxCHECK( aSymbolBuf, false );
|
2021-06-15 12:31:28 +00:00
|
|
|
LIB_SYMBOL* libSymbol = aSymbolBuf->GetSymbol();
|
|
|
|
LIB_SYMBOL* originalSymbol = aSymbolBuf->GetOriginal();
|
|
|
|
wxCHECK( libSymbol && originalSymbol, false );
|
2019-11-06 19:15:42 +00:00
|
|
|
SYMBOL_LIB_TABLE::SAVE_T result;
|
2021-03-03 00:19:08 +00:00
|
|
|
PROPERTIES properties;
|
|
|
|
properties.emplace( SCH_LEGACY_PLUGIN::PropBuffering, "" );
|
|
|
|
|
2022-05-06 23:12:25 +00:00
|
|
|
wxString errorMsg = _( "Error saving symbol %s to library '%s'." ) + wxS( "\n%s" );
|
|
|
|
|
2021-03-03 00:19:08 +00:00
|
|
|
// Delete the original symbol if the symbol name has been changed.
|
2021-06-15 12:31:28 +00:00
|
|
|
if( libSymbol->GetName() != originalSymbol->GetName() )
|
2021-03-03 00:19:08 +00:00
|
|
|
{
|
2022-05-06 23:12:25 +00:00
|
|
|
// DeleteSymbol may throw
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if( aLibTable->LoadSymbol( m_libName, originalSymbol->GetName() ) )
|
|
|
|
aLibTable->DeleteSymbol( m_libName, originalSymbol->GetName() );
|
|
|
|
}
|
|
|
|
catch( const IO_ERROR& ioe )
|
|
|
|
{
|
|
|
|
wxLogError( errorMsg, UnescapeString( originalSymbol->GetName() ), m_libName,
|
|
|
|
ioe.What() );
|
|
|
|
return false;
|
|
|
|
}
|
2021-03-03 00:19:08 +00:00
|
|
|
}
|
2019-11-06 19:15:42 +00:00
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
if( libSymbol->IsAlias() )
|
2019-11-06 19:15:42 +00:00
|
|
|
{
|
2021-06-15 12:31:28 +00:00
|
|
|
LIB_SYMBOL* newCachedSymbol = new LIB_SYMBOL( *libSymbol );
|
|
|
|
std::shared_ptr< LIB_SYMBOL > bufferedParent = libSymbol->GetParent().lock();
|
2019-11-06 19:15:42 +00:00
|
|
|
|
|
|
|
wxCHECK( bufferedParent, false );
|
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
LIB_SYMBOL* cachedParent = aLibTable->LoadSymbol( m_libName, bufferedParent->GetName() );
|
2019-11-06 19:15:42 +00:00
|
|
|
|
|
|
|
if( !cachedParent )
|
|
|
|
{
|
2021-06-10 18:51:46 +00:00
|
|
|
cachedParent = new LIB_SYMBOL( *bufferedParent.get() );
|
2021-06-15 12:31:28 +00:00
|
|
|
newCachedSymbol->SetParent( cachedParent );
|
2019-11-06 19:15:42 +00:00
|
|
|
result = aLibTable->SaveSymbol( m_libName, cachedParent );
|
|
|
|
wxCHECK( result == SYMBOL_LIB_TABLE::SAVE_OK, false );
|
2021-06-15 12:31:28 +00:00
|
|
|
result = aLibTable->SaveSymbol( m_libName, newCachedSymbol );
|
2019-11-06 19:15:42 +00:00
|
|
|
wxCHECK( result == SYMBOL_LIB_TABLE::SAVE_OK, false );
|
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
LIB_SYMBOL* originalParent = new LIB_SYMBOL( *bufferedParent.get() );
|
|
|
|
aSymbolBuf->SetOriginal( originalParent );
|
2021-06-15 12:31:28 +00:00
|
|
|
originalSymbol = new LIB_SYMBOL( *libSymbol );
|
|
|
|
originalSymbol->SetParent( originalParent );
|
|
|
|
aSymbolBuf->SetOriginal( originalSymbol );
|
2019-11-06 19:15:42 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-06-15 12:31:28 +00:00
|
|
|
newCachedSymbol->SetParent( cachedParent );
|
|
|
|
result = aLibTable->SaveSymbol( m_libName, newCachedSymbol );
|
2019-11-06 19:15:42 +00:00
|
|
|
wxCHECK( result == SYMBOL_LIB_TABLE::SAVE_OK, false );
|
|
|
|
|
2021-07-28 20:04:53 +00:00
|
|
|
auto originalBufferedParent = GetBuffer( bufferedParent->GetName() );
|
2019-11-06 19:15:42 +00:00
|
|
|
wxCHECK( originalBufferedParent, false );
|
2021-06-15 12:31:28 +00:00
|
|
|
originalSymbol = new LIB_SYMBOL( *libSymbol );
|
|
|
|
originalSymbol->SetParent( originalBufferedParent->GetSymbol() );
|
|
|
|
aSymbolBuf->SetOriginal( originalSymbol );
|
2019-11-06 19:15:42 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-09 16:23:46 +00:00
|
|
|
else
|
2019-11-06 19:15:42 +00:00
|
|
|
{
|
2020-01-09 16:23:46 +00:00
|
|
|
wxArrayString derivedSymbols;
|
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
if( GetDerivedSymbolNames( libSymbol->GetName(), derivedSymbols ) == 0 )
|
2020-01-09 16:23:46 +00:00
|
|
|
{
|
2021-06-15 12:31:28 +00:00
|
|
|
result = aLibTable->SaveSymbol( m_libName, new LIB_SYMBOL( *libSymbol ) );
|
2020-01-09 16:23:46 +00:00
|
|
|
wxCHECK( result == SYMBOL_LIB_TABLE::SAVE_OK, false );
|
2021-06-15 12:31:28 +00:00
|
|
|
aSymbolBuf->SetOriginal( new LIB_SYMBOL( *libSymbol ) );
|
2020-01-09 16:23:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-06-15 12:31:28 +00:00
|
|
|
LIB_SYMBOL* parentSymbol = new LIB_SYMBOL( *libSymbol );
|
2020-01-09 16:23:46 +00:00
|
|
|
|
|
|
|
aLibTable->SaveSymbol( m_libName, parentSymbol );
|
|
|
|
|
|
|
|
for( auto entry : derivedSymbols )
|
|
|
|
{
|
2021-07-28 20:04:53 +00:00
|
|
|
std::shared_ptr<SYMBOL_BUFFER> symbol = GetBuffer( entry );
|
2022-03-25 19:51:05 +00:00
|
|
|
|
|
|
|
wxCHECK2( symbol, continue );
|
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
LIB_SYMBOL* derivedSymbol = new LIB_SYMBOL( *symbol->GetSymbol() );
|
2020-01-09 16:23:46 +00:00
|
|
|
derivedSymbol->SetParent( parentSymbol );
|
|
|
|
result = aLibTable->SaveSymbol( m_libName, derivedSymbol );
|
|
|
|
wxCHECK( result == SYMBOL_LIB_TABLE::SAVE_OK, false );
|
|
|
|
}
|
|
|
|
}
|
2019-11-06 19:15:42 +00:00
|
|
|
}
|
2017-11-12 17:55:20 +00:00
|
|
|
|
2017-11-13 13:58:39 +00:00
|
|
|
++m_hash;
|
|
|
|
return true;
|
|
|
|
}
|
2017-11-12 17:55:20 +00:00
|
|
|
|
|
|
|
|
2021-07-28 20:04:53 +00:00
|
|
|
bool SYMBOL_LIBRARY_MANAGER::LIB_BUFFER::SaveBuffer( std::shared_ptr<SYMBOL_BUFFER> aSymbolBuf,
|
|
|
|
const wxString& aFileName,
|
2021-03-13 13:28:52 +00:00
|
|
|
SCH_PLUGIN* aPlugin, bool aBuffer )
|
2017-11-13 13:58:39 +00:00
|
|
|
{
|
2021-06-10 18:51:46 +00:00
|
|
|
wxCHECK( aSymbolBuf, false );
|
2021-06-15 12:31:28 +00:00
|
|
|
LIB_SYMBOL* libSymbol = aSymbolBuf->GetSymbol();
|
|
|
|
LIB_SYMBOL* originalSymbol = aSymbolBuf->GetOriginal();
|
|
|
|
wxCHECK( libSymbol && originalSymbol, false );
|
2021-03-13 13:28:52 +00:00
|
|
|
wxCHECK( !aFileName.IsEmpty(), false );
|
2017-11-13 13:58:39 +00:00
|
|
|
|
2021-06-26 19:21:30 +00:00
|
|
|
wxString errorMsg = _( "Error saving symbol %s to library '%s'." ) + wxS( "\n%s" );
|
2020-01-09 16:23:46 +00:00
|
|
|
|
2017-11-13 13:58:39 +00:00
|
|
|
// set properties to prevent save file on every symbol save
|
|
|
|
PROPERTIES properties;
|
|
|
|
properties.emplace( SCH_LEGACY_PLUGIN::PropBuffering, "" );
|
|
|
|
|
2021-03-03 00:19:08 +00:00
|
|
|
// Delete the original symbol if the symbol name has been changed.
|
2021-06-15 12:31:28 +00:00
|
|
|
if( libSymbol->GetName() != originalSymbol->GetName() )
|
2021-03-03 00:19:08 +00:00
|
|
|
{
|
2022-05-06 23:12:25 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
if( aPlugin->LoadSymbol( aFileName, originalSymbol->GetName() ) )
|
|
|
|
aPlugin->DeleteSymbol( aFileName, originalSymbol->GetName(), &properties );
|
|
|
|
}
|
|
|
|
catch( const IO_ERROR& ioe )
|
|
|
|
{
|
|
|
|
wxLogError( errorMsg, UnescapeString( originalSymbol->GetName() ), aFileName,
|
|
|
|
ioe.What() );
|
|
|
|
return false;
|
|
|
|
}
|
2021-03-03 00:19:08 +00:00
|
|
|
}
|
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
if( libSymbol->IsAlias() )
|
2019-11-06 19:15:42 +00:00
|
|
|
{
|
2021-06-15 12:31:28 +00:00
|
|
|
LIB_SYMBOL* newCachedSymbol = new LIB_SYMBOL( *libSymbol );
|
|
|
|
std::shared_ptr< LIB_SYMBOL > bufferedParent = libSymbol->GetParent().lock();
|
2019-11-06 19:15:42 +00:00
|
|
|
|
|
|
|
wxCHECK( bufferedParent, false );
|
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
LIB_SYMBOL* cachedParent = nullptr;
|
2020-01-09 16:23:46 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2021-03-13 13:28:52 +00:00
|
|
|
cachedParent = aPlugin->LoadSymbol( aFileName, bufferedParent->GetName() );
|
2020-01-09 16:23:46 +00:00
|
|
|
}
|
2020-02-29 21:53:51 +00:00
|
|
|
catch( const IO_ERROR& )
|
2020-01-09 16:23:46 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-06 19:15:42 +00:00
|
|
|
|
|
|
|
if( !cachedParent )
|
|
|
|
{
|
2021-06-10 18:51:46 +00:00
|
|
|
cachedParent = new LIB_SYMBOL( *bufferedParent.get() );
|
2021-06-15 12:31:28 +00:00
|
|
|
newCachedSymbol->SetParent( cachedParent );
|
2020-01-09 16:23:46 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2021-03-13 13:28:52 +00:00
|
|
|
aPlugin->SaveSymbol( aFileName, cachedParent, aBuffer ? &properties : nullptr );
|
2020-01-09 16:23:46 +00:00
|
|
|
}
|
|
|
|
catch( const IO_ERROR& ioe )
|
|
|
|
{
|
2021-06-30 10:53:04 +00:00
|
|
|
wxLogError( errorMsg, UnescapeString( cachedParent->GetName() ), aFileName,
|
|
|
|
ioe.What() );
|
2020-01-09 16:23:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2021-06-15 12:31:28 +00:00
|
|
|
aPlugin->SaveSymbol( aFileName, newCachedSymbol, aBuffer ? &properties : nullptr );
|
2020-01-09 16:23:46 +00:00
|
|
|
}
|
|
|
|
catch( const IO_ERROR& ioe )
|
|
|
|
{
|
2021-06-30 10:53:04 +00:00
|
|
|
wxLogError( errorMsg, UnescapeString( newCachedSymbol->GetName() ), aFileName,
|
|
|
|
ioe.What() );
|
2020-01-09 16:23:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-11-06 19:15:42 +00:00
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
LIB_SYMBOL* originalParent = new LIB_SYMBOL( *bufferedParent.get() );
|
|
|
|
aSymbolBuf->SetOriginal( originalParent );
|
2021-06-15 12:31:28 +00:00
|
|
|
originalSymbol = new LIB_SYMBOL( *libSymbol );
|
|
|
|
originalSymbol->SetParent( originalParent );
|
|
|
|
aSymbolBuf->SetOriginal( originalSymbol );
|
2019-11-06 19:15:42 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-06-15 12:31:28 +00:00
|
|
|
newCachedSymbol->SetParent( cachedParent );
|
2020-01-09 16:23:46 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2021-06-15 12:31:28 +00:00
|
|
|
aPlugin->SaveSymbol( aFileName, newCachedSymbol, aBuffer ? &properties : nullptr );
|
2020-01-09 16:23:46 +00:00
|
|
|
}
|
|
|
|
catch( const IO_ERROR& ioe )
|
|
|
|
{
|
2021-06-30 10:53:04 +00:00
|
|
|
wxLogError( errorMsg, UnescapeString( newCachedSymbol->GetName() ), aFileName,
|
|
|
|
ioe.What() );
|
2020-01-09 16:23:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-11-06 19:15:42 +00:00
|
|
|
|
2021-07-28 20:04:53 +00:00
|
|
|
auto originalBufferedParent = GetBuffer( bufferedParent->GetName() );
|
2019-11-06 19:15:42 +00:00
|
|
|
wxCHECK( originalBufferedParent, false );
|
2021-06-15 12:31:28 +00:00
|
|
|
originalSymbol = new LIB_SYMBOL( *libSymbol );
|
|
|
|
originalSymbol->SetParent( originalBufferedParent->GetSymbol() );
|
|
|
|
aSymbolBuf->SetOriginal( originalSymbol );
|
2019-11-06 19:15:42 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-09 16:23:46 +00:00
|
|
|
else
|
2019-11-06 19:15:42 +00:00
|
|
|
{
|
2020-01-09 16:23:46 +00:00
|
|
|
wxArrayString derivedSymbols;
|
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
if( GetDerivedSymbolNames( libSymbol->GetName(), derivedSymbols ) == 0 )
|
2020-01-09 16:23:46 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2021-06-15 12:31:28 +00:00
|
|
|
aPlugin->SaveSymbol( aFileName, new LIB_SYMBOL( *libSymbol ),
|
2020-01-09 16:23:46 +00:00
|
|
|
aBuffer ? &properties : nullptr );
|
|
|
|
}
|
|
|
|
catch( const IO_ERROR& ioe )
|
|
|
|
{
|
2021-06-30 10:53:04 +00:00
|
|
|
wxLogError( errorMsg, UnescapeString( libSymbol->GetName() ), aFileName,
|
|
|
|
ioe.What() );
|
2020-01-09 16:23:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
aSymbolBuf->SetOriginal( new LIB_SYMBOL( *libSymbol ) );
|
2020-01-09 16:23:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-06-15 12:31:28 +00:00
|
|
|
LIB_SYMBOL* parentSymbol = new LIB_SYMBOL( *libSymbol );
|
2020-01-09 16:23:46 +00:00
|
|
|
|
|
|
|
// Save the modified root symbol.
|
|
|
|
try
|
|
|
|
{
|
2021-03-13 13:28:52 +00:00
|
|
|
aPlugin->SaveSymbol( aFileName, parentSymbol, aBuffer ? &properties : nullptr );
|
2020-01-09 16:23:46 +00:00
|
|
|
}
|
|
|
|
catch( const IO_ERROR& ioe )
|
|
|
|
{
|
2021-06-30 10:53:04 +00:00
|
|
|
wxLogError( errorMsg, UnescapeString( libSymbol->GetName() ), aFileName,
|
|
|
|
ioe.What() );
|
2020-01-09 16:23:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
aSymbolBuf->SetOriginal( new LIB_SYMBOL( *libSymbol ) );
|
2020-01-09 16:23:46 +00:00
|
|
|
|
|
|
|
// Save the derived symbols.
|
|
|
|
for( auto entry : derivedSymbols )
|
|
|
|
{
|
2021-07-28 20:04:53 +00:00
|
|
|
std::shared_ptr<SYMBOL_BUFFER> symbol = GetBuffer( entry );
|
2022-03-25 19:51:05 +00:00
|
|
|
|
|
|
|
wxCHECK2( symbol, continue );
|
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
LIB_SYMBOL* derivedSymbol = new LIB_SYMBOL( *symbol->GetSymbol() );
|
2020-01-09 16:23:46 +00:00
|
|
|
derivedSymbol->SetParent( parentSymbol );
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2021-06-10 18:51:46 +00:00
|
|
|
aPlugin->SaveSymbol( aFileName, new LIB_SYMBOL( *derivedSymbol ),
|
2020-01-09 16:23:46 +00:00
|
|
|
aBuffer ? &properties : nullptr );
|
|
|
|
}
|
|
|
|
catch( const IO_ERROR& ioe )
|
|
|
|
{
|
2021-06-30 10:53:04 +00:00
|
|
|
wxLogError( errorMsg, UnescapeString( derivedSymbol->GetName() ), aFileName,
|
|
|
|
ioe.What() );
|
2020-01-09 16:23:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-06 19:15:42 +00:00
|
|
|
}
|
|
|
|
|
2017-11-13 13:58:39 +00:00
|
|
|
++m_hash;
|
|
|
|
return true;
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-28 20:04:53 +00:00
|
|
|
std::shared_ptr<SYMBOL_LIBRARY_MANAGER::SYMBOL_BUFFER>
|
2020-10-31 01:27:16 +00:00
|
|
|
SYMBOL_LIBRARY_MANAGER::LIB_BUFFER::GetBuffer( const wxString& aAlias ) const
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2021-06-15 12:31:28 +00:00
|
|
|
for( auto entry : m_symbols )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2021-06-15 12:31:28 +00:00
|
|
|
if( entry->GetSymbol()->GetName() == aAlias )
|
2019-11-06 19:15:42 +00:00
|
|
|
return entry;
|
|
|
|
}
|
2017-11-12 17:55:20 +00:00
|
|
|
|
2021-07-28 20:04:53 +00:00
|
|
|
return std::shared_ptr<SYMBOL_BUFFER>( nullptr );
|
2019-11-06 19:15:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-31 01:27:16 +00:00
|
|
|
bool SYMBOL_LIBRARY_MANAGER::LIB_BUFFER::HasDerivedSymbols( const wxString& aParentName ) const
|
2019-11-06 19:15:42 +00:00
|
|
|
{
|
2021-06-15 12:31:28 +00:00
|
|
|
for( auto entry : m_symbols )
|
2019-11-06 19:15:42 +00:00
|
|
|
{
|
2021-06-15 12:31:28 +00:00
|
|
|
if( entry->GetSymbol()->IsAlias() )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2021-06-15 12:31:28 +00:00
|
|
|
LIB_SYMBOL_SPTR parent = entry->GetSymbol()->GetParent().lock();
|
2019-11-06 19:15:42 +00:00
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
// Check for inherited symbol without a valid parent.
|
2019-11-06 19:15:42 +00:00
|
|
|
wxCHECK( parent, false );
|
|
|
|
|
|
|
|
if( parent->GetName() == aParentName )
|
|
|
|
return true;
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-06 19:15:42 +00:00
|
|
|
return false;
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-31 01:27:16 +00:00
|
|
|
void SYMBOL_LIBRARY_MANAGER::LIB_BUFFER::GetRootSymbolNames( wxArrayString& aRootSymbolNames )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2021-06-15 12:31:28 +00:00
|
|
|
for( auto entry : m_symbols )
|
2019-11-06 19:15:42 +00:00
|
|
|
{
|
2021-06-15 12:31:28 +00:00
|
|
|
if( entry->GetSymbol()->IsAlias() )
|
2019-11-06 19:15:42 +00:00
|
|
|
continue;
|
2017-11-12 17:55:20 +00:00
|
|
|
|
2021-06-30 10:53:04 +00:00
|
|
|
aRootSymbolNames.Add( UnescapeString( entry->GetSymbol()->GetName() ) );
|
2019-11-06 19:15:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-31 01:27:16 +00:00
|
|
|
size_t SYMBOL_LIBRARY_MANAGER::LIB_BUFFER::GetDerivedSymbolNames( const wxString& aSymbolName,
|
|
|
|
wxArrayString& aList )
|
2020-01-09 16:23:46 +00:00
|
|
|
{
|
|
|
|
wxCHECK( !aSymbolName.IsEmpty(), 0 );
|
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
for( auto entry : m_symbols )
|
2020-01-09 16:23:46 +00:00
|
|
|
{
|
2021-06-15 12:31:28 +00:00
|
|
|
if( entry->GetSymbol()->IsAlias() )
|
2020-01-09 16:23:46 +00:00
|
|
|
{
|
2021-06-15 12:31:28 +00:00
|
|
|
LIB_SYMBOL_SPTR parent = entry->GetSymbol()->GetParent().lock();
|
2020-01-09 16:23:46 +00:00
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
// Check for inherited symbol without a valid parent.
|
2020-01-09 16:23:46 +00:00
|
|
|
wxCHECK( parent, false );
|
|
|
|
|
|
|
|
if( parent->GetName() == aSymbolName )
|
2021-06-15 12:31:28 +00:00
|
|
|
aList.Add( entry->GetSymbol()->GetName() );
|
2020-01-09 16:23:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return aList.GetCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-28 20:04:53 +00:00
|
|
|
int SYMBOL_LIBRARY_MANAGER::LIB_BUFFER::removeChildSymbols( std::shared_ptr<SYMBOL_BUFFER> aSymbolBuf )
|
2019-11-06 19:15:42 +00:00
|
|
|
{
|
2021-06-15 12:31:28 +00:00
|
|
|
wxCHECK( aSymbolBuf && aSymbolBuf->GetSymbol()->IsRoot(), 0 );
|
2019-11-06 19:15:42 +00:00
|
|
|
|
|
|
|
int cnt = 0;
|
2021-07-28 20:04:53 +00:00
|
|
|
std::deque< std::shared_ptr<SYMBOL_BUFFER> >::iterator it = m_symbols.begin();
|
2019-11-06 19:15:42 +00:00
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
while( it != m_symbols.end() )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
if( (*it)->GetSymbol()->IsRoot() )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2019-11-06 19:15:42 +00:00
|
|
|
++it;
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
2019-11-06 19:15:42 +00:00
|
|
|
else
|
|
|
|
{
|
2021-06-15 12:31:28 +00:00
|
|
|
LIB_SYMBOL_SPTR parent = (*it)->GetSymbol()->GetParent().lock();
|
2017-11-12 17:55:20 +00:00
|
|
|
|
2019-11-06 19:15:42 +00:00
|
|
|
wxCHECK2( parent, ++it; continue );
|
2017-11-12 17:55:20 +00:00
|
|
|
|
2021-06-15 12:31:28 +00:00
|
|
|
if( parent->GetName() == aSymbolBuf->GetSymbol()->GetName() )
|
2019-11-06 19:15:42 +00:00
|
|
|
{
|
2021-06-15 12:31:28 +00:00
|
|
|
wxCHECK2( parent == aSymbolBuf->GetSymbol()->SharedPtr(), ++it; continue );
|
2019-11-06 19:15:42 +00:00
|
|
|
|
|
|
|
m_deleted.emplace_back( *it );
|
2021-06-15 12:31:28 +00:00
|
|
|
it = m_symbols.erase( it );
|
2019-11-06 19:15:42 +00:00
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
2019-11-06 19:15:42 +00:00
|
|
|
return cnt;
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|