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.

261 lines
10 KiB

15 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-2010 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. #include <eda_rect.h>
  34. class SHAPE_POLY_SET;
  35. /*
  36. * An aperture macro defines a complex shape and is a list of aperture primitives.
  37. * Each aperture primitive defines a simple shape (circle, rect, regular polygon...)
  38. * Inside a given aperture primitive, a fixed list of parameters defines info
  39. * about the shape: size, thickness, number of vertex ...
  40. *
  41. * Each parameter can be an immediate value or a defered value.
  42. * When value is defered, it is defined when the aperture macro is instancied by
  43. * an ADD macro command
  44. * Note also a defered parameter can be defined in aperture macro,
  45. * but outside aperture primitives. Example
  46. * %AMRECTHERM*
  47. * $4=$3/2* parameter $4 is half value of parameter $3
  48. * 21,1,$1-$3,$2-$3,0-$1/2-$4,0-$2/2-$4,0*
  49. * For the aperture primitive, parameters $1 to $3 will be defined in ADD command,
  50. * and $4 is defined inside the macro
  51. *
  52. * Each basic shape can be a positive shape or a negative shape.
  53. * a negative shape is "local" to the whole shape.
  54. * It must be seen like a hole in the shape, and not like a standard negative object.
  55. */
  56. /**
  57. * Enum AM_PRIMITIVE_ID
  58. * is the set of all "aperture macro primitives" (primitive numbers). See
  59. * Table 3 in http://gerbv.sourceforge.net/docs/rs274xrevd_e.pdf
  60. * aperture macro primitives are basic shapes which can be combined to create a complex shape
  61. * This complex shape is flashed.
  62. */
  63. enum AM_PRIMITIVE_ID {
  64. AMP_UNKNOWN = -1, // A value for uninitialized AM_PRIMITIVE.
  65. AMP_COMMENT = 0, // A primitive description is not really a primitive, this is a 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 marquer: 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), rotation)
  74. AMP_MOIRE = 6, // A cross hair with n concentric circles + rotation
  75. AMP_THERMAL = 7 // Thermal shape (pos, outer and inner diameter, cross hair thickness + rotation)
  76. };
  77. /**
  78. * Struct AM_PRIMITIVE
  79. * holds an aperture macro primitive as given in Table 3 of
  80. * http://gerbv.sourceforge.net/docs/rs274xrevd_e.pdf
  81. */
  82. class AM_PRIMITIVE
  83. {
  84. public:
  85. AM_PRIMITIVE_ID primitive_id; ///< The primitive type
  86. AM_PARAMS params; ///< A sequence of parameters used by
  87. // the primitive
  88. bool m_GerbMetric; // units for this primitive:
  89. // false = Inches, true = metric
  90. public: AM_PRIMITIVE( bool aGerbMetric, AM_PRIMITIVE_ID aId = AMP_UNKNOWN )
  91. {
  92. primitive_id = aId;
  93. m_GerbMetric = aGerbMetric;
  94. }
  95. ~AM_PRIMITIVE() {}
  96. /**
  97. * Function IsAMPrimitiveExposureOn
  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. /** GetShapeDim
  107. * Calculate a value that can be used to evaluate the size of text
  108. * when displaying the D-Code of an item
  109. * due to the complexity of the shape of some primitives
  110. * one cannot calculate the "size" of a shape (only a bounding box)
  111. * but here, the "dimension" of the shape is the diameter of the primitive
  112. * or for lines the width of the line
  113. * @param aParent = the parent GERBER_DRAW_ITEM which is actually drawn
  114. * @return a dimension, or -1 if no dim to calculate
  115. */
  116. int GetShapeDim( const GERBER_DRAW_ITEM* aParent );
  117. /**
  118. * Function drawBasicShape
  119. * Draw (in fact generate the actual polygonal shape of) the primitive shape of an aperture macro instance.
  120. * @param aParent = the parent GERBER_DRAW_ITEM which is actually drawn
  121. * @param aShapeBuffer = a SHAPE_POLY_SET to put the shape converted to a polygon
  122. * @param aShapePos = the actual shape position
  123. */
  124. void DrawBasicShape( const GERBER_DRAW_ITEM* aParent,
  125. SHAPE_POLY_SET& aShapeBuffer,
  126. wxPoint aShapePos );
  127. private:
  128. /**
  129. * Function ConvertShapeToPolygon
  130. * convert a shape to an equivalent polygon.
  131. * Arcs and circles are approximated by segments
  132. * Useful when a shape is not a graphic primitive (shape with hole,
  133. * rotated shape ... ) and cannot be easily drawn.
  134. */
  135. void ConvertShapeToPolygon( const GERBER_DRAW_ITEM* aParent,
  136. std::vector<wxPoint>& aBuffer );
  137. };
  138. typedef std::vector<AM_PRIMITIVE> AM_PRIMITIVES;
  139. /**
  140. * Struct APERTURE_MACRO
  141. * helps support the "aperture macro" defined within standard RS274X.
  142. */
  143. struct APERTURE_MACRO
  144. {
  145. wxString name; ///< The name of the aperture macro
  146. AM_PRIMITIVES primitives; ///< A sequence of AM_PRIMITIVEs
  147. /* A defered parameter can be defined in aperture macro,
  148. * but outside aperture primitives. Example
  149. * %AMRECTHERM*
  150. * $4=$3/2* parameter $4 is half value of parameter $3
  151. * m_localparamStack handle a list of local defered parameters
  152. */
  153. AM_PARAMS m_localparamStack;
  154. SHAPE_POLY_SET m_shape; ///< The shape of the item, calculated by GetApertureMacroShape
  155. EDA_RECT m_boundingBox; ///< The bounding box of the item, calculated by GetApertureMacroShape
  156. /**
  157. * function GetLocalParam
  158. * Usually, parameters are defined inside the aperture primitive
  159. * using immediate mode or defered mode.
  160. * in defered mode the value is defined in a DCODE that want to use the aperture macro.
  161. * But some parameters are defined outside the aperture primitive
  162. * and are local to the aperture macro
  163. * @return the value of a defered parameter defined inside the aperture macro
  164. * @param aDcode = the D_CODE that uses this apertur macro and define defered parameters
  165. * @param aParamId = the param id (defined by $3 or $5 ..) to evaluate
  166. */
  167. double GetLocalParam( const D_CODE* aDcode, unsigned aParamId ) const;
  168. /**
  169. * Function GetApertureMacroShape
  170. * Calculate the primitive shape for flashed items.
  171. * When an item is flashed, this is the shape of the item
  172. * @param aParent = the parent GERBER_DRAW_ITEM which is actually drawn
  173. * @return The shape of the item
  174. */
  175. SHAPE_POLY_SET* GetApertureMacroShape( const GERBER_DRAW_ITEM* aParent, wxPoint aShapePos );
  176. /**
  177. * Function DrawApertureMacroShape
  178. * Draw the primitive shape for flashed items.
  179. * When an item is flashed, this is the shape of the item
  180. * @param aParent = the parent GERBER_DRAW_ITEM which is actually drawn
  181. * @param aClipBox = DC clip box (NULL is no clip)
  182. * @param aDC = device context
  183. * @param aColor = the color of shape
  184. * @param aShapePos = the actual shape position
  185. * @param aFilledShape = true to draw in filled mode, false to draw in skecth mode
  186. */
  187. void DrawApertureMacroShape( GERBER_DRAW_ITEM* aParent, EDA_RECT* aClipBox, wxDC* aDC,
  188. COLOR4D aColor, wxPoint aShapePos, bool aFilledShape );
  189. /**
  190. * Function GetShapeDim
  191. * Calculate a value that can be used to evaluate the size of text
  192. * when displaying the D-Code of an item
  193. * due to the complexity of a shape using many primitives
  194. * one cannot calculate the "size" of a shape (only abounding box)
  195. * but most of aperture macro are using one or few primitives
  196. * and the "dimension" of the shape is the diameter of the primitive
  197. * (or the max diameter of primitives)
  198. * @param aParent = the parent GERBER_DRAW_ITEM which is actually drawn
  199. * @return a dimension, or -1 if no dim to calculate
  200. */
  201. int GetShapeDim( GERBER_DRAW_ITEM* aParent );
  202. /// Returns the bounding box of the shape
  203. EDA_RECT GetBoundingBox() const
  204. {
  205. return m_boundingBox;
  206. }
  207. };
  208. /**
  209. * Struct APERTURE_MACRO_less_than
  210. * is used by std:set<APERTURE_MACRO> instantiation which uses
  211. * APERTURE_MACRO.name as its key.
  212. */
  213. struct APERTURE_MACRO_less_than
  214. {
  215. // a "less than" test on two APERTURE_MACROs (.name wxStrings)
  216. bool operator()( const APERTURE_MACRO& am1, const APERTURE_MACRO& am2 ) const
  217. {
  218. return am1.name.Cmp( am2.name ) < 0; // case specific wxString compare
  219. }
  220. };
  221. /**
  222. * Type APERTURE_MACRO_SET
  223. * is a sorted collection of APERTURE_MACROS whose key is the name field in
  224. * the APERTURE_MACRO.
  225. */
  226. typedef std::set<APERTURE_MACRO, APERTURE_MACRO_less_than> APERTURE_MACRO_SET;
  227. typedef std::pair<APERTURE_MACRO_SET::iterator, bool> APERTURE_MACRO_SET_PAIR;
  228. #endif // ifndef AM_PRIMITIVE_H