KiCad PCB EDA Suite
eagle_parser.cpp File Reference
#include <eagle_parser.h>
#include <functional>
#include <sstream>
#include <iomanip>
#include <cstdio>

Go to the source code of this file.

Functions

wxString escapeName (const wxString &aNetName)
 

Translates Eagle special characters to their counterparts in KiCad.

More...
 
template<>
wxString Convert< wxString > (const wxString &aValue)
 
template<>
std::string Convert< std::string > (const wxString &aValue)
 
template<>
double Convert< double > (const wxString &aValue)
 
template<>
int Convert< int > (const wxString &aValue)
 
template<>
bool Convert< bool > (const wxString &aValue)
 
template<>
EROT Convert< EROT > (const wxString &aRot)
 parse an Eagle XML "rot" field. More...
 
template<>
ECOORD Convert< ECOORD > (const wxString &aCoord)
 
template<typename T >
parseRequiredAttribute (wxXmlNode *aNode, const wxString &aAttribute)
 Function parseRequiredAttribute parsese the aAttribute of the XML node aNode. More...
 
template<typename T >
OPTIONAL_XML_ATTRIBUTE< T > parseOptionalAttribute (wxXmlNode *aNode, const wxString &aAttribute)
 Function parseOptionalAttribute parses the aAttribute of the XML node aNode. More...
 
NODE_MAP MapChildren (wxXmlNode *aCurrentNode)
 Function MapChildren provides an easy access to the children of an XML node via their names. More...
 
timestamp_t EagleTimeStamp (wxXmlNode *aTree)
 

Make a unique time stamp

More...
 
timestamp_t EagleModuleTstamp (const wxString &aName, const wxString &aValue, int aUnit)
 

Computes module timestamp basing on its name, value and unit

More...
 
wxPoint ConvertArcCenter (const wxPoint &aStart, const wxPoint &aEnd, double aAngle)
 

Convert an Eagle curve end to a KiCad center for S_ARC

More...
 
static int parseAlignment (const wxString &aAlignment)
 

Variables

constexpr auto DEFAULT_ALIGNMENT = ETEXT::BOTTOM_LEFT
 

Function Documentation

◆ Convert< bool >()

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

Definition at line 166 of file eagle_parser.cpp.

167 {
168  if( aValue != "yes" && aValue != "no" )
169  throw XML_PARSER_ERROR( "Conversion to bool failed. Original value, '" +
170  aValue.ToStdString() +
171  "', is neither 'yes' nor 'no'." );
172 
173  return aValue == "yes";
174 }
Class XML_PARSER_ERROR implements a simple wrapper around runtime_error to isolate the errors thrown ...
Definition: eagle_parser.h:70

◆ Convert< double >()

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

Definition at line 143 of file eagle_parser.cpp.

144 {
145  double value;
146 
147  if( aValue.ToDouble( &value ) )
148  return value;
149  else
150  throw XML_PARSER_ERROR( "Conversion to double failed. Original value: '" +
151  aValue.ToStdString() + "'." );
152 }
Class XML_PARSER_ERROR implements a simple wrapper around runtime_error to isolate the errors thrown ...
Definition: eagle_parser.h:70

◆ Convert< ECOORD >()

template<>
ECOORD Convert< ECOORD > ( const wxString &  aCoord)

Definition at line 197 of file eagle_parser.cpp.

198 {
199  // Eagle uses millimeters as the default unit
200  return ECOORD( aCoord, ECOORD::EAGLE_UNIT::EU_MM );
201 }

◆ Convert< EROT >()

template<>
EROT Convert< EROT > ( const wxString &  aRot)

parse an Eagle XML "rot" field.

Unfortunately the DTD seems not to explain this format very well. [S][M]R<degrees>. Examples: "R90", "MR180", "SR180"

Definition at line 180 of file eagle_parser.cpp.

181 {
182  EROT value;
183 
184  value.spin = aRot.find( 'S' ) != aRot.npos;
185  value.mirror = aRot.find( 'M' ) != aRot.npos;
186  value.degrees = strtod( aRot.c_str()
187  + 1 // skip leading 'R'
188  + int( value.spin ) // skip optional leading 'S'
189  + int( value.mirror ), // skip optional leading 'M'
190  NULL );
191 
192  return value;
193 }
Eagle rotation.
Definition: eagle_parser.h:479
bool mirror
Definition: eagle_parser.h:481
double degrees
Definition: eagle_parser.h:483
bool spin
Definition: eagle_parser.h:482

References EROT::degrees, EROT::mirror, and EROT::spin.

◆ Convert< int >()

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

Definition at line 156 of file eagle_parser.cpp.

157 {
158  if( aValue.IsEmpty() )
159  throw XML_PARSER_ERROR( "Conversion to int failed. Original value is empty." );
160 
161  return wxAtoi( aValue );
162 }
Class XML_PARSER_ERROR implements a simple wrapper around runtime_error to isolate the errors thrown ...
Definition: eagle_parser.h:70

◆ Convert< std::string >()

template<>
std::string Convert< std::string > ( const wxString &  aValue)

Definition at line 136 of file eagle_parser.cpp.

137 {
138  return std::string( aValue.ToUTF8() );
139 }

◆ Convert< wxString >()

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

Definition at line 129 of file eagle_parser.cpp.

130 {
131  return aValue;
132 }

◆ ConvertArcCenter()

wxPoint ConvertArcCenter ( const wxPoint &  aStart,
const wxPoint &  aEnd,
double  aAngle 
)

Convert an Eagle curve end to a KiCad center for S_ARC

Definition at line 280 of file eagle_parser.cpp.

281 {
282  // Eagle give us start and end.
283  // S_ARC wants start to give the center, and end to give the start.
284  double dx = aEnd.x - aStart.x, dy = aEnd.y - aStart.y;
285  wxPoint mid = ( aStart + aEnd ) / 2;
286 
287  double dlen = sqrt( dx*dx + dy*dy );
288 
289  if( !std::isnormal( dlen ) || !std::isnormal( aAngle ) )
290  {
292  wxString::Format( _( "Invalid Arc with radius %f and angle %f" ), dlen, aAngle ) );
293  }
294 
295  double dist = dlen / ( 2 * tan( DEG2RAD( aAngle ) / 2 ) );
296 
297  wxPoint center(
298  mid.x + dist * ( dy / dlen ),
299  mid.y - dist * ( dx / dlen )
300  );
301 
302  return center;
303 }
static const int dist[10][10]
Definition: ar_matrix.cpp:320
#define THROW_IO_ERROR(msg)
#define _(s)
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
double DEG2RAD(double deg)
Definition: trigo.h:210

References _, DEG2RAD(), dist, Format(), and THROW_IO_ERROR.

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

◆ EagleModuleTstamp()

timestamp_t EagleModuleTstamp ( const wxString &  aName,
const wxString &  aValue,
int  aUnit 
)

Computes module timestamp basing on its name, value and unit

Definition at line 270 of file eagle_parser.cpp.

271 {
272  std::size_t h1 = std::hash<wxString>{}( aName );
273  std::size_t h2 = std::hash<wxString>{}( aValue );
274  std::size_t h3 = std::hash<int>{}( aUnit );
275 
276  return (timestamp_t)( h1 ^ (h2 << 1) ^ (h3 << 2) );
277 }
uint32_t timestamp_t
timestamp_t is our type to represent unique IDs for all kinds of elements; historically simply the ti...
Definition: common.h:53

Referenced by SCH_EAGLE_PLUGIN::loadInstance(), and SCH_EAGLE_PLUGIN::loadSchematic().

◆ EagleTimeStamp()

timestamp_t EagleTimeStamp ( wxXmlNode *  aTree)

Make a unique time stamp

Definition at line 263 of file eagle_parser.cpp.

264 {
265  // in this case from a unique tree memory location
266  return (timestamp_t) reinterpret_cast<uintptr_t>( aTree );
267 }
uint32_t timestamp_t
timestamp_t is our type to represent unique IDs for all kinds of elements; historically simply the ti...
Definition: common.h:53

Referenced by EAGLE_PLUGIN::loadPlain(), EAGLE_PLUGIN::loadPolygon(), EAGLE_PLUGIN::loadSignals(), EAGLE_PLUGIN::packageCircle(), EAGLE_PLUGIN::packagePolygon(), EAGLE_PLUGIN::packageRectangle(), and EAGLE_PLUGIN::packageText().

◆ escapeName()

wxString escapeName ( const wxString &  aNetName)

Translates Eagle special characters to their counterparts in KiCad.

Definition at line 37 of file eagle_parser.cpp.

38 {
39  wxString ret( aNetName );
40 
41  ret.Replace( "~", "~~" );
42  ret.Replace( "!", "~" );
43 
44  return ret;
45 }

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

◆ MapChildren()

NODE_MAP MapChildren ( wxXmlNode *  aCurrentNode)

Function MapChildren provides 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 - a map linking the name of each children to the children itself (via a wxXmlNode*)

Definition at line 239 of file eagle_parser.cpp.

240 {
241  // Map node_name -> node_pointer
242  NODE_MAP nodesMap;
243 
244  // Loop through all children mapping them in nodesMap
245  if( aCurrentNode )
246  aCurrentNode = aCurrentNode->GetChildren();
247 
248  while( aCurrentNode )
249  {
250  // Create a new pair in the map
251  // key: current node name
252  // value: current node pointer
253  nodesMap[aCurrentNode->GetName()] = aCurrentNode;
254 
255  // Get next child
256  aCurrentNode = aCurrentNode->GetNext();
257  }
258 
259  return nodesMap;
260 }
std::unordered_map< wxString, wxXmlNode * > NODE_MAP
Definition: eagle_parser.h:48

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

◆ parseAlignment()

static int parseAlignment ( const wxString &  aAlignment)
static

Definition at line 306 of file eagle_parser.cpp.

307 {
308  // (bottom-left | bottom-center | bottom-right | center-left |
309  // center | center-right | top-left | top-center | top-right)
310  if( aAlignment == "center" )
311  return ETEXT::CENTER;
312  else if( aAlignment == "center-right" )
313  return ETEXT::CENTER_RIGHT;
314  else if( aAlignment == "top-left" )
315  return ETEXT::TOP_LEFT;
316  else if( aAlignment == "top-center" )
317  return ETEXT::TOP_CENTER;
318  else if( aAlignment == "top-right" )
319  return ETEXT::TOP_RIGHT;
320  else if( aAlignment == "bottom-left" )
321  return ETEXT::BOTTOM_LEFT;
322  else if( aAlignment == "bottom-center" )
323  return ETEXT::BOTTOM_CENTER;
324  else if( aAlignment == "bottom-right" )
325  return ETEXT::BOTTOM_RIGHT;
326  else if( aAlignment == "center-left" )
327  return ETEXT::CENTER_LEFT;
328 
329  return DEFAULT_ALIGNMENT;
330 }
constexpr auto DEFAULT_ALIGNMENT

References ETEXT::BOTTOM_CENTER, ETEXT::BOTTOM_LEFT, ETEXT::BOTTOM_RIGHT, ETEXT::CENTER, ETEXT::CENTER_LEFT, ETEXT::CENTER_RIGHT, DEFAULT_ALIGNMENT, ETEXT::TOP_CENTER, ETEXT::TOP_LEFT, and ETEXT::TOP_RIGHT.

Referenced by EATTR::EATTR(), and ETEXT::ETEXT().

◆ parseOptionalAttribute()

template<typename T >
OPTIONAL_XML_ATTRIBUTE<T> parseOptionalAttribute ( wxXmlNode *  aNode,
const wxString &  aAttribute 
)

Function parseOptionalAttribute parses the aAttribute of the XML node aNode.

Parameters
aNodeis the node whose attribute will be parsed.
aAttributeis the attribute that will be parsed.
Returns
OPTIONAL_XML_ATTRIBUTE<T> - an optional XML attribute, parsed as the specified type if found.

Definition at line 233 of file eagle_parser.cpp.

234 {
235  return OPTIONAL_XML_ATTRIBUTE<T>( aNode->GetAttribute( aAttribute ) );
236 }
Class OPTIONAL_XML_ATTRIBUTE models an optional XML attribute.
Definition: eagle_parser.h:190

◆ parseRequiredAttribute()

template<typename T >
T parseRequiredAttribute ( wxXmlNode *  aNode,
const wxString &  aAttribute 
)

Function parseRequiredAttribute parsese the aAttribute of the XML node aNode.

Parameters
aNodeis the node whose attribute will be parsed.
aAttributeis the attribute that will be parsed.
Exceptions
XML_PARSER_ERROR- exception thrown if the required attribute is missing
Returns
T - the attributed parsed as the specified type.

Definition at line 213 of file eagle_parser.cpp.

214 {
215  wxString value;
216 
217  if( aNode->GetAttribute( aAttribute, &value ) )
218  return Convert<T>( value );
219  else
220  throw XML_PARSER_ERROR( "The required attribute " + aAttribute + " is missing." );
221 }
Class XML_PARSER_ERROR implements a simple wrapper around runtime_error to isolate the errors thrown ...
Definition: eagle_parser.h:70

Variable Documentation

◆ DEFAULT_ALIGNMENT

constexpr auto DEFAULT_ALIGNMENT = ETEXT::BOTTOM_LEFT

Definition at line 34 of file eagle_parser.cpp.

Referenced by EATTR::EATTR(), ETEXT::ETEXT(), and parseAlignment().