KiCad PCB EDA Suite
class_gerber_file_image.h
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2010-2016 Jean-Pierre Charras jp.charras at wanadoo.fr
5  * Copyright (C) 1992-2016 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 #ifndef CLASS_GERBER_FILE_IMAGE_H
26 #define CLASS_GERBER_FILE_IMAGE_H
27 
28 #include <vector>
29 #include <set>
30 
31 #include <dcode.h>
32 #include <class_gerber_draw_item.h>
33 #include <class_aperture_macro.h>
34 #include <gbr_netlist_metadata.h>
35 
36 // An useful macro used when reading gerber files;
37 #define IsNumber( x ) ( ( ( (x) >= '0' ) && ( (x) <='9' ) ) \
38  || ( (x) == '-' ) || ( (x) == '+' ) || ( (x) == '.' ) )
39 
40 class GERBVIEW_FRAME;
41 class D_CODE;
42 
43 /* gerber files have different parameters to define units and how items must be plotted.
44  * some are for the entire file, and other can change along a file.
45  * In Gerber world:
46  * an image is the entire gerber file and its "global" parameters
47  * a layer (that is very different from a board layer) is just a sub set of a file that
48  * have specific parameters
49  * if a Image parameter is set more than once, only the last value is used
50  * Some parameters can change along a file and are not layer specific: they are stored
51  * in GERBER_ITEM items, when instancied.
52  *
53  * In GerbView, to handle these parameters, there are 2 classes:
54  * GERBER_FILE_IMAGE : the main class containing most of parameters and data to plot a graphic layer
55  * Some of them can change along the file
56  * There is one GERBER_FILE_IMAGE per file and one graphic layer per file or GERBER_FILE_IMAGE
57  * GerbView does not read and merge 2 gerber file in one graphic layer:
58  * I believe this is not possible due to the constraints in Image parameters.
59  * GERBER_LAYER : containing the subset of parameters that is layer speficic
60  * A GERBER_FILE_IMAGE must include one GERBER_LAYER to define all parameters to plot a file.
61  * But a GERBER_FILE_IMAGE can use more than one GERBER_LAYER.
62  */
63 
64 class GERBER_FILE_IMAGE;
65 class X2_ATTRIBUTE;
67 
68 
70 {
71  friend class GERBER_FILE_IMAGE;
72 public:
73 
74  // These parameters are layer specfic:
75  wxString m_LayerName; // Layer name, from LN <name>* command
76  bool m_LayerNegative; // true = Negative Layer: command LP
77  wxRealPoint m_StepForRepeat; // X and Y offsets for Step and Repeat command
78  int m_XRepeatCount; // The repeat count on X axis
79  int m_YRepeatCount; // The repeat count on Y axis
80  bool m_StepForRepeatMetric; // false = Inches, true = metric
81  // needed here because repeated
82  // gerber items can have coordinates
83  // in different units than step parameters
84  // and the actual coordinates calculation must handle this
85 
86 public:
87  GERBER_LAYER();
88  ~GERBER_LAYER();
89 private:
90  void ResetDefaultValues();
91 };
92 
99 {
101  bool m_Exposure;
102 
103  GERBER_LAYER m_GBRLayerParams; // hold params for the current gerber layer
104 
105 public:
106  DLIST<GERBER_DRAW_ITEM> m_Drawings; // linked list of Gerber Items to draw
107 
108  bool m_InUse; // true if this image is currently in use
109  // (a file is loaded in it)
110  bool m_IsVisible; // true if the draw layer is visible and must be drawn
111  // false if it must be not drawn
112  COLOR4D m_PositiveDrawColor; // The color used to draw positive items
113  wxString m_FileName; // Full File Name for this layer
114  wxString m_ImageName; // Image name, from IN <name>* command
115  bool m_IsX2_file; // true if a X2 gerber attribute was found in file
116  X2_ATTRIBUTE_FILEFUNCTION* m_FileFunction; // file function parameters, found in a %TF command
117  // or a G04
118  wxString m_MD5_value; // MD5 value found in a %TF.MD5 command
119  wxString m_PartString; // string found in a %TF.Part command
120  int m_GraphicLayer; // Graphic layer Number
121  bool m_ImageNegative; // true = Negative image
122  bool m_ImageJustifyXCenter; // Image Justify Center on X axis (default = false)
123  bool m_ImageJustifyYCenter; // Image Justify Center on Y axis (default = false)
124  wxPoint m_ImageJustifyOffset; // Image Justify Offset on XY axis (default = 0,0)
125  bool m_GerbMetric; // false = Inches, true = metric
126  bool m_Relative; // false = absolute Coord, true = relative Coord
127  bool m_NoTrailingZeros; // true: remove tailing zeros.
128  wxPoint m_ImageOffset; // Coord Offset, from IO command
129  wxSize m_FmtScale; // Fmt 2.3: m_FmtScale = 3, fmt 3.4: m_FmtScale = 4
130  wxSize m_FmtLen; // Nb chars per coord. ex fmt 2.3, m_FmtLen = 5
131  int m_ImageRotation; // Image rotation (0, 90, 180, 270 only) in degrees
132  double m_LocalRotation; // Local rotation, in degrees, added to m_ImageRotation
133  // Note this value is stored in 0.1 degrees
134  wxPoint m_Offset; // Coord Offset, from OF command
135  wxRealPoint m_Scale; // scale (X and Y) of layer.
136  bool m_SwapAxis; // false (default) if A = X and B = Y
137  // true if A = Y, B = X
138  bool m_MirrorA; // true: miror / axe A (X)
139  bool m_MirrorB; // true: miror / axe B (Y)
140  int m_Iterpolation; // Linear, 90 arc, Circ.
141  int m_Current_Tool; // Current Tool (Dcode) number selected
142  int m_Last_Pen_Command; // Current or last pen state (0..9, set by Dn option with n <10
143  int m_CommandState; // state of gerber analysis command.
144  int m_LineNum; // Line number of the gerber file while reading
145  wxPoint m_CurrentPos; // current specified coord for plot
146  wxPoint m_PreviousPos; // old current specified coord for plot
147  wxPoint m_IJPos; // IJ coord (for arcs & circles )
148 
149  FILE* m_Current_File; // Current file to read
150 
151  int m_Selected_Tool; // For hightlight: current selected Dcode
152  bool m_Has_DCode; // true = DCodes in file
153  // (false = no DCode -> separate DCode file
154  bool m_360Arc_enbl; // Enbl 360 deg circular interpolation
155  bool m_PolygonFillMode; // Enable polygon mode (read coord as a polygon descr)
156  int m_PolygonFillModeState; // In polygon mode: 0 = first segm, 1 = next segm
157 
159 
160  GBR_NETLIST_METADATA m_NetAttributeDict; // the net attributes set by a %TO.CN, %TO.C and/or %TO.N
161  // add object attribute command.
162  wxString m_AperFunction; // the aperture function set by a %TA.AperFunction, xxx
163  // (stores thre xxx value).
164 
165  std::map<wxString, int> m_ComponentsList; // list of components
166  std::map<wxString, int> m_NetnamesList; // list of net names
167 
168 private:
169  wxArrayString m_messagesList; // A list of messages created when reading a file
170  int m_hasNegativeItems; // true if the image is negative or has some negative items
171  // Used to optimize drawing, because when there are no
172  // negative items screen refresh does not need
173  // to build an intermediate bitmap specfic to this image
174  // -1 = negative items are
175  // 0 = no negative items found
176  // 1 = have negative items found
188  char* GetNextLine( char *aBuff, char* aText, FILE* aFile );
189 
190  bool GetEndOfBlock( char* buff, char*& text, FILE* gerber_file );
191 
192 public:
193  GERBER_FILE_IMAGE( int layer );
194  virtual ~GERBER_FILE_IMAGE();
195 
196  wxString GetClass() const override
197  {
198  return wxT( "GERBER_FILE_IMAGE" );
199  }
200 
202 
210  bool LoadGerberFile( const wxString& aFullFileName );
211 
212  const wxArrayString& GetMessages() const { return m_messagesList; }
213 
217  int GetDcodesCount();
218 
219  virtual void ResetDefaultValues();
220 
222 
228 
234  {
235  return m_GBRLayerParams;
236  }
237 
244  bool HasNegativeItems();
245 
252  {
253  m_messagesList.Clear();
254  }
255 
260  void AddMessageToList( const wxString& aMessage )
261  {
262  m_messagesList.Add( aMessage );
263  }
264 
268  void InitToolTable();
269 
274  wxPoint ReadXYCoord( char*& Text );
275 
282  wxPoint ReadIJCoord( char*& Text );
283 
284  // functions to read G commands or D commands:
285  int GCodeNumber( char*& Text );
286  int DCodeNumber( char*& Text );
287 
288  // functions to execute G commands or D commands:
289  bool Execute_G_Command( char*& text, int G_command );
290  bool Execute_DCODE_Command( char*& text, int D_command );
291 
296  bool ReadRS274XCommand( char *aBuff, char* & text );
297 
302  bool ExecuteRS274XCommand( int command, char* aBuff,
303  char*& text );
304 
305 
316  bool ReadApertureMacro( char *aBuff, char* & text,
317  FILE * gerber_file );
318 
319 
330  D_CODE* GetDCODEOrCreate( int aDCODE, bool aCreateIfNoExist = true );
331 
340  D_CODE* GetDCODE( int aDCODE ) const;
341 
350 
359  void StepAndRepeatItem( const GERBER_DRAW_ITEM& aItem );
360 
368  void DisplayImageInfo( GERBVIEW_FRAME* aMainFrame );
369 
380  void RemoveAttribute( X2_ATTRIBUTE& aAttribute );
381 
383  SEARCH_RESULT Visit( INSPECTOR inspector, void* testData, const KICAD_T scanTypes[] ) override;
384 
385 #if defined(DEBUG)
386 
387  void Show( int nestLevel, std::ostream& os ) const override { ShowDummy( os ); }
388 
389 #endif
390 };
391 
392 #endif // ifndef CLASS_GERBER_FILE_IMAGE_H
COLOR4D GetPositiveDrawColor() const
APERTURE_MACRO_SET m_aperture_macros
a collection of APERTURE_MACROS, sorted by name
class X2_ATTRIBUTE_FILEFUNCTION ( from TF.FileFunction in Gerber file) Example file function: TF...
void AddMessageToList(const wxString &aMessage)
Function AddMessageToList Add a message to the message list.
X2_ATTRIBUTE_FILEFUNCTION * m_FileFunction
bool HasNegativeItems()
Function HasNegativeItems.
class X2_ATTRIBUTE The attribute value consists of a number of substrings separated by a comma ...
bool GetEndOfBlock(char *buff, char *&text, FILE *gerber_file)
Definition: rs274x.cpp:907
bool Execute_DCODE_Command(char *&text, int D_command)
Definition: rs274d.cpp:584
D_CODE * GetDCODE(int aDCODE) const
Function GetDCODE returns a pointer to the D_CODE within this GERBER for the given aDCODE...
wxString GetClass() const override
Function GetClass returns the class name.
wxPoint ReadXYCoord(char *&Text)
Function ReadXYCoord Returns the current coordinate type pointed to by XnnYnn Text (XnnnnYmmmm) ...
wxPoint ReadIJCoord(char *&Text)
Function ReadIJCoord Returns the current coordinate type pointed to by InnJnn Text (InnnnJmmmm) These...
char * GetNextLine(char *aBuff, char *aText, FILE *aFile)
Function GetNextLine test for an end of line if an end of line is found: read a new line...
Definition: rs274x.cpp:933
wxRealPoint m_StepForRepeat
Class GERBER_FILE_IMAGE holds the Image data and parameters for one gerber file and layer parameters ...
bool ReadApertureMacro(char *aBuff, char *&text, FILE *gerber_file)
Function ReadApertureMacro reads in an aperture macro and saves it in m_aperture_macros.
Definition: rs274x.cpp:961
void ClearMessageList()
Function ClearMessageList Clear the message list Call it before reading a Gerber file.
this class handle info which can be added in a gerber file as attribute of an obtect the GBR_INFO_TYP...
SEARCH_RESULT Visit(INSPECTOR inspector, void *testData, const KICAD_T scanTypes[]) override
>
KICAD_T
Enum KICAD_T is the set of class identification values, stored in EDA_ITEM::m_StructType.
Definition: typeinfo.h:90
D_CODE * m_Aperture_List[TOOLS_MAX_COUNT]
Dcode (Aperture) List for this layer (max 999)
int GCodeNumber(char *&Text)
Definition: rs274d.cpp:415
bool ExecuteRS274XCommand(int command, char *aBuff, char *&text)
Function ExecuteRS274XCommand executes 1 command.
Definition: rs274x.cpp:244
const INSPECTOR_FUNC & INSPECTOR
Definition: base_struct.h:118
void DisplayImageInfo(GERBVIEW_FRAME *aMainFrame)
Function DisplayImageInfo has knowledge about the frame and how and where to put status information a...
int DCodeNumber(char *&Text)
Definition: rs274d.cpp:438
bool LoadGerberFile(const wxString &aFullFileName)
Read and load a gerber file.
Definition: readgerb.cpp:104
const wxArrayString & GetMessages() const
APERTURE_MACRO * FindApertureMacro(const APERTURE_MACRO &aLookup)
Function FindApertureMacro looks up a previously read in aperture macro.
std::map< wxString, int > m_NetnamesList
void RemoveAttribute(X2_ATTRIBUTE &aAttribute)
Function RemoveAttribute.
bool m_Exposure
whether an aperture macro tool is flashed on or off
bool Execute_G_Command(char *&text, int G_command)
Definition: rs274d.cpp:458
Class D_CODE holds a gerber DCODE (also called Aperture) definition.
Definition: dcode.h:82
DLIST< GERBER_DRAW_ITEM > m_Drawings
GERBER_DRAW_ITEM * GetItemsList()
Function GetItemsList.
GBR_NETLIST_METADATA m_NetAttributeDict
#define TOOLS_MAX_COUNT
Definition: dcode.h:73
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:165
void Clear_GERBER_FILE_IMAGE()
Struct APERTURE_MACRO helps support the "aperture macro" defined within standard RS274X.
void InitToolTable()
Function InitToolTable.
SEARCH_RESULT
Definition: base_struct.h:82
std::set< APERTURE_MACRO, APERTURE_MACRO_less_than > APERTURE_MACRO_SET
Type APERTURE_MACRO_SET is a sorted collection of APERTURE_MACROS whose key is the name field in the ...
void StepAndRepeatItem(const GERBER_DRAW_ITEM &aItem)
Function StepAndRepeatItem Gerber format has a command Step an Repeat This function must be called wh...
std::map< wxString, int > m_ComponentsList
D_CODE * GetDCODEOrCreate(int aDCODE, bool aCreateIfNoExist=true)
Function GetDCODEOrCreate returns a pointer to the D_CODE within this GERBER for the given aDCODE...
GERBER_LAYER & GetLayerParams()
Function GetLayerParams.
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39
bool ReadRS274XCommand(char *aBuff, char *&text)
Function ReadRS274XCommand reads a single RS274X command terminated with a %.
Definition: rs274x.cpp:191