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.

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