diff --git a/eeschema/sch_plugins/cadstar/cadstar_sch_archive_loader.cpp b/eeschema/sch_plugins/cadstar/cadstar_sch_archive_loader.cpp index ed5c488814..abb6f991b6 100644 --- a/eeschema/sch_plugins/cadstar/cadstar_sch_archive_loader.cpp +++ b/eeschema/sch_plugins/cadstar/cadstar_sch_archive_loader.cpp @@ -2683,19 +2683,49 @@ CADSTAR_SCH_ARCHIVE_LOADER::SYMDEF_ID CADSTAR_SCH_ARCHIVE_LOADER::getSymDefFromName( const wxString& aSymdefName, const wxString& aSymDefAlternate ) { - // Do a case-insensitive comparison - for( std::pair symPair : Library.SymbolDefinitions ) + if( m_SymDefNamesCache.size() != Library.SymbolDefinitions.size() ) { - SYMDEF_ID id = symPair.first; - SYMDEF_SCM symdef = symPair.second; + // Re-initialise + m_SymDefNamesCache.clear(); + m_DefaultSymDefNamesCache.clear(); - if( symdef.ReferenceName.Lower() == aSymdefName.Lower() - && symdef.Alternate.Lower() == aSymDefAlternate.Lower() ) + // Create a lower case cache to avoid searching each time + for( auto& [id, symdef] : Library.SymbolDefinitions ) { - return id; + wxString refKey = symdef.ReferenceName.Lower(); + wxString altKey = symdef.Alternate.Lower(); + + m_SymDefNamesCache[{ refKey, altKey }] = id; + + // Secondary cache to find symbols just by the Name (e.g. if the alternate + // does not exist, we still want to return a symbo - the same behaviour + // as CADSTAR + + if( !m_DefaultSymDefNamesCache.count( refKey ) ) + { + m_DefaultSymDefNamesCache.insert( { refKey, id } ); + } + else if( altKey.IsEmpty() ) + { + // Always use the empty alternate if it exists + m_DefaultSymDefNamesCache[refKey] = id; + } } } + wxString refKeyToFind = aSymdefName.Lower(); + wxString altKeyToFind = aSymDefAlternate.Lower(); + + if( m_SymDefNamesCache.count( { refKeyToFind, altKeyToFind } ) ) + { + return m_SymDefNamesCache[{ refKeyToFind, altKeyToFind }]; + } + else if( m_DefaultSymDefNamesCache.count( refKeyToFind ) ) + { + return m_DefaultSymDefNamesCache[refKeyToFind]; + } + + return SYMDEF_ID(); } diff --git a/eeschema/sch_plugins/cadstar/cadstar_sch_archive_loader.h b/eeschema/sch_plugins/cadstar/cadstar_sch_archive_loader.h index b3982fe17e..a53be989b2 100644 --- a/eeschema/sch_plugins/cadstar/cadstar_sch_archive_loader.h +++ b/eeschema/sch_plugins/cadstar/cadstar_sch_archive_loader.h @@ -130,6 +130,16 @@ private: std::map m_pinNumsMap; ///< Cadstar Part->KiCad Pin number map std::map m_symDefTerminalsMap; + /** + * Cache storing symbol names and alternates to symdef IDs + */ + std::map, SYMDEF_ID> m_SymDefNamesCache; + + /** + * Cache storing symbol names (default alternate) to symdef IDs + */ + std::map m_DefaultSymDefNamesCache; + /** * Cadstar->KiCad Lib Symbols loaded so far. Note that in CADSTAR each symbol represents just a * gate, so the LIB_SYMBOLs contained here are not imported directly - they are just an interim