KiCad PCB EDA Suite
eagle_parser.cpp File Reference
#include <plugins/eagle/eagle_parser.h>
#include <kicad_string.h>
#include <richio.h>
#include <functional>
#include <cstdio>

Go to the source code of this file.

Functions

wxString escapeName (const wxString &aNetName)
 
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)
 Parse aAttribute of the XML node aNode. More...
 
template<typename T >
OPTIONAL_XML_ATTRIBUTE< T > parseOptionalAttribute (wxXmlNode *aNode, const wxString &aAttribute)
 Parse option aAttribute of the XML node aNode. More...
 
NODE_MAP MapChildren (wxXmlNode *aCurrentNode)
 Provide an easy access to the children of an XML node via their names. More...
 
wxPoint ConvertArcCenter (const wxPoint &aStart, const wxPoint &aEnd, double aAngle)
 
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 173 of file eagle_parser.cpp.

174 {
175  if( aValue != "yes" && aValue != "no" )
176  throw XML_PARSER_ERROR( "Conversion to bool failed. Original value, '" +
177  aValue.ToStdString() +
178  "', is neither 'yes' nor 'no'." );
179 
180  return aValue == "yes";
181 }
Implement a simple wrapper around runtime_error to isolate the errors thrown by the Eagle XML parser.
Definition: eagle_parser.h:68

◆ Convert< double >()

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

Definition at line 150 of file eagle_parser.cpp.

151 {
152  double value;
153 
154  if( aValue.ToCDouble( &value ) )
155  return value;
156  else
157  throw XML_PARSER_ERROR( "Conversion to double failed. Original value: '" +
158  aValue.ToStdString() + "'." );
159 }
Implement a simple wrapper around runtime_error to isolate the errors thrown by the Eagle XML parser.
Definition: eagle_parser.h:68

◆ Convert< ECOORD >()

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

Definition at line 204 of file eagle_parser.cpp.

205 {
206  // Eagle uses millimeters as the default unit
207  return ECOORD( aCoord, ECOORD::EAGLE_UNIT::EU_MM );
208 }

◆ 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 187 of file eagle_parser.cpp.

188 {
189  EROT value;
190 
191  value.spin = aRot.find( 'S' ) != aRot.npos;
192  value.mirror = aRot.find( 'M' ) != aRot.npos;
193  value.degrees = strtod( aRot.c_str()
194  + 1 // skip leading 'R'
195  + int( value.spin ) // skip optional leading 'S'
196  + int( value.mirror ), // skip optional leading 'M'
197  NULL );
198 
199  return value;
200 }
Eagle rotation.
Definition: eagle_parser.h:470
bool mirror
Definition: eagle_parser.h:472
double degrees
Definition: eagle_parser.h:474
#define NULL
bool spin
Definition: eagle_parser.h:473

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

◆ Convert< int >()

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

Definition at line 163 of file eagle_parser.cpp.

164 {
165  if( aValue.IsEmpty() )
166  throw XML_PARSER_ERROR( "Conversion to int failed. Original value is empty." );
167 
168  return wxAtoi( aValue );
169 }
Implement a simple wrapper around runtime_error to isolate the errors thrown by the Eagle XML parser.
Definition: eagle_parser.h:68

◆ Convert< std::string >()

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

Definition at line 143 of file eagle_parser.cpp.

144 {
145  return std::string( aValue.ToUTF8() );
146 }

◆ Convert< wxString >()

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

Definition at line 136 of file eagle_parser.cpp.

137 {
138  return aValue;
139 }

◆ ConvertArcCenter()

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

Definition at line 270 of file eagle_parser.cpp.

271 {
272  // Eagle give us start and end.
273  // S_ARC wants start to give the center, and end to give the start.
274  double dx = aEnd.x - aStart.x, dy = aEnd.y - aStart.y;
275  wxPoint mid = ( aStart + aEnd ) / 2;
276 
277  double dlen = sqrt( dx*dx + dy*dy );
278 
279  if( !std::isnormal( dlen ) || !std::isnormal( aAngle ) )
280  {
282  wxString::Format( _( "Invalid Arc with radius %f and angle %f" ), dlen, aAngle ) );
283  }
284 
285  double dist = dlen / ( 2 * tan( DEG2RAD( aAngle ) / 2 ) );
286 
287  wxPoint center(
288  mid.x + dist * ( dy / dlen ),
289  mid.y - dist * ( dx / dlen )
290  );
291 
292  return center;
293 }
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:231
#define _(s)
Definition: 3d_actions.cpp:33
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38

References _, DEG2RAD(), 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().

◆ escapeName()

wxString escapeName ( const wxString &  aNetName)

Definition at line 39 of file eagle_parser.cpp.

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

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)

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 246 of file eagle_parser.cpp.

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

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 296 of file eagle_parser.cpp.

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

Parse option 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 240 of file eagle_parser.cpp.

241 {
242  return OPTIONAL_XML_ATTRIBUTE<T>( aNode->GetAttribute( aAttribute ) );
243 }
Model an optional XML attribute.
Definition: eagle_parser.h:188

◆ parseRequiredAttribute()

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

Parse 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 220 of file eagle_parser.cpp.

221 {
222  wxString value;
223 
224  if( aNode->GetAttribute( aAttribute, &value ) )
225  return Convert<T>( value );
226  else
227  throw XML_PARSER_ERROR( "The required attribute " + aAttribute + " is missing." );
228 }
Implement a simple wrapper around runtime_error to isolate the errors thrown by the Eagle XML parser.
Definition: eagle_parser.h:68

Variable Documentation

◆ DEFAULT_ALIGNMENT

constexpr auto DEFAULT_ALIGNMENT = ETEXT::BOTTOM_LEFT

Definition at line 36 of file eagle_parser.cpp.

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