KiCad PCB EDA Suite
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-2019 Jean-Pierre Charras jp.charras at wanadoo.fr
5  * Copyright (C) 1992-2021 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 GERBER_FILE_IMAGE_H
26 #define GERBER_FILE_IMAGE_H
27 
28 #include <vector>
29 #include <set>
30 
31 #include <dcode.h>
32 #include <gerber_draw_item.h>
33 #include <am_primitive.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 typedef std::vector<GERBER_DRAW_ITEM*> GERBER_DRAW_ITEMS;
41 
42 class GERBVIEW_FRAME;
43 class D_CODE;
44 
45 /* Gerber files have different parameters to define units and how items must be plotted.
46  * some are for the entire file, and other can change along a file.
47  * In Gerber world:
48  * an image is the entire gerber file and its "global" parameters
49  * a layer (that is very different from a board layer) is just a sub set of a file that
50  * have specific parameters
51  * if a Image parameter is set more than once, only the last value is used
52  * Some parameters can change along a file and are not layer specific: they are stored
53  * in GERBER_ITEM items, when instanced.
54  *
55  * In GerbView, to handle these parameters, there are 2 classes:
56  * GERBER_FILE_IMAGE : the main class containing most of parameters and data to plot a
57  * graphic layer
58  * Some of them can change along the file
59  * There is one GERBER_FILE_IMAGE per file and one graphic layer per file or GERBER_FILE_IMAGE
60  * GerbView does not read and merge 2 gerber file in one graphic layer:
61  * I believe this is not possible due to the constraints in Image parameters.
62  * GERBER_LAYER : containing the subset of parameters that is layer specific
63  * A GERBER_FILE_IMAGE must include one GERBER_LAYER to define all parameters to plot a file.
64  * But a GERBER_FILE_IMAGE can use more than one GERBER_LAYER.
65  */
66 
67 class GERBER_FILE_IMAGE;
68 class X2_ATTRIBUTE;
70 
71 // For arcs, coordinates need 3 info: start point, end point and center or radius
72 // In Excellon files it can be a A## value (radius) or I#J# center coordinates (like in gerber)
73 // We need to know the last read type when reading a list of routing coordinates
75 {
77  ARC_INFO_TYPE_CENTER, // last info is a IJ command: arc center is given
78  ARC_INFO_TYPE_RADIUS, // last info is a A command: arc radius is given
79 };
80 
82 {
83 public:
84  GERBER_LAYER();
85  ~GERBER_LAYER();
86 
87 private:
88  void ResetDefaultValues();
89  friend class GERBER_FILE_IMAGE;
90 
91 public:
92  // These parameters are layer specific:
93  wxString m_LayerName; // Layer name, from LN <name>* command
94  bool m_LayerNegative; // true = Negative Layer: command LP
95  wxRealPoint m_StepForRepeat; // X and Y offsets for Step and Repeat command
96  int m_XRepeatCount; // The repeat count on X axis
97  int m_YRepeatCount; // The repeat count on Y axis
98  bool m_StepForRepeatMetric; // false = Inches, true = metric
99  // needed here because repeated
100  // gerber items can have coordinates
101  // in different units than step parameters
102  // and the actual coordinates calculation must handle this
103 };
104 
111 {
112 public:
113  GERBER_FILE_IMAGE( int layer );
114  virtual ~GERBER_FILE_IMAGE();
115 
116  wxString GetClass() const override
117  {
118  return wxT( "GERBER_FILE_IMAGE" );
119  }
120 
129  bool LoadGerberFile( const wxString& aFullFileName );
130 
131  const wxArrayString& GetMessages() const { return m_messagesList; }
132 
136  int GetDcodesCount();
137 
141  virtual void ResetDefaultValues();
142 
144 
149 
153  int GetItemsCount() { return m_drawings.size(); }
154 
161  {
162  m_drawings.push_back( aItem );
163  }
164 
169  {
170  return m_drawings.back();
171  }
172 
177  {
178  return m_GBRLayerParams;
179  }
180 
185  bool HasNegativeItems();
186 
193  {
194  m_messagesList.Clear();
195  }
196 
200  void AddMessageToList( const wxString& aMessage )
201  {
202  m_messagesList.Add( aMessage );
203  }
204 
205  void InitToolTable();
206 
215  wxPoint ReadXYCoord( char*& aText, bool aExcellonMode = false );
216 
223  wxPoint ReadIJCoord( char*& Text );
224 
225  // functions to read G commands or D commands:
226  int GCodeNumber( char*& Text );
227  int DCodeNumber( char*& Text );
228 
238  D_CODE* GetDCODEOrCreate( int aDCODE, bool aCreateIfNoExist = true );
239 
247  D_CODE* GetDCODE( int aDCODE ) const;
248 
256 
266  void StepAndRepeatItem( const GERBER_DRAW_ITEM& aItem );
267 
273  void DisplayImageInfo( GERBVIEW_FRAME* aMainFrame );
274 
286  void RemoveAttribute( X2_ATTRIBUTE& aAttribute );
287 
289  SEARCH_RESULT Visit( INSPECTOR inspector, void* testData, const KICAD_T scanTypes[] ) override;
290 
291 #if defined(DEBUG)
292 
293  void Show( int nestLevel, std::ostream& os ) const override { ShowDummy( os ); }
294 
295 #endif
296 
297 private:
310  char* GetNextLine( char *aBuff, unsigned int aBuffSize, char* aText, FILE* aFile );
311 
312  bool GetEndOfBlock( char* aBuff, unsigned int aBuffSize, char*& aText, FILE* aGerberFile );
313 
317  bool ReadRS274XCommand( char *aBuff, unsigned int aBuffSize, char*& aText );
318 
322  bool ExecuteRS274XCommand( int aCommand, char* aBuff, unsigned int aBuffSize, char*& aText );
323 
333  int ReadXCommandID( char*& text );
334 
346  bool ReadApertureMacro( char *aBuff, unsigned int aBuffSize,
347  char* & text, FILE * gerber_file );
348 
349  // functions to execute G commands or D basic commands:
350  bool Execute_G_Command( char*& text, int G_command );
351  bool Execute_DCODE_Command( char*& text, int D_command );
352 
353 public:
354  bool m_InUse; // true if this image is currently in use
355  // (a file is loaded in it)
356 
359  // false if it must be not drawn
360  COLOR4D m_PositiveDrawColor; // The color used to draw positive items
361  wxString m_FileName; // Full File Name for this layer
362  wxString m_ImageName; // Image name, from IN <name>* command
363 
366  X2_ATTRIBUTE_FILEFUNCTION* m_FileFunction; // file function parameters, found in a
367  // %TF command or a G04
368  wxString m_MD5_value; // MD5 value found in a %TF.MD5 command
369  wxString m_PartString; // string found in a %TF.Part command
370  int m_GraphicLayer; // Graphic layer Number
371  bool m_ImageNegative; // true = Negative image
372 
375 
378 
381  bool m_GerbMetric; // false = Inches, true = metric
382 
385  bool m_NoTrailingZeros; // true: remove tailing zeros.
386  wxPoint m_ImageOffset; // Coord Offset, from IO command
387 
389  wxSize m_FmtScale;
390 
392  wxSize m_FmtLen;
393 
396 
400  wxPoint m_Offset; // Coord Offset, from OF command
401  wxRealPoint m_Scale; // scale (X and Y) of layer.
402  bool m_SwapAxis; // false (default) if A = X and B = Y
403  // true if A = Y, B = X
404  bool m_MirrorA; // true: mirror / axis A (X)
405  bool m_MirrorB; // true: mirror / axis B (Y)
406  int m_Iterpolation; // Linear, 90 arc, Circ.
407  int m_Current_Tool; // Current Tool (Dcode) number selected
408 
411  int m_CommandState; // state of gerber analysis command.
412 
415  wxPoint m_CurrentPos; // current specified coord for plot
416  wxPoint m_PreviousPos; // old current specified coord for plot
417  wxPoint m_IJPos; // IJ coord (for arcs & circles )
418 
421 
424 
427  FILE* m_Current_File; // Current file to read
428 
429  int m_Selected_Tool; // For highlight: current selected Dcode
430 
433 
434  // true = some DCodes in file are not defined (broken file or deprecated RS274D file).
436  bool m_360Arc_enbl; // Enable 360 deg circular interpolation
437 
438  // Set to true when a circular interpolation command type is found. Mandatory before
439  // drawing an arc.
441 
442  // Enable polygon mode (read coord as a polygon descr)
444 
445  // In polygon mode: 0 = first segm, 1 = next segm
447 
450 
451  // the net attributes set by a %TO.CN, %TO.C and/or %TO.N add object attribute command.
453 
454  // the aperture function set by a %TA.AperFunction, xxx (stores the xxx value).
455  wxString m_AperFunction;
456 
457  std::map<wxString, int> m_ComponentsList; // list of components
458  std::map<wxString, int> m_NetnamesList; // list of net names
459 
462 
465 
466  GERBER_LAYER m_GBRLayerParams; // hold params for the current gerber layer
467  GERBER_DRAW_ITEMS m_drawings; // linked list of Gerber Items to draw
468 
469 private:
470  wxArrayString m_messagesList; // A list of messages created when reading a file
471 
483 };
484 
485 #endif // ifndef GERBER_FILE_IMAGE_H
APERTURE_MACRO_SET m_aperture_macros
X2_ATTRIBUTE_FILEFUNCTION ( from TF.FileFunction in Gerber file) Example file function: TF....
int m_LineNum
< Line number of the gerber file while reading.
void AddMessageToList(const wxString &aMessage)
Add a message to the message list.
wxString m_LayerName
X2_ATTRIBUTE_FILEFUNCTION * m_FileFunction
The attribute value consists of a number of substrings separated by a comma.
bool Execute_DCODE_Command(char *&text, int D_command)
Definition: rs274d.cpp:579
wxString GetClass() const override
Return the class name.
GERBER_DRAW_ITEMS & GetItems()
int m_Last_Pen_Command
< Current or last pen state (0..9, set by Dn option with n < 10.
wxPoint ReadIJCoord(char *&Text)
Return the current coordinate type pointed to by InnJnn Text (InnnnJmmmm)
bool m_ImageJustifyXCenter
< Image Justify Center on X axis (default = false).
bool m_LastCoordIsIJPos
< True if a IJ coord was read (for arcs & circles ).
wxRealPoint m_StepForRepeat
bool m_ImageJustifyYCenter
Image Justify Offset on XY axis (default = 0,0).
GERBER_FILE_IMAGE(int layer)
D_CODE * GetDCODE(int aDCODE) const
Return a pointer to the D_CODE within this GERBER for the given aDCODE.
Hold the image data and parameters for one gerber file and layer parameters.
COLOR4D GetPositiveDrawColor() const
wxSize m_FmtLen
Image rotation (0, 90, 180, 270 only) in degrees.
bool m_IsVisible
< True if the draw layer is visible and must be drawn.
void ClearMessageList()
Clear the message list.
Information which can be added in a gerber file as attribute of an object.
SEARCH_RESULT Visit(INSPECTOR inspector, void *testData, const KICAD_T scanTypes[]) override
May be re-implemented for each derived class in order to handle all the types given by its member dat...
wxSize m_FmtScale
< Fmt 2.3: m_FmtScale = 3, fmt 3.4: m_FmtScale = 4.
wxArrayString m_messagesList
int ReadXCommandID(char *&text)
Read two bytes of data and assembles them into an int with the first byte in the sequence put into th...
Definition: rs274x.cpp:115
const INSPECTOR_FUNC & INSPECTOR
Definition: eda_item.h:94
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:77
D_CODE * m_Aperture_List[TOOLS_MAX_COUNT]
< Dcode (Aperture) List for this layer (max TOOLS_MAX_COUNT: see dcode.h)
int m_ArcRadius
Identifier for arc data type (IJ (center) or A## (radius)).
int m_PolygonFillModeState
a collection of APERTURE_MACROS, sorted by name
int GCodeNumber(char *&Text)
Definition: rs274d.cpp:399
bool m_IsX2_file
< True if a X2 gerber attribute was found in file.
bool ExecuteRS274XCommand(int aCommand, char *aBuff, unsigned int aBuffSize, char *&aText)
Execute a RS274X command.
Definition: rs274x.cpp:198
GERBER_DRAW_ITEMS m_drawings
GERBER_DRAW_ITEM * GetLastItemInList() const
char * GetNextLine(char *aBuff, unsigned int aBuffSize, char *aText, FILE *aFile)
Test for an end of line.
Definition: rs274x.cpp:935
void DisplayImageInfo(GERBVIEW_FRAME *aMainFrame)
Display information about image parameters in the status bar.
void AddItemToList(GERBER_DRAW_ITEM *aItem)
Add a new GERBER_DRAW_ITEM item to the drawings list.
GERBER_LAYER m_GBRLayerParams
int DCodeNumber(char *&Text)
Definition: rs274d.cpp:422
const wxArrayString & GetMessages() const
bool LoadGerberFile(const wxString &aFullFileName)
Read and load a gerber file.
Definition: readgerb.cpp:120
std::vector< GERBER_DRAW_ITEM * > GERBER_DRAW_ITEMS
APERTURE_MACRO * FindApertureMacro(const APERTURE_MACRO &aLookup)
Look up a previously read in aperture macro.
bool m_Relative
< false = absolute Coord, true = relative Coord.
bool m_StepForRepeatMetric
wxPoint ReadXYCoord(char *&aText, bool aExcellonMode=false)
Return the current coordinate type pointed to by XnnYnn Text (XnnnnYmmmm).
bool ReadApertureMacro(char *aBuff, unsigned int aBuffSize, char *&text, FILE *gerber_file)
Read in an aperture macro and saves it in m_aperture_macros.
Definition: rs274x.cpp:963
std::map< wxString, int > m_NetnamesList
std::set< APERTURE_MACRO, APERTURE_MACRO_less_than > APERTURE_MACRO_SET
A sorted collection of APERTURE_MACROS whose key is the name field in the APERTURE_MACRO.
Definition: am_primitive.h:265
void RemoveAttribute(X2_ATTRIBUTE &aAttribute)
Called when a TD command is found the Gerber file.
bool m_Has_DCode
< True if has DCodes in file or false if no DCodes found. Perhaps deprecated RS274D file.
int m_hasNegativeItems
True if the image is negative or has some negative items.
bool Execute_G_Command(char *&text, int G_command)
Definition: rs274d.cpp:443
LAST_EXTRA_ARC_DATA_TYPE m_LastArcDataType
A gerber DCODE (also called Aperture) definition.
Definition: dcode.h:80
GBR_NETLIST_METADATA m_NetAttributeDict
bool ReadRS274XCommand(char *aBuff, unsigned int aBuffSize, char *&aText)
Read a single RS274X command terminated with a %.
Definition: rs274x.cpp:143
bool GetEndOfBlock(char *aBuff, unsigned int aBuffSize, char *&aText, FILE *aGerberFile)
Definition: rs274x.cpp:909
#define TOOLS_MAX_COUNT
Definition: dcode.h:72
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:100
Support the "aperture macro" defined within standard RS274X.
Definition: am_primitive.h:167
SEARCH_RESULT
Definition: eda_item.h:41
void StepAndRepeatItem(const GERBER_DRAW_ITEM &aItem)
Gerber format has a command Step an Repeat.
std::map< wxString, int > m_ComponentsList
D_CODE * GetDCODEOrCreate(int aDCODE, bool aCreateIfNoExist=true)
Return a pointer to the D_CODE within this GERBER for the given aDCODE.
GERBER_LAYER & GetLayerParams()
int m_ImageRotation
Local rotation in degrees added to m_ImageRotation.
virtual void ResetDefaultValues()
Set all parameters to a default value, before reading a file.
LAST_EXTRA_ARC_DATA_TYPE
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:103