KiCad PCB EDA Suite
Loading...
Searching...
No Matches
gerber_file_image.cpp
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) 1992-2019 Jean-Pierre Charras jp.charras at wanadoo.fr
5 * Copyright (C) 1992-2023 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#include <gerbview.h>
26#include <gerbview_frame.h>
27#include <gerber_file_image.h>
28#include <macros.h>
30#include <algorithm>
31#include <map>
32#include <core/arraydim.h>
33
34
39extern int scaletoIU( double aCoord, bool isMetric ); // defined it rs274d_read_XY_and_IJ_coordinates.cpp
40
41/* Format Gerber: NOTES:
42 * Tools and D_CODES
43 * tool number (identification of shapes)
44 * 1 to 999
45 *
46 * D_CODES:
47 * D01 ... D9 = action codes:
48 * D01 = activating light (lower pen) when di placement
49 * D02 = light extinction (lift pen) when di placement
50 * D03 Flash
51 * D09 = VAPE Flash
52 * D10 ... = Identification Tool (Opening)
53 *
54 * For tools:
55 * DCode min = D10
56 * DCode max = 999
57 */
58
59
61{
63}
64
65
67{
68}
69
70
72{
73 m_LayerName = wxT( "no name" ); // Layer name from the LN command
74 m_LayerNegative = false; // true = Negative Layer
75 m_StepForRepeat.x = m_StepForRepeat.y = 0; // X and Y offsets for Step and Repeat command
76 m_XRepeatCount = 1; // The repeat count on X axis
77 m_YRepeatCount = 1; // The repeat count on Y axis
78 m_StepForRepeatMetric = false; // false = Inches, true = metric
79}
80
81
83 EDA_ITEM( nullptr, GERBER_IMAGE_T )
84{
85 m_GraphicLayer = aLayer; // Graphic layer Number
86 m_PositiveDrawColor = WHITE; // The color used to draw positive items for this image
87
89 m_FileFunction = nullptr; // file function parameters
90
92
93 for( unsigned ii = 0; ii < arrayDim( m_Aperture_List ); ii++ )
94 m_Aperture_List[ii] = nullptr;
95}
96
97
99{
100 for( GERBER_DRAW_ITEM* item : GetItems() )
101 delete item;
102
103 m_drawings.clear();
104
105 for( unsigned ii = 0; ii < arrayDim( m_Aperture_List ); ii++ )
106 delete m_Aperture_List[ii];
107
108 delete m_FileFunction;
109}
110
111
112D_CODE* GERBER_FILE_IMAGE::GetDCODEOrCreate( int aDCODE, bool aCreateIfNoExist )
113{
114 unsigned ndx = aDCODE - FIRST_DCODE;
115
116 if( ndx < (unsigned) arrayDim( m_Aperture_List ) )
117 {
118 // lazily create the D_CODE if it does not exist.
119 if( aCreateIfNoExist )
120 {
121 if( m_Aperture_List[ndx] == nullptr )
122 m_Aperture_List[ndx] = new D_CODE( ndx + FIRST_DCODE );
123 }
124
125 return m_Aperture_List[ndx];
126 }
127
128 return nullptr;
129}
130
131
133{
134 unsigned ndx = aDCODE - FIRST_DCODE;
135
136 if( ndx < (unsigned) arrayDim( m_Aperture_List ) )
137 return m_Aperture_List[ndx];
138
139 return nullptr;
140}
141
142
144{
145 APERTURE_MACRO_SET::iterator iter = m_aperture_macros.find( aLookup );
146
147 if( iter != m_aperture_macros.end() )
148 {
149 APERTURE_MACRO* pam = (APERTURE_MACRO*) &(*iter);
150 return pam;
151 }
152
153 return nullptr; // not found
154}
155
156
158{
159 m_InUse = false;
161 m_FileName.Empty();
162 m_ImageName = wxEmptyString; // Image name from the IN command (deprecated)
163 m_ImageNegative = false; // true = Negative image
164 m_IsX2_file = false; // true only if a %TF, %TA or %TD command
165 delete m_FileFunction; // file function parameters
166 m_FileFunction = nullptr;
167 m_MD5_value.Empty(); // MD5 value found in a %TF.MD5 command
168 m_PartString.Empty(); // string found in a %TF.Part command
169 m_hasNegativeItems = -1; // set to uninitialized
170 m_ImageJustifyOffset = VECTOR2I( 0, 0 ); // Image justify Offset
171 m_ImageJustifyXCenter = false; // Image Justify Center on X axis (default = false)
172 m_ImageJustifyYCenter = false; // Image Justify Center on Y axis (default = false)
173 m_GerbMetric = false; // false = Inches (default), true = metric
174 m_Relative = false; // false = absolute Coord,
175 // true = relative Coord
176 m_NoTrailingZeros = false; // true: trailing zeros deleted
177 m_ImageOffset.x = m_ImageOffset.y = 0; // Coord Offset, from IO command
178 m_ImageRotation = 0; // Allowed 0, 90, 180, 270 (in degree)
179 m_LocalRotation = 0.0; // Layer rotation from RO command (in 0.1 degree)
180 m_Offset.x = 0;
181 m_Offset.y = 0; // Coord Offset, from OF command
182 m_Scale.x = m_Scale.y = 1.0; // scale (A and B) this layer
183 m_MirrorA = false; // true: mirror / axe A (default = X)
184 m_MirrorB = false; // true: mirror / axe B (default = Y)
185 m_SwapAxis = false; // false if A = X, B = Y; true if A =Y, B = Y
186 m_Has_DCode = false; // true = DCodes in file
187 // false = no DCode-> perhaps deprecated RS274D file
188 m_Has_MissingDCode = false; // true = some D_Codes are used, but not defined
189 // perhaps deprecated RS274D file
190 m_FmtScale.x = m_FmtScale.y = 4; // Initialize default format to 3.4 => 4
191 m_FmtLen.x = m_FmtLen.y = 3 + 4; // Initialize default format len = 3+4
192
193 m_Iterpolation = GERB_INTERPOL_LINEAR_1X; // Linear, 90 arc, Circ.
194 m_360Arc_enbl = true; // 360 deg circular mode (G75) selected as default
195 // interpolation disable
196 m_AsArcG74G75Cmd = false; // false until a G74 or G75 command is found
197 m_Current_Tool = 0; // Current Dcode selected
198 m_CommandState = 0; // State of the current command
199 m_CurrentPos.x = m_CurrentPos.y = 0; // current specified coord
200 m_PreviousPos.x = m_PreviousPos.y = 0; // last specified coord
201 m_IJPos.x = m_IJPos.y = 0; // current centre coord for
202 // plot arcs & circles
203 m_LastCoordIsIJPos = false; // True only after a IJ coordinate is read
204 m_ArcRadius = 0; // radius of arcs in circular interpol (given by A## command).
205 // in command like X##Y##A##
206 m_LastArcDataType = ARC_INFO_TYPE_NONE; // Extra coordinate info type for arcs
207 // (radius or IJ center coord)
208 m_LineNum = 0; // line number in file being read
209 m_Current_File = nullptr; // Gerber file to read
210 m_PolygonFillMode = false;
212 m_Selected_Tool = 0;
214 m_Exposure = false;
215}
216
217
218/* Function HasNegativeItems
219 * return true if at least one item must be drawn in background color
220 * used to optimize screen refresh
221 */
223{
224 if( m_hasNegativeItems < 0 ) // negative items are not yet searched: find them if any
225 {
226 if( m_ImageNegative ) // A negative layer is expected having always negative objects.
227 {
229 }
230 else
231 {
233
234 for( GERBER_DRAW_ITEM* item : GetItems() )
235 {
236 if( item->GetLayer() != m_GraphicLayer )
237 continue;
238
239 if( item->HasNegativeItems() )
240 {
242 break;
243 }
244 }
245 }
246 }
247
248 return m_hasNegativeItems == 1;
249}
250
252{
253 int count = 0;
254
255 for( unsigned ii = 0; ii < arrayDim( m_Aperture_List ); ii++ )
256 {
257 if( m_Aperture_List[ii] )
258 {
259 if( m_Aperture_List[ii]->m_InUse || m_Aperture_List[ii]->m_Defined )
260 ++count;
261 }
262
263 }
264
265 return count;
266}
267
268
278{
279 if( GetLayerParams().m_XRepeatCount < 2 && GetLayerParams().m_YRepeatCount < 2 )
280 return; // Nothing to repeat
281
282 // Duplicate item:
283 for( int ii = 0; ii < GetLayerParams().m_XRepeatCount; ii++ )
284 {
285 for( int jj = 0; jj < GetLayerParams().m_YRepeatCount; jj++ )
286 {
287 // the first gerber item already exists (this is the template)
288 // create duplicate only if ii or jj > 0
289 if( jj == 0 && ii == 0 )
290 continue;
291
292 GERBER_DRAW_ITEM* dupItem = new GERBER_DRAW_ITEM( aItem );
293 VECTOR2I move_vector;
294 move_vector.x = scaletoIU( ii * GetLayerParams().m_StepForRepeat.x,
295 GetLayerParams().m_StepForRepeatMetric );
296 move_vector.y = scaletoIU( jj * GetLayerParams().m_StepForRepeat.y,
297 GetLayerParams().m_StepForRepeatMetric );
298 dupItem->MoveXY( move_vector );
299 AddItemToList( dupItem );
300 }
301 }
302}
303
304
315{
316 wxString msg;
317
318 aMainFrame->ClearMsgPanel();
319
320 // Display the Gerber variant (X1 / X2
321 aMainFrame->AppendMsgPanel( _( "Format" ), m_IsX2_file ? wxT( "X2" ) : wxT( "X1" ) );
322
323 // Display Image name (Image specific). IM command (Image Name) is deprecated
324 // So non empty image name is very rare, probably never found
325 if( !m_ImageName.IsEmpty() )
326 aMainFrame->AppendMsgPanel( _( "Image name" ), m_ImageName );
327
328 // Display graphic layer number used to draw this Image
329 // (not a Gerber parameter but is also image specific)
330 msg.Printf( wxT( "%d" ), m_GraphicLayer + 1 );
331 aMainFrame->AppendMsgPanel( _( "Graphic layer" ), msg );
332
333 // Display Image rotation (Image specific)
334 msg.Printf( wxT( "%d" ), m_ImageRotation );
335 aMainFrame->AppendMsgPanel( _( "Img Rot." ), msg );
336
337 // Display Image polarity (Image specific)
338 msg = m_ImageNegative ? _("Negative") : _("Normal");
339 aMainFrame->AppendMsgPanel( _( "Polarity" ), msg );
340
341 // Display Image justification and offset for justification (Image specific)
342 msg = m_ImageJustifyXCenter ? _("Center") : _("Normal");
343 aMainFrame->AppendMsgPanel( _( "X Justify" ), msg );
344
345 msg = m_ImageJustifyYCenter ? _("Center") : _("Normal");
346 aMainFrame->AppendMsgPanel( _( "Y Justify" ), msg );
347
348 msg.Printf( wxT( "X=%s Y=%s" ),
351
352 aMainFrame->AppendMsgPanel( _( "Image Justify Offset" ), msg );
353}
354
355
357{
358 /* Called when a %TD command is found
359 * Remove the attribute specified by the %TD command.
360 * is no attribute, all current attributes specified by the %TO and the %TA
361 * commands are cleared.
362 * if a attribute name is specified (for instance %TD.CN*%) is specified,
363 * only this attribute is cleared
364 */
365 wxString cmd = aAttribute.GetPrm( 0 );
367
368 if( cmd.IsEmpty() || cmd == wxT( ".AperFunction" ) )
369 m_AperFunction.Clear();
370}
371
372
374 const std::vector<KICAD_T>& aScanTypes )
375{
376 for( KICAD_T scanType : aScanTypes )
377 {
378 if( scanType == GERBER_DRAW_ITEM_T )
379 {
380 if( IterateForward( GetItems(), inspector, testData, { scanType } ) == INSPECT_RESULT::QUIT )
381 return INSPECT_RESULT::QUIT;
382 }
383 }
384
385 return INSPECT_RESULT::CONTINUE;
386}
int scaletoIU(double aCoord, bool isMetric)
Convert a distance given in floating point to our internal units.
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Returns # of elements in an array.
Definition: arraydim.h:31
Support the "aperture macro" defined within standard RS274X.
A gerber DCODE (also called Aperture) definition.
Definition: dcode.h:80
virtual void ClearMsgPanel()
Clear all messages from the message panel.
void AppendMsgPanel(const wxString &aTextUpper, const wxString &aTextLower, int aPadding=6)
Append a message to the message panel.
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:85
static INSPECT_RESULT IterateForward(std::deque< T > &aList, INSPECTOR inspector, void *testData, const std::vector< KICAD_T > &scanTypes)
This changes first parameter to avoid the DList and use the main queue instead.
Definition: eda_item.h:285
void ClearAttribute(const wxString *aName)
Remove the net attribute specified by aName.
void MoveXY(const VECTOR2I &aMoveVector)
Move this object.
int m_Last_Pen_Command
Current or last pen state (0..9, set by Dn option with n < 10.
APERTURE_MACRO * FindApertureMacro(const APERTURE_MACRO &aLookup)
Look up a previously read in aperture macro.
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.
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.
bool m_SwapAxis
false if A = X and B = Y (default); true if A = Y, B = X
GERBER_FILE_IMAGE(int layer)
double m_LocalRotation
Local rotation added to m_ImageRotation.
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
Identifier for arc data type (IJ (center) or A## (radius)).
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)
bool m_ImageJustifyYCenter
Image Justify Center on Y axis (default = false)
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
LAST_EXTRA_ARC_DATA_TYPE m_LastArcDataType
int m_LineNum
Line number of the gerber file while reading.
COLOR4D m_PositiveDrawColor
The color used to draw positive items.
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.
wxString m_MD5_value
MD5 value found in a TF.MD5 command.
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 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.
int m_PolygonFillModeState
a collection of APERTURE_MACROS, sorted by name
bool m_Has_DCode
< True if has DCodes in file or false if no DCodes found. Perhaps deprecated RS274D file.
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.
INSPECT_RESULT Visit(INSPECTOR inspector, void *testData, 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...
void AddItemToList(GERBER_DRAW_ITEM *aItem)
Add a new GERBER_DRAW_ITEM item to the drawings list.
wxSize m_FmtLen
Nb chars per coord. ex fmt 2.3, m_FmtLen = 5.
D_CODE * m_Aperture_List[TOOLS_MAX_COUNT]
< Dcode (Aperture) List for this layer (max TOOLS_MAX_COUNT: see dcode.h)
int m_GraphicLayer
Graphic layer Number.
APERTURE_MACRO_SET m_aperture_macros
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
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
A value ( = radius in circular routing in Excellon files ).
VECTOR2I m_Scale
scale (X and Y) of layer.
GERBER_DRAW_ITEMS & GetItems()
VECTOR2I m_ImageJustifyOffset
Image Justify Offset on XY axis (default = 0,0)
GERBER_LAYER m_GBRLayerParams
GERBER_LAYER & GetLayerParams()
void DisplayImageInfo(GERBVIEW_FRAME *aMainFrame)
Display information about image parameters in the status bar.
wxRealPoint m_StepForRepeat
bool m_StepForRepeatMetric
wxString m_LayerName
wxString MessageTextFromValue(double aValue, bool aAddUnitLabel=true, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
A lower-precision version of StringFromValue().
The attribute value consists of a number of substrings separated by a comma.
const wxString & GetPrm(int aIdx)
@ WHITE
Definition: color4d.h:47
#define FIRST_DCODE
Definition: dcode.h:69
#define _(s)
INSPECT_RESULT
Definition: eda_item.h:42
const INSPECTOR_FUNC & INSPECTOR
Definition: eda_item.h:78
int scaletoIU(double aCoord, bool isMetric)
Function scaletoIU converts a distance given in floating point to our internal units.
@ ARC_INFO_TYPE_NONE
@ GERB_INTERPOL_LINEAR_1X
Definition: gerbview.h:35
This file contains miscellaneous commonly used macros and functions.
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:78
@ GERBER_DRAW_ITEM_T
Definition: typeinfo.h:204
@ GERBER_IMAGE_T
Definition: typeinfo.h:205
VECTOR2< int > VECTOR2I
Definition: vector2d.h:588