From 168bf5e4b02e10aa573a649bcca9bf6a11182ccd Mon Sep 17 00:00:00 2001 From: Wayne Stambaugh Date: Sat, 4 Nov 2017 21:48:30 -0400 Subject: [PATCH] Symbol library archive bug fixes. Move FindAlias() inside a try/catch block as is can throw an exception. Don't abort adding symbols to library when a symbol cannot be found in the symbol library table or the cache library. Instead, queue up error messages and display them after attempting to add all of the symbols. Always save the library file even if some of the symbols could not be archived. Catch IO_ERRORs in SCH_COMPONENT resolve to prevent unhandled exceptions further up the stack. --- eeschema/libarch.cpp | 65 ++++++++++++++++++++++++++------------ eeschema/sch_component.cpp | 29 ++++++++++------- 2 files changed, 62 insertions(+), 32 deletions(-) diff --git a/eeschema/libarch.cpp b/eeschema/libarch.cpp index 3a18cbff1e..3935c4dc7b 100644 --- a/eeschema/libarch.cpp +++ b/eeschema/libarch.cpp @@ -66,7 +66,8 @@ bool SCH_EDIT_FRAME::CreateArchiveLibraryCacheFile( bool aUseCurrentSheetFilenam bool SCH_EDIT_FRAME::CreateArchiveLibrary( const wxString& aFileName ) { - wxString msg; + wxString tmp; + wxString errorMsg; SCH_SCREENS screens; // Create a new empty library to archive components: @@ -87,27 +88,28 @@ bool SCH_EDIT_FRAME::CreateArchiveLibrary( const wxString& aFileName ) if( item->Type() != SCH_COMPONENT_T ) continue; + LIB_PART* part = nullptr; SCH_COMPONENT* component = (SCH_COMPONENT*) item; - if( archLib->FindAlias( component->GetLibId().GetLibItemName() ) ) - continue; - - LIB_PART* part = GetLibPart( component->GetLibId() ); - - if( !part ) + try { - try - { - part = Prj().SchLibs()->GetCacheLibrary()->FindPart( - component->GetLibId().GetLibItemName() ); - } - catch( ... /* IO_ERROR ioe */ ) - { - msg.Printf( _( "Failed to add symbol %s to library file '%s'" ), - component->GetLibId().GetLibItemName().wx_str(), aFileName ); - DisplayError( this, msg ); - return false; - } + if( archLib->FindAlias( component->GetLibId().GetLibItemName() ) ) + continue; + + part = GetLibPart( component->GetLibId(), true ); + } + catch( const IO_ERROR& ioe ) + { + // Queue up error messages for later. + tmp.Printf( _( "Failed to add symbol %s to library file." ), + component->GetLibId().GetLibItemName().wx_str(), aFileName ); + + // Don't bail out here. Attempt to add as many of the symbols to the library + // as possible. + } + catch( ... ) + { + tmp = _( "Unexpected exception occurred." ); } if( part ) @@ -115,17 +117,38 @@ bool SCH_EDIT_FRAME::CreateArchiveLibrary( const wxString& aFileName ) // AddPart() does first clone the part before adding. archLib->AddPart( part ); } + else + { + tmp.Printf( _( "Symbol %s not found in any library or cache." ), + component->GetLibId().Format().wx_str() ); + } + + if( !tmp.empty() ) + { + if( errorMsg.empty() ) + errorMsg += tmp; + else + errorMsg += "\n" + tmp; + } } } + if( !errorMsg.empty() ) + { + tmp.Printf( _( "Errors occurred creating symbol library %s." ), aFileName ); + DisplayErrorMessage( this, tmp, errorMsg ); + } + + archLib->EnableBuffering( false ); + try { archLib->Save( false ); } catch( ... /* IO_ERROR ioe */ ) { - msg.Printf( _( "Failed to save symbol library file '%s'" ), aFileName ); - DisplayError( this, msg ); + errorMsg.Printf( _( "Failed to save symbol library file '%s'" ), aFileName ); + DisplayError( this, errorMsg ); return false; } diff --git a/eeschema/sch_component.cpp b/eeschema/sch_component.cpp index d92860b8af..3f3e7fb2ce 100644 --- a/eeschema/sch_component.cpp +++ b/eeschema/sch_component.cpp @@ -315,18 +315,25 @@ bool SCH_COMPONENT::Resolve( SYMBOL_LIB_TABLE& aLibTable, PART_LIB* aCacheLib ) { LIB_ALIAS* alias = nullptr; - if( !m_lib_id.GetLibNickname().empty() && aLibTable.HasLibrary( m_lib_id.GetLibNickname() ) ) - alias = aLibTable.LoadSymbol( m_lib_id ); - - // Fall back to cache library. This is temporary until the new schematic file - // format is implemented. - if( !alias && aCacheLib ) - alias = aCacheLib->FindAlias( m_lib_id.GetLibItemName() ); - - if( alias && alias->GetPart() ) + try { - m_part = alias->GetPart()->SharedPtr(); - return true; + if( m_lib_id.IsValid() ) + alias = aLibTable.LoadSymbol( m_lib_id ); + + // Fall back to cache library. This is temporary until the new schematic file + // format is implemented. + if( !alias && aCacheLib ) + alias = aCacheLib->FindAlias( m_lib_id.GetLibItemName() ); + + if( alias && alias->GetPart() ) + { + m_part = alias->GetPart()->SharedPtr(); + return true; + } + } + catch( const IO_ERROR& ioe ) + { + wxLogDebug( "Cannot resolve library symbol %s", m_lib_id.Format().wx_str() ); } return false;