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.

278 lines
12 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 2018 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. /**
  25. * a class to handle special data (items attributes) during plot.
  26. * used in Gerber plotter to generate auxiliary data during plot
  27. * (for instance info associated to apertures and flashed pads)
  28. *
  29. * @file gbr_metadata.h
  30. */
  31. #ifndef GBR_METADATA_H
  32. #define GBR_METADATA_H
  33. #include <gbr_netlist_metadata.h>
  34. /** creates the TF.CreationDate attribute:
  35. * The attribute value must conform to the full version of the ISO 8601
  36. * date and time format, including time and time zone. Note that this is
  37. * the date the Gerber file is effectively created,
  38. * not the time the project of PCB was started
  39. * @param aFormat = string compatibility: X1, X2, GBRJOB or NC drill synthax.
  40. * exemple of structured comment (compatible X1 gerber)
  41. * G04 #@! TF.CreationDate,2018-11-21T08:49:16+01:00* (exemple of X1 attribute)
  42. * exemple NC drill files
  43. * ; #@! TF.CreationDate,2018-11-21T08:49:16+01:00* (exemple of NC drill comment)
  44. * exemple of X2 attribute:
  45. * %TF.CreationDate,2018-11-06T08:25:24+01:00*%
  46. */
  47. enum GBR_NC_STRING_FORMAT // Options for string format in some attribute strings
  48. {
  49. GBR_NC_STRING_FORMAT_X1,
  50. GBR_NC_STRING_FORMAT_X2,
  51. GBR_NC_STRING_FORMAT_GBRJOB,
  52. GBR_NC_STRING_FORMAT_NCDRILL
  53. };
  54. wxString GbrMakeCreationDateAttributeString( GBR_NC_STRING_FORMAT aFormat );
  55. /** A helper function to build a project GUID using format RFC4122 Version 1 or 4
  56. * from the project name, because a kicad project has no specific GUID
  57. * RFC4122 is used mainly for its syntax, because fields have no meaning for Gerber files
  58. * and therefore the GUID generated has no meaning because it do not use any time and time stamp
  59. * specific to the project, just a random pattern (random is here a pattern specific to a project).
  60. *
  61. * See en.wikipedia.org/wiki/Universally_unique_identifier
  62. */
  63. wxString GbrMakeProjectGUIDfromString( wxString& aText );
  64. // this class handle info which can be added in a gerber file as attribute
  65. // of an aperture, by the %TA.AperFunction command
  66. // This attribute is added when creating a new aperture (command %ADDxx)
  67. // Only one aperture attribute can be added to a given aperture
  68. //
  69. class GBR_APERTURE_METADATA
  70. {
  71. public:
  72. enum GBR_APERTURE_ATTRIB
  73. {
  74. GBR_APERTURE_ATTRIB_NONE, ///< uninitialized attribute
  75. GBR_APERTURE_ATTRIB_ETCHEDCMP, ///< aperture used for etched components
  76. GBR_APERTURE_ATTRIB_CONDUCTOR, ///< aperture used for connected items like tracks (not vias)
  77. GBR_APERTURE_ATTRIB_EDGECUT, ///< aperture used for board cutout
  78. GBR_APERTURE_ATTRIB_NONCONDUCTOR, ///< aperture used for not connected items (texts, outlines on copper)
  79. GBR_APERTURE_ATTRIB_VIAPAD, ///< aperture used for vias
  80. GBR_APERTURE_ATTRIB_COMPONENTPAD, ///< aperture used for through hole component on outer layer
  81. GBR_APERTURE_ATTRIB_SMDPAD_SMDEF, ///< aperture used for SMD pad. Excluded BGA pads which have their own type
  82. GBR_APERTURE_ATTRIB_SMDPAD_CUDEF, ///< aperture used for SMD pad with a solder mask defined by the solder mask
  83. GBR_APERTURE_ATTRIB_BGAPAD_SMDEF, ///< aperture used for BGA pads with a solder mask defined by the copper shape
  84. GBR_APERTURE_ATTRIB_BGAPAD_CUDEF, ///< aperture used for BGA pad with a solder mask defined by the solder mask
  85. GBR_APERTURE_ATTRIB_CONNECTORPAD, ///< aperture used for edge connector pad (outer layers)
  86. GBR_APERTURE_ATTRIB_WASHERPAD, ///< aperture used for mechanical pads (NPTH)
  87. GBR_APERTURE_ATTRIB_TESTPOINT, ///< aperture used for test point pad (outer layers)
  88. GBR_APERTURE_ATTRIB_FIDUCIAL_GLBL, ///< aperture used for fiducial pad (outer layers), at board level
  89. GBR_APERTURE_ATTRIB_FIDUCIAL_LOCAL, ///< aperture used for fiducial pad (outer layers), at footprint level
  90. GBR_APERTURE_ATTRIB_HEATSINKPAD, ///< aperture used for heat sink pad (typically for SMDs)
  91. GBR_APERTURE_ATTRIB_CASTELLATEDPAD, ///< aperture used for castellated pads in copper layer files
  92. GBR_APERTURE_ATTRIB_CASTELLATEDDRILL, ///< aperture used for castellated pads in drill files
  93. GBR_APERTURE_ATTRIB_VIADRILL, ///< aperture used for via holes in drill files
  94. GBR_APERTURE_ATTRIB_CMP_DRILL, ///< aperture used for pad holes in drill files
  95. GBR_APERTURE_ATTRIB_CMP_OBLONG_DRILL, ///< aperture used for pads oblong holes in drill files
  96. GBR_APERTURE_ATTRIB_CMP_POSITION, ///< aperture used for flashed cmp position in placement files
  97. GBR_APERTURE_ATTRIB_PAD1_POSITION, ///< aperture used for flashed pin 1 (or A1 or AA1) position in placement files
  98. GBR_APERTURE_ATTRIB_PADOTHER_POSITION, ///< aperture used for flashed pads position in placement files
  99. GBR_APERTURE_ATTRIB_CMP_BODY, ///< aperture used to draw component physical body outline
  100. ///< (without pins) in placement files
  101. GBR_APERTURE_ATTRIB_CMP_LEAD2LEAD, ///< aperture used to draw component physical body outline
  102. ///< (with pins) in placement files
  103. GBR_APERTURE_ATTRIB_CMP_FOOTPRINT, ///< aperture used to draw component footprint bounding box
  104. ///< in placement files
  105. GBR_APERTURE_ATTRIB_CMP_COURTYARD, ///< aperture used to draw component outline courtyard
  106. ///< in placement files
  107. GBR_APERTURE_ATTRIB_END ///< sentinel: max value
  108. };
  109. GBR_APERTURE_METADATA()
  110. :m_ApertAttribute( GBR_APERTURE_ATTRIB_NONE )
  111. {}
  112. /**
  113. * @return the string corresponding to the aperture attribute
  114. */
  115. static std::string GetAttributeName( GBR_APERTURE_ATTRIB aAttribute );
  116. std::string GetAttributeName()
  117. {
  118. return GetAttributeName( m_ApertAttribute );
  119. }
  120. /**
  121. * @return the full command string corresponding to the aperture attribute
  122. * like "%TA.AperFunction,<function>*%"
  123. * @param aUseX1StructuredComment = false in X2 mode,
  124. * and true in X1 mode to add the net attribut
  125. * inside a compatible X1 structured comment starting by "G04 #@! "
  126. * like "G04 #@! TA.AperFunction,<function>*"
  127. */
  128. static std::string FormatAttribute( GBR_APERTURE_ATTRIB aAttribute,
  129. bool aUseX1StructuredComment );
  130. std::string FormatAttribute( bool aUseX1StructuredComment )
  131. {
  132. return FormatAttribute( m_ApertAttribute, aUseX1StructuredComment );
  133. }
  134. // The id of the aperture attribute
  135. GBR_APERTURE_ATTRIB m_ApertAttribute;
  136. };
  137. // this class handle metadata which can be added in a gerber file as attribute
  138. // in X2 format
  139. class GBR_METADATA
  140. {
  141. public:
  142. GBR_METADATA(): m_isCopper( false) {}
  143. void SetApertureAttrib( GBR_APERTURE_METADATA::GBR_APERTURE_ATTRIB aApertAttribute )
  144. {
  145. m_ApertureMetadata.m_ApertAttribute = aApertAttribute;
  146. }
  147. GBR_APERTURE_METADATA::GBR_APERTURE_ATTRIB GetApertureAttrib()
  148. {
  149. return m_ApertureMetadata.m_ApertAttribute;
  150. }
  151. void SetNetAttribType( int aNetAttribType )
  152. {
  153. m_NetlistMetadata.m_NetAttribType = aNetAttribType;
  154. }
  155. int GetNetAttribType() const
  156. {
  157. return m_NetlistMetadata.m_NetAttribType;
  158. }
  159. void SetNetName( const wxString& aNetname ) { m_NetlistMetadata.m_Netname = aNetname; }
  160. void SetPadName( const wxString& aPadname, bool aUseUTF8 = false, bool aEscapeString = false )
  161. {
  162. m_NetlistMetadata.m_Padname.SetField( aPadname, aUseUTF8, aEscapeString );
  163. }
  164. void SetPadPinFunction( const wxString& aPadPinFunction, bool aUseUTF8, bool aEscapeString )
  165. {
  166. m_NetlistMetadata.m_PadPinFunction.SetField( aPadPinFunction, aUseUTF8, aEscapeString );
  167. }
  168. void SetCmpReference( const wxString& aComponentRef )
  169. {
  170. m_NetlistMetadata.m_Cmpref = aComponentRef;
  171. }
  172. /**
  173. * Allowed attributes are not the same on board copper layers and on other layers
  174. * Therefore a flag can be set or reset when attributes can be depending on layers
  175. */
  176. bool IsCopper() { return m_isCopper; }
  177. void SetCopper( bool aValue ) { m_isCopper = aValue; }
  178. /**
  179. * a item to handle aperture attribute:
  180. */
  181. GBR_APERTURE_METADATA m_ApertureMetadata;
  182. /**
  183. * a item to handle object attribute:
  184. */
  185. GBR_NETLIST_METADATA m_NetlistMetadata;
  186. private:
  187. /**
  188. * if the metadata is relative to a copper layer or not. This is a flag
  189. * which can be set/reset when an attribute for a given item depends on the fact
  190. * a copper layer or a non copper layer is plotted.
  191. * The initial state in false.
  192. */
  193. bool m_isCopper;
  194. };
  195. /**
  196. * This helper function "normalize" aString and convert it to a Gerber std::string
  197. * Normalisation means convert any code > 0x7F and unautorized code to a hexadecimal
  198. * 16 bits sequence unicode
  199. * unautorized codes are ',' '*' '%' '\'
  200. * @param aString = the wxString to convert
  201. * @return a std::string (ASCII7 coded) compliant with a gerber string
  202. */
  203. std::string FormatStringToGerber( const wxString& aString );
  204. /**
  205. * Similar to FormatStringToGerber.
  206. * "normalize" aString and convert it to a Gerber compatible wxString
  207. * Normalisation means unautorized code to a hexadecimal 16 bits sequence unicode
  208. * and, on request convert any code > 0x7F.
  209. * unautorized codes are ',' '*' '%' '\'
  210. * @param aString = the wxString to convert
  211. * @param aAllowUtf8Chars = false to convert non ASCII7 values to unicode sequence
  212. * @param aQuoteString = true to double quote the returned string
  213. * @return a wxString without unautorized chars (and converted non ASCII7 chars on request)
  214. */
  215. wxString ConvertNotAllowedCharsInGerber( const wxString& aString, bool aAllowUtf8Chars, bool aQuoteString );
  216. /**
  217. * This helper function make the inverse conversion of FormatStringToGerber()
  218. * It converts a "normalized" gerber string and convert it to a 16 bits sequence unicode
  219. * @param aString = the wxString compliant with a gerber string format
  220. * @return a wxString (unicode 16) from the gerber string
  221. */
  222. wxString FormatStringFromGerber( const wxString& aString );
  223. /**
  224. * Generates the string to print to a gerber file, to set a net attribute
  225. * for a graphic object.
  226. * @param aPrintedText is the string to print
  227. * @param aLastNetAttributes is the current full set of attributes.
  228. * @param aData is the GBR_NETLIST_METADATA associated to the graphic object (can be NULL
  229. * if no associated metadata, and aClearPreviousAttributes will be set to false)
  230. * @param aClearPreviousAttributes returns true if the full set of attributes
  231. * must be deleted from file before adding new attribute (happens when a previous
  232. * attribute does not exist no more).
  233. * @param aUseX1StructuredComment = false in X2 mode, and true in X1 mode to add the net attribut
  234. * in compatible X1 structured comment (i.e. prefixed by "G04 #@! ")
  235. * @return false if nothing can be done (GBR_NETLIST_METADATA has GBR_APERTURE_ATTRIB_NONE,
  236. * and true if OK
  237. * if the new attribute(s) is the same as current attribute(s), aPrintedText
  238. * will be empty
  239. */
  240. bool FormatNetAttribute( std::string& aPrintedText, std::string& aLastNetAttributes,
  241. GBR_NETLIST_METADATA* aData, bool& aClearPreviousAttributes,
  242. bool aUseX1StructuredComment );
  243. #endif // GBR_METADATA_H