/* * This program source code file is part of KiCad, a free EDA CAD application. * * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck * Copyright (C) 2012-2016 KiCad Developers, see AUTHORS.txt for contributors. * Copyright (C) 2017 CERN. * @author Alejandro GarcĂ­a Montoro * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, you may find one here: * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html * or you may search the http://www.gnu.org website for the version 2 license, * or you may write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ #include #include #include #include #include using std::string; // Template specializations below parse wxString to the used types: // - string // - double // - int // - bool // - EROT template<> string Convert( const wxString& aValue ) { return aValue.ToStdString(); } template <> double Convert( const wxString& aValue ) { double value; if( aValue.ToDouble( &value ) ) return value; else throw XML_PARSER_ERROR( "Conversion to double failed. Original value: '" + aValue.ToStdString() + "'." ); } template <> int Convert( const wxString& aValue ) { if( aValue.IsEmpty() ) throw XML_PARSER_ERROR( "Conversion to int failed. Original value is empty." ); return wxAtoi( aValue ); } template <> bool Convert( const wxString& aValue ) { if( aValue != "yes" && aValue != "no" ) throw XML_PARSER_ERROR( "Conversion to bool failed. Original value, '" + aValue.ToStdString() + "', is neither 'yes' nor 'no'." ); return aValue == "yes"; } /// parse an Eagle XML "rot" field. Unfortunately the DTD seems not to explain /// this format very well. [S][M]R. Examples: "R90", "MR180", "SR180" template<> EROT Convert( const wxString& aRot ) { EROT value; value.spin = aRot.find( 'S' ) != aRot.npos; value.mirror = aRot.find( 'M' ) != aRot.npos; value.degrees = strtod( aRot.c_str() + 1 // skip leading 'R' + int( value.spin ) // skip optional leading 'S' + int( value.mirror ), // skip optional leading 'M' NULL ); return value; } /** * Function parseRequiredAttribute * parsese the aAttribute of the XML node aNode. * @param aNode is the node whose attribute will be parsed. * @param aAttribute is the attribute that will be parsed. * @throw XML_PARSER_ERROR - exception thrown if the required attribute is missing * @return T - the attributed parsed as the specified type. */ template T parseRequiredAttribute( wxXmlNode* aNode, string aAttribute ) { wxString value; if( aNode->GetAttribute( aAttribute, &value ) ) return Convert( value ); else throw XML_PARSER_ERROR( "The required attribute " + aAttribute + " is missing." ); } /** * Function parseOptionalAttribute * parses the aAttribute of the XML node aNode. * @param aNode is the node whose attribute will be parsed. * @param aAttribute is the attribute that will be parsed. * @return OPTIONAL_XML_ATTRIBUTE - an optional XML attribute, parsed as the specified type if * found. */ template OPTIONAL_XML_ATTRIBUTE parseOptionalAttribute( wxXmlNode* aNode, string aAttribute ) { return OPTIONAL_XML_ATTRIBUTE( aNode->GetAttribute( aAttribute ) ); } EWIRE::EWIRE( wxXmlNode* aWire ) { /* */ x1 = parseRequiredAttribute( aWire, "x1" ); y1 = parseRequiredAttribute( aWire, "y1" ); x2 = parseRequiredAttribute( aWire, "x2" ); y2 = parseRequiredAttribute( aWire, "y2" ); width = parseRequiredAttribute( aWire, "width" ); layer = parseRequiredAttribute( aWire, "layer" ); curve = parseOptionalAttribute( aWire, "curve" ); opt_string s = parseOptionalAttribute( aWire, "style" ); if( s == "continuous" ) style = EWIRE::CONTINUOUS; else if( s == "longdash" ) style = EWIRE::LONGDASH; else if( s == "shortdash" ) style = EWIRE::SHORTDASH; else if( s == "dashdot" ) style = EWIRE::DASHDOT; s = parseOptionalAttribute( aWire, "cap" ); if( s == "round" ) cap = EWIRE::ROUND; else if( s == "flat" ) cap = EWIRE::FLAT; } EVIA::EVIA( wxXmlNode* aVia ) { /* */ x = parseRequiredAttribute( aVia, "x" ); y = parseRequiredAttribute( aVia, "y" ); string ext = parseRequiredAttribute( aVia, "extent" ); sscanf( ext.c_str(), "%d-%d", &layer_front_most, &layer_back_most ); drill = parseRequiredAttribute( aVia, "drill" ); diam = parseOptionalAttribute( aVia, "diameter" ); shape = parseOptionalAttribute( aVia, "shape" ); } ECIRCLE::ECIRCLE( wxXmlNode* aCircle ) { /* */ x = parseRequiredAttribute( aCircle, "x" ); y = parseRequiredAttribute( aCircle, "y" ); radius = parseRequiredAttribute( aCircle, "radius" ); width = parseRequiredAttribute( aCircle, "width" ); layer = parseRequiredAttribute( aCircle, "layer" ); } ERECT::ERECT( wxXmlNode* aRect ) { /* */ x1 = parseRequiredAttribute( aRect, "x1" ); y1 = parseRequiredAttribute( aRect, "y1" ); x2 = parseRequiredAttribute( aRect, "x2" ); y2 = parseRequiredAttribute( aRect, "y2" ); layer = parseRequiredAttribute( aRect, "layer" ); rot = parseOptionalAttribute( aRect, "rot" ); } EATTR::EATTR( wxXmlNode* aTree ) { /* or context -- constant %Bool; "no" -- only in context -- > */ name = parseRequiredAttribute( aTree, "name" ); value = parseOptionalAttribute( aTree, "value" ); x = parseOptionalAttribute( aTree, "x" ); y = parseOptionalAttribute( aTree, "y" ); size = parseOptionalAttribute( aTree, "size" ); // KiCad cannot currently put a TEXTE_MODULE on a different layer than the MODULE // Eagle can it seems. layer = parseOptionalAttribute( aTree, "layer" ); ratio = parseOptionalAttribute( aTree, "ratio" ); rot = parseOptionalAttribute( aTree, "rot" ); opt_string stemp = parseOptionalAttribute( aTree, "display" ); // (off | value | name | both) if( stemp == "off" ) display = EATTR::Off; else if( stemp == "value" ) display = EATTR::VALUE; else if( stemp == "name" ) display = EATTR::NAME; else if( stemp == "both" ) display = EATTR::BOTH; } EDIMENSION::EDIMENSION( wxXmlNode* aDimension ) { /* */ x1 = parseRequiredAttribute( aDimension, "x1" ); y1 = parseRequiredAttribute( aDimension, "y1" ); x2 = parseRequiredAttribute( aDimension, "x2" ); y2 = parseRequiredAttribute( aDimension, "y2" ); x3 = parseRequiredAttribute( aDimension, "x3" ); y3 = parseRequiredAttribute( aDimension, "y3" ); layer = parseRequiredAttribute( aDimension, "layer" ); opt_string dimType = parseOptionalAttribute( aDimension, "dtype" ); if( !dimType ) { // default type is parallel } } ETEXT::ETEXT( wxXmlNode* aText ) { /* */ text = aText->GetNodeContent(); x = parseRequiredAttribute( aText, "x" ); y = parseRequiredAttribute( aText, "y" ); size = parseRequiredAttribute( aText, "size" ); layer = parseRequiredAttribute( aText, "layer" ); font = parseOptionalAttribute( aText, "font" ); ratio = parseOptionalAttribute( aText, "ratio" ); rot = parseOptionalAttribute( aText, "rot" ); opt_string stemp = parseOptionalAttribute( aText, "align" ); // (bottom-left | bottom-center | bottom-right | center-left | // center | center-right | top-left | top-center | top-right) if( stemp == "center" ) align = ETEXT::CENTER; else if( stemp == "center-right" ) align = ETEXT::CENTER_RIGHT; else if( stemp == "top-left" ) align = ETEXT::TOP_LEFT; else if( stemp == "top-center" ) align = ETEXT::TOP_CENTER; else if( stemp == "top-right" ) align = ETEXT::TOP_RIGHT; else if( stemp == "bottom-left" ) align = ETEXT::BOTTOM_LEFT; else if( stemp == "bottom-center" ) align = ETEXT::BOTTOM_CENTER; else if( stemp == "bottom-right" ) align = ETEXT::BOTTOM_RIGHT; else if( stemp == "center-left" ) align = ETEXT::CENTER_LEFT; } EPAD::EPAD( wxXmlNode* aPad ) { /* */ // #REQUIRED says DTD, throw exception if not found name = parseRequiredAttribute( aPad, "name" ); x = parseRequiredAttribute( aPad, "x" ); y = parseRequiredAttribute( aPad, "y" ); drill = parseRequiredAttribute( aPad, "drill" ); // Optional attributes diameter = parseOptionalAttribute( aPad, "diameter" ); opt_string s = parseOptionalAttribute( aPad, "shape" ); // (square | round | octagon | long | offset) if( s == "square" ) shape = EPAD::SQUARE; else if( s == "round" ) shape = EPAD::ROUND; else if( s == "octagon" ) shape = EPAD::OCTAGON; else if( s == "long" ) shape = EPAD::LONG; else if( s == "offset" ) shape = EPAD::OFFSET; rot = parseOptionalAttribute( aPad, "rot" ); stop = parseOptionalAttribute( aPad, "stop" ); thermals = parseOptionalAttribute( aPad, "thermals" ); first = parseOptionalAttribute( aPad, "first" ); } ESMD::ESMD( wxXmlNode* aSMD ) { /* */ // DTD #REQUIRED, throw exception if not found name = parseRequiredAttribute( aSMD, "name" ); x = parseRequiredAttribute( aSMD, "x" ); y = parseRequiredAttribute( aSMD, "y" ); dx = parseRequiredAttribute( aSMD, "dx" ); dy = parseRequiredAttribute( aSMD, "dy" ); layer = parseRequiredAttribute( aSMD, "layer" ); roundness = parseOptionalAttribute( aSMD, "roundness" ); rot = parseOptionalAttribute( aSMD, "rot" ); thermals = parseOptionalAttribute( aSMD, "thermals" ); stop = parseOptionalAttribute( aSMD, "stop" ); thermals = parseOptionalAttribute( aSMD, "thermals" ); cream = parseOptionalAttribute( aSMD, "cream" ); } EVERTEX::EVERTEX( wxXmlNode* aVertex ) { /* */ x = parseRequiredAttribute( aVertex, "x" ); y = parseRequiredAttribute( aVertex, "y" ); } EPOLYGON::EPOLYGON( wxXmlNode* aPolygon ) { /* or context -- orphans %Bool; "no" -- only in context -- thermals %Bool; "yes" -- only in context -- rank %Int; "0" -- 1..6 in context, 0 or 7 in context -- > */ width = parseRequiredAttribute( aPolygon, "width" ); layer = parseRequiredAttribute( aPolygon, "layer" ); spacing = parseOptionalAttribute( aPolygon, "spacing" ); isolate = parseOptionalAttribute( aPolygon, "isolate" ); opt_string s = parseOptionalAttribute( aPolygon, "pour" ); // default pour to solid fill pour = EPOLYGON::SOLID; // (solid | hatch | cutout) if( s == "hatch" ) pour = EPOLYGON::HATCH; else if( s == "cutout" ) pour = EPOLYGON::CUTOUT; orphans = parseOptionalAttribute( aPolygon, "orphans" ); thermals = parseOptionalAttribute( aPolygon, "thermals" ); rank = parseOptionalAttribute( aPolygon, "rank" ); } EHOLE::EHOLE( wxXmlNode* aHole ) { /* */ // #REQUIRED: x = parseRequiredAttribute( aHole, "x" ); y = parseRequiredAttribute( aHole, "y" ); drill = parseRequiredAttribute( aHole, "drill" ); } EELEMENT::EELEMENT( wxXmlNode* aElement ) { /* */ // #REQUIRED name = parseRequiredAttribute( aElement, "name" ); library = parseRequiredAttribute( aElement, "library" ); value = parseRequiredAttribute( aElement, "value" ); package = parseRequiredAttribute( aElement, "package" ); ReplaceIllegalFileNameChars( &package ); x = parseRequiredAttribute( aElement, "x" ); y = parseRequiredAttribute( aElement, "y" ); // optional locked = parseOptionalAttribute( aElement, "locked" ); smashed = parseOptionalAttribute( aElement, "smashed" ); rot = parseOptionalAttribute( aElement, "rot" ); } ELAYER::ELAYER( wxXmlNode* aLayer ) { /* */ number = parseRequiredAttribute( aLayer, "number" ); name = parseRequiredAttribute( aLayer, "name" ); color = parseRequiredAttribute( aLayer, "color" ); fill = 1; // Temporary value. visible = parseOptionalAttribute( aLayer, "visible" ); active = parseOptionalAttribute( aLayer, "active" ); } NODE_MAP MapChildren( wxXmlNode* currentNode ) { // Map node_name -> node_pointer NODE_MAP nodesMap; // Loop through all children mapping them in nodesMap currentNode = currentNode->GetChildren(); while( currentNode ) { // Create a new pair in the map // key: current node name // value: current node pointer nodesMap[currentNode->GetName().ToStdString()] = currentNode; // Get next child currentNode = currentNode->GetNext(); } return nodesMap; } string makeKey( const string& aFirst, const string& aSecond ) { string key = aFirst + '\x02' + aSecond; return key; } unsigned long timeStamp( wxXmlNode* aTree ) { // in this case from a unique tree memory location return (unsigned long)(void*) aTree; } wxPoint kicad_arc_center( const wxPoint& aStart, const wxPoint& aEnd, double aAngle ) { // Eagle give us start and end. // S_ARC wants start to give the center, and end to give the start. double dx = aEnd.x - aStart.x, dy = aEnd.y - aStart.y; wxPoint mid = ( aStart + aEnd ) / 2; double dlen = sqrt( dx * dx + dy * dy ); double dist = dlen / ( 2 * tan( DEG2RAD( aAngle ) / 2 ) ); wxPoint center( mid.x + dist * ( dy / dlen ), mid.y - dist * ( dx / dlen ) ); return center; }