KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 The 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, see <https://www.gnu.org/licenses/>.
19 */
20
21#ifndef GERBER_FILE_IMAGE_H
22#define GERBER_FILE_IMAGE_H
23
24#include <vector>
25#include <set>
26
27#include <dcode.h>
28#include <gerber_draw_item.h>
29#include <am_primitive.h>
30#include <aperture_macro.h>
32
33typedef std::vector<GERBER_DRAW_ITEM*> GERBER_DRAW_ITEMS;
34
35class GERBVIEW_FRAME;
36class D_CODE;
37
38/* Gerber files have different parameters to define units and how items must be plotted.
39 * some are for the entire file, and other can change along a file.
40 * In Gerber world:
41 * an image is the entire gerber file and its "global" parameters
42 * a layer (that is very different from a board layer) is just a sub set of a file that
43 * have specific parameters
44 * if a Image parameter is set more than once, only the last value is used
45 * Some parameters can change along a file and are not layer specific: they are stored
46 * in GERBER_ITEM items, when instanced.
47 *
48 * In GerbView, to handle these parameters, there are 2 classes:
49 * GERBER_FILE_IMAGE : the main class containing most of parameters and data to plot a
50 * graphic layer
51 * Some of them can change along the file
52 * There is one GERBER_FILE_IMAGE per file and one graphic layer per file or GERBER_FILE_IMAGE
53 * GerbView does not read and merge 2 gerber file in one graphic layer:
54 * I believe this is not possible due to the constraints in Image parameters.
55 * GERBER_LAYER : containing the subset of parameters that is layer specific
56 * A GERBER_FILE_IMAGE must include one GERBER_LAYER to define all parameters to plot a file.
57 * But a GERBER_FILE_IMAGE can use more than one GERBER_LAYER.
58 */
59
61class X2_ATTRIBUTE;
63
64// For arcs, coordinates need 3 info: start point, end point and center or radius
65// In Excellon files it can be a A## value (radius) or I#J# center coordinates (like in gerber)
66// We need to know the last read type when reading a list of routing coordinates
68{
70 ARC_INFO_TYPE_CENTER, // last info is a IJ command: arc center is given
71 ARC_INFO_TYPE_RADIUS, // last info is a A command: arc radius is given
72};
73
75{
76public:
79
80private:
81 void ResetDefaultValues();
82 friend class GERBER_FILE_IMAGE;
83
84public:
85 // These parameters are layer specific:
86 wxString m_LayerName; // Layer name, from LN <name>* command
87 bool m_LayerNegative; // true = Negative Layer: command LP
88 wxRealPoint m_StepForRepeat; // X and Y offsets for Step and Repeat command
89 int m_XRepeatCount; // The repeat count on X axis
90 int m_YRepeatCount; // The repeat count on Y axis
91 bool m_StepForRepeatMetric; // false = Inches, true = metric
92 // needed here because repeated
93 // gerber items can have coordinates
94 // in different units than step parameters
95 // and the actual coordinates calculation must handle this
96};
97
98// Currently, the Gerber file is parsed line by line.
99// This is most of time OK
100// GERBER_BUFZ is the max size of a single line of text from a gerber file.
101// But warning: in rare cases some files can have *very long* lines,
102// so the buffer must be very large.
103// I saw a file using only one line of 1,400,000 chars
104#define GERBER_BUFZ 5000000
105
112{
113public:
114 GERBER_FILE_IMAGE( int layer );
115 virtual ~GERBER_FILE_IMAGE();
116
117 wxString GetClass() const override { return wxT( "GERBER_FILE_IMAGE" ); }
118
127 static bool TestFileIsRS274( const wxString& aFullFileName );
128
137 bool LoadGerberFile( const wxString& aFullFileName );
138
139 const wxArrayString& GetMessages() const { return m_messagesList; }
140
144 int GetDcodesCount();
145
149 virtual void ResetDefaultValues();
150
152
157
161 const GERBER_DRAW_ITEMS& GetItems() const { return m_drawings; }
162
166 int GetItemsCount() const { return m_drawings.size(); }
167
173 void AddItemToList( GERBER_DRAW_ITEM* aItem ) { m_drawings.push_back( aItem ); }
174
179
184
189 bool HasNegativeItems();
190
197
201 void AddMessageToList( const wxString& aMessage );
202
211 VECTOR2I ReadXYCoord( char*& aText, bool aExcellonMode = false );
212
219 VECTOR2I ReadIJCoord( char*& Text );
220
226 int CodeNumber( char*& aText );
227
237 D_CODE* GetDCODEOrCreate( int aDCODE, bool aCreateIfNoExist = true );
238
246 D_CODE* GetDCODE( int aDCODE ) const;
247
255
269 void StepAndRepeatItem( const GERBER_DRAW_ITEM& aItem );
270
279
285 void DisplayImageInfo( GERBVIEW_FRAME* aMainFrame );
286
295 double CalculateCopperArea();
296
304 void SetDrawOffetAndRotation( VECTOR2D aOffsetMM, EDA_ANGLE aRotation );
305
317 void RemoveAttribute( X2_ATTRIBUTE& aAttribute );
318
320 INSPECT_RESULT Visit( INSPECTOR aInspector, void* aTestData, const std::vector<KICAD_T>& aScanTypes ) override;
321
322#if defined( DEBUG )
323
324 void Show( int nestLevel, std::ostream& os ) const override { ShowDummy( os ); }
325
326#endif
327
328private:
341 char* GetNextLine( char* aBuff, unsigned int aBuffSize, char* aText, FILE* aFile );
342
343 bool GetEndOfBlock( char* aBuff, unsigned int aBuffSize, char*& aText, FILE* aGerberFile );
344
348 bool ReadRS274XCommand( char* aBuff, unsigned int aBuffSize, char*& aText );
349
353 bool ExecuteRS274XCommand( int aCommand, char* aBuff, unsigned int aBuffSize, char*& aText );
354
364 int ReadXCommandID( char*& text );
365
377 bool ReadApertureMacro( char* aBuff, unsigned int aBuffSize, char*& text, FILE* gerber_file );
378
379 // functions to execute G commands or D basic commands:
380 bool Execute_G_Command( char*& text, int G_command );
381 bool Execute_DCODE_Command( char*& text, int D_command );
382
383public:
384 bool m_InUse;
388 wxString m_FileName;
390 wxString m_ImageName;
391
395 wxString m_MD5_value;
396 wxString m_PartString;
399
403
408 wxSize m_FmtScale;
409 wxSize m_FmtLen;
410
414
430
433
436
439 FILE* m_Current_File; // Current file to read
440
441 int m_Selected_Tool; // For highlight: current selected Dcode
442
445
446 // true = some DCodes in file are not defined (broken file or deprecated RS274D file).
448 bool m_360Arc_enbl; // Enable 360 deg circular interpolation
449
450 // Set to true when a circular interpolation command type is found. Mandatory before
451 // drawing an arc.
453
454 // Enable polygon mode (read coord as a polygon descr)
456
457 // In polygon mode: 0 = first segm, 1 = next segm
459
462
463 // the net attributes set by a %TO.CN, %TO.C and/or %TO.N add object attribute command.
465
466 // the aperture function set by a %TA.AperFunction, xxx (stores the xxx value).
468
469 std::map<wxString, int> m_ComponentsList; // list of components
470 std::map<wxString, int> m_NetnamesList; // list of net names
471
473 std::map<int, D_CODE*> m_ApertureList;
474
477
478 GERBER_LAYER m_GBRLayerParams; // hold params for the current gerber layer
479 GERBER_DRAW_ITEMS m_drawings; // linked list of Gerber Items to draw
480
481 bool m_SRBlockCollecting; // true when inside an SR block with
482 // repeat count > 1; items are collected
483 // and replicated as a whole block
484 int m_SRBlockStartIdx; // index into m_drawings where the
485 // current SR block begins
486
491
492 // A large buffer to store one line
493 static char m_LineBuffer[GERBER_BUFZ + 1];
494
495private:
496 wxArrayString m_messagesList; // A list of messages created when reading a file
497
509};
510
511#endif // ifndef GERBER_FILE_IMAGE_H
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.
Support the "aperture macro" defined within standard RS274X.
A gerber DCODE (also called Aperture) definition.
Definition dcode.h:76
EDA_ITEM(EDA_ITEM *parent, KICAD_T idType, bool isSCH_ITEM=false, bool isBOARD_ITEM=false)
Definition eda_item.cpp:84
Information which can be added in a gerber file as attribute of an object.
Hold the image data and parameters for one gerber file and layer parameters.
bool Execute_G_Command(char *&text, int G_command)
Definition rs274d.cpp:419
bool LoadGerberFile(const wxString &aFullFileName)
Read and load a gerber file.
Definition readgerb.cpp:236
void SetDrawOffetAndRotation(VECTOR2D aOffsetMM, EDA_ANGLE aRotation)
Set the offset and rotation to draw a file image Does not change any coordinate od draw items.
int m_Last_Pen_Command
Current or last pen state (0..9, set by Dn option with n < 10.
GERBER_DRAW_ITEM * GetLastItemInList() const
APERTURE_MACRO * FindApertureMacro(const APERTURE_MACRO &aLookup)
Look up a previously read in aperture macro.
VECTOR2I ReadIJCoord(char *&Text)
Return the current coordinate type pointed to by InnJnn Text (InnnnJmmmm)
X2_ATTRIBUTE_FILEFUNCTION * m_FileFunction
file function parameters, found in a %TF command or a G04
void RemoveAttribute(X2_ATTRIBUTE &aAttribute)
Called when a TD command is found the Gerber file.
static char m_LineBuffer[GERBER_BUFZ+1]
int m_hasNegativeItems
True if the image is negative or has some negative items.
virtual void ResetDefaultValues()
Set all parameters to a default value, before reading a file.
const wxArrayString & GetMessages() const
bool m_SwapAxis
false if A = X and B = Y (default); true if A = Y, B = X
void ClearMessageList()
Clear the message list.
double m_LocalRotation
Local rotation added to m_ImageRotation.
const GERBER_DRAW_ITEMS & GetItems() const
VECTOR2I m_Offset
Coord Offset, from OF command.
wxSize m_FmtScale
Fmt 2.3: m_FmtScale = 3, fmt 3.4: m_FmtScale = 4.
int m_ArcRadius
A value ( = radius in circular routing in Excellon files ).
wxString m_FileName
Full File Name for this layer.
void StepAndRepeatItem(const GERBER_DRAW_ITEM &aItem)
Gerber format has a command Step an Repeat.
bool m_ImageJustifyXCenter
Image Justify Center on X axis (default = false)
void FinishStepAndRepeatBlock()
Called when an SR block is closed (SR*%).
bool m_ImageJustifyYCenter
Image Justify Center on Y axis (default = false)
VECTOR2I m_DisplayOffset
Parameters used only to draw (display) items on this layer.
VECTOR2I m_PreviousPos
old current specified coord for plot
bool m_InUse
true if this image is currently in use (a file is loaded in it) false if it must be not drawn
bool m_ImageNegative
true = Negative image
void AddMessageToList(const wxString &aMessage)
Add a message to the message list.
LAST_EXTRA_ARC_DATA_TYPE m_LastArcDataType
Identifier for arc data type (IJ (center) or A## (radius)).
bool m_Exposure
Whether an aperture macro tool is flashed on or off.
wxArrayString m_messagesList
COLOR4D GetPositiveDrawColor() const
int m_LineNum
Line number of the gerber file while reading.
COLOR4D m_PositiveDrawColor
The color used to draw positive items.
std::map< wxString, int > m_ComponentsList
std::map< int, D_CODE * > m_ApertureList
Dcode (Aperture) List for this layer (see dcode.h)
VECTOR2I m_IJPos
IJ coord (for arcs & circles )
wxString m_ImageName
Image name, from IN <name>* command.
bool m_Relative
false = absolute Coord, true = relative Coord.
bool Execute_DCODE_Command(char *&text, int D_command)
Definition rs274d.cpp:551
wxString m_MD5_value
MD5 value found in a %TF.MD5 command.
char * GetNextLine(char *aBuff, unsigned int aBuffSize, char *aText, FILE *aFile)
Test for an end of line.
Definition rs274x.cpp:968
static bool TestFileIsRS274(const wxString &aFullFileName)
Performs a heuristics-based check of whether the file is an RS274 gerber file.
Definition readgerb.cpp:143
bool m_MirrorB
true: mirror / axis B (Y)
wxString m_PartString
string found in a %TF.Part command
D_CODE * GetDCODE(int aDCODE) const
Return a pointer to the D_CODE within this GERBER for the given aDCODE.
bool ReadRS274XCommand(char *aBuff, unsigned int aBuffSize, char *&aText)
Read a single RS274X command terminated with a %.
Definition rs274x.cpp:141
double CalculateCopperArea()
Calculate the total copper area in the image.
bool m_IsX2_file
True if a X2 gerber attribute was found in file.
int m_ImageRotation
Image rotation (0, 90, 180, 270 only) in degrees.
VECTOR2I m_ImageOffset
Coord Offset, from IO command.
bool m_Has_DCode
True if has DCodes in file or false if no DCodes found. Perhaps deprecated RS274D file.
bool GetEndOfBlock(char *aBuff, unsigned int aBuffSize, char *&aText, FILE *aGerberFile)
Definition rs274x.cpp:942
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:995
int m_Current_Tool
Current Tool (Dcode) number selected.
D_CODE * GetDCODEOrCreate(int aDCODE, bool aCreateIfNoExist=true)
Return a pointer to the D_CODE within this GERBER for the given aDCODE.
void AddItemToList(GERBER_DRAW_ITEM *aItem)
Add a new GERBER_DRAW_ITEM item to the drawings list.
VECTOR2I ReadXYCoord(char *&aText, bool aExcellonMode=false)
Return the current coordinate type pointed to by XnnYnn Text (XnnnnYmmmm).
bool ExecuteRS274XCommand(int aCommand, char *aBuff, unsigned int aBuffSize, char *&aText)
Execute a RS274X command.
Definition rs274x.cpp:197
wxSize m_FmtLen
Nb chars per coord. ex fmt 2.3, m_FmtLen = 5.
VECTOR2D m_Scale
scale (X and Y) of layer.
std::map< wxString, int > m_NetnamesList
wxString GetClass() const override
Return the class name.
int m_GraphicLayer
Graphic layer Number.
APERTURE_MACRO_SET m_aperture_macros
a collection of APERTURE_MACROS, sorted by name
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:113
GERBER_DRAW_ITEMS m_drawings
bool m_GerbMetric
false = Inches, true = metric
GBR_NETLIST_METADATA m_NetAttributeDict
int m_CommandState
state of gerber analysis command
wxString m_ArchiveFileName
Archive containing this layer, if loaded from a ZIP file.
bool m_NoTrailingZeros
true: remove tailing zeros.
int m_Iterpolation
Linear, 90 arc, Circ.
VECTOR2I m_CurrentPos
current specified coord for plot
bool m_MirrorA
true: mirror / axis A (X)
bool m_LastCoordIsIJPos
True if a IJ coord was read (for arcs & circles ).
GERBER_DRAW_ITEMS & GetItems()
VECTOR2I m_ImageJustifyOffset
Image Justify Offset on XY axis (default = 0,0)
int CodeNumber(char *&aText)
Reads the next number and returns the value.
Definition rs274d.cpp:401
GERBER_LAYER m_GBRLayerParams
GERBER_LAYER & GetLayerParams()
void DisplayImageInfo(GERBVIEW_FRAME *aMainFrame)
Display information about image parameters in the status bar.
INSPECT_RESULT Visit(INSPECTOR aInspector, void *aTestData, const std::vector< KICAD_T > &aScanTypes) override
May be re-implemented for each derived class in order to handle all the types given by its member dat...
bool HasNegativeItems()
Return true if at least one item must be drawn in background color used to optimize screen refresh.
friend class GERBER_FILE_IMAGE
wxRealPoint m_StepForRepeat
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
X2_ATTRIBUTE_FILEFUNCTION ( from TF.FileFunction in Gerber file) Example file function: TF....
The attribute value consists of a number of substrings separated by a comma.
INSPECT_RESULT
Definition eda_item.h:44
const INSPECTOR_FUNC & INSPECTOR
std::function passed to nested users by ref, avoids copying std::function.
Definition eda_item.h:91
LAST_EXTRA_ARC_DATA_TYPE
@ ARC_INFO_TYPE_NONE
@ ARC_INFO_TYPE_RADIUS
@ ARC_INFO_TYPE_CENTER
#define GERBER_BUFZ
std::vector< GERBER_DRAW_ITEM * > GERBER_DRAW_ITEMS
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708
VECTOR2< double > VECTOR2D
Definition vector2d.h:707