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.

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