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.

286 lines
13 KiB

  1. /******************/
  2. /* class_GERBER.h */
  3. /******************/
  4. #ifndef _CLASS_GERBER_H_
  5. #define _CLASS_GERBER_H_
  6. #include <vector>
  7. #include <set>
  8. #include "dcode.h"
  9. #include "class_gerber_draw_item.h"
  10. #include "class_aperture_macro.h"
  11. // An useful macro used when reading gerber files;
  12. #define IsNumber( x ) ( ( ( (x) >= '0' ) && ( (x) <='9' ) ) \
  13. || ( (x) == '-' ) || ( (x) == '+' ) || ( (x) == '.' ) )
  14. class GERBVIEW_FRAME;
  15. class BOARD;
  16. class D_CODE;
  17. /* gerber files have different parameters to define units and how items must be plotted.
  18. * some are for the entire file, and other can change along a file.
  19. * In Gerber world:
  20. * an image is the entire gerber file and its "global" parameters
  21. * a layer (that is very different from a board layer) is just a sub set of a file that
  22. * have specific parameters
  23. * if a Image parameter is set more than once, only the last value is used
  24. * Some parameters can change along a file and are not layer specific: they are stored
  25. * in GERBER_ITEM items, when instancied.
  26. *
  27. * In GerbView, to handle these parameters, there are 2 classes:
  28. * GERBER_IMAGE : the main class containing most of parameters and data to plot a graphic layer
  29. * Some of them can change along the file
  30. * There is one GERBER_IMAGE per file and one graphic layer per file or GERBER_IMAGE
  31. * GerbView does not read and merge 2 gerber file in one graphic layer:
  32. * I believe this is not possible due to the constraints in Image parameters.
  33. * GERBER_LAYER : containing the subset of parameters that is layer speficic
  34. * A GERBER_IMAGE must include one GERBER_LAYER to define all parameters to plot a file.
  35. * But a GERBER_IMAGE can use more than one GERBER_LAYER.
  36. */
  37. class GERBER_IMAGE;
  38. class GERBER_LAYER
  39. {
  40. friend class GERBER_IMAGE;
  41. public:
  42. // These parameters are layer specfic:
  43. wxString m_LayerName; // Layer name, from LN <name>* command
  44. bool m_LayerNegative; // true = Negative Layer: command LP
  45. wxRealPoint m_StepForRepeat; // X and Y offsets for Step and Repeat command
  46. int m_XRepeatCount; // The repeat count on X axis
  47. int m_YRepeatCount; // The repeat count on Y axis
  48. bool m_StepForRepeatMetric; // false = Inches, true = metric
  49. // needed here because repeated
  50. // gerber items can have coordinates
  51. // in different units than step parameters
  52. // and the actual coordinates calculation must handle this
  53. public:
  54. GERBER_LAYER();
  55. ~GERBER_LAYER();
  56. private:
  57. void ResetDefaultValues();
  58. };
  59. /**
  60. * Class GERBER_IMAGE
  61. * holds the Image data and parameters for one gerber file
  62. * and layer parameters (TODO: move them in GERBER_LAYER class
  63. */
  64. class GERBER_IMAGE
  65. {
  66. GERBVIEW_FRAME* m_Parent; // the parent GERBVIEW_FRAME (used to display messages...)
  67. D_CODE* m_Aperture_List[TOOLS_MAX_COUNT]; ///< Dcode (Aperture) List for this layer (max 999)
  68. bool m_Exposure; ///< whether an aperture macro tool is flashed on or off
  69. BOARD* m_Pcb;
  70. GERBER_LAYER m_GBRLayerParams; // hold params for the current gerber layer
  71. public:
  72. bool m_InUse; // true if this image is currently in use
  73. // (a file is loaded in it)
  74. wxString m_FileName; // Full File Name for this layer
  75. wxString m_ImageName; // Image name, from IN <name>* command
  76. int m_GraphicLayer; // Graphic layer Number
  77. bool m_ImageNegative; // true = Negative image
  78. bool m_ImageJustifyXCenter; // Image Justify Center on X axis (default = false)
  79. bool m_ImageJustifyYCenter; // Image Justify Center on Y axis (default = false)
  80. wxPoint m_ImageJustifyOffset; // Image Justify Offset on XY axis (default = 0,0)
  81. bool m_GerbMetric; // false = Inches, true = metric
  82. bool m_Relative; // false = absolute Coord, true = relative Coord
  83. bool m_NoTrailingZeros; // true: remove tailing zeros.
  84. bool m_DecimalFormat; // true: use floating point notations for coordinates
  85. // If true, overrides m_NoTrailingZeros parameter.
  86. wxPoint m_ImageOffset; // Coord Offset, from IO command
  87. wxSize m_FmtScale; // Fmt 2.3: m_FmtScale = 3, fmt 3.4: m_FmtScale = 4
  88. wxSize m_FmtLen; // Nb chars per coord. ex fmt 2.3, m_FmtLen = 5
  89. int m_ImageRotation; // Image rotation (0, 90, 180, 270 only) in degrees
  90. double m_LocalRotation; // Local rotation, in degrees, added to m_ImageRotation
  91. // Note this value is stored in 0.1 degrees
  92. wxPoint m_Offset; // Coord Offset, from OF command
  93. wxRealPoint m_Scale; // scale (X and Y) of layer.
  94. bool m_SwapAxis; // false (default) if A = X and B = Y
  95. // true if A = Y, B = X
  96. bool m_MirrorA; // true: miror / axe A (X)
  97. bool m_MirrorB; // true: miror / axe B (Y)
  98. int m_Iterpolation; // Linear, 90 arc, Circ.
  99. int m_Current_Tool; // Current Tool (Dcode) number selected
  100. int m_Last_Pen_Command; // Current or last pen state (0..9, set by Dn option with n <10
  101. int m_CommandState; // state of gerber analysis command.
  102. wxPoint m_CurrentPos; // current specified coord for plot
  103. wxPoint m_PreviousPos; // old current specified coord for plot
  104. wxPoint m_IJPos; // IJ coord (for arcs & circles )
  105. FILE* m_Current_File; // Current file to read
  106. #define INCLUDE_FILES_CNT_MAX 10
  107. FILE* m_FilesList[INCLUDE_FILES_CNT_MAX + 2]; // Included files list
  108. int m_FilesPtr; // Stack pointer for files list
  109. int m_Selected_Tool; // For hightlight: current selected Dcode
  110. bool m_Has_DCode; // true = DCodes in file
  111. // (false = no DCode -> separate DCode file
  112. bool m_360Arc_enbl; // Enbl 360 deg circular interpolation
  113. bool m_PolygonFillMode; // Enable polygon mode (read coord as a polygon descr)
  114. int m_PolygonFillModeState; // In polygon mode: 0 = first segm, 1 = next segm
  115. APERTURE_MACRO_SET m_aperture_macros; ///< a collection of APERTURE_MACROS, sorted by name
  116. private:
  117. int m_hasNegativeItems; // true if the image is negative or has some negative items
  118. // Used to optimize drawing, because when there are no
  119. // negative items screen refresh does not need
  120. // to build an intermediate bitmap specfic to this image
  121. // -1 = negative items are
  122. // 0 = no negative items found
  123. // 1 = have negative items found
  124. public:
  125. GERBER_IMAGE( GERBVIEW_FRAME* aParent, int layer );
  126. virtual ~GERBER_IMAGE();
  127. void Clear_GERBER_IMAGE();
  128. int ReturnUsedDcodeNumber();
  129. virtual void ResetDefaultValues();
  130. /**
  131. * Function GetParent
  132. * @return the GERBVIEW_FRAME parent of this GERBER_IMAGE
  133. */
  134. GERBVIEW_FRAME* GetParent()
  135. {
  136. return m_Parent;
  137. }
  138. /**
  139. * Function GetLayerParams
  140. * @return the current layers params
  141. */
  142. GERBER_LAYER& GetLayerParams()
  143. {
  144. return m_GBRLayerParams;
  145. }
  146. /**
  147. * Function HasNegativeItems
  148. * @return true if at least one item must be drawn in background color
  149. * used to optimize screen refresh (when no items are in background color
  150. * refresh can be faster)
  151. */
  152. bool HasNegativeItems();
  153. /**
  154. * Function ReportMessage
  155. * Add a message (a string) in message list
  156. * for instance when reading a Gerber file
  157. * @param aMessage = the straing to add in list
  158. */
  159. void ReportMessage( const wxString aMessage );
  160. /**
  161. * Function ClearMessageList
  162. * Clear the message list
  163. * Call it before reading a Gerber file
  164. */
  165. void ClearMessageList();
  166. /**
  167. * Function InitToolTable
  168. */
  169. void InitToolTable();
  170. /**
  171. * Function ReadXYCoord
  172. * Returns the current coordinate type pointed to by XnnYnn Text (XnnnnYmmmm)
  173. */
  174. wxPoint ReadXYCoord( char*& Text );
  175. /**
  176. * Function ReadIJCoord
  177. * Returns the current coordinate type pointed to by InnJnn Text (InnnnJmmmm)
  178. * These coordinates are relative, so if coordinate is absent, it's value
  179. * defaults to 0
  180. */
  181. wxPoint ReadIJCoord( char*& Text );
  182. // functions to read G commands or D commands:
  183. int ReturnGCodeNumber( char*& Text );
  184. int ReturnDCodeNumber( char*& Text );
  185. // functions to execute G commands or D commands:
  186. bool Execute_G_Command( char*& text, int G_command );
  187. bool Execute_DCODE_Command( char*& text, int D_command );
  188. /**
  189. * Function ReadRS274XCommand
  190. * reads a single RS274X command terminated with a %
  191. */
  192. bool ReadRS274XCommand( char aBuff[GERBER_BUFZ], char* & text );
  193. /**
  194. * Function ExecuteRS274XCommand
  195. * executes 1 command
  196. */
  197. bool ExecuteRS274XCommand( int command, char aBuff[GERBER_BUFZ],
  198. char* & text );
  199. /**
  200. * Function ReadApertureMacro
  201. * reads in an aperture macro and saves it in m_aperture_macros.
  202. * @param aBuff a character buffer at least GERBER_BUFZ long that can be
  203. * used to read successive lines from the gerber file.
  204. * @param text A reference to a character pointer which gives the initial
  205. * text to read from.
  206. * @param gerber_file Which file to read from for continuation.
  207. * @return bool - true if a macro was read in successfully, else false.
  208. */
  209. bool ReadApertureMacro( char aBuff[GERBER_BUFZ], char* & text,
  210. FILE * gerber_file );
  211. /**
  212. * Function GetDCODE
  213. * returns a pointer to the D_CODE within this GERBER for the given
  214. * \a aDCODE.
  215. * @param aDCODE The numeric value of the D_CODE to look up.
  216. * @param createIfNoExist If true, then create the D_CODE if it does not
  217. * exist.
  218. * @return D_CODE* - the one implied by the given \a aDCODE, or NULL
  219. * if the requested \a aDCODE is out of range.
  220. */
  221. D_CODE* GetDCODE( int aDCODE, bool createIfNoExist = true );
  222. /**
  223. * Function FindApertureMacro
  224. * looks up a previously read in aperture macro.
  225. * @param aLookup A dummy APERTURE_MACRO with [only] the name field set.
  226. * @return APERTURE_MACRO* - the one with a matching name, or NULL if
  227. * not found.
  228. */
  229. APERTURE_MACRO* FindApertureMacro( const APERTURE_MACRO& aLookup );
  230. /**
  231. * Function StepAndRepeatItem
  232. * Gerber format has a command Step an Repeat
  233. * This function must be called when reading a gerber file and
  234. * after creating a new gerber item that must be repeated
  235. * (i.e when m_XRepeatCount or m_YRepeatCount are > 1)
  236. * @param aItem = the item to repeat
  237. */
  238. void StepAndRepeatItem( const GERBER_DRAW_ITEM& aItem );
  239. /**
  240. * Function DisplayImageInfo
  241. * has knowledge about the frame and how and where to put status information
  242. * about this object into the frame's message panel.
  243. * Display info about Image Parameters.
  244. */
  245. void DisplayImageInfo( void );
  246. };
  247. #endif // ifndef _CLASS_GERBER_H_