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

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

Definition at line 164 of file eagle_parser.cpp.

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

Definition at line 141 of file eagle_parser.cpp.

142 {
143  double value;
144 
145  if( aValue.ToDouble( &value ) )
146  return value;
147  else
148  throw XML_PARSER_ERROR( "Conversion to double failed. Original value: '" +
149  aValue.ToStdString() + "'." );
150 }
Class XML_PARSER_ERROR implements a simple wrapper around runtime_error to isolate the errors thrown ...
Definition: eagle_parser.h:70
template<>
ECOORD Convert< ECOORD > ( const wxString &  aCoord)

Definition at line 195 of file eagle_parser.cpp.

196 {
197  // Eagle uses millimeters as the default unit
198  return ECOORD( aCoord, ECOORD::EAGLE_UNIT::EU_MM );
199 }
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 178 of file eagle_parser.cpp.

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

179 {
180  EROT value;
181 
182  value.spin = aRot.find( 'S' ) != aRot.npos;
183  value.mirror = aRot.find( 'M' ) != aRot.npos;
184  value.degrees = strtod( aRot.c_str()
185  + 1 // skip leading 'R'
186  + int( value.spin ) // skip optional leading 'S'
187  + int( value.mirror ), // skip optional leading 'M'
188  NULL );
189 
190  return value;
191 }
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
template<>
int Convert< int > ( const wxString &  aValue)

Definition at line 154 of file eagle_parser.cpp.

155 {
156  if( aValue.IsEmpty() )
157  throw XML_PARSER_ERROR( "Conversion to int failed. Original value is empty." );
158 
159  return wxAtoi( aValue );
160 }
Class XML_PARSER_ERROR implements a simple wrapper around runtime_error to isolate the errors thrown ...
Definition: eagle_parser.h:70
template<>
std::string Convert< std::string > ( const wxString &  aValue)

Definition at line 134 of file eagle_parser.cpp.

135 {
136  return std::string( aValue.ToUTF8() );
137 }
template<>
wxString Convert< wxString > ( const wxString &  aValue)

Definition at line 127 of file eagle_parser.cpp.

Referenced by Convert().

128 {
129  return aValue;
130 }
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 278 of file eagle_parser.cpp.

References DEG2RAD(), dist, wxPoint::x, and wxPoint::y.

Referenced by EAGLE_PLUGIN::loadPlain(), EAGLE_PLUGIN::loadPolygon(), EAGLE_PLUGIN::loadSignals(), SCH_EAGLE_PLUGIN::loadSymbolWire(), OPTIONAL_XML_ATTRIBUTE< bool >::operator->(), EAGLE_PLUGIN::packagePolygon(), and EAGLE_PLUGIN::packageWire().

279 {
280  // Eagle give us start and end.
281  // S_ARC wants start to give the center, and end to give the start.
282  double dx = aEnd.x - aStart.x, dy = aEnd.y - aStart.y;
283  wxPoint mid = ( aStart + aEnd ) / 2;
284 
285  double dlen = sqrt( dx*dx + dy*dy );
286  wxASSERT( dlen != 0 );
287  wxASSERT( aAngle != 0 );
288  double dist = dlen / ( 2 * tan( DEG2RAD( aAngle ) / 2 ) );
289 
290  wxPoint center(
291  mid.x + dist * ( dy / dlen ),
292  mid.y - dist * ( dx / dlen )
293  );
294 
295  return center;
296 }
static const int dist[10][10]
Definition: ar_matrix.cpp:320
double DEG2RAD(double deg)
Definition: trigo.h:199
timestamp_t EagleModuleTstamp ( const wxString &  aName,
const wxString &  aValue,
int  aUnit 
)

Computes module timestamp basing on its name, value and unit

Definition at line 268 of file eagle_parser.cpp.

Referenced by SCH_EAGLE_PLUGIN::loadInstance(), SCH_EAGLE_PLUGIN::loadSchematic(), and OPTIONAL_XML_ATTRIBUTE< bool >::operator->().

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

Make a unique time stamp

Definition at line 261 of file eagle_parser.cpp.

Referenced by EAGLE_PLUGIN::loadPlain(), EAGLE_PLUGIN::loadPolygon(), EAGLE_PLUGIN::loadSignals(), OPTIONAL_XML_ATTRIBUTE< bool >::operator->(), EAGLE_PLUGIN::packageCircle(), EAGLE_PLUGIN::packagePolygon(), EAGLE_PLUGIN::packageRectangle(), and EAGLE_PLUGIN::packageText().

262 {
263  // in this case from a unique tree memory location
264  return (timestamp_t) reinterpret_cast<uintptr_t>( aTree );
265 }
long timestamp_t
timestamp_t is our type to represent unique IDs for all kinds of elements; historically simply the ti...
Definition: common.h:52
wxString escapeName ( const wxString &  aNetName)

Translates Eagle special characters to their counterparts in KiCad.

Definition at line 37 of file eagle_parser.cpp.

References OPTIONAL_XML_ATTRIBUTE< T >::m_isAvailable, OPTIONAL_XML_ATTRIBUTE< T >::OPTIONAL_XML_ATTRIBUTE(), and OPTIONAL_XML_ATTRIBUTE< T >::Set().

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

38 {
39  wxString ret( aNetName );
40 
41  ret.Replace( "~", "~~" );
42  ret.Replace( "!", "~" );
43 
44  return ret;
45 }
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 237 of file eagle_parser.cpp.

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(), SCH_EAGLE_PLUGIN::loadSheet(), and OPTIONAL_XML_ATTRIBUTE< bool >::operator->().

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

Definition at line 299 of file eagle_parser.cpp.

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().

300 {
301  // (bottom-left | bottom-center | bottom-right | center-left |
302  // center | center-right | top-left | top-center | top-right)
303  if( aAlignment == "center" )
304  return ETEXT::CENTER;
305  else if( aAlignment == "center-right" )
306  return ETEXT::CENTER_RIGHT;
307  else if( aAlignment == "top-left" )
308  return ETEXT::TOP_LEFT;
309  else if( aAlignment == "top-center" )
310  return ETEXT::TOP_CENTER;
311  else if( aAlignment == "top-right" )
312  return ETEXT::TOP_RIGHT;
313  else if( aAlignment == "bottom-left" )
314  return ETEXT::BOTTOM_LEFT;
315  else if( aAlignment == "bottom-center" )
316  return ETEXT::BOTTOM_CENTER;
317  else if( aAlignment == "bottom-right" )
318  return ETEXT::BOTTOM_RIGHT;
319  else if( aAlignment == "center-left" )
320  return ETEXT::CENTER_LEFT;
321 
322  return DEFAULT_ALIGNMENT;
323 }
constexpr auto DEFAULT_ALIGNMENT
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 231 of file eagle_parser.cpp.

232 {
233  return OPTIONAL_XML_ATTRIBUTE<T>( aNode->GetAttribute( aAttribute ) );
234 }
Class OPTIONAL_XML_ATTRIBUTE models an optional XML attribute.
Definition: eagle_parser.h:190
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 211 of file eagle_parser.cpp.

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

Variable Documentation

constexpr auto DEFAULT_ALIGNMENT = ETEXT::BOTTOM_LEFT

Definition at line 34 of file eagle_parser.cpp.

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