KiCad PCB EDA Suite
eagle_parser.h File Reference
#include <map>
#include <memory>
#include <unordered_map>
#include <wx/xml/xml.h>
#include <wx/string.h>
#include <wx/filename.h>
#include <layer_ids.h>
#include <trigo.h>
#include <core/wx_stl_compat.h>

Go to the source code of this file.

Classes

struct  XML_PARSER_ERROR
 Implement a simple wrapper around runtime_error to isolate the errors thrown by the Eagle XML parser. More...
 
struct  TRIPLET
 segment (element) of our XPATH into the Eagle XML document tree in PTREE form. More...
 
class  XPATH
 Keep track of what we are working on within a PTREE. More...
 
class  OPTIONAL_XML_ATTRIBUTE< T >
 Model an optional XML attribute. More...
 
struct  ECOORD
 
struct  ENET
 Eagle net. More...
 
struct  EROT
 Eagle rotation. More...
 
struct  EWIRE
 Eagle wire. More...
 
struct  EJUNCTION
 Eagle Junction. More...
 
struct  ELABEL
 Eagle label. More...
 
struct  EVIA
 Eagle via. More...
 
struct  ECIRCLE
 Eagle circle. More...
 
struct  ERECT
 Eagle XML rectangle in binary. More...
 
struct  EATTR
 Parse an Eagle "attribute" XML element. More...
 
struct  EDIMENSION
 Eagle dimension element. More...
 
struct  ETEXT
 Eagle text element. More...
 
struct  EFRAME
 Parse an Eagle frame element. More...
 
struct  EPAD_COMMON
 Structure holding common properties for through-hole and SMD pads. More...
 
struct  EPAD
 Eagle thru hole pad. More...
 
struct  ESMD
 Eagle SMD pad. More...
 
struct  EPIN
 Eagle pin element. More...
 
struct  EVERTEX
 Eagle vertex. More...
 
struct  EPOLYGON
 Eagle polygon, without vertices which are parsed as needed. More...
 
struct  EHOLE
 Eagle hole element. More...
 
struct  EELEMENT
 Eagle element element. More...
 
struct  ELAYER
 
struct  EAGLE_LAYER
 
struct  EPART
 
struct  EINSTANCE
 
struct  EGATE
 
struct  ECONNECT
 
struct  EDEVICE
 
struct  EDEVICE_SET
 
struct  ECLASS
 

Typedefs

typedef std::unordered_map< wxString, wxXmlNode * > NODE_MAP
 
typedef std::map< wxString, FOOTPRINT * > FOOTPRINT_MAP
 
typedef std::map< wxString, EINSTANCE * > EINSTANCE_MAP
 
typedef std::map< wxString, std::unique_ptr< EPART > > EPART_MAP
 Translates Eagle special characters to their counterparts in KiCad. More...
 
typedef OPTIONAL_XML_ATTRIBUTE< wxString > opt_wxString
 
typedef OPTIONAL_XML_ATTRIBUTE< int > opt_int
 
typedef OPTIONAL_XML_ATTRIBUTE< double > opt_double
 
typedef OPTIONAL_XML_ATTRIBUTE< bool > opt_bool
 
typedef OPTIONAL_XML_ATTRIBUTE< EROTopt_erot
 
typedef OPTIONAL_XML_ATTRIBUTE< ECOORDopt_ecoord
 

Functions

wxString escapeName (const wxString &aNetName)
 Interprets special characters in Eagle text and converts them to KiCAD notation. More...
 
wxString interpretText (const wxString &aText)
 Translates Eagle special text reference to a KiCad variable reference. More...
 
bool substituteVariable (wxString *aText)
 
static wxXmlNode * getChildrenNodes (NODE_MAP &aMap, const wxString &aName)
 
template<typename T >
Convert (const wxString &aValue)
 Convert a wxString to a generic type T. More...
 
template<>
wxString Convert< wxString > (const wxString &aValue)
 
NODE_MAP MapChildren (wxXmlNode *aCurrentNode)
 Provide an easy access to the children of an XML node via their names. More...
 
VECTOR2I ConvertArcCenter (const VECTOR2I &aStart, const VECTOR2I &aEnd, double aAngle)
 

Typedef Documentation

◆ EINSTANCE_MAP

typedef std::map<wxString, EINSTANCE*> EINSTANCE_MAP

Definition at line 51 of file eagle_parser.h.

◆ EPART_MAP

typedef std::map<wxString, std::unique_ptr<EPART> > EPART_MAP

Translates Eagle special characters to their counterparts in KiCad.

Definition at line 52 of file eagle_parser.h.

◆ FOOTPRINT_MAP

typedef std::map<wxString, FOOTPRINT*> FOOTPRINT_MAP

Definition at line 50 of file eagle_parser.h.

◆ NODE_MAP

typedef std::unordered_map<wxString, wxXmlNode*> NODE_MAP

Definition at line 49 of file eagle_parser.h.

◆ opt_bool

Definition at line 375 of file eagle_parser.h.

◆ opt_double

Definition at line 374 of file eagle_parser.h.

◆ opt_ecoord

Definition at line 377 of file eagle_parser.h.

◆ opt_erot

Definition at line 376 of file eagle_parser.h.

◆ opt_int

Definition at line 373 of file eagle_parser.h.

◆ opt_wxString

Definition at line 372 of file eagle_parser.h.

Function Documentation

◆ Convert()

template<typename T >
T Convert ( const wxString &  aValue)

Convert a wxString to a generic type T.

Parameters
aValueis a wxString containing the value that will be converted to type T.
Exceptions
XML_PARSER_ERROR- an exception is thrown if the parsing fails or if the conversion to type T is unknown.

Definition at line 179 of file eagle_parser.h.

180{
181 throw XML_PARSER_ERROR( "Conversion failed. Unknown type." );
182}
Implement a simple wrapper around runtime_error to isolate the errors thrown by the Eagle XML parser.
Definition: eagle_parser.h:75

◆ Convert< wxString >()

template<>
wxString Convert< wxString > ( const wxString &  aValue)

Definition at line 227 of file eagle_parser.cpp.

228{
229 return aValue;
230}

◆ ConvertArcCenter()

VECTOR2I ConvertArcCenter ( const VECTOR2I aStart,
const VECTOR2I aEnd,
double  aAngle 
)

Definition at line 361 of file eagle_parser.cpp.

362{
363 // Eagle give us start and end.
364 // S_ARC wants start to give the center, and end to give the start.
365 double dx = aEnd.x - aStart.x, dy = aEnd.y - aStart.y;
366 VECTOR2I mid = ( aStart + aEnd ) / 2;
367
368 double dlen = sqrt( dx*dx + dy*dy );
369
370 if( !std::isnormal( dlen ) || !std::isnormal( aAngle ) )
371 {
372 THROW_IO_ERROR( wxString::Format( _( "Invalid Arc with radius %f and angle %f" ),
373 dlen,
374 aAngle ) );
375 }
376
377 double dist = dlen / ( 2 * tan( DEG2RAD( aAngle ) / 2 ) );
378
379 VECTOR2I center(
380 mid.x + dist * ( dy / dlen ),
381 mid.y - dist * ( dx / dlen )
382 );
383
384 return center;
385}
#define _(s)
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
double DEG2RAD(double deg)
Definition: trigo.h:195

References _, DEG2RAD(), Format(), THROW_IO_ERROR, VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by EAGLE_PLUGIN::loadPlain(), EAGLE_PLUGIN::loadPolygon(), EAGLE_PLUGIN::loadSignals(), SCH_EAGLE_PLUGIN::loadSymbolWire(), SCH_EAGLE_PLUGIN::loadWire(), EAGLE_PLUGIN::packagePolygon(), and EAGLE_PLUGIN::packageWire().

◆ escapeName()

wxString escapeName ( const wxString &  aNetName)

Interprets special characters in Eagle text and converts them to KiCAD notation.

Definition at line 40 of file eagle_parser.cpp.

41{
42 wxString ret( aNetName );
43
44 ret.Replace( "!", "~" );
45
46 return ConvertToNewOverbarNotation( ret );
47}
wxString ConvertToNewOverbarNotation(const wxString &aOldStr)
Convert the old ~...~ overbar notation to the new ~{...} one.

References ConvertToNewOverbarNotation().

Referenced by SCH_EAGLE_PLUGIN::loadLabel(), SCH_EAGLE_PLUGIN::loadPlainText(), SCH_EAGLE_PLUGIN::loadSegments(), EAGLE_PLUGIN::loadSignals(), and SCH_EAGLE_PLUGIN::loadSymbol().

◆ getChildrenNodes()

static wxXmlNode * getChildrenNodes ( NODE_MAP aMap,
const wxString &  aName 
)
inlinestatic

Definition at line 63 of file eagle_parser.h.

64{
65 auto it = aMap.find( aName );
66 return it == aMap.end() ? nullptr : it->second->GetChildren();
67}

Referenced by SCH_EAGLE_PLUGIN::countNets(), EDEVICE::EDEVICE(), SCH_EAGLE_PLUGIN::loadLibrary(), SCH_EAGLE_PLUGIN::loadSchematic(), and SCH_EAGLE_PLUGIN::loadSheet().

◆ interpretText()

wxString interpretText ( const wxString &  aText)

Translates Eagle special text reference to a KiCad variable reference.

Definition at line 50 of file eagle_parser.cpp.

51{
52 wxString token = aText.Upper();
53
54 if( substituteVariable( &token ) )
55 return token;
56
57 wxString text;
58 bool sectionOpen = false;
59
60 for( wxString::size_type i = 0; i < aText.size(); i++ )
61 {
62 // Interpret escaped characters
63 if( aText[ i ] == '\\' )
64 {
65 if( i + 1 != aText.size() )
66 text.Append( aText[ i + 1 ] );
67
68 i++;
69 continue;
70 }
71
72 // Escape ~ for KiCAD
73 if( aText[i] == '~' )
74 {
75 text.Append( '~' );
76 text.Append( '~' );
77 continue;
78 }
79
80 if( aText[ i ] == '!' )
81 {
82 if( sectionOpen )
83 {
84 text.Append( '~' );
85 sectionOpen = false;
86 continue;
87 }
88
89 static wxString escapeChars( wxT( " )]}'\"" ) );
90
91 if( i + 1 != aText.size() && escapeChars.Find( aText[i + 1] ) == wxNOT_FOUND )
92 {
93 sectionOpen = true;
94 text.Append( '~' );
95 }
96 else
97 {
98 text.Append( aText[ i ] );
99 }
100
101 continue;
102 }
103
104 if( aText[i] == ',' && sectionOpen )
105 {
106 text.Append( '~' );
107 sectionOpen = false;
108 }
109
110 text.Append( aText[ i ] );
111 }
112
113 return text;
114}
bool substituteVariable(wxString *aText)

References substituteVariable(), and text.

Referenced by EAGLE_PLUGIN::loadPlain(), SCH_EAGLE_PLUGIN::loadPlainText(), SCH_EAGLE_PLUGIN::loadSymbolText(), and EAGLE_PLUGIN::packageText().

◆ MapChildren()

NODE_MAP MapChildren ( wxXmlNode *  aCurrentNode)

Provide an easy access to the children of an XML node via their names.

Parameters
currentNodeis a pointer to a wxXmlNode, whose children will be mapped.
Returns
NODE_MAP is a map linking the name of each children to the children itself (via a wxXmlNode*) Convert an Eagle curve end to a KiCad center for S_ARC

Definition at line 337 of file eagle_parser.cpp.

338{
339 // Map node_name -> node_pointer
340 NODE_MAP nodesMap;
341
342 // Loop through all children mapping them in nodesMap
343 if( aCurrentNode )
344 aCurrentNode = aCurrentNode->GetChildren();
345
346 while( aCurrentNode )
347 {
348 // Create a new pair in the map
349 // key: current node name
350 // value: current node pointer
351 nodesMap[aCurrentNode->GetName()] = aCurrentNode;
352
353 // Get next child
354 aCurrentNode = aCurrentNode->GetNext();
355 }
356
357 return nodesMap;
358}
std::unordered_map< wxString, wxXmlNode * > NODE_MAP
Definition: eagle_parser.h:49

Referenced by EAGLE_PLUGIN::cacheLib(), SCH_EAGLE_PLUGIN::countNets(), EDEVICE::EDEVICE(), SCH_EAGLE_PLUGIN::Load(), EAGLE_PLUGIN::loadAllSections(), SCH_EAGLE_PLUGIN::loadDrawing(), EAGLE_PLUGIN::loadLibrary(), SCH_EAGLE_PLUGIN::loadLibrary(), SCH_EAGLE_PLUGIN::loadSchematic(), SCH_EAGLE_PLUGIN::loadSegments(), and SCH_EAGLE_PLUGIN::loadSheet().

◆ substituteVariable()

bool substituteVariable ( wxString *  aText)

Definition at line 117 of file eagle_parser.cpp.

118{
119 if ( *aText == wxT( ">NAME" ) ) *aText = wxT( "${REFERENCE}" );
120 else if( *aText == wxT( ">VALUE" ) ) *aText = wxT( "${VALUE}" );
121 else if( *aText == wxT( ">PART" ) ) *aText = wxT( "${REFERENCE}" );
122 else if( *aText == wxT( ">GATE" ) ) *aText = wxT( "${UNIT}" );
123 else if( *aText == wxT( ">MODULE" ) ) *aText = wxT( "${FOOTPRINT_NAME}" );
124 else if( *aText == wxT( ">SHEETNR" ) ) *aText = wxT( "${#}" );
125 else if( *aText == wxT( ">SHEETS" ) ) *aText = wxT( "${##}" );
126 else if( *aText == wxT( ">SHEET" ) ) *aText = wxT( "${#}/${##}" );
127 else if( *aText == wxT( ">SHEETNR_TOTAL" ) ) *aText = wxT( "${#}" );
128 else if( *aText == wxT( ">SHEETS_TOTAL" ) ) *aText = wxT( "${##}" );
129 else if( *aText == wxT( ">SHEET_TOTAL" ) ) *aText = wxT( "${#}/${##}" );
130 else if( *aText == wxT( ">SHEET_HEADLINE" ) ) *aText = wxT( "${SHEETNAME}" );
131 else if( *aText == wxT( ">ASSEMBLY_VARIANT" ) ) *aText = wxT( "${ASSEMBLY_VARIANT}" );
132 else if( *aText == wxT( ">DRAWING_NAME" ) ) *aText = wxT( "${PROJECTNAME}" );
133 else if( *aText == wxT( ">LAST_DATE_TIME" ) ) *aText = wxT( "${CURRENT_DATE}" );
134 else if( *aText == wxT( ">PLOT_DATE_TIME" ) ) *aText = wxT( "${CURRENT_DATE}" );
135 else return false;
136
137 return true;
138}

Referenced by interpretText().