/* * This program source code file is part of KiCad, a free EDA CAD application. * * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck * Copyright (C) 2012-2021 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 #include constexpr auto DEFAULT_ALIGNMENT = ETEXT::BOTTOM_LEFT; wxString escapeName( const wxString& aNetName ) { wxString ret( aNetName ); ret.Replace( "!", "~" ); return ConvertToNewOverbarNotation( ret ); } template<> template<> OPTIONAL_XML_ATTRIBUTE::OPTIONAL_XML_ATTRIBUTE( wxString aData ) { m_isAvailable = !aData.IsEmpty(); if( m_isAvailable ) Set( aData ); } ECOORD::ECOORD( const wxString& aValue, enum ECOORD::EAGLE_UNIT aUnit ) { // This array is used to adjust the fraction part value basing on the number of digits // in the fraction. constexpr int DIVIDERS[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000 }; constexpr unsigned int DIVIDERS_MAX_IDX = sizeof( DIVIDERS ) / sizeof( DIVIDERS[0] ) - 1; int integer, fraction, pre_fraction, post_fraction; // The following check is needed to handle correctly negative fractions where the integer // part == 0. bool negative = ( aValue[0] == '-' ); // %n is used to find out how many digits contains the fraction part, e.g. 0.001 contains 3 // digits. int ret = sscanf( aValue.c_str(), "%d.%n%d%n", &integer, &pre_fraction, &fraction, &post_fraction ); if( ret == 0 ) throw XML_PARSER_ERROR( "Invalid coordinate" ); // process the integer part value = ConvertToNm( integer, aUnit ); // process the fraction part if( ret == 2 ) { int digits = post_fraction - pre_fraction; // adjust the number of digits if necessary as we cannot handle anything smaller than // nanometers (rounding). if( (unsigned) digits > DIVIDERS_MAX_IDX ) { int diff = digits - DIVIDERS_MAX_IDX; digits = DIVIDERS_MAX_IDX; fraction /= DIVIDERS[diff]; } int frac_value = ConvertToNm( fraction, aUnit ) / DIVIDERS[digits]; // keep the sign in mind value = negative ? value - frac_value : value + frac_value; } } long long int ECOORD::ConvertToNm( int aValue, enum EAGLE_UNIT aUnit ) { long long int ret; switch( aUnit ) { default: case EU_NM: ret = aValue; break; case EU_MM: ret = (long long) aValue * 1000000; break; case EU_INCH: ret = (long long) aValue * 25400000; break; case EU_MIL: ret = (long long) aValue * 25400; break; } if( ( ret > 0 ) != ( aValue > 0 ) ) wxLogError( _( "Invalid size %lld: too large" ), aValue ); return ret; } // Template specializations below parse wxString to the used types: // - wxString (preferred) // - string // - double // - int // - bool // - EROT // - ECOORD template <> wxString Convert( const wxString& aValue ) { return aValue; } template <> std::string Convert( const wxString& aValue ) { return std::string( aValue.ToUTF8() ); } template <> double Convert( const wxString& aValue ) { double value; if( aValue.ToCDouble( &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' nullptr ); return value; } template<> ECOORD Convert( const wxString& aCoord ) { // Eagle uses millimeters as the default unit return ECOORD( aCoord, ECOORD::EAGLE_UNIT::EU_MM ); } /** * Parse \a aAttribute of the XML node \a 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, const wxString& aAttribute ) { wxString value; if( aNode->GetAttribute( aAttribute, &value ) ) return Convert( value ); else throw XML_PARSER_ERROR( "The required attribute " + aAttribute + " is missing." ); } /** * Parse option \a aAttribute of the XML node \a 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, const wxString& aAttribute ) { return OPTIONAL_XML_ATTRIBUTE( aNode->GetAttribute( aAttribute ) ); } NODE_MAP MapChildren( wxXmlNode* aCurrentNode ) { // Map node_name -> node_pointer NODE_MAP nodesMap; // Loop through all children mapping them in nodesMap if( aCurrentNode ) aCurrentNode = aCurrentNode->GetChildren(); while( aCurrentNode ) { // Create a new pair in the map // key: current node name // value: current node pointer nodesMap[aCurrentNode->GetName()] = aCurrentNode; // Get next child aCurrentNode = aCurrentNode->GetNext(); } return nodesMap; } VECTOR2I ConvertArcCenter( const VECTOR2I& aStart, const VECTOR2I& 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; VECTOR2I mid = ( aStart + aEnd ) / 2; double dlen = sqrt( dx*dx + dy*dy ); if( !std::isnormal( dlen ) || !std::isnormal( aAngle ) ) { THROW_IO_ERROR( wxString::Format( _( "Invalid Arc with radius %f and angle %f" ), dlen, aAngle ) ); } double dist = dlen / ( 2 * tan( DEG2RAD( aAngle ) / 2 ) ); VECTOR2I center( mid.x + dist * ( dy / dlen ), mid.y - dist * ( dx / dlen ) ); return center; } static int parseAlignment( const wxString& aAlignment ) { // (bottom-left | bottom-center | bottom-right | center-left | // center | center-right | top-left | top-center | top-right) if( aAlignment == "center" ) return ETEXT::CENTER; else if( aAlignment == "center-right" ) return ETEXT::CENTER_RIGHT; else if( aAlignment == "top-left" ) return ETEXT::TOP_LEFT; else if( aAlignment == "top-center" ) return ETEXT::TOP_CENTER; else if( aAlignment == "top-right" ) return ETEXT::TOP_RIGHT; else if( aAlignment == "bottom-left" ) return ETEXT::BOTTOM_LEFT; else if( aAlignment == "bottom-center" ) return ETEXT::BOTTOM_CENTER; else if( aAlignment == "bottom-right" ) return ETEXT::BOTTOM_RIGHT; else if( aAlignment == "center-left" ) return ETEXT::CENTER_LEFT; return DEFAULT_ALIGNMENT; } 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_wxString 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; } EJUNCTION::EJUNCTION( wxXmlNode* aJunction ) { /* */ x = parseRequiredAttribute( aJunction, "x" ); y = parseRequiredAttribute( aJunction, "y" ); } ELABEL::ELABEL( wxXmlNode* aLabel, const wxString& aNetName ) { /* */ x = parseRequiredAttribute( aLabel, "x" ); y = parseRequiredAttribute( aLabel, "y" ); size = parseRequiredAttribute( aLabel, "size" ); layer = parseRequiredAttribute( aLabel, "layer" ); rot = parseOptionalAttribute( aLabel, "rot" ); xref = parseOptionalAttribute( aLabel, "xref" ); netname = aNetName; } EVIA::EVIA( wxXmlNode* aVia ) { /* */ x = parseRequiredAttribute( aVia, "x" ); y = parseRequiredAttribute( aVia, "y" ); wxString 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" ); layer = parseOptionalAttribute( aTree, "layer" ); ratio = parseOptionalAttribute( aTree, "ratio" ); rot = parseOptionalAttribute( aTree, "rot" ); opt_wxString stemp = parseOptionalAttribute( aTree, "display" ); // (off | value | name | both) if( stemp == "off" ) display = EATTR::Off; else if( stemp == "name" ) display = EATTR::NAME; else if( stemp == "both" ) display = EATTR::BOTH; else // "value" is the default display = EATTR::VALUE; stemp = parseOptionalAttribute( aTree, "align" ); align = stemp ? parseAlignment( *stemp ) : DEFAULT_ALIGNMENT; } 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" ); dimensionType = parseOptionalAttribute( aDimension, "dtype" ); } 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_wxString stemp = parseOptionalAttribute( aText, "align" ); align = stemp ? parseAlignment( *stemp ) : DEFAULT_ALIGNMENT; } wxSize ETEXT::ConvertSize() const { wxSize textsize; if( font ) { const wxString& fontName = font.CGet(); if( fontName == "vector" ) { textsize = wxSize( size.ToSchUnits(), size.ToSchUnits() ); } else if( fontName == "fixed" ) { textsize = wxSize( size.ToSchUnits(), size.ToSchUnits() * 0.80 ); } else { textsize = wxSize( size.ToSchUnits(), size.ToSchUnits() ); } } else { textsize = wxSize( size.ToSchUnits() * 0.85, size.ToSchUnits() ); } return textsize; } EFRAME::EFRAME( wxXmlNode* aFrameNode ) { /* * * */ border_left = true; border_top = true; border_right = true; border_bottom = true; x1 = parseRequiredAttribute( aFrameNode, "x1" ); y1 = parseRequiredAttribute( aFrameNode, "y1" ); x2 = parseRequiredAttribute( aFrameNode, "x2" ); y2 = parseRequiredAttribute( aFrameNode, "y2" ); columns = parseRequiredAttribute( aFrameNode, "columns" ); rows = parseRequiredAttribute( aFrameNode, "rows" ); layer = parseRequiredAttribute( aFrameNode, "layer" ); border_left = parseOptionalAttribute( aFrameNode, "border-left" ); border_top = parseOptionalAttribute( aFrameNode, "border-top" ); border_right = parseOptionalAttribute( aFrameNode, "border-right" ); border_bottom = parseOptionalAttribute( aFrameNode, "border-bottom" ); } EPAD_COMMON::EPAD_COMMON( wxXmlNode* aPad ) { // #REQUIRED says DTD, throw exception if not found name = parseRequiredAttribute( aPad, "name" ); x = parseRequiredAttribute( aPad, "x" ); y = parseRequiredAttribute( aPad, "y" ); rot = parseOptionalAttribute( aPad, "rot" ); stop = parseOptionalAttribute( aPad, "stop" ); thermals = parseOptionalAttribute( aPad, "thermals" ); } EPAD::EPAD( wxXmlNode* aPad ) : EPAD_COMMON( aPad ) { /* */ // #REQUIRED says DTD, throw exception if not found drill = parseRequiredAttribute( aPad, "drill" ); // Optional attributes diameter = parseOptionalAttribute( aPad, "diameter" ); opt_wxString 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; first = parseOptionalAttribute( aPad, "first" ); } ESMD::ESMD( wxXmlNode* aSMD ) : EPAD_COMMON( aSMD ) { /* */ // DTD #REQUIRED, throw exception if not found dx = parseRequiredAttribute( aSMD, "dx" ); dy = parseRequiredAttribute( aSMD, "dy" ); layer = parseRequiredAttribute( aSMD, "layer" ); roundness = parseOptionalAttribute( aSMD, "roundness" ); cream = parseOptionalAttribute( aSMD, "cream" ); } EPIN::EPIN( wxXmlNode* aPin ) { /* */ // DTD #REQUIRED, throw exception if not found name = parseRequiredAttribute( aPin, "name" ); x = parseRequiredAttribute( aPin, "x" ); y = parseRequiredAttribute( aPin, "y" ); visible = parseOptionalAttribute( aPin, "visible" ); length = parseOptionalAttribute( aPin, "length" ); direction = parseOptionalAttribute( aPin, "direction" ); function = parseOptionalAttribute( aPin, "function" ); swaplevel = parseOptionalAttribute( aPin, "swaplevel" ); rot = parseOptionalAttribute( aPin, "rot" ); } EVERTEX::EVERTEX( wxXmlNode* aVertex ) { /* */ x = parseRequiredAttribute( aVertex, "x" ); y = parseRequiredAttribute( aVertex, "y" ); curve = parseOptionalAttribute( aVertex, "curve" ); } 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_wxString 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" ); std::string p = parseRequiredAttribute( aElement, "package" ); ReplaceIllegalFileNameChars( &p, '_' ); package = wxString::FromUTF8( p.c_str() ); 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" ); } EPART::EPART( wxXmlNode* aPart ) { /* * * */ // #REQUIRED name = parseRequiredAttribute( aPart, "name" ); library = parseRequiredAttribute( aPart, "library" ); deviceset = parseRequiredAttribute( aPart, "deviceset" ); device = parseRequiredAttribute( aPart, "device" ); technology = parseOptionalAttribute( aPart, "technology" ); value = parseOptionalAttribute( aPart, "value" ); for( wxXmlNode* child = aPart->GetChildren(); child; child = child->GetNext() ) { if( child->GetName() == "attribute" ) { std::string aname, avalue; for( wxXmlAttribute* x = child->GetAttributes(); x; x = x->GetNext() ) { if( x->GetName() == "name" ) aname = x->GetValue(); else if( x->GetName() == "value" ) avalue = x->GetValue(); } if( aname.size() && avalue.size() ) attribute[aname] = avalue; } else if( child->GetName() == "variant" ) { std::string aname, avalue; for( wxXmlAttribute* x = child->GetAttributes(); x; x = x->GetNext() ) { if( x->GetName() == "name" ) aname = x->GetValue(); else if( x->GetName() == "value" ) avalue = x->GetValue(); } if( aname.size() && avalue.size() ) variant[aname] = avalue; } } } EINSTANCE::EINSTANCE( wxXmlNode* aInstance ) { /* * * */ part = parseRequiredAttribute( aInstance, "part" ); gate = parseRequiredAttribute( aInstance, "gate" ); x = parseRequiredAttribute( aInstance, "x" ); y = parseRequiredAttribute( aInstance, "y" ); // optional smashed = parseOptionalAttribute( aInstance, "smashed" ); rot = parseOptionalAttribute( aInstance, "rot" ); } EGATE::EGATE( wxXmlNode* aGate ) { /* * * */ name = parseRequiredAttribute( aGate, "name" ); symbol = parseRequiredAttribute( aGate, "symbol" ); x = parseRequiredAttribute( aGate, "x" ); y = parseRequiredAttribute( aGate, "y" ); opt_wxString stemp = parseOptionalAttribute( aGate, "addlevel" ); // (off | value | name | both) if( stemp == "must" ) addlevel = EGATE::MUST; else if( stemp == "can" ) addlevel = EGATE::CAN; else if( stemp == "next" ) addlevel = EGATE::NEXT; else if( stemp == "request" ) addlevel = EGATE::REQUEST; else if( stemp == "always" ) addlevel = EGATE::ALWAYS; else addlevel = EGATE::NEXT; } ECONNECT::ECONNECT( wxXmlNode* aConnect ) { /* * * */ gate = parseRequiredAttribute( aConnect, "gate" ); pin = parseRequiredAttribute( aConnect, "pin" ); pad = parseRequiredAttribute( aConnect, "pad" ); } EDEVICE::EDEVICE( wxXmlNode* aDevice ) { /* */ name = parseRequiredAttribute( aDevice, "name" ); opt_wxString pack = parseOptionalAttribute( aDevice, "package" ); if( pack ) { std::string p( pack->c_str() ); ReplaceIllegalFileNameChars( &p, '_' ); package.Set( wxString::FromUTF8( p.c_str() ) ); } NODE_MAP aDeviceChildren = MapChildren( aDevice ); wxXmlNode* connectNode = getChildrenNodes( aDeviceChildren, "connects" ); while( connectNode ) { connects.emplace_back( connectNode ); connectNode = connectNode->GetNext(); } } EDEVICE_SET::EDEVICE_SET( wxXmlNode* aDeviceSet ) { /* */ name = parseRequiredAttribute( aDeviceSet, "name" ); prefix = parseOptionalAttribute( aDeviceSet, "prefix" ); uservalue = parseOptionalAttribute( aDeviceSet, "uservalue" ); } ECLASS::ECLASS( wxXmlNode* aClass ) { number = parseRequiredAttribute( aClass, "number" ); name = parseRequiredAttribute( aClass, "name" ); for( wxXmlNode* child = aClass->GetChildren(); child; child = child->GetNext() ) { if( child->GetName() == "clearance" ) { wxString to = parseRequiredAttribute( child, "class" ); ECOORD value = parseRequiredAttribute( child, "value" ); clearanceMap[to] = value; } } }