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 8f625086b1)
This commit is contained in:
Seth Hillbrand
2025-08-11 08:49:16 -07:00
parent 3fb8f22fd3
commit 9fe656a33c
2 changed files with 25 additions and 9 deletions
+20 -9
View File
@@ -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;
}
+5
View File
@@ -32,6 +32,8 @@
#include <wx/xml/xml.h>
#include <memory>
#include <map>
#include <set>
class BOARD;
class BOARD_ITEM;
@@ -328,6 +330,9 @@ private:
PROGRESS_REPORTER* m_progress_reporter;
mutable std::set<wxString> m_element_names; //<! Track generated element names
mutable std::map<wxString, wxString> m_generated_names; //<! Map input keys to unique names
std::set<wxUniChar> m_acceptable_chars; //<! IPC2581B and C have differing sets of allowed characters in names
wxXmlDocument* m_xml_doc;