KiCad PCB EDA Suite
eagle_parser.cpp File Reference
#include <plugins/eagle/eagle_parser.h>
#include <string_utils.h>
#include <richio.h>
#include <wx/log.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...
 
VECTOR2I ConvertArcCenter (const VECTOR2I &aStart, const VECTOR2I &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:69

◆ 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}

◆ 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 nullptr );
198
199 return value;
200}
Eagle rotation.
Definition: eagle_parser.h:471
double degrees
Definition: eagle_parser.h:474
bool spin
Definition: eagle_parser.h:473
bool mirror
Definition: eagle_parser.h:472

References EROT::degrees, EROT::mirror, 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}

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

VECTOR2I ConvertArcCenter ( const VECTOR2I aStart,
const VECTOR2I 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 VECTOR2I mid = ( aStart + aEnd ) / 2;
276
277 double dlen = sqrt( dx*dx + dy*dy );
278
279 if( !std::isnormal( dlen ) || !std::isnormal( aAngle ) )
280 {
281 THROW_IO_ERROR( wxString::Format( _( "Invalid Arc with radius %f and angle %f" ),
282 dlen,
283 aAngle ) );
284 }
285
286 double dist = dlen / ( 2 * tan( DEG2RAD( aAngle ) / 2 ) );
287
288 VECTOR2I center(
289 mid.x + dist * ( dy / dlen ),
290 mid.y - dist * ( dx / dlen )
291 );
292
293 return center;
294}
#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(), EAGLE_PLUGIN::packagePolygon(), and EAGLE_PLUGIN::packageWire().

◆ escapeName()

wxString escapeName ( const wxString &  aNetName)

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

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

◆ parseAlignment()

static int parseAlignment ( const wxString &  aAlignment)
static

Definition at line 297 of file eagle_parser.cpp.

298{
299 // (bottom-left | bottom-center | bottom-right | center-left |
300 // center | center-right | top-left | top-center | top-right)
301 if( aAlignment == "center" )
302 return ETEXT::CENTER;
303 else if( aAlignment == "center-right" )
304 return ETEXT::CENTER_RIGHT;
305 else if( aAlignment == "top-left" )
306 return ETEXT::TOP_LEFT;
307 else if( aAlignment == "top-center" )
308 return ETEXT::TOP_CENTER;
309 else if( aAlignment == "top-right" )
310 return ETEXT::TOP_RIGHT;
311 else if( aAlignment == "bottom-left" )
312 return ETEXT::BOTTOM_LEFT;
313 else if( aAlignment == "bottom-center" )
315 else if( aAlignment == "bottom-right" )
316 return ETEXT::BOTTOM_RIGHT;
317 else if( aAlignment == "center-left" )
318 return ETEXT::CENTER_LEFT;
319
320 return DEFAULT_ALIGNMENT;
321}
constexpr auto DEFAULT_ALIGNMENT
@ BOTTOM_CENTER
Definition: eagle_parser.h:655
@ BOTTOM_RIGHT
Definition: eagle_parser.h:657
@ TOP_CENTER
Definition: eagle_parser.h:649
@ TOP_LEFT
Definition: eagle_parser.h:650
@ TOP_RIGHT
Definition: eagle_parser.h:651
@ CENTER_RIGHT
Definition: eagle_parser.h:654
@ CENTER_LEFT
Definition: eagle_parser.h:648
@ BOTTOM_LEFT
Definition: eagle_parser.h:656

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:189

◆ 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}

Variable Documentation

◆ DEFAULT_ALIGNMENT

constexpr auto DEFAULT_ALIGNMENT = ETEXT::BOTTOM_LEFT
constexpr

Definition at line 37 of file eagle_parser.cpp.

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