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
|
2020-02-13 13:39:52 +00:00
|
|
|
* Copyright (C) 2019-2020 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <lib_manager.h>
|
|
|
|
#include <class_libentry.h>
|
|
|
|
#include <class_library.h>
|
2018-01-30 10:49:51 +00:00
|
|
|
#include <lib_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>
|
2017-11-12 17:55:20 +00:00
|
|
|
#include <list>
|
|
|
|
|
|
|
|
|
2019-06-05 19:15:57 +00:00
|
|
|
LIB_MANAGER::LIB_MANAGER( LIB_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 );
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-13 11:44:12 +00:00
|
|
|
void LIB_MANAGER::Sync( bool aForce,
|
|
|
|
std::function<void( int, int, const wxString& )> aProgressCallback )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2019-07-10 17:59:39 +00:00
|
|
|
m_logger.Activate();
|
2017-11-12 13:24:55 +00:00
|
|
|
{
|
2019-08-15 07:19:15 +00:00
|
|
|
int libTableHash = symTable()->GetModifyHash();
|
|
|
|
|
|
|
|
if( aForce || m_syncHash != libTableHash )
|
|
|
|
{
|
|
|
|
getAdapter()->Sync( aForce, aProgressCallback );
|
|
|
|
m_syncHash = libTableHash;
|
|
|
|
}
|
2017-11-12 13:24:55 +00:00
|
|
|
}
|
2019-08-15 07:19:15 +00:00
|
|
|
m_logger.Deactivate();
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-13 11:44:12 +00:00
|
|
|
bool LIB_MANAGER::HasModifications() const
|
|
|
|
{
|
2019-07-10 17:59:39 +00:00
|
|
|
for( const auto& lib : m_libs )
|
2019-06-13 11:44:12 +00:00
|
|
|
{
|
2020-10-22 11:48:28 +00:00
|
|
|
if( lib.second.IsModified() && !IsLibraryReadOnly( lib.first ) )
|
2019-06-13 11:44:12 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-12 17:55:20 +00:00
|
|
|
int LIB_MANAGER::GetHash() const
|
|
|
|
{
|
2018-01-08 14:18:08 +00:00
|
|
|
int hash = symTable()->GetModifyHash();
|
2017-11-12 17:55:20 +00:00
|
|
|
|
2019-07-10 17:59:39 +00:00
|
|
|
for( const auto& lib : m_libs )
|
|
|
|
hash += lib.second.GetHash();
|
2017-11-12 17:55:20 +00:00
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int LIB_MANAGER::GetLibraryHash( const wxString& aLibrary ) const
|
|
|
|
{
|
|
|
|
const auto libBufIt = m_libs.find( aLibrary );
|
2017-11-12 13:24:55 +00:00
|
|
|
|
|
|
|
if( libBufIt != m_libs.end() )
|
|
|
|
return libBufIt->second.GetHash();
|
|
|
|
|
2018-02-16 10:59:22 +00:00
|
|
|
auto 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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wxArrayString LIB_MANAGER::GetLibraryNames() const
|
|
|
|
{
|
|
|
|
wxArrayString res;
|
|
|
|
|
2018-01-08 14:18:08 +00:00
|
|
|
for( const auto& libName : symTable()->GetLogicalLibs() )
|
2017-11-12 17:55:20 +00:00
|
|
|
res.Add( libName );
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-16 10:59:22 +00:00
|
|
|
SYMBOL_LIB_TABLE_ROW* LIB_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
|
|
|
|
{
|
|
|
|
row = symTable()->FindRow( aLibrary );
|
|
|
|
}
|
|
|
|
catch( const IO_ERROR& e )
|
|
|
|
{
|
2019-07-02 15:15:26 +00:00
|
|
|
wxLogMessage( _( "Cannot find library \"%s\" in the Symbol Library Table (%s)" ),
|
|
|
|
aLibrary, e.What() );
|
2018-02-16 10:59:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return row;
|
2018-02-06 15:14:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-13 13:39:52 +00:00
|
|
|
bool LIB_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
|
|
|
|
|
|
|
|
auto it = m_libs.find( aLibrary );
|
|
|
|
|
|
|
|
if( it != m_libs.end() )
|
|
|
|
{
|
|
|
|
// Handle buffered library
|
|
|
|
LIB_BUFFER& libBuf = it->second;
|
|
|
|
|
|
|
|
const auto& partBuffers = libBuf.GetBuffers();
|
|
|
|
|
|
|
|
for( const auto& partBuf : partBuffers )
|
|
|
|
{
|
|
|
|
if( !libBuf.SaveBuffer( partBuf, &*pi, true ) )
|
|
|
|
{
|
|
|
|
// Something went wrong, but try to save other libraries
|
|
|
|
res = false;
|
|
|
|
}
|
|
|
|
}
|
2017-11-14 13:40:37 +00:00
|
|
|
|
|
|
|
// clear the deleted parts 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
|
|
|
|
PROPERTIES properties;
|
|
|
|
properties.emplace( SCH_LEGACY_PLUGIN::PropBuffering, "" );
|
|
|
|
|
|
|
|
for( auto part : getOriginalParts( aLibrary ) )
|
2019-11-06 19:15:42 +00:00
|
|
|
{
|
|
|
|
LIB_PART* newSymbol;
|
|
|
|
|
|
|
|
if( part->IsAlias() )
|
|
|
|
{
|
|
|
|
std::shared_ptr< LIB_PART > oldParent = part->GetParent().lock();
|
|
|
|
|
|
|
|
wxCHECK_MSG( oldParent, false,
|
|
|
|
wxString::Format( "Derived symbol '%s' found with undefined parent.",
|
|
|
|
part->GetName() ) );
|
|
|
|
|
|
|
|
LIB_PART* libParent = pi->LoadSymbol( aLibrary, oldParent->GetName(), &properties );
|
|
|
|
|
|
|
|
if( !libParent )
|
|
|
|
{
|
|
|
|
libParent = new LIB_PART( *oldParent.get() );
|
|
|
|
pi->SaveSymbol( aLibrary, libParent, &properties );
|
|
|
|
}
|
|
|
|
|
|
|
|
newSymbol = new LIB_PART( *part );
|
|
|
|
newSymbol->SetParent( libParent );
|
|
|
|
pi->SaveSymbol( aLibrary, newSymbol, &properties );
|
|
|
|
}
|
|
|
|
else if( !pi->LoadSymbol( aLibrary, part->GetName(), &properties ) )
|
|
|
|
{
|
|
|
|
pi->SaveSymbol( aLibrary, new LIB_PART( *part ), &properties );
|
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-12 17:55:20 +00:00
|
|
|
bool LIB_MANAGER::IsLibraryModified( const wxString& aLibrary ) const
|
|
|
|
{
|
|
|
|
auto it = m_libs.find( aLibrary );
|
|
|
|
return it != m_libs.end() ? it->second.IsModified() : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool LIB_MANAGER::IsPartModified( const wxString& aAlias, const wxString& aLibrary ) const
|
|
|
|
{
|
|
|
|
auto libIt = m_libs.find( aLibrary );
|
|
|
|
|
|
|
|
if( libIt == m_libs.end() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const LIB_BUFFER& buf = libIt->second;
|
|
|
|
auto partBuf = buf.GetBuffer( aAlias );
|
|
|
|
return partBuf ? partBuf->IsModified() : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-13 22:13:10 +00:00
|
|
|
bool LIB_MANAGER::ClearLibraryModified( const wxString& aLibrary ) const
|
|
|
|
{
|
|
|
|
auto libIt = m_libs.find( aLibrary );
|
|
|
|
|
|
|
|
if( libIt == m_libs.end() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for( auto& partBuf : libIt->second.GetBuffers() )
|
|
|
|
{
|
|
|
|
SCH_SCREEN* screen = partBuf->GetScreen();
|
|
|
|
|
|
|
|
if( screen )
|
|
|
|
screen->ClrModify();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool LIB_MANAGER::ClearPartModified( const wxString& aAlias, const wxString& aLibrary ) const
|
|
|
|
{
|
|
|
|
auto libI = m_libs.find( aLibrary );
|
|
|
|
|
|
|
|
if( libI == m_libs.end() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto partBuf = libI->second.GetBuffer( aAlias );
|
|
|
|
wxCHECK( partBuf, false );
|
|
|
|
|
|
|
|
partBuf->GetScreen()->ClrModify();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-12 17:55:20 +00:00
|
|
|
bool LIB_MANAGER::IsLibraryReadOnly( const wxString& aLibrary ) const
|
|
|
|
{
|
|
|
|
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-08-29 23:19:05 +00:00
|
|
|
bool LIB_MANAGER::IsLibraryLoaded( const wxString& aLibrary ) const
|
|
|
|
{
|
|
|
|
wxCHECK( LibraryExists( aLibrary ), false );
|
|
|
|
|
|
|
|
return symTable()->IsSymbolLibLoaded( aLibrary );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-06 19:15:42 +00:00
|
|
|
std::list<LIB_PART*> LIB_MANAGER::GetAliases( const wxString& aLibrary ) const
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2019-11-06 19:15:42 +00:00
|
|
|
std::list<LIB_PART*> ret;
|
2017-11-12 17:55:20 +00:00
|
|
|
wxCHECK( LibraryExists( aLibrary ), ret );
|
|
|
|
|
|
|
|
auto libIt = m_libs.find( aLibrary );
|
|
|
|
|
|
|
|
if( libIt != m_libs.end() )
|
|
|
|
{
|
|
|
|
for( auto& partBuf : libIt->second.GetBuffers() )
|
|
|
|
{
|
2019-11-06 19:15:42 +00:00
|
|
|
ret.push_back( partBuf->GetPart() );
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-06 19:15:42 +00:00
|
|
|
std::vector<LIB_PART*> 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LIB_PART* LIB_MANAGER::GetBufferedPart( const wxString& aAlias, const wxString& aLibrary )
|
|
|
|
{
|
|
|
|
wxCHECK( LibraryExists( aLibrary ), nullptr );
|
|
|
|
|
|
|
|
// try the library buffers first
|
|
|
|
LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
|
|
|
|
LIB_PART* bufferedPart = libBuf.GetPart( aAlias );
|
|
|
|
|
|
|
|
if( !bufferedPart ) // no buffer part found
|
|
|
|
{
|
2017-11-13 14:25:55 +00:00
|
|
|
// create a copy of the part
|
2018-02-13 07:50:51 +00:00
|
|
|
try
|
|
|
|
{
|
2019-11-06 19:15:42 +00:00
|
|
|
LIB_PART* part = symTable()->LoadSymbol( aLibrary, aAlias );
|
2018-11-21 19:50:26 +00:00
|
|
|
|
2019-11-06 19:15:42 +00:00
|
|
|
if( part == nullptr )
|
2018-11-21 19:50:26 +00:00
|
|
|
THROW_IO_ERROR( _( "Symbol not found." ) );
|
|
|
|
|
2019-11-06 19:15:42 +00:00
|
|
|
LIB_PART* bufferedParent = nullptr;
|
|
|
|
|
|
|
|
// Create parent symbols on demand so parent symbol can be set.
|
|
|
|
if( part->IsAlias() )
|
|
|
|
{
|
|
|
|
std::shared_ptr< LIB_PART > parent = part->GetParent().lock();
|
|
|
|
wxCHECK_MSG( parent, nullptr,
|
|
|
|
wxString::Format( "Derived symbol '%s' found with undefined parent.",
|
|
|
|
part->GetName() ) );
|
|
|
|
|
|
|
|
// Check if the parent symbol buffer has already be created.
|
|
|
|
bufferedParent = libBuf.GetPart( parent->GetName() );
|
|
|
|
|
|
|
|
if( !bufferedParent )
|
|
|
|
{
|
|
|
|
bufferedParent = new LIB_PART( *parent.get() );
|
2020-05-21 02:27:48 +00:00
|
|
|
libBuf.CreateBuffer( bufferedParent, new SCH_SCREEN );
|
2019-11-06 19:15:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bufferedPart = new LIB_PART( *part );
|
|
|
|
|
|
|
|
if( bufferedParent )
|
|
|
|
bufferedPart->SetParent( bufferedParent );
|
|
|
|
|
2020-05-21 02:27:48 +00:00
|
|
|
libBuf.CreateBuffer( bufferedPart, 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
|
|
|
{
|
2019-07-02 15:15:26 +00:00
|
|
|
wxLogMessage( _( "Error loading symbol \"%s\" from library \"%s\". (%s)" ),
|
|
|
|
aAlias, aLibrary, e.What() );
|
2018-01-10 11:04:34 +00:00
|
|
|
bufferedPart = nullptr;
|
|
|
|
}
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return bufferedPart;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SCH_SCREEN* LIB_MANAGER::GetScreen( const wxString& aAlias, const wxString& aLibrary )
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
auto partBuf = buf.GetBuffer( aAlias );
|
|
|
|
return partBuf ? partBuf->GetScreen() : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-09 08:37:31 +00:00
|
|
|
bool LIB_MANAGER::UpdatePart( LIB_PART* aPart, const wxString& aLibrary )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
|
|
|
wxCHECK( LibraryExists( aLibrary ), false );
|
|
|
|
wxCHECK( aPart, false );
|
|
|
|
LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
|
2018-01-09 08:37:31 +00:00
|
|
|
auto partBuf = libBuf.GetBuffer( aPart->GetName() );
|
2017-11-12 17:55:20 +00:00
|
|
|
|
2019-12-26 13:00:59 +00:00
|
|
|
if( partBuf ) // Existing symbol.
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2019-12-26 13:00:59 +00:00
|
|
|
LIB_PART* bufferedPart = const_cast< LIB_PART* >( partBuf->GetPart() );
|
2019-12-13 21:51:59 +00:00
|
|
|
|
2019-12-26 13:00:59 +00:00
|
|
|
wxCHECK( bufferedPart, false );
|
2019-12-13 21:51:59 +00:00
|
|
|
|
2019-12-26 13:00:59 +00:00
|
|
|
*bufferedPart = *aPart;
|
|
|
|
partBuf->GetScreen()->SetModify();
|
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
|
|
|
{
|
2019-12-26 13:00:59 +00:00
|
|
|
LIB_PART* partCopy = new LIB_PART( *aPart, nullptr );
|
|
|
|
|
|
|
|
partCopy->SetLibId( LIB_ID( aLibrary, aPart->GetLibId().GetLibItemName() ) );
|
|
|
|
|
2020-05-21 02:27:48 +00:00
|
|
|
SCH_SCREEN* screen = new SCH_SCREEN;
|
2017-11-12 17:55:20 +00:00
|
|
|
libBuf.CreateBuffer( partCopy, screen );
|
|
|
|
screen->SetModify();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-06 19:15:42 +00:00
|
|
|
bool LIB_MANAGER::UpdatePartAfterRename( LIB_PART* aPart, const wxString& aOldName,
|
2018-01-19 18:56:01 +00:00
|
|
|
const wxString& aLibrary )
|
|
|
|
{
|
|
|
|
LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
|
2019-11-06 19:15:42 +00:00
|
|
|
auto partBuf = libBuf.GetBuffer( aOldName );
|
|
|
|
|
2019-12-16 15:11:06 +00:00
|
|
|
wxCHECK( partBuf, false );
|
2018-01-19 18:56:01 +00:00
|
|
|
|
2019-12-26 13:00:59 +00:00
|
|
|
libBuf.UpdateBuffer( partBuf, aPart );
|
2019-02-27 15:34:51 +00:00
|
|
|
SetCurrentPart( aPart->GetName() );
|
2018-08-25 12:21:09 +00:00
|
|
|
m_frame.SyncLibraries( false );
|
|
|
|
|
2018-01-19 18:56:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-12 17:55:20 +00:00
|
|
|
bool LIB_MANAGER::FlushPart( const wxString& aAlias, const wxString& aLibrary )
|
|
|
|
{
|
|
|
|
auto it = m_libs.find( aLibrary );
|
|
|
|
|
|
|
|
if( it == m_libs.end() ) // no items to flush
|
|
|
|
return true;
|
|
|
|
|
|
|
|
auto partBuf = it->second.GetBuffer( aAlias );
|
|
|
|
wxCHECK( partBuf, false );
|
|
|
|
|
2018-01-08 14:18:08 +00:00
|
|
|
return it->second.SaveBuffer( partBuf, symTable() );
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-19 18:56:01 +00:00
|
|
|
LIB_ID LIB_MANAGER::RevertPart( 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
|
|
|
|
|
|
|
auto partBuf = it->second.GetBuffer( aAlias );
|
2018-01-19 18:56:01 +00:00
|
|
|
wxCHECK( partBuf, LIB_ID( aLibrary, aAlias ) );
|
|
|
|
LIB_PART original( *partBuf->GetOriginal() );
|
2017-11-12 17:55:20 +00:00
|
|
|
|
2018-01-19 18:56:01 +00:00
|
|
|
if( original.GetName() != aAlias )
|
|
|
|
{
|
|
|
|
UpdatePartAfterRename( &original, aAlias, aLibrary );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
partBuf->SetPart( new LIB_PART( original ) );
|
|
|
|
m_frame.SyncLibraries( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
return LIB_ID( aLibrary, original.GetName() );
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool LIB_MANAGER::RevertLibrary( const wxString& aLibrary )
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-13 11:44:12 +00:00
|
|
|
bool LIB_MANAGER::RevertAll()
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
RevertPart( lib.first, buffer->GetOriginal()->GetName() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return retv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-12 17:55:20 +00:00
|
|
|
bool LIB_MANAGER::RemovePart( const wxString& aAlias, const wxString& aLibrary )
|
|
|
|
{
|
|
|
|
LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
|
|
|
|
auto partBuf = libBuf.GetBuffer( aAlias );
|
|
|
|
wxCHECK( partBuf, false );
|
|
|
|
|
2019-11-06 19:15:42 +00:00
|
|
|
bool retv = true;
|
|
|
|
|
|
|
|
retv &= libBuf.DeleteBuffer( partBuf );
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-06 19:15:42 +00:00
|
|
|
LIB_PART* LIB_MANAGER::GetAlias( const wxString& aAlias, 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() )
|
|
|
|
{
|
|
|
|
LIB_PART* part = libIt->second.GetPart( aAlias );
|
|
|
|
|
|
|
|
if( part )
|
2019-11-06 19:15:42 +00:00
|
|
|
return part;
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the original part
|
2019-11-06 19:15:42 +00:00
|
|
|
LIB_PART* 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 )
|
|
|
|
{
|
2019-07-02 15:15:26 +00:00
|
|
|
wxLogMessage( _( "Cannot load symbol \"%s\" from library \"%s\" (%s)" ),
|
|
|
|
aAlias, aLibrary, e.What() );
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool LIB_MANAGER::PartExists( const wxString& aAlias, const wxString& aLibrary ) const
|
|
|
|
{
|
|
|
|
auto libBufIt = m_libs.find( aLibrary );
|
2019-11-06 19:15:42 +00:00
|
|
|
LIB_PART* 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
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-28 09:43:41 +00:00
|
|
|
bool LIB_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
|
|
|
|
2017-11-12 17:55:20 +00:00
|
|
|
wxString LIB_MANAGER::GetUniqueLibraryName() const
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-06 19:15:42 +00:00
|
|
|
void LIB_MANAGER::GetRootSymbolNames( const wxString& aLibraryName,
|
|
|
|
wxArrayString& aRootSymbolNames )
|
|
|
|
{
|
|
|
|
LIB_BUFFER& libBuf = getLibraryBuffer( aLibraryName );
|
|
|
|
|
|
|
|
libBuf.GetRootSymbolNames( aRootSymbolNames );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-12 15:44:53 +00:00
|
|
|
bool LIB_MANAGER:: HasDerivedSymbols( const wxString& aSymbolName,
|
|
|
|
const wxString& aLibraryName )
|
|
|
|
{
|
|
|
|
LIB_BUFFER& libBuf = getLibraryBuffer( aLibraryName );
|
|
|
|
|
|
|
|
return libBuf.HasDerivedSymbols( aSymbolName );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-12 17:55:20 +00:00
|
|
|
wxString LIB_MANAGER::getLibraryName( const wxString& aFilePath )
|
|
|
|
{
|
|
|
|
wxFileName fn( aFilePath );
|
|
|
|
return fn.GetName();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-22 13:02:45 +00:00
|
|
|
bool LIB_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() );
|
|
|
|
|
|
|
|
if( relPath.IsEmpty() )
|
|
|
|
relPath = aFilePath;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-08 14:18:08 +00:00
|
|
|
SYMBOL_LIB_TABLE* LIB_MANAGER::symTable() const
|
|
|
|
{
|
|
|
|
return m_frame.Prj().SchSymbolLibTable();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-13 13:58:39 +00:00
|
|
|
std::set<LIB_PART*> LIB_MANAGER::getOriginalParts( const wxString& aLibrary )
|
|
|
|
{
|
|
|
|
std::set<LIB_PART*> parts;
|
2017-11-13 14:25:55 +00:00
|
|
|
wxCHECK( LibraryExists( aLibrary ), parts );
|
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 )
|
|
|
|
{
|
2019-11-06 19:15:42 +00:00
|
|
|
LIB_PART* alias = symTable()->LoadSymbol( aLibrary, aliasName );
|
|
|
|
parts.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
|
|
|
{
|
2019-07-02 15:15:26 +00:00
|
|
|
wxLogMessage( _( "Cannot enumerate library \"%s\" (%s)" ), aLibrary, e.What() );
|
2018-02-13 07:50:51 +00:00
|
|
|
}
|
2017-11-13 13:58:39 +00:00
|
|
|
|
|
|
|
return parts;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-12 17:55:20 +00:00
|
|
|
LIB_MANAGER::LIB_BUFFER& LIB_MANAGER::getLibraryBuffer( const wxString& aLibrary )
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2017-11-13 13:58:39 +00:00
|
|
|
for( auto part : getOriginalParts( aLibrary ) )
|
2019-11-06 19:15:42 +00:00
|
|
|
{
|
|
|
|
LIB_PART* newSymbol;
|
|
|
|
|
|
|
|
if( part->IsAlias() )
|
|
|
|
{
|
|
|
|
std::shared_ptr< LIB_PART > oldParent = part->GetParent().lock();
|
|
|
|
|
|
|
|
wxCHECK_MSG( oldParent, buf,
|
|
|
|
wxString::Format( "Derived symbol '%s' found with undefined parent.",
|
|
|
|
part->GetName() ) );
|
|
|
|
|
|
|
|
LIB_PART* libParent = buf.GetPart( oldParent->GetName() );
|
|
|
|
|
|
|
|
if( !libParent )
|
|
|
|
{
|
|
|
|
libParent = new LIB_PART( *oldParent.get() );
|
2020-05-21 02:27:48 +00:00
|
|
|
buf.CreateBuffer( libParent, new SCH_SCREEN );
|
2019-11-06 19:15:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
newSymbol = new LIB_PART( *part );
|
|
|
|
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
|
|
|
}
|
|
|
|
else if( !buf.GetPart( part->GetName() ) )
|
|
|
|
{
|
2020-05-21 02:27:48 +00:00
|
|
|
buf.CreateBuffer( new LIB_PART( *part ), new SCH_SCREEN );
|
2019-11-06 19:15:42 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-12 17:55:20 +00:00
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-05 19:15:57 +00:00
|
|
|
LIB_MANAGER::PART_BUFFER::PART_BUFFER( LIB_PART* aPart, std::unique_ptr<SCH_SCREEN> aScreen ) :
|
2019-06-13 11:44:12 +00:00
|
|
|
m_screen( std::move( aScreen ) ),
|
2019-06-05 19:15:57 +00:00
|
|
|
m_part( aPart )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
|
|
|
m_original = new LIB_PART( *aPart );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LIB_MANAGER::PART_BUFFER::~PART_BUFFER()
|
|
|
|
{
|
|
|
|
delete m_part;
|
|
|
|
delete m_original;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LIB_MANAGER::PART_BUFFER::SetPart( LIB_PART* aPart )
|
|
|
|
{
|
|
|
|
wxCHECK( m_part != aPart, /* void */ );
|
|
|
|
wxASSERT( aPart );
|
|
|
|
delete m_part;
|
|
|
|
m_part = aPart;
|
2018-11-21 19:50:26 +00:00
|
|
|
|
|
|
|
// If the part moves libraries then the original moves with it
|
|
|
|
if( m_original->GetLibId().GetLibNickname() != m_part->GetLibId().GetLibNickname() )
|
|
|
|
{
|
|
|
|
m_original->SetLibId( LIB_ID( m_part->GetLibId().GetLibNickname(),
|
|
|
|
m_original->GetLibId().GetLibItemName() ) );
|
|
|
|
}
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LIB_MANAGER::PART_BUFFER::SetOriginal( LIB_PART* aPart )
|
|
|
|
{
|
|
|
|
wxCHECK( m_original != aPart, /* void */ );
|
|
|
|
wxASSERT( aPart );
|
|
|
|
delete m_original;
|
|
|
|
m_original = aPart;
|
2018-11-21 19:50:26 +00:00
|
|
|
|
|
|
|
// The original is not allowed to have a different library than its part
|
|
|
|
if( m_original->GetLibId().GetLibNickname() != m_part->GetLibId().GetLibNickname() )
|
|
|
|
{
|
|
|
|
m_original->SetLibId( LIB_ID( m_part->GetLibId().GetLibNickname(),
|
|
|
|
m_original->GetLibId().GetLibItemName() ) );
|
|
|
|
}
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool LIB_MANAGER::PART_BUFFER::IsModified() const
|
|
|
|
{
|
|
|
|
return m_screen && m_screen->IsModify();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-06 19:15:42 +00:00
|
|
|
LIB_PART* LIB_MANAGER::LIB_BUFFER::GetPart( const wxString& aAlias ) const
|
|
|
|
{
|
|
|
|
auto buf = GetBuffer( aAlias );
|
|
|
|
|
|
|
|
if( !buf )
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
LIB_PART* part = buf->GetPart();
|
|
|
|
|
|
|
|
wxCHECK( part, nullptr );
|
|
|
|
|
|
|
|
return part;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-12 17:55:20 +00:00
|
|
|
bool LIB_MANAGER::LIB_BUFFER::CreateBuffer( LIB_PART* aCopy, SCH_SCREEN* aScreen )
|
|
|
|
{
|
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 );
|
|
|
|
auto partBuf = std::make_shared<PART_BUFFER>( aCopy, std::move( screen ) );
|
2017-11-12 17:55:20 +00:00
|
|
|
m_parts.push_back( partBuf );
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-13 11:44:12 +00:00
|
|
|
bool LIB_MANAGER::LIB_BUFFER::UpdateBuffer( LIB_MANAGER::PART_BUFFER::PTR aPartBuf,
|
|
|
|
LIB_PART* aCopy )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2019-12-26 13:00:59 +00:00
|
|
|
wxCHECK( aCopy && aPartBuf, false );
|
|
|
|
|
|
|
|
LIB_PART* bufferedPart = aPartBuf->GetPart();
|
2017-11-12 17:55:20 +00:00
|
|
|
|
2019-12-26 13:00:59 +00:00
|
|
|
wxCHECK( bufferedPart, false );
|
|
|
|
|
|
|
|
*bufferedPart = *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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool LIB_MANAGER::LIB_BUFFER::DeleteBuffer( LIB_MANAGER::PART_BUFFER::PTR aPartBuf )
|
|
|
|
{
|
|
|
|
auto partBufIt = std::find( m_parts.begin(), m_parts.end(), aPartBuf );
|
|
|
|
wxCHECK( partBufIt != m_parts.end(), false );
|
2019-11-06 19:15:42 +00:00
|
|
|
|
|
|
|
bool retv = true;
|
|
|
|
|
|
|
|
// Remove all derived symbols to prevent broken inheritance.
|
2019-12-12 19:36:29 +00:00
|
|
|
if( aPartBuf->GetPart()->IsRoot() && HasDerivedSymbols( aPartBuf->GetPart()->GetName() )
|
2020-09-26 11:40:32 +00:00
|
|
|
&& removeChildSymbols( aPartBuf ) == 0 )
|
|
|
|
{
|
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
|
|
|
|
2017-11-12 17:55:20 +00:00
|
|
|
m_deleted.emplace_back( *partBufIt );
|
|
|
|
m_parts.erase( partBufIt );
|
|
|
|
++m_hash;
|
|
|
|
|
2019-11-06 19:15:42 +00:00
|
|
|
return retv;
|
2017-11-12 17:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool LIB_MANAGER::LIB_BUFFER::SaveBuffer( LIB_MANAGER::PART_BUFFER::PTR aPartBuf,
|
2019-08-15 07:19:15 +00:00
|
|
|
SYMBOL_LIB_TABLE* aLibTable )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
|
|
|
wxCHECK( aPartBuf, false );
|
|
|
|
LIB_PART* part = aPartBuf->GetPart();
|
|
|
|
wxCHECK( part, false );
|
2019-11-06 19:15:42 +00:00
|
|
|
SYMBOL_LIB_TABLE::SAVE_T result;
|
|
|
|
|
|
|
|
if( part->IsAlias() )
|
|
|
|
{
|
|
|
|
LIB_PART* originalPart;
|
|
|
|
LIB_PART* newCachedPart = new LIB_PART( *part );
|
|
|
|
std::shared_ptr< LIB_PART > bufferedParent = part->GetParent().lock();
|
|
|
|
|
|
|
|
wxCHECK( bufferedParent, false );
|
|
|
|
|
|
|
|
LIB_PART* cachedParent = aLibTable->LoadSymbol( m_libName, bufferedParent->GetName() );
|
|
|
|
|
|
|
|
if( !cachedParent )
|
|
|
|
{
|
|
|
|
cachedParent = new LIB_PART( *bufferedParent.get() );
|
|
|
|
newCachedPart->SetParent( cachedParent );
|
|
|
|
result = aLibTable->SaveSymbol( m_libName, cachedParent );
|
|
|
|
wxCHECK( result == SYMBOL_LIB_TABLE::SAVE_OK, false );
|
|
|
|
result = aLibTable->SaveSymbol( m_libName, newCachedPart );
|
|
|
|
wxCHECK( result == SYMBOL_LIB_TABLE::SAVE_OK, false );
|
|
|
|
|
|
|
|
LIB_PART* originalParent = new LIB_PART( *bufferedParent.get() );
|
|
|
|
aPartBuf->SetOriginal( originalParent );
|
|
|
|
originalPart = new LIB_PART( *part );
|
|
|
|
originalPart->SetParent( originalParent );
|
|
|
|
aPartBuf->SetOriginal( originalPart );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newCachedPart->SetParent( cachedParent );
|
|
|
|
result = aLibTable->SaveSymbol( m_libName, newCachedPart );
|
|
|
|
wxCHECK( result == SYMBOL_LIB_TABLE::SAVE_OK, false );
|
|
|
|
|
|
|
|
LIB_MANAGER::PART_BUFFER::PTR originalBufferedParent =
|
|
|
|
GetBuffer( bufferedParent->GetName() );
|
|
|
|
wxCHECK( originalBufferedParent, false );
|
|
|
|
originalPart = new LIB_PART( *part );
|
|
|
|
originalPart->SetParent( originalBufferedParent->GetPart() );
|
|
|
|
aPartBuf->SetOriginal( originalPart );
|
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
|
|
|
|
if( GetDerivedSymbolNames( part->GetName(), derivedSymbols ) == 0 )
|
|
|
|
{
|
|
|
|
result = aLibTable->SaveSymbol( m_libName, new LIB_PART( *part ) );
|
|
|
|
wxCHECK( result == SYMBOL_LIB_TABLE::SAVE_OK, false );
|
|
|
|
aPartBuf->SetOriginal( new LIB_PART( *part ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LIB_PART* parentSymbol = new LIB_PART( *part );
|
|
|
|
|
|
|
|
aLibTable->SaveSymbol( m_libName, parentSymbol );
|
|
|
|
|
|
|
|
for( auto entry : derivedSymbols )
|
|
|
|
{
|
|
|
|
LIB_MANAGER::PART_BUFFER::PTR symbol = GetBuffer( entry );
|
|
|
|
LIB_PART* derivedSymbol = new LIB_PART( *symbol->GetPart() );
|
|
|
|
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
|
|
|
|
|
|
|
|
2017-11-13 13:58:39 +00:00
|
|
|
bool LIB_MANAGER::LIB_BUFFER::SaveBuffer( LIB_MANAGER::PART_BUFFER::PTR aPartBuf,
|
2019-08-15 07:19:15 +00:00
|
|
|
SCH_PLUGIN* aPlugin, bool aBuffer )
|
2017-11-13 13:58:39 +00:00
|
|
|
{
|
|
|
|
wxCHECK( aPartBuf, false );
|
|
|
|
LIB_PART* part = aPartBuf->GetPart();
|
|
|
|
wxCHECK( part, false );
|
|
|
|
|
2020-01-09 16:23:46 +00:00
|
|
|
wxString errorMsg = _( "An error \"%s\" occurred saving symbol \"%s\" to library \"%s\"" );
|
|
|
|
|
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, "" );
|
|
|
|
|
2019-11-06 19:15:42 +00:00
|
|
|
if( part->IsAlias() )
|
|
|
|
{
|
|
|
|
LIB_PART* originalPart;
|
|
|
|
LIB_PART* newCachedPart = new LIB_PART( *part );
|
|
|
|
std::shared_ptr< LIB_PART > bufferedParent = part->GetParent().lock();
|
|
|
|
|
|
|
|
wxCHECK( bufferedParent, false );
|
|
|
|
|
2020-01-09 16:23:46 +00:00
|
|
|
LIB_PART* cachedParent = nullptr;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
cachedParent = aPlugin->LoadSymbol( m_libName, bufferedParent->GetName() );
|
|
|
|
}
|
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 )
|
|
|
|
{
|
|
|
|
cachedParent = new LIB_PART( *bufferedParent.get() );
|
|
|
|
newCachedPart->SetParent( cachedParent );
|
2020-01-09 16:23:46 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
aPlugin->SaveSymbol( m_libName, cachedParent, aBuffer ? &properties : nullptr );
|
|
|
|
}
|
|
|
|
catch( const IO_ERROR& ioe )
|
|
|
|
{
|
|
|
|
wxLogError( errorMsg, ioe.What(), cachedParent->GetName() );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
aPlugin->SaveSymbol( m_libName, newCachedPart, aBuffer ? &properties : nullptr );
|
|
|
|
}
|
|
|
|
catch( const IO_ERROR& ioe )
|
|
|
|
{
|
|
|
|
wxLogError( errorMsg, ioe.What(), newCachedPart->GetName() );
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-06 19:15:42 +00:00
|
|
|
|
|
|
|
LIB_PART* originalParent = new LIB_PART( *bufferedParent.get() );
|
|
|
|
aPartBuf->SetOriginal( originalParent );
|
|
|
|
originalPart = new LIB_PART( *part );
|
|
|
|
originalPart->SetParent( originalParent );
|
|
|
|
aPartBuf->SetOriginal( originalPart );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newCachedPart->SetParent( cachedParent );
|
2020-01-09 16:23:46 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
aPlugin->SaveSymbol( m_libName, newCachedPart, aBuffer ? &properties : nullptr );
|
|
|
|
}
|
|
|
|
catch( const IO_ERROR& ioe )
|
|
|
|
{
|
|
|
|
wxLogError( errorMsg, ioe.What(), newCachedPart->GetName() );
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-06 19:15:42 +00:00
|
|
|
|
|
|
|
LIB_MANAGER::PART_BUFFER::PTR originalBufferedParent =
|
|
|
|
GetBuffer( bufferedParent->GetName() );
|
|
|
|
wxCHECK( originalBufferedParent, false );
|
|
|
|
originalPart = new LIB_PART( *part );
|
|
|
|
originalPart->SetParent( originalBufferedParent->GetPart() );
|
|
|
|
aPartBuf->SetOriginal( originalPart );
|
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
|
|
|
|
if( GetDerivedSymbolNames( part->GetName(), derivedSymbols ) == 0 )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
aPlugin->SaveSymbol( m_libName, new LIB_PART( *part ),
|
|
|
|
aBuffer ? &properties : nullptr );
|
|
|
|
}
|
|
|
|
catch( const IO_ERROR& ioe )
|
|
|
|
{
|
|
|
|
wxLogError( errorMsg, ioe.What(), part->GetName() );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
aPartBuf->SetOriginal( new LIB_PART( *part ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LIB_PART* parentSymbol = new LIB_PART( *part );
|
|
|
|
|
|
|
|
// Save the modified root symbol.
|
|
|
|
try
|
|
|
|
{
|
2020-08-04 04:28:31 +00:00
|
|
|
aPlugin->SaveSymbol( m_libName, parentSymbol, aBuffer ? &properties : nullptr );
|
2020-01-09 16:23:46 +00:00
|
|
|
}
|
|
|
|
catch( const IO_ERROR& ioe )
|
|
|
|
{
|
|
|
|
wxLogError( errorMsg, ioe.What(), part->GetName() );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
aPartBuf->SetOriginal( new LIB_PART( *part ) );
|
|
|
|
|
|
|
|
// Save the derived symbols.
|
|
|
|
for( auto entry : derivedSymbols )
|
|
|
|
{
|
|
|
|
LIB_MANAGER::PART_BUFFER::PTR symbol = GetBuffer( entry );
|
|
|
|
LIB_PART* derivedSymbol = new LIB_PART( *symbol->GetPart() );
|
|
|
|
derivedSymbol->SetParent( parentSymbol );
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
aPlugin->SaveSymbol( m_libName, new LIB_PART( *derivedSymbol ),
|
|
|
|
aBuffer ? &properties : nullptr );
|
|
|
|
}
|
|
|
|
catch( const IO_ERROR& ioe )
|
|
|
|
{
|
|
|
|
wxLogError( errorMsg, ioe.What(), derivedSymbol->GetName() );
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-06 19:15:42 +00:00
|
|
|
LIB_MANAGER::PART_BUFFER::PTR LIB_MANAGER::LIB_BUFFER::GetBuffer( const wxString& aAlias ) const
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2019-11-06 19:15:42 +00:00
|
|
|
for( auto entry : m_parts )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2019-11-06 19:15:42 +00:00
|
|
|
if( entry->GetPart()->GetName() == aAlias )
|
|
|
|
return entry;
|
|
|
|
}
|
2017-11-12 17:55:20 +00:00
|
|
|
|
2019-11-06 19:15:42 +00:00
|
|
|
return PART_BUFFER::PTR( nullptr );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool LIB_MANAGER::LIB_BUFFER::HasDerivedSymbols( const wxString& aParentName ) const
|
|
|
|
{
|
|
|
|
for( auto entry : m_parts )
|
|
|
|
{
|
|
|
|
if( entry->GetPart()->IsAlias() )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2019-11-06 19:15:42 +00:00
|
|
|
PART_SPTR parent = entry->GetPart()->GetParent().lock();
|
|
|
|
|
|
|
|
// Check for inherited part without a valid parent.
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-06 19:15:42 +00:00
|
|
|
void LIB_MANAGER::LIB_BUFFER::GetRootSymbolNames( wxArrayString& aRootSymbolNames )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
2019-11-06 19:15:42 +00:00
|
|
|
for( auto entry : m_parts )
|
|
|
|
{
|
|
|
|
if( entry->GetPart()->IsAlias() )
|
|
|
|
continue;
|
2017-11-12 17:55:20 +00:00
|
|
|
|
2019-11-06 19:15:42 +00:00
|
|
|
aRootSymbolNames.Add( entry->GetPart()->GetName() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-09 16:23:46 +00:00
|
|
|
size_t LIB_MANAGER::LIB_BUFFER::GetDerivedSymbolNames( const wxString& aSymbolName,
|
|
|
|
wxArrayString& aList )
|
|
|
|
{
|
|
|
|
wxCHECK( !aSymbolName.IsEmpty(), 0 );
|
|
|
|
|
|
|
|
for( auto entry : m_parts )
|
|
|
|
{
|
|
|
|
if( entry->GetPart()->IsAlias() )
|
|
|
|
{
|
|
|
|
PART_SPTR parent = entry->GetPart()->GetParent().lock();
|
|
|
|
|
|
|
|
// Check for inherited part without a valid parent.
|
|
|
|
wxCHECK( parent, false );
|
|
|
|
|
|
|
|
if( parent->GetName() == aSymbolName )
|
|
|
|
aList.Add( entry->GetPart()->GetName() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return aList.GetCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-06 19:15:42 +00:00
|
|
|
int LIB_MANAGER::LIB_BUFFER::removeChildSymbols( LIB_MANAGER::PART_BUFFER::PTR aPartBuf )
|
|
|
|
{
|
|
|
|
wxCHECK( aPartBuf && aPartBuf->GetPart()->IsRoot(), 0 );
|
|
|
|
|
|
|
|
int cnt = 0;
|
|
|
|
std::deque< LIB_MANAGER::PART_BUFFER::PTR >::iterator it = m_parts.begin();
|
|
|
|
|
|
|
|
while( it != m_parts.end() )
|
2017-11-12 17:55:20 +00:00
|
|
|
{
|
|
|
|
|
2019-11-06 19:15:42 +00:00
|
|
|
if( (*it)->GetPart()->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
|
|
|
|
{
|
|
|
|
PART_SPTR parent = (*it)->GetPart()->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
|
|
|
|
2019-11-06 19:15:42 +00:00
|
|
|
if( parent->GetName() == aPartBuf->GetPart()->GetName() )
|
|
|
|
{
|
|
|
|
wxCHECK2( parent == aPartBuf->GetPart()->SharedPtr(), ++it; continue );
|
|
|
|
|
|
|
|
m_deleted.emplace_back( *it );
|
|
|
|
it = m_parts.erase( it );
|
|
|
|
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
|
|
|
}
|