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.

328 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-2020 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. * Handle special data (items attributes) during plot.
  26. *
  27. * Used in Gerber plotter to generate auxiliary data during plot (for instance info associated
  28. * to apertures and flashed pads)
  29. *
  30. * @file gbr_metadata.h
  31. */
  32. #ifndef GBR_METADATA_H
  33. #define GBR_METADATA_H
  34. #include <gbr_netlist_metadata.h>
  35. /**
  36. * Create a gerber TF.CreationDate attribute.
  37. *
  38. * The attribute value must conform to the full version of the ISO 8601 date and time format,
  39. * including time and time zone.
  40. *
  41. * Example of structured comment (compatible X1 gerber)
  42. * G04 #@! TF.CreationDate,2018-11-21T08:49:16+01:00* (example of X1 attribute)
  43. *
  44. * Example NC drill files
  45. * ; #@! TF.CreationDate,2018-11-21T08:49:16+01:00* (example of NC drill comment)
  46. *
  47. * Example of X2 attribute:
  48. * %TF.CreationDate,2018-11-06T08:25:24+01:00*%
  49. *
  50. * @note This is the date the Gerber file is effectively created, not the time the project
  51. * PCB was started.
  52. *
  53. * @param aFormat string compatibility: X1, X2, GBRJOB or NC drill syntax.
  54. */
  55. enum GBR_NC_STRING_FORMAT // Options for string format in some attribute strings
  56. {
  57. GBR_NC_STRING_FORMAT_X1,
  58. GBR_NC_STRING_FORMAT_X2,
  59. GBR_NC_STRING_FORMAT_GBRJOB,
  60. GBR_NC_STRING_FORMAT_NCDRILL
  61. };
  62. wxString GbrMakeCreationDateAttributeString( GBR_NC_STRING_FORMAT aFormat );
  63. /**
  64. * Build a project GUID using format RFC4122 Version 1 or 4 from the project name, because
  65. * a KiCad project has no specific GUID.
  66. *
  67. * RFC4122 is used mainly for its syntax, because fields have no meaning for Gerber files
  68. * and therefore the GUID generated has no meaning because it do not use any time and time
  69. * stamp specific to the project, just a random pattern (random is here a pattern specific
  70. * to a project).
  71. *
  72. * See en.wikipedia.org/wiki/Universally_unique_identifier
  73. */
  74. wxString GbrMakeProjectGUIDfromString( const wxString& aText );
  75. // this class handle info which can be added in a gerber file as attribute
  76. // of an aperture, by the %TA.AperFunction command
  77. // This attribute is added when creating a new aperture (command %ADDxx)
  78. // Only one aperture attribute can be added to a given aperture
  79. //
  80. class GBR_APERTURE_METADATA
  81. {
  82. public:
  83. enum GBR_APERTURE_ATTRIB
  84. {
  85. GBR_APERTURE_ATTRIB_NONE, ///< uninitialized attribute.
  86. GBR_APERTURE_ATTRIB_ETCHEDCMP, ///< aperture used for etched components.
  87. ///< aperture used for connected items like tracks (not vias).
  88. GBR_APERTURE_ATTRIB_CONDUCTOR,
  89. GBR_APERTURE_ATTRIB_EDGECUT, ///< aperture used for board cutout,
  90. ///< aperture used for not connected items (texts, outlines on copper).
  91. GBR_APERTURE_ATTRIB_NONCONDUCTOR,
  92. GBR_APERTURE_ATTRIB_VIAPAD, ///< aperture used for vias.
  93. ///< aperture used for through hole component on outer layer.
  94. GBR_APERTURE_ATTRIB_COMPONENTPAD,
  95. ///< aperture used for SMD pad. Excluded BGA pads which have their own type.
  96. GBR_APERTURE_ATTRIB_SMDPAD_SMDEF,
  97. ///< aperture used for SMD pad with a solder mask defined by the solder mask.
  98. GBR_APERTURE_ATTRIB_SMDPAD_CUDEF,
  99. ///< aperture used for BGA pads with a solder mask defined by the copper shape.
  100. GBR_APERTURE_ATTRIB_BGAPAD_SMDEF,
  101. ///< aperture used for BGA pad with a solder mask defined by the solder mask.
  102. GBR_APERTURE_ATTRIB_BGAPAD_CUDEF,
  103. ///< aperture used for edge connector pad (outer layers).
  104. GBR_APERTURE_ATTRIB_CONNECTORPAD,
  105. GBR_APERTURE_ATTRIB_WASHERPAD, ///< aperture used for mechanical pads (NPTH).
  106. GBR_APERTURE_ATTRIB_TESTPOINT, ///< aperture used for test point pad (outer layers).
  107. ///< aperture used for fiducial pad (outer layers), at board level.
  108. GBR_APERTURE_ATTRIB_FIDUCIAL_GLBL,
  109. ///< aperture used for fiducial pad (outer layers), at footprint level.
  110. GBR_APERTURE_ATTRIB_FIDUCIAL_LOCAL,
  111. ///< aperture used for heat sink pad (typically for SMDs).
  112. GBR_APERTURE_ATTRIB_HEATSINKPAD,
  113. ///< aperture used for castellated pads in copper layer files.
  114. GBR_APERTURE_ATTRIB_CASTELLATEDPAD,
  115. ///< aperture used for castellated pads in drill files.
  116. GBR_APERTURE_ATTRIB_CASTELLATEDDRILL,
  117. GBR_APERTURE_ATTRIB_VIADRILL, ///< aperture used for via holes in drill files.
  118. GBR_APERTURE_ATTRIB_CMP_DRILL, ///< aperture used for pad holes in drill files.
  119. ///< aperture used for pads oblong holes in drill files.
  120. GBR_APERTURE_ATTRIB_CMP_OBLONG_DRILL,
  121. ///< aperture used for flashed cmp position in placement files.
  122. GBR_APERTURE_ATTRIB_CMP_POSITION,
  123. ///< aperture used for flashed pin 1 (or A1 or AA1) position in placement files.
  124. GBR_APERTURE_ATTRIB_PAD1_POS,
  125. ///< aperture used for flashed pads position in placement files.
  126. GBR_APERTURE_ATTRIB_PADOTHER_POS,
  127. ///< aperture used to draw component physical body outline without pins in placement files.
  128. GBR_APERTURE_ATTRIB_CMP_BODY,
  129. ///< aperture used to draw component physical body outline with pins in placement files.
  130. GBR_APERTURE_ATTRIB_CMP_LEAD2LEAD,
  131. ///< aperture used to draw component footprint bounding box in placement files.
  132. GBR_APERTURE_ATTRIB_CMP_FOOTPRINT,
  133. ///< aperture used to draw component outline courtyard in placement files.
  134. GBR_APERTURE_ATTRIB_CMP_COURTYARD,
  135. GBR_APERTURE_ATTRIB_END ///< sentinel: max value
  136. };
  137. GBR_APERTURE_METADATA()
  138. :m_ApertAttribute( GBR_APERTURE_ATTRIB_NONE )
  139. {}
  140. /**
  141. * @return the string corresponding to the aperture attribute.
  142. */
  143. static std::string GetAttributeName( GBR_APERTURE_ATTRIB aAttribute );
  144. std::string GetAttributeName()
  145. {
  146. return GetAttributeName( m_ApertAttribute );
  147. }
  148. /**
  149. * @param aUseX1StructuredComment false in X2 mode and true in X1 mode to add the net
  150. * attribute inside a compatible X1 structured comment
  151. * starting by "G04 #@! "
  152. * @return the full command string corresponding to the aperture attribute
  153. * like "%TA.AperFunction,<function>*%"
  154. */
  155. static std::string FormatAttribute( GBR_APERTURE_ATTRIB aAttribute,
  156. bool aUseX1StructuredComment );
  157. std::string FormatAttribute( bool aUseX1StructuredComment )
  158. {
  159. return FormatAttribute( m_ApertAttribute, aUseX1StructuredComment );
  160. }
  161. // The id of the aperture attribute
  162. GBR_APERTURE_ATTRIB m_ApertAttribute;
  163. };
  164. /**
  165. * Metadata which can be added in a gerber file as attribute in X2 format.
  166. */
  167. class GBR_METADATA
  168. {
  169. public:
  170. GBR_METADATA(): m_isCopper( false) {}
  171. void SetApertureAttrib( GBR_APERTURE_METADATA::GBR_APERTURE_ATTRIB aApertAttribute )
  172. {
  173. m_ApertureMetadata.m_ApertAttribute = aApertAttribute;
  174. }
  175. GBR_APERTURE_METADATA::GBR_APERTURE_ATTRIB GetApertureAttrib()
  176. {
  177. return m_ApertureMetadata.m_ApertAttribute;
  178. }
  179. void SetNetAttribType( int aNetAttribType )
  180. {
  181. m_NetlistMetadata.m_NetAttribType = aNetAttribType;
  182. }
  183. int GetNetAttribType() const
  184. {
  185. return m_NetlistMetadata.m_NetAttribType;
  186. }
  187. void SetNetName( const wxString& aNetname ) { m_NetlistMetadata.m_Netname = aNetname; }
  188. void SetPadName( const wxString& aPadname, bool aUseUTF8 = false, bool aEscapeString = false )
  189. {
  190. m_NetlistMetadata.m_Padname.SetField( aPadname, aUseUTF8, aEscapeString );
  191. }
  192. void SetPadPinFunction( const wxString& aPadPinFunction, bool aUseUTF8, bool aEscapeString )
  193. {
  194. m_NetlistMetadata.m_PadPinFunction.SetField( aPadPinFunction, aUseUTF8, aEscapeString );
  195. }
  196. void SetCmpReference( const wxString& aComponentRef )
  197. {
  198. m_NetlistMetadata.m_Cmpref = aComponentRef;
  199. }
  200. /**
  201. * Allowed attributes are not the same on board copper layers and on other layers.
  202. *
  203. * A flag can be set or reset when attributes can be depending on layers
  204. */
  205. bool IsCopper() { return m_isCopper; }
  206. void SetCopper( bool aValue ) { m_isCopper = aValue; }
  207. /**
  208. * An item to handle aperture attribute.
  209. */
  210. GBR_APERTURE_METADATA m_ApertureMetadata;
  211. /**
  212. * An item to handle object attribute.
  213. */
  214. GBR_NETLIST_METADATA m_NetlistMetadata;
  215. private:
  216. /**
  217. * If the metadata is relative to a copper layer or not, this flag which can be set/reset
  218. * when an attribute for a given item depends on whether a copper layer or a non copper
  219. * layer is plotted. The initial state i false.
  220. */
  221. bool m_isCopper;
  222. };
  223. /**
  224. * Normalize \a aString and convert it to a Gerber std::string.
  225. *
  226. * Normalization means convert any code > 0x7F and unauthorized code to a hexadecimal
  227. * 16 bit sequence Unicode. Illegal characters are ',' '*' '%' '\'.
  228. *
  229. * @param aString the string to convert.
  230. * @return an ASCII7 coded compliant gerber string.
  231. */
  232. std::string FormatStringToGerber( const wxString& aString );
  233. /**
  234. * Normalize \a aString and convert it to a Gerber compatible wxString.
  235. *
  236. * Normalization means convert to a hexadecimal 16 bit sequence Unicode and on request
  237. * convert any code > 0x7F. Illegal characters are ',' '*' '%' '\'.
  238. *
  239. * @param aString the string to convert.
  240. * @param aAllowUtf8Chars false to convert non ASCII7 values to Unicode sequence.
  241. * @param aQuoteString true to double quote the returned string.
  242. * @return a without illegal chars (and converted non ASCII7 chars on request)
  243. */
  244. wxString ConvertNotAllowedCharsInGerber( const wxString& aString, bool aAllowUtf8Chars,
  245. bool aQuoteString );
  246. /**
  247. * Convert a gerber string into a 16 bit Unicode string.
  248. *
  249. * @param aString the gerber string to format.
  250. * @return a 16 bit Unicode string.
  251. */
  252. wxString FormatStringFromGerber( const wxString& aString );
  253. /**
  254. * Generate the string to set a net attribute for a graphic object to print to a gerber file.
  255. *
  256. * @param aPrintedText is the string to print.
  257. * @param aLastNetAttributes is the current full set of attributes.
  258. * @param aData is the #GBR_NETLIST_METADATA associated to the graphic object (can be NULL
  259. * if no associated metadata, and aClearPreviousAttributes will be set to false)
  260. * @param aClearPreviousAttributes returns true if the full set of attributes must be deleted
  261. * from file before adding new attribute (happens when a previous
  262. * attribute no longer exists).
  263. * @param aUseX1StructuredComment false in X2 mode and true in X1 mode to add the net attribute
  264. * in compatible X1 structured comment (i.e. prefixed by "G04 #@! ")
  265. * @return false if nothing can be done (GBR_NETLIST_METADATA has GBR_APERTURE_ATTRIB_NONE,
  266. * and true if OK. If the new attribute(s) is the same as current attribute(s),
  267. * \a aPrintedText will be empty.
  268. */
  269. bool FormatNetAttribute( std::string& aPrintedText, std::string& aLastNetAttributes,
  270. const GBR_NETLIST_METADATA* aData, bool& aClearPreviousAttributes,
  271. bool aUseX1StructuredComment );
  272. #endif // GBR_METADATA_H