Eagle schematic import: Handling of escaped text

This commit is contained in:
Dag Lem 2023-01-18 12:33:36 +00:00 committed by Wayne Stambaugh
parent 3af9c658e6
commit cff0560088
4 changed files with 74 additions and 80 deletions

View File

@ -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 ) bool substituteVariable( wxString* aText )
{ {
if ( *aText == wxT( ">NAME" ) ) *aText = wxT( "${REFERENCE}" ); if ( *aText == wxT( ">NAME" ) ) *aText = wxT( "${REFERENCE}" );

View File

@ -54,6 +54,9 @@ typedef std::map<wxString, std::unique_ptr<EPART>> EPART_MAP;
///< Translates Eagle special characters to their counterparts in KiCad. ///< Translates Eagle special characters to their counterparts in KiCad.
wxString escapeName( const wxString& aNetName ); 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 ///< Translates Eagle special text reference to a KiCad variable reference
bool substituteVariable( wxString* aText ); bool substituteVariable( wxString* aText );

View File

@ -2357,11 +2357,7 @@ LIB_TEXT* SCH_EAGLE_PLUGIN::loadSymbolText( std::unique_ptr<LIB_SYMBOL>& aSymbol
// Strip the whitespace from both ends of each line. // Strip the whitespace from both ends of each line.
while( tokenizer.HasMoreTokens() ) while( tokenizer.HasMoreTokens() )
{ {
wxString tmp = tokenizer.GetNextToken().Trim( true ).Trim( false ); wxString tmp = interpretText( tokenizer.GetNextToken().Trim( true ).Trim( false ) );
wxString var = tmp.Upper();
if( substituteVariable( &var ) )
tmp = var;
if( tokenizer.HasMoreTokens() ) if( tokenizer.HasMoreTokens() )
tmp += wxT( "\n" ); tmp += wxT( "\n" );
@ -2577,11 +2573,7 @@ SCH_TEXT* SCH_EAGLE_PLUGIN::loadPlainText( wxXmlNode* aSchText )
// Strip the whitespace from both ends of each line. // Strip the whitespace from both ends of each line.
while( tokenizer.HasMoreTokens() ) while( tokenizer.HasMoreTokens() )
{ {
wxString tmp = tokenizer.GetNextToken().Trim( true ).Trim( false ); wxString tmp = interpretText( tokenizer.GetNextToken().Trim( true ).Trim( false ) );
wxString var = tmp.Upper();
if( substituteVariable( &var ) )
tmp = var;
if( tokenizer.HasMoreTokens() ) if( tokenizer.HasMoreTokens() )
tmp += wxT( "\n" ); tmp += wxT( "\n" );

View File

@ -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 void EAGLE_PLUGIN::setKeepoutSettingsToZone( ZONE* aZone, int aLayer ) const
{ {
if( aLayer == EAGLE_LAYER::TRESTRICT || aLayer == EAGLE_LAYER::BRESTRICT ) 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 ); m_board->Add( pcbtxt, ADD_MODE::APPEND );
pcbtxt->SetLayer( layer ); pcbtxt->SetLayer( layer );
wxString kicadText = interpret_text( t.text ); wxString kicadText = interpretText( t.text );
pcbtxt->SetText( kicadText ); pcbtxt->SetText( kicadText );
pcbtxt->SetTextPos( VECTOR2I( kicad_x( t.x ), kicad_y( t.y ) ) ); 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 ); textItem = new FP_TEXT( aFootprint );
aFootprint->Add( textItem ); aFootprint->Add( textItem );
textItem->SetText( interpret_text( t.text ) ); textItem->SetText( interpretText( t.text ) );
} }
VECTOR2I pos( kicad_x( t.x ), kicad_y( t.y ) ); VECTOR2I pos( kicad_x( t.x ), kicad_y( t.y ) );