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.

269 lines
11 KiB

3 years ago
3 years ago
  1. /**
  2. * @file aperture_macro.h
  3. */
  4. #ifndef AM_PRIMITIVE_H
  5. #define AM_PRIMITIVE_H
  6. /*
  7. * This program source code file is part of KiCad, a free EDA CAD application.
  8. *
  9. * Copyright (C) 1992-2010 Jean-Pierre Charras <jp.charras at wanadoo.fr>
  10. * Copyright (C) 2010 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  11. * Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * as published by the Free Software Foundation; either version 2
  16. * of the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, you may find one here:
  25. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  26. * or you may search the http://www.gnu.org website for the version 2 license,
  27. * or you may write to the Free Software Foundation, Inc.,
  28. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  29. */
  30. #include <vector>
  31. #include <set>
  32. #include <am_param.h>
  33. class SHAPE_POLY_SET;
  34. /*
  35. * An aperture macro defines a complex shape and is a list of aperture primitives.
  36. * Each aperture primitive defines a simple shape (circle, rect, regular polygon...)
  37. * Inside a given aperture primitive, a fixed list of parameters defines info
  38. * about the shape: size, thickness, number of vertex ...
  39. *
  40. * Each parameter can be an immediate value or a deferred value.
  41. * When value is deferred, it is defined when the aperture macro is instanced by
  42. * an ADD macro command
  43. * Note also a deferred parameter can be defined in aperture macro,
  44. * but outside aperture primitives. Example
  45. * %AMRECTHERM*
  46. * $4=$3/2* parameter $4 is half value of parameter $3
  47. * 21,1,$1-$3,$2-$3,0-$1/2-$4,0-$2/2-$4,0*
  48. * For the aperture primitive, parameters $1 to $3 will be defined in ADD command,
  49. * and $4 is defined inside the macro
  50. *
  51. * Each basic shape can be a positive shape or a negative shape.
  52. * a negative shape is "local" to the whole shape.
  53. * It must be seen like a hole in the shape, and not like a standard negative object.
  54. */
  55. /**
  56. * The set of all "aperture macro primitives" (primitive numbers).
  57. *
  58. * See Table 3 in http://gerbv.sourceforge.net/docs/rs274xrevd_e.pdf aperture macro primitives
  59. * are basic shapes which can be combined to create a complex shape. This complex shape is
  60. * flashed.
  61. */
  62. enum AM_PRIMITIVE_ID {
  63. AMP_UNKNOWN = -1, // A value for uninitialized AM_PRIMITIVE.
  64. AMP_COMMENT = 0, // A primitive description is not really a primitive, this is a
  65. // comment
  66. AMP_CIRCLE = 1, // Circle. (diameter and position)
  67. AMP_LINE2 = 2, // Line with rectangle ends. (Width, start and end pos + rotation)
  68. AMP_LINE20 = 20, // Same as AMP_LINE2
  69. AMP_LINE_CENTER = 21, // Rectangle. (height, width and center pos + rotation)
  70. AMP_LINE_LOWER_LEFT = 22, // Rectangle. (height, width and left bottom corner pos + rotation)
  71. AMP_EOF = 3, // End Of File marker: not really a shape
  72. AMP_OUTLINE = 4, // Free polyline (n corners + rotation)
  73. AMP_POLYGON = 5, // Closed regular polygon(diameter, number of vertices (3 to 10),
  74. // rotation)
  75. AMP_MOIRE = 6, // A cross hair with n concentric circles + rotation
  76. AMP_THERMAL = 7 // Thermal shape (pos, outer and inner diameter, cross hair
  77. // thickness + rotation)
  78. };
  79. /**
  80. * An aperture macro primitive as given in Table 3 of
  81. * http://gerbv.sourceforge.net/docs/rs274xrevd_e.pdf
  82. */
  83. class AM_PRIMITIVE
  84. {
  85. public:
  86. AM_PRIMITIVE_ID primitive_id; ///< The primitive type
  87. AM_PARAMS params; ///< A sequence of parameters used by
  88. // the primitive
  89. bool m_GerbMetric; // units for this primitive:
  90. // false = Inches, true = metric
  91. AM_PRIMITIVE( bool aGerbMetric, AM_PRIMITIVE_ID aId = AMP_UNKNOWN )
  92. {
  93. primitive_id = aId;
  94. m_GerbMetric = aGerbMetric;
  95. }
  96. ~AM_PRIMITIVE() {}
  97. /**
  98. * @return true if the first parameter is not 0 (it can be only 0 or 1).
  99. * Some but not all primitives use the first parameter as an exposure control.
  100. * Others are always ON.
  101. * In a aperture macro shape, a basic primitive with exposure off is a hole in the shape
  102. * it is NOT a negative shape
  103. */
  104. bool IsAMPrimitiveExposureOn( const GERBER_DRAW_ITEM* aParent ) const;
  105. /* Draw functions: */
  106. /**
  107. * Calculate a value that can be used to evaluate the size of text when displaying the
  108. * D-Code of an item.
  109. *
  110. * Due to the complexity of the shape of some primitives one cannot calculate the "size"
  111. * of a shape (only a bounding box) but here, the "dimension" of the shape is the diameter
  112. * of the primitive or for lines the width of the line.
  113. *
  114. * @param aParent is the parent GERBER_DRAW_ITEM which is actually drawn
  115. * @return a dimension, or -1 if no dim to calculate
  116. */
  117. int GetShapeDim( const GERBER_DRAW_ITEM* aParent );
  118. /**
  119. * Draw (in fact generate the actual polygonal shape of) the primitive shape of an aperture
  120. * macro instance.
  121. *
  122. * @param aParent is the parent GERBER_DRAW_ITEM which is actually drawn.
  123. * @param aShapeBuffer is a SHAPE_POLY_SET to put the shape converted to a polygon.
  124. * @param aShapePos is the actual shape position.
  125. */
  126. void DrawBasicShape( const GERBER_DRAW_ITEM* aParent,
  127. SHAPE_POLY_SET& aShapeBuffer,
  128. const VECTOR2I& aShapePos );
  129. private:
  130. /**
  131. * Convert a shape to an equivalent polygon.
  132. *
  133. * Arcs and circles are approximated by segments. Useful when a shape is not a graphic
  134. * primitive (shape with hole, rotated shape ... ) and cannot be easily drawn.
  135. *
  136. * @note Some schapes conbining circles and solid lines (rectangles), only rectangles are
  137. * converted because circles are very easy to draw (no rotation problem) so convert
  138. * them in polygons and draw them as polygons is not a good idea.
  139. */
  140. void ConvertShapeToPolygon( const GERBER_DRAW_ITEM* aParent, std::vector<VECTOR2I>& aBuffer );
  141. };
  142. typedef std::vector<AM_PRIMITIVE> AM_PRIMITIVES;
  143. /**
  144. * Support the "aperture macro" defined within standard RS274X.
  145. */
  146. struct APERTURE_MACRO
  147. {
  148. /**
  149. * Usually, parameters are defined inside the aperture primitive using immediate mode or
  150. * deferred mode.
  151. *
  152. * In deferred mode the value is defined in a DCODE that want to use the aperture macro.
  153. * Some parameters are defined outside the aperture primitive and are local to the aperture
  154. * macro.
  155. *
  156. * @return the value of a deferred parameter defined inside the aperture macro.
  157. * @param aDcode is the D_CODE that uses this aperture macro and define deferred parameters.
  158. * @param aParamId is the param id (defined by $3 or $5 ..) to evaluate.
  159. */
  160. double GetLocalParam( const D_CODE* aDcode, unsigned aParamId ) const;
  161. /**
  162. * Calculate the primitive shape for flashed items.
  163. *
  164. * When an item is flashed, this is the shape of the item.
  165. *
  166. * @param aParent is the parent #GERBER_DRAW_ITEM which is actually drawn.
  167. * @return the shape of the item.
  168. */
  169. SHAPE_POLY_SET* GetApertureMacroShape( const GERBER_DRAW_ITEM* aParent,
  170. const VECTOR2I& aShapePos );
  171. /**
  172. * Draw the primitive shape for flashed items.
  173. *
  174. * When an item is flashed, this is the shape of the item.
  175. *
  176. * @param aParent is the parent GERBER_DRAW_ITEM which is actually drawn.
  177. * @param aDC is the device context.
  178. * @param aColor is the color of shape.
  179. * @param aShapePos is the actual shape position.
  180. * @param aFilledShape set to true to draw in filled mode, false to draw in sketch mode.
  181. */
  182. void DrawApertureMacroShape( const GERBER_DRAW_ITEM* aParent, wxDC* aDC,
  183. const COLOR4D& aColor,
  184. const VECTOR2I& aShapePos, bool aFilledShape );
  185. /**
  186. * C
  187. *
  188. * alculate a value that can be used to evaluate the size of text when displaying the
  189. * D-Code of an item.
  190. *
  191. * Due to the complexity of a shape using many primitives one cannot calculate the "size" of
  192. * a shape (only abounding box) but most of aperture macro are using one or few primitives
  193. * and the "dimension" of the shape is the diameter of the primitive (or the max diameter of
  194. * primitives).
  195. *
  196. * @param aParent is the parent #GERBER_DRAW_ITEM which is actually drawn.
  197. * @return a dimension, or -1 if no dim to calculate.
  198. */
  199. int GetShapeDim( GERBER_DRAW_ITEM* aParent );
  200. /// Return the bounding box of the shape.
  201. BOX2I GetBoundingBox() const
  202. {
  203. return m_boundingBox;
  204. }
  205. wxString name; ///< The name of the aperture macro
  206. AM_PRIMITIVES primitives; ///< A sequence of AM_PRIMITIVEs
  207. /* A deferred parameter can be defined in aperture macro,
  208. * but outside aperture primitives. Example
  209. * %AMRECTHERM*
  210. * $4=$3/2* parameter $4 is half value of parameter $3
  211. * m_localparamStack handle a list of local deferred parameters
  212. */
  213. AM_PARAMS m_localparamStack;
  214. SHAPE_POLY_SET m_shape; ///< The shape of the item, calculated by GetApertureMacroShape
  215. BOX2I m_boundingBox; ///< The bounding box of the item, calculated by
  216. ///< GetApertureMacroShape.
  217. };
  218. /**
  219. * Used by std:set<APERTURE_MACRO> instantiation which uses APERTURE_MACRO.name as its key.
  220. */
  221. struct APERTURE_MACRO_less_than
  222. {
  223. // a "less than" test on two APERTURE_MACROs (.name wxStrings)
  224. bool operator()( const APERTURE_MACRO& am1, const APERTURE_MACRO& am2 ) const
  225. {
  226. return am1.name.Cmp( am2.name ) < 0; // case specific wxString compare
  227. }
  228. };
  229. /**
  230. * A sorted collection of APERTURE_MACROS whose key is the name field in the APERTURE_MACRO.
  231. */
  232. typedef std::set<APERTURE_MACRO, APERTURE_MACRO_less_than> APERTURE_MACRO_SET;
  233. typedef std::pair<APERTURE_MACRO_SET::iterator, bool> APERTURE_MACRO_SET_PAIR;
  234. #endif // ifndef AM_PRIMITIVE_H