From cff056008893aef87e650f6124a564c1b019a9c0 Mon Sep 17 00:00:00 2001 From: Dag Lem Date: Wed, 18 Jan 2023 12:33:36 +0000 Subject: [PATCH] Eagle schematic import: Handling of escaped text --- common/plugins/eagle/eagle_parser.cpp | 67 +++++++++++++++++ common/plugins/eagle/eagle_parser.h | 3 + .../sch_plugins/eagle/sch_eagle_plugin.cpp | 12 +--- pcbnew/plugins/eagle/eagle_plugin.cpp | 72 +------------------ 4 files changed, 74 insertions(+), 80 deletions(-) diff --git a/common/plugins/eagle/eagle_parser.cpp b/common/plugins/eagle/eagle_parser.cpp index ddd6028e8b..61120ee7d5 100644 --- a/common/plugins/eagle/eagle_parser.cpp +++ b/common/plugins/eagle/eagle_parser.cpp @@ -47,6 +47,73 @@ wxString escapeName( const wxString& aNetName ) } +wxString interpretText( const wxString& aText ) +{ + wxString token = aText.Upper(); + + if( substituteVariable( &token ) ) + return token; + + wxString text; + bool sectionOpen = false; + + for( wxString::size_type i = 0; i < aText.size(); i++ ) + { + // Interpret escaped characters + if( aText[ i ] == '\\' ) + { + if( i + 1 != aText.size() ) + text.Append( aText[ i + 1 ] ); + + i++; + continue; + } + + // Escape ~ for KiCAD + if( aText[i] == '~' ) + { + text.Append( '~' ); + text.Append( '~' ); + continue; + } + + if( aText[ i ] == '!' ) + { + if( sectionOpen ) + { + text.Append( '~' ); + sectionOpen = false; + continue; + } + + static wxString escapeChars( wxT( " )]}'\"" ) ); + + if( i + 1 != aText.size() && escapeChars.Find( aText[i + 1] ) == wxNOT_FOUND ) + { + sectionOpen = true; + text.Append( '~' ); + } + else + { + text.Append( aText[ i ] ); + } + + continue; + } + + if( aText[i] == ',' && sectionOpen ) + { + text.Append( '~' ); + sectionOpen = false; + } + + text.Append( aText[ i ] ); + } + + return text; +} + + bool substituteVariable( wxString* aText ) { if ( *aText == wxT( ">NAME" ) ) *aText = wxT( "${REFERENCE}" ); diff --git a/common/plugins/eagle/eagle_parser.h b/common/plugins/eagle/eagle_parser.h index 16d3925df1..30457f86b4 100644 --- a/common/plugins/eagle/eagle_parser.h +++ b/common/plugins/eagle/eagle_parser.h @@ -54,6 +54,9 @@ typedef std::map> EPART_MAP; ///< Translates Eagle special characters to their counterparts in KiCad. wxString escapeName( const wxString& aNetName ); +///< Interprets special characters in Eagle text and converts them to KiCAD notation. +wxString interpretText( const wxString& aText ); + ///< Translates Eagle special text reference to a KiCad variable reference bool substituteVariable( wxString* aText ); diff --git a/eeschema/sch_plugins/eagle/sch_eagle_plugin.cpp b/eeschema/sch_plugins/eagle/sch_eagle_plugin.cpp index 78714684c4..f5876240b3 100644 --- a/eeschema/sch_plugins/eagle/sch_eagle_plugin.cpp +++ b/eeschema/sch_plugins/eagle/sch_eagle_plugin.cpp @@ -2357,11 +2357,7 @@ LIB_TEXT* SCH_EAGLE_PLUGIN::loadSymbolText( std::unique_ptr& aSymbol // Strip the whitespace from both ends of each line. while( tokenizer.HasMoreTokens() ) { - wxString tmp = tokenizer.GetNextToken().Trim( true ).Trim( false ); - wxString var = tmp.Upper(); - - if( substituteVariable( &var ) ) - tmp = var; + wxString tmp = interpretText( tokenizer.GetNextToken().Trim( true ).Trim( false ) ); if( tokenizer.HasMoreTokens() ) tmp += wxT( "\n" ); @@ -2577,11 +2573,7 @@ SCH_TEXT* SCH_EAGLE_PLUGIN::loadPlainText( wxXmlNode* aSchText ) // Strip the whitespace from both ends of each line. while( tokenizer.HasMoreTokens() ) { - wxString tmp = tokenizer.GetNextToken().Trim( true ).Trim( false ); - wxString var = tmp.Upper(); - - if( substituteVariable( &var ) ) - tmp = var; + wxString tmp = interpretText( tokenizer.GetNextToken().Trim( true ).Trim( false ) ); if( tokenizer.HasMoreTokens() ) tmp += wxT( "\n" ); diff --git a/pcbnew/plugins/eagle/eagle_plugin.cpp b/pcbnew/plugins/eagle/eagle_plugin.cpp index 4eaed81b80..e9d375f969 100644 --- a/pcbnew/plugins/eagle/eagle_plugin.cpp +++ b/pcbnew/plugins/eagle/eagle_plugin.cpp @@ -114,74 +114,6 @@ static wxString makeKey( const wxString& aFirst, const wxString& aSecond ) } -/// interpret special characters in Eagle text and converts them to KiCAD notation -static wxString interpret_text( const wxString& aText ) -{ - wxString token = aText.Upper(); - - if( substituteVariable( &token ) ) - return token; - - wxString text; - bool sectionOpen = false; - - for( wxString::size_type i = 0; i < aText.size(); i++ ) - { - // Interpret escaped characters - if( aText[ i ] == '\\' ) - { - if( i + 1 != aText.size() ) - text.Append( aText[ i + 1 ] ); - - i++; - continue; - } - - // Escape ~ for KiCAD - if( aText[i] == '~' ) - { - text.Append( '~' ); - text.Append( '~' ); - continue; - } - - if( aText[ i ] == '!' ) - { - if( sectionOpen ) - { - text.Append( '~' ); - sectionOpen = false; - continue; - } - - static wxString escapeChars( wxT( " )]}'\"" ) ); - - if( i + 1 != aText.size() && escapeChars.Find( aText[i + 1] ) == wxNOT_FOUND ) - { - sectionOpen = true; - text.Append( '~' ); - } - else - { - text.Append( aText[ i ] ); - } - - continue; - } - - if( aText[i] == ',' && sectionOpen ) - { - text.Append( '~' ); - sectionOpen = false; - } - - text.Append( aText[ i ] ); - } - - return text; -} - - void EAGLE_PLUGIN::setKeepoutSettingsToZone( ZONE* aZone, int aLayer ) const { if( aLayer == EAGLE_LAYER::TRESTRICT || aLayer == EAGLE_LAYER::BRESTRICT ) @@ -764,7 +696,7 @@ void EAGLE_PLUGIN::loadPlain( wxXmlNode* aGraphics ) m_board->Add( pcbtxt, ADD_MODE::APPEND ); pcbtxt->SetLayer( layer ); - wxString kicadText = interpret_text( t.text ); + wxString kicadText = interpretText( t.text ); pcbtxt->SetText( kicadText ); pcbtxt->SetTextPos( VECTOR2I( kicad_x( t.x ), kicad_y( t.y ) ) ); @@ -2062,7 +1994,7 @@ void EAGLE_PLUGIN::packageText( FOOTPRINT* aFootprint, wxXmlNode* aTree ) const textItem = new FP_TEXT( aFootprint ); aFootprint->Add( textItem ); - textItem->SetText( interpret_text( t.text ) ); + textItem->SetText( interpretText( t.text ) ); } VECTOR2I pos( kicad_x( t.x ), kicad_y( t.y ) );