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.

279 lines
11 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. double srRoundness; ///< corner rounding ratio for SMD pads (percentage)
  50. int srMinRoundness; ///< corner rounding radius, minimum size (Eagle mils, here nanometers)
  51. int srMaxRoundness; ///< corner rounding radius, maximum size (Eagle mils, here nanometers)
  52. double rvPadTop; ///< top pad size as percent of drill size
  53. // double rvPadBottom; ///< bottom pad size as percent of drill size
  54. double rlMinPadTop; ///< minimum copper annulus on through hole pads
  55. double rlMaxPadTop; ///< maximum copper annulus on through hole pads
  56. double rvViaOuter; ///< copper annulus is this percent of via hole
  57. double rlMinViaOuter; ///< minimum copper annulus on via
  58. double rlMaxViaOuter; ///< maximum copper annulus on via
  59. double mdWireWire; ///< wire to wire spacing I presume.
  60. ERULES() :
  61. psElongationLong ( 100 ),
  62. psElongationOffset ( 0 ),
  63. mvStopFrame ( 1.0 ),
  64. mvCreamFrame ( 0.0 ),
  65. mlMinStopFrame ( Mils2iu( 4.0 ) ),
  66. mlMaxStopFrame ( Mils2iu( 4.0 ) ),
  67. mlMinCreamFrame ( Mils2iu( 0.0 ) ),
  68. mlMaxCreamFrame ( Mils2iu( 0.0 ) ),
  69. srRoundness ( 0.0 ),
  70. srMinRoundness ( Mils2iu( 0.0 ) ),
  71. srMaxRoundness ( Mils2iu( 0.0 ) ),
  72. rvPadTop ( 0.25 ),
  73. // rvPadBottom ( 0.25 ),
  74. rlMinPadTop ( Mils2iu( 10 ) ),
  75. rlMaxPadTop ( Mils2iu( 20 ) ),
  76. rvViaOuter ( 0.25 ),
  77. rlMinViaOuter ( Mils2iu( 10 ) ),
  78. rlMaxViaOuter ( Mils2iu( 20 ) ),
  79. mdWireWire ( 0 )
  80. {}
  81. void parse( wxXmlNode* aRules );
  82. };
  83. /**
  84. * Class EAGLE_PLUGIN
  85. * works with Eagle 6.x XML board files and footprints to implement the
  86. * Pcbnew PLUGIN API, or a portion of it.
  87. */
  88. class EAGLE_PLUGIN : public PLUGIN
  89. {
  90. public:
  91. //-----<PUBLIC PLUGIN API>--------------------------------------------------
  92. const wxString PluginName() const override;
  93. BOARD* Load( const wxString& aFileName, BOARD* aAppendToMe,
  94. const PROPERTIES* aProperties = NULL ) override;
  95. const wxString GetFileExtension() const override;
  96. void FootprintEnumerate( wxArrayString& aFootprintNames, const wxString& aLibraryPath,
  97. const PROPERTIES* aProperties = NULL) override;
  98. MODULE* FootprintLoad( const wxString& aLibraryPath, const wxString& aFootprintName,
  99. const PROPERTIES* aProperties = NULL ) override;
  100. long long GetLibraryTimestamp( const wxString& aLibraryPath ) const override
  101. {
  102. return getModificationTime( aLibraryPath ).GetValue().GetValue();
  103. }
  104. bool IsFootprintLibWritable( const wxString& aLibraryPath ) override
  105. {
  106. return false; // until someone writes others like FootprintSave(), etc.
  107. }
  108. void FootprintLibOptions( PROPERTIES* aProperties ) const override;
  109. /*
  110. void Save( const wxString& aFileName, BOARD* aBoard, const PROPERTIES* aProperties = NULL );
  111. void FootprintSave( const wxString& aLibraryPath, const MODULE* aFootprint, const PROPERTIES* aProperties = NULL );
  112. void FootprintDelete( const wxString& aLibraryPath, const wxString& aFootprintName, const PROPERTIES* aProperties = NULL );
  113. void FootprintLibCreate( const wxString& aLibraryPath, const PROPERTIES* aProperties = NULL );
  114. bool FootprintLibDelete( const wxString& aLibraryPath, const PROPERTIES* aProperties = NULL );
  115. */
  116. //-----</PUBLIC PLUGIN API>-------------------------------------------------
  117. typedef int BIU;
  118. EAGLE_PLUGIN();
  119. ~EAGLE_PLUGIN();
  120. private:
  121. typedef std::vector<ELAYER> ELAYERS;
  122. typedef ELAYERS::const_iterator EITER;
  123. int m_cu_map[17]; ///< map eagle to kicad, cu layers only.
  124. std::map<int, ELAYER> m_eagleLayers; ///< Eagle layers data stored by the layer number
  125. ERULES* m_rules; ///< Eagle design rules.
  126. XPATH* m_xpath; ///< keeps track of what we are working on within
  127. ///< XML document during a Load().
  128. int m_hole_count; ///< generates unique module names from eagle "hole"s.
  129. NET_MAP m_pads_to_nets; ///< net list
  130. MODULE_MAP m_templates; ///< is part of a MODULE factory that operates
  131. ///< using copy construction.
  132. ///< lookup key is either libname.packagename or simply
  133. ///< packagename if FootprintLoad() or FootprintEnumberate()
  134. const PROPERTIES* m_props; ///< passed via Save() or Load(), no ownership, may be NULL.
  135. BOARD* m_board; ///< which BOARD is being worked on, no ownership here
  136. int m_min_trace; ///< smallest trace we find on Load(), in BIU.
  137. int m_min_via; ///< smallest via we find on Load(), in BIU.
  138. int m_min_via_hole; ///< smallest via diameter hole we find on Load(), in BIU.
  139. wxString m_lib_path;
  140. wxDateTime m_mod_time;
  141. /// initialize PLUGIN like a constructor would, and futz with fresh BOARD if needed.
  142. void init( const PROPERTIES* aProperties );
  143. void clear_cu_map();
  144. /// Convert an Eagle distance to a KiCad distance.
  145. int kicad_y( const ECOORD& y ) const { return -y.ToPcbUnits(); }
  146. int kicad_x( const ECOORD& x ) const { return x.ToPcbUnits(); }
  147. /// create a font size (fontz) from an eagle font size scalar
  148. wxSize kicad_fontz( const ECOORD& d ) const;
  149. /// Convert an Eagle layer to a KiCad layer.
  150. PCB_LAYER_ID kicad_layer( int aLayer ) const;
  151. /// Get Eagle layer name by its number
  152. const wxString& eagle_layer_name( int aLayer ) const;
  153. /// This PLUGIN only caches one footprint library, this determines which one.
  154. void cacheLib( const wxString& aLibraryPath );
  155. /// get a file's or dir's modification time.
  156. static wxDateTime getModificationTime( const wxString& aPath );
  157. // all these loadXXX() throw IO_ERROR or ptree_error exceptions:
  158. void loadAllSections( wxXmlNode* aDocument );
  159. void loadDesignRules( wxXmlNode* aDesignRules );
  160. void loadLayerDefs( wxXmlNode* aLayers );
  161. void loadPlain( wxXmlNode* aPlain );
  162. void loadSignals( wxXmlNode* aSignals );
  163. /**
  164. * Function loadLibrary
  165. * loads the Eagle "library" XML element, which can occur either under
  166. * a "libraries" element (if a *.brd file) or under a "drawing" element if a
  167. * *.lbr file.
  168. * @param aLib is the portion of the loaded XML document tree that is the "library"
  169. * element.
  170. * @param aLibName is a pointer to the library name or NULL. If NULL this means
  171. * we are loading a *.lbr not a *.brd file and the key used in m_templates is to exclude
  172. * the library name.
  173. */
  174. void loadLibrary( wxXmlNode* aLib, const wxString* aLibName );
  175. void loadLibraries( wxXmlNode* aLibs );
  176. void loadElements( wxXmlNode* aElements );
  177. /** Loads a copper or keepout polygon and adds it to the board.
  178. *
  179. * @return The loaded zone or nullptr if was not processed.
  180. */
  181. ZONE_CONTAINER* loadPolygon( wxXmlNode* aPolyNode );
  182. void orientModuleAndText( MODULE* m, const EELEMENT& e, const EATTR* nameAttr, const EATTR* valueAttr );
  183. void orientModuleText( MODULE* m, const EELEMENT& e, TEXTE_MODULE* txt, const EATTR* a );
  184. /// move the BOARD into the center of the page
  185. void centerBoard();
  186. /**
  187. * Function fmtDEG
  188. * formats an angle in a way particular to a board file format. This function
  189. * is the opposite or complement of degParse(). One has to know what the
  190. * other is doing.
  191. */
  192. wxString fmtDEG( double aAngle ) const;
  193. /**
  194. * Function makeModule
  195. * creates a MODULE from an Eagle package.
  196. */
  197. MODULE* makeModule( wxXmlNode* aPackage, const wxString& aPkgName ) const;
  198. void packageWire( MODULE* aModule, wxXmlNode* aTree ) const;
  199. void packagePad( MODULE* aModule, wxXmlNode* aTree ) const;
  200. void packageText( MODULE* aModule, wxXmlNode* aTree ) const;
  201. void packageRectangle( MODULE* aModule, wxXmlNode* aTree ) const;
  202. void packagePolygon( MODULE* aModule, wxXmlNode* aTree ) const;
  203. void packageCircle( MODULE* aModule, wxXmlNode* aTree ) const;
  204. void packageHole( MODULE* aModule, wxXmlNode* aTree ) const;
  205. void packageSMD( MODULE* aModule, wxXmlNode* aTree ) const;
  206. ///> Handles common pad properties
  207. void transferPad( const EPAD_COMMON& aEaglePad, D_PAD* aPad ) const;
  208. ///> Deletes the footprint templates list
  209. void deleteTemplates();
  210. };
  211. #endif // EAGLE_PLUGIN_H_