From 9fe656a33c8fff6d64d4b15de51192e2fa625ec0 Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Mon, 11 Aug 2025 08:49:16 -0700 Subject: [PATCH] Remove namespacing from IPC2581 output Because some readers maintain global namespace per the standard, we make sure that we don't overlap names between namespaces but keep the minimum viable name for each element Fixes https://gitlab.com/kicad/code/kicad/-/issues/19914 (cherry picked from commit 8f625086b1c225aa7fc9551a9947b9b2d7be289b) --- pcbnew/pcb_io/ipc2581/pcb_io_ipc2581.cpp | 29 ++++++++++++++++-------- pcbnew/pcb_io/ipc2581/pcb_io_ipc2581.h | 5 ++++ 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/pcbnew/pcb_io/ipc2581/pcb_io_ipc2581.cpp b/pcbnew/pcb_io/ipc2581/pcb_io_ipc2581.cpp index 30b95d8010..db6701d431 100644 --- a/pcbnew/pcb_io/ipc2581/pcb_io_ipc2581.cpp +++ b/pcbnew/pcb_io/ipc2581/pcb_io_ipc2581.cpp @@ -153,23 +153,24 @@ wxXmlNode* PCB_IO_IPC2581::appendNode( wxXmlNode* aParent, const wxString& aName wxString PCB_IO_IPC2581::genString( const wxString& aStr, const char* aPrefix ) const { + // Build a key using the prefix and original string so that repeated calls for the same + // element return the same generated name. + wxString key = aPrefix ? wxString( aPrefix ) + wxT( ":" ) + aStr : aStr; + + auto it = m_generated_names.find( key ); + + if( it != m_generated_names.end() ) + return it->second; + wxString str; if( m_version == 'C' ) { str = aStr; str.Replace( wxT( ":" ), wxT( "_" ) ); - - if( aPrefix ) - str.Prepend( wxString( aPrefix ) + wxT( ":" ) ); - else - str.Prepend( wxT( "KI:" ) ); } else { - if( aPrefix ) - str = wxString::Format( "%s_", aPrefix ); - for( wxString::const_iterator iter = aStr.begin(); iter != aStr.end(); ++iter ) { if( !m_acceptable_chars.count( *iter ) ) @@ -179,7 +180,17 @@ wxString PCB_IO_IPC2581::genString( const wxString& aStr, const char* aPrefix ) } } - return str; + wxString base = str; + wxString name = base; + int suffix = 1; + + while( m_element_names.count( name ) ) + name = wxString::Format( "%s_%d", base, suffix++ ); + + m_element_names.insert( name ); + m_generated_names[key] = name; + + return name; } diff --git a/pcbnew/pcb_io/ipc2581/pcb_io_ipc2581.h b/pcbnew/pcb_io/ipc2581/pcb_io_ipc2581.h index 67e4f41769..c0e83d5f23 100644 --- a/pcbnew/pcb_io/ipc2581/pcb_io_ipc2581.h +++ b/pcbnew/pcb_io/ipc2581/pcb_io_ipc2581.h @@ -32,6 +32,8 @@ #include #include +#include +#include class BOARD; class BOARD_ITEM; @@ -328,6 +330,9 @@ private: PROGRESS_REPORTER* m_progress_reporter; + mutable std::set m_element_names; // m_generated_names; // m_acceptable_chars; //