You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

295 lines
12 KiB

  1. #ifndef EAGLE_PLUGIN_H_
  2. #define EAGLE_PLUGIN_H_
  3. /*
  4. * This program source code file is part of KiCad, a free EDA CAD application.
  5. *
  6. * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  7. * Copyright (C) 2012-2017 KiCad Developers, see AUTHORS.txt for contributors.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, you may find one here:
  21. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  22. * or you may search the http://www.gnu.org website for the version 2 license,
  23. * or you may write to the Free Software Foundation, Inc.,
  24. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  25. */
  26. #include <io_mgr.h>
  27. #include <layers_id_colors_and_visibility.h>
  28. #include <eagle_parser.h>
  29. #include <map>
  30. #include <wx/xml/xml.h>
  31. class D_PAD;
  32. class TEXTE_MODULE;
  33. typedef std::map<wxString, MODULE*> MODULE_MAP;
  34. typedef std::vector<ZONE_CONTAINER*> ZONES;
  35. typedef std::map<wxString, ENET> NET_MAP;
  36. typedef NET_MAP::const_iterator NET_MAP_CITER;
  37. /// subset of eagle.drawing.board.designrules in the XML document
  38. struct ERULES
  39. {
  40. int psElongationLong; ///< percent over 100%. 0-> not elongated, 100->twice as wide as is tall
  41. ///< Goes into making a scaling factor for "long" pads.
  42. int psElongationOffset; ///< the offset of the hole within the "long" pad.
  43. double mvStopFrame; ///< solder mask, expressed as percentage of the smaller pad/via dimension
  44. double mvCreamFrame; ///< solderpaste mask, expressed as percentage of the smaller pad/via dimension
  45. int mlMinStopFrame; ///< solder mask, minimum size (Eagle mils, here nanometers)
  46. int mlMaxStopFrame; ///< solder mask, maximum size (Eagle mils, here nanometers)
  47. int mlMinCreamFrame; ///< solder paste mask, minimum size (Eagle mils, here nanometers)
  48. int mlMaxCreamFrame; ///< solder paste mask, maximum size (Eagle mils, here nanometers)
  49. int psTop; ///< Shape of the top pads
  50. int psBottom; ///< Shape of the bottom pads
  51. int psFirst; ///< Shape of the first pads
  52. double srRoundness; ///< corner rounding ratio for SMD pads (percentage)
  53. int srMinRoundness; ///< corner rounding radius, minimum size (Eagle mils, here nanometers)
  54. int srMaxRoundness; ///< corner rounding radius, maximum size (Eagle mils, here nanometers)
  55. double rvPadTop; ///< top pad size as percent of drill size
  56. // double rvPadBottom; ///< bottom pad size as percent of drill size
  57. double rlMinPadTop; ///< minimum copper annulus on through hole pads
  58. double rlMaxPadTop; ///< maximum copper annulus on through hole pads
  59. double rvViaOuter; ///< copper annulus is this percent of via hole
  60. double rlMinViaOuter; ///< minimum copper annulus on via
  61. double rlMaxViaOuter; ///< maximum copper annulus on via
  62. double mdWireWire; ///< wire to wire spacing I presume.
  63. ERULES() :
  64. psElongationLong ( 100 ),
  65. psElongationOffset ( 0 ),
  66. mvStopFrame ( 1.0 ),
  67. mvCreamFrame ( 0.0 ),
  68. mlMinStopFrame ( Mils2iu( 4.0 ) ),
  69. mlMaxStopFrame ( Mils2iu( 4.0 ) ),
  70. mlMinCreamFrame ( Mils2iu( 0.0 ) ),
  71. mlMaxCreamFrame ( Mils2iu( 0.0 ) ),
  72. psTop ( EPAD::UNDEF ),
  73. psBottom ( EPAD::UNDEF ),
  74. psFirst ( EPAD::UNDEF ),
  75. srRoundness ( 0.0 ),
  76. srMinRoundness ( Mils2iu( 0.0 ) ),
  77. srMaxRoundness ( Mils2iu( 0.0 ) ),
  78. rvPadTop ( 0.25 ),
  79. // rvPadBottom ( 0.25 ),
  80. rlMinPadTop ( Mils2iu( 10 ) ),
  81. rlMaxPadTop ( Mils2iu( 20 ) ),
  82. rvViaOuter ( 0.25 ),
  83. rlMinViaOuter ( Mils2iu( 10 ) ),
  84. rlMaxViaOuter ( Mils2iu( 20 ) ),
  85. mdWireWire ( 0 )
  86. {}
  87. void parse( wxXmlNode* aRules );
  88. };
  89. /**
  90. * Class EAGLE_PLUGIN
  91. * works with Eagle 6.x XML board files and footprints to implement the
  92. * Pcbnew PLUGIN API, or a portion of it.
  93. */
  94. class EAGLE_PLUGIN : public PLUGIN
  95. {
  96. public:
  97. //-----<PUBLIC PLUGIN API>--------------------------------------------------
  98. const wxString PluginName() const override;
  99. BOARD* Load( const wxString& aFileName, BOARD* aAppendToMe,
  100. const PROPERTIES* aProperties = NULL ) override;
  101. const wxString GetFileExtension() const override;
  102. void FootprintEnumerate( wxArrayString& aFootprintNames, const wxString& aLibraryPath,
  103. const PROPERTIES* aProperties = NULL) override;
  104. MODULE* FootprintLoad( const wxString& aLibraryPath, const wxString& aFootprintName,
  105. const PROPERTIES* aProperties = NULL ) override;
  106. long long GetLibraryTimestamp( const wxString& aLibraryPath ) const override
  107. {
  108. return getModificationTime( aLibraryPath ).GetValue().GetValue();
  109. }
  110. bool IsFootprintLibWritable( const wxString& aLibraryPath ) override
  111. {
  112. return false; // until someone writes others like FootprintSave(), etc.
  113. }
  114. void FootprintLibOptions( PROPERTIES* aProperties ) const override;
  115. /*
  116. void Save( const wxString& aFileName, BOARD* aBoard, const PROPERTIES* aProperties = NULL );
  117. void FootprintSave( const wxString& aLibraryPath, const MODULE* aFootprint, const PROPERTIES* aProperties = NULL );
  118. void FootprintDelete( const wxString& aLibraryPath, const wxString& aFootprintName, const PROPERTIES* aProperties = NULL );
  119. void FootprintLibCreate( const wxString& aLibraryPath, const PROPERTIES* aProperties = NULL );
  120. bool FootprintLibDelete( const wxString& aLibraryPath, const PROPERTIES* aProperties = NULL );
  121. */
  122. //-----</PUBLIC PLUGIN API>-------------------------------------------------
  123. typedef int BIU;
  124. EAGLE_PLUGIN();
  125. ~EAGLE_PLUGIN();
  126. private:
  127. typedef std::vector<ELAYER> ELAYERS;
  128. typedef ELAYERS::const_iterator EITER;
  129. int m_cu_map[17]; ///< map eagle to kicad, cu layers only.
  130. std::map<int, ELAYER> m_eagleLayers; ///< Eagle layers data stored by the layer number
  131. ERULES* m_rules; ///< Eagle design rules.
  132. XPATH* m_xpath; ///< keeps track of what we are working on within
  133. ///< XML document during a Load().
  134. int m_hole_count; ///< generates unique module names from eagle "hole"s.
  135. NET_MAP m_pads_to_nets; ///< net list
  136. MODULE_MAP m_templates; ///< is part of a MODULE factory that operates
  137. ///< using copy construction.
  138. ///< lookup key is either libname.packagename or simply
  139. ///< packagename if FootprintLoad() or FootprintEnumberate()
  140. const PROPERTIES* m_props; ///< passed via Save() or Load(), no ownership, may be NULL.
  141. BOARD* m_board; ///< which BOARD is being worked on, no ownership here
  142. int m_min_trace; ///< smallest trace we find on Load(), in BIU.
  143. int m_min_via; ///< smallest via we find on Load(), in BIU.
  144. int m_min_via_hole; ///< smallest via diameter hole we find on Load(), in BIU.
  145. wxString m_lib_path;
  146. wxDateTime m_mod_time;
  147. /// initialize PLUGIN like a constructor would, and futz with fresh BOARD if needed.
  148. void init( const PROPERTIES* aProperties );
  149. void clear_cu_map();
  150. /// Convert an Eagle distance to a KiCad distance.
  151. int kicad_y( const ECOORD& y ) const { return -y.ToPcbUnits(); }
  152. int kicad_x( const ECOORD& x ) const { return x.ToPcbUnits(); }
  153. /// create a font size (fontz) from an eagle font size scalar
  154. wxSize kicad_fontz( const ECOORD& d ) const;
  155. /// Convert an Eagle layer to a KiCad layer.
  156. PCB_LAYER_ID kicad_layer( int aLayer ) const;
  157. /// Get Eagle layer name by its number
  158. const wxString& eagle_layer_name( int aLayer ) const;
  159. /// This PLUGIN only caches one footprint library, this determines which one.
  160. void cacheLib( const wxString& aLibraryPath );
  161. /// get a file's or dir's modification time.
  162. static wxDateTime getModificationTime( const wxString& aPath );
  163. // all these loadXXX() throw IO_ERROR or ptree_error exceptions:
  164. void loadAllSections( wxXmlNode* aDocument );
  165. void loadDesignRules( wxXmlNode* aDesignRules );
  166. void loadLayerDefs( wxXmlNode* aLayers );
  167. void loadPlain( wxXmlNode* aPlain );
  168. void loadSignals( wxXmlNode* aSignals );
  169. /**
  170. * Function loadLibrary
  171. * loads the Eagle "library" XML element, which can occur either under
  172. * a "libraries" element (if a *.brd file) or under a "drawing" element if a
  173. * *.lbr file.
  174. * @param aLib is the portion of the loaded XML document tree that is the "library"
  175. * element.
  176. * @param aLibName is a pointer to the library name or NULL. If NULL this means
  177. * we are loading a *.lbr not a *.brd file and the key used in m_templates is to exclude
  178. * the library name.
  179. */
  180. void loadLibrary( wxXmlNode* aLib, const wxString* aLibName );
  181. void loadLibraries( wxXmlNode* aLibs );
  182. void loadElements( wxXmlNode* aElements );
  183. /** Loads a copper or keepout polygon and adds it to the board.
  184. *
  185. * @return The loaded zone or nullptr if was not processed.
  186. */
  187. ZONE_CONTAINER* loadPolygon( wxXmlNode* aPolyNode );
  188. void orientModuleAndText( MODULE* m, const EELEMENT& e, const EATTR* nameAttr, const EATTR* valueAttr );
  189. void orientModuleText( MODULE* m, const EELEMENT& e, TEXTE_MODULE* txt, const EATTR* a );
  190. /// move the BOARD into the center of the page
  191. void centerBoard();
  192. /**
  193. * Function fmtDEG
  194. * formats an angle in a way particular to a board file format. This function
  195. * is the opposite or complement of degParse(). One has to know what the
  196. * other is doing.
  197. */
  198. wxString fmtDEG( double aAngle ) const;
  199. /**
  200. * Function makeModule
  201. * creates a MODULE from an Eagle package.
  202. */
  203. MODULE* makeModule( wxXmlNode* aPackage, const wxString& aPkgName ) const;
  204. void packageWire( MODULE* aModule, wxXmlNode* aTree ) const;
  205. void packagePad( MODULE* aModule, wxXmlNode* aTree ) const;
  206. void packageText( MODULE* aModule, wxXmlNode* aTree ) const;
  207. void packageRectangle( MODULE* aModule, wxXmlNode* aTree ) const;
  208. void packagePolygon( MODULE* aModule, wxXmlNode* aTree ) const;
  209. void packageCircle( MODULE* aModule, wxXmlNode* aTree ) const;
  210. /**
  211. * Function packageHole
  212. * @parameter aModule - The KiCad module to which to assign the hole
  213. * @parameter aTree - The Eagle XML node that is of type "hole"
  214. * @parameter aCenter - If true, center the hole in the module and
  215. * offset the module position
  216. */
  217. void packageHole( MODULE* aModule, wxXmlNode* aTree, bool aCenter ) const;
  218. void packageSMD( MODULE* aModule, wxXmlNode* aTree ) const;
  219. ///> Handles common pad properties
  220. void transferPad( const EPAD_COMMON& aEaglePad, D_PAD* aPad ) const;
  221. ///> Deletes the footprint templates list
  222. void deleteTemplates();
  223. };
  224. #endif // EAGLE_PLUGIN_H_