From 6f90a63381a88bdcc8ad3c57192a233a59d08f98 Mon Sep 17 00:00:00 2001 From: Wayne Stambaugh Date: Sun, 20 Nov 2022 12:04:44 -0500 Subject: [PATCH] Fix legacy schematic symbol instance data bug. --- .../sch_plugins/legacy/sch_legacy_plugin.cpp | 21 +++++++----- eeschema/sch_symbol.cpp | 33 ++++++++++++------- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/eeschema/sch_plugins/legacy/sch_legacy_plugin.cpp b/eeschema/sch_plugins/legacy/sch_legacy_plugin.cpp index 08d84d037b..8e0edf5112 100644 --- a/eeschema/sch_plugins/legacy/sch_legacy_plugin.cpp +++ b/eeschema/sch_plugins/legacy/sch_legacy_plugin.cpp @@ -1352,20 +1352,23 @@ SCH_SYMBOL* SCH_LEGACY_PLUGIN::loadSymbol( LINE_READER& aReader ) { KIID_PATH path; path.push_back( m_rootSheet->GetScreen()->GetUuid() ); - symbol->AddHierarchicalReference( path, - symbol->GetField( REFERENCE_FIELD )->GetText(), - symbol->GetUnit(), - symbol->GetField( VALUE_FIELD )->GetText(), - symbol->GetField( FOOTPRINT_FIELD )->GetText() ); + + SYMBOL_INSTANCE_REFERENCE instance; + instance.m_Path = path; + instance.m_Reference = symbol->GetField( REFERENCE_FIELD )->GetText(); + instance.m_Unit = symbol->GetUnit(); + instance.m_Value = symbol->GetField( VALUE_FIELD )->GetText(); + instance.m_Footprint = symbol->GetField( FOOTPRINT_FIELD )->GetText(); + symbol->AddHierarchicalReference( instance ); } else { for( const SYMBOL_INSTANCE_REFERENCE& instance : symbol->GetInstanceReferences() ) { - symbol->AddHierarchicalReference( instance.m_Path, instance.m_Reference, - instance.m_Unit, - symbol->GetField( VALUE_FIELD )->GetText(), - symbol->GetField( FOOTPRINT_FIELD )->GetText() ); + SYMBOL_INSTANCE_REFERENCE tmpInstance = instance; + tmpInstance.m_Value = symbol->GetField( VALUE_FIELD )->GetText(); + tmpInstance.m_Footprint = symbol->GetField( FOOTPRINT_FIELD )->GetText(); + symbol->AddHierarchicalReference( tmpInstance ); } } } diff --git a/eeschema/sch_symbol.cpp b/eeschema/sch_symbol.cpp index 29e591a99a..0fb5b50720 100644 --- a/eeschema/sch_symbol.cpp +++ b/eeschema/sch_symbol.cpp @@ -588,23 +588,34 @@ void SCH_SYMBOL::AddHierarchicalReference( const KIID_PATH& aPath, const wxStrin void SCH_SYMBOL::AddHierarchicalReference( const SYMBOL_INSTANCE_REFERENCE& aInstance ) { - // Search for an existing path and remove it if found (should not occur) - for( unsigned ii = 0; ii < m_instanceReferences.size(); ii++ ) + KIID_PATH searchPath( aInstance.m_Path ); + + std::vector::iterator resultIt; + + do { - if( m_instanceReferences[ii].m_Path == aInstance.m_Path ) + resultIt = std::find_if( m_instanceReferences.begin(), m_instanceReferences.end(), + [searchPath]( const auto& it ) + { + return it.m_Path == searchPath; + } ); + + if( resultIt != m_instanceReferences.end() ) { wxLogTrace( traceSchSheetPaths, "Removing symbol instance:\n" - " sheet path %s\n" - " reference %s, unit %d from symbol %s.", - aInstance.m_Path.AsString(), - m_instanceReferences[ii].m_Reference, - m_instanceReferences[ii].m_Unit, - m_Uuid.AsString() ); + " sheet path %s\n" + " reference %s, unit %d from symbol %s.", + aInstance.m_Path.AsString(), + resultIt->m_Reference, + resultIt->m_Unit, + m_Uuid.AsString() ); - m_instanceReferences.erase( m_instanceReferences.begin() + ii ); - ii--; + // Instance data should be unique by path. Double check just in case there was + // some buggy code in the past. + resultIt = m_instanceReferences.erase( resultIt ); } } + while( resultIt != m_instanceReferences.end() ); SYMBOL_INSTANCE_REFERENCE instance = aInstance;