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#include <core/profile.h>
34
35
40extern int scaletoIU( double aCoord, bool isMetric ); // defined it rs274d_read_XY_and_IJ_coordinates.cpp
41
42/* Format Gerber: NOTES:
43 * Tools and D_CODES
44 * tool number (identification of shapes)
45 * 1 to 999
46 *
47 * D_CODES:
48 * D01 ... D9 = action codes:
49 * D01 = activating light (lower pen) when di placement
50 * D02 = light extinction (lift pen) when di placement
51 * D03 Flash
52 * D09 = VAPE Flash
53 * D10 ... = Identification Tool (Opening)
54 *
55 * For tools:
56 * DCode min = D10
57 * DCode max = 999
58 */
59
60
62{
64}
65
66
68{
69}
70
71
73{
74 m_LayerName = wxT( "no name" ); // Layer name from the LN command
75 m_LayerNegative = false; // true = Negative Layer
76 m_StepForRepeat.x = m_StepForRepeat.y = 0; // X and Y offsets for Step and Repeat command
77 m_XRepeatCount = 1; // The repeat count on X axis
78 m_YRepeatCount = 1; // The repeat count on Y axis
79 m_StepForRepeatMetric = false; // false = Inches, true = metric
80}
81
82
84 EDA_ITEM( nullptr, GERBER_IMAGE_T )
85{
86 m_GraphicLayer = aLayer; // Graphic layer Number
87 m_PositiveDrawColor = WHITE; // The color used to draw positive items for this image
88
90 m_FileFunction = nullptr; // file function parameters
91
93
94 for( unsigned ii = 0; ii < arrayDim( m_Aperture_List ); ii++ )
95 m_Aperture_List[ii] = nullptr;
96}
97
98
100{
101 // Reverse iteration to avoid O(N^2) memcpy in a vector erase downstream.
102 // It results in a O(N^2) std::find, which is somewhat faster.
103 for( auto it = GetItems().rbegin(); it < GetItems().rend(); it++ )
104 delete *it;
105
106 m_drawings.clear();
107
108 for( unsigned ii = 0; ii < arrayDim( m_Aperture_List ); ii++ )
109 delete m_Aperture_List[ii];
110
111 delete m_FileFunction;
112}
113
114
116{
119 m_DisplayRotation = aRotation;
120
121 // Clear m_AbsolutePolygon member of Gerber items, because draw coordinates
122 // are now outdated
123 for( GERBER_DRAW_ITEM* item : GetItems() )
124 item->m_AbsolutePolygon.RemoveAllContours();
125}
126
127
128D_CODE* GERBER_FILE_IMAGE::GetDCODEOrCreate( int aDCODE, bool aCreateIfNoExist )
129{
130 unsigned ndx = aDCODE - FIRST_DCODE;
131
132 if( ndx < (unsigned) arrayDim( m_Aperture_List ) )
133 {
134 // lazily create the D_CODE if it does not exist.
135 if( aCreateIfNoExist )
136 {
137 if( m_Aperture_List[ndx] == nullptr )
138 m_Aperture_List[ndx] = new D_CODE( ndx + FIRST_DCODE );
139 }
140
141 return m_Aperture_List[ndx];
142 }
143
144 return nullptr;
145}
146
147
149{
150 unsigned ndx = aDCODE - FIRST_DCODE;
151
152 if( ndx < (unsigned) arrayDim( m_Aperture_List ) )
153 return m_Aperture_List[ndx];
154
155 return nullptr;
156}
157
158
160{
161 APERTURE_MACRO_SET::iterator iter = m_aperture_macros.find( aLookup );
162
163 if( iter != m_aperture_macros.end() )
164 {
165 APERTURE_MACRO* pam = (APERTURE_MACRO*) &(*iter);
166 return pam;
167 }
168
169 return nullptr; // not found
170}
171
172
174{
175 m_InUse = false;
177 m_FileName.Empty();
178 m_ImageName = wxEmptyString; // Image name from the IN command (deprecated)
179 m_ImageNegative = false; // true = Negative image
180 m_IsX2_file = false; // true only if a %TF, %TA or %TD command
181 delete m_FileFunction; // file function parameters
182 m_FileFunction = nullptr;
183 m_MD5_value.Empty(); // MD5 value found in a %TF.MD5 command
184 m_PartString.Empty(); // string found in a %TF.Part command
185 m_hasNegativeItems = -1; // set to uninitialized
186 m_ImageJustifyOffset = VECTOR2I( 0, 0 ); // Image justify Offset
187 m_ImageJustifyXCenter = false; // Image Justify Center on X axis (default = false)
188 m_ImageJustifyYCenter = false; // Image Justify Center on Y axis (default = false)
189 m_GerbMetric = false; // false = Inches (default), true = metric
190 m_Relative = false; // false = absolute Coord,
191 // true = relative Coord
192 m_NoTrailingZeros = false; // true: trailing zeros deleted
193 m_ImageOffset.x = m_ImageOffset.y = 0; // Coord Offset, from IO command
194 m_ImageRotation = 0; // Allowed 0, 90, 180, 270 (in degree)
195 m_LocalRotation = 0.0; // Layer rotation from RO command (in 0.1 degree)
196 m_Offset.x = 0;
197 m_Offset.y = 0; // Coord Offset, from OF command
198 m_Scale.x = m_Scale.y = 1.0; // scale (A and B) this layer
199 m_MirrorA = false; // true: mirror / axe A (default = X)
200 m_MirrorB = false; // true: mirror / axe B (default = Y)
201 m_SwapAxis = false; // false if A = X, B = Y; true if A =Y, B = Y
202 m_Has_DCode = false; // true = DCodes in file
203 // false = no DCode-> perhaps deprecated RS274D file
204 m_Has_MissingDCode = false; // true = some D_Codes are used, but not defined
205 // perhaps deprecated RS274D file
206 m_FmtScale.x = m_FmtScale.y = 4; // Initialize default format to 3.4 => 4
207 m_FmtLen.x = m_FmtLen.y = 3 + 4; // Initialize default format len = 3+4
208
209 m_Iterpolation = GERB_INTERPOL_LINEAR_1X; // Linear, 90 arc, Circ.
210 m_360Arc_enbl = true; // 360 deg circular mode (G75) selected as default
211 // interpolation disable
212 m_AsArcG74G75Cmd = false; // false until a G74 or G75 command is found
213 m_Current_Tool = 0; // Current Dcode selected
214 m_CommandState = 0; // State of the current command
215 m_CurrentPos.x = m_CurrentPos.y = 0; // current specified coord
216 m_PreviousPos.x = m_PreviousPos.y = 0; // last specified coord
217 m_IJPos.x = m_IJPos.y = 0; // current centre coord for
218 // plot arcs & circles
219 m_LastCoordIsIJPos = false; // True only after a IJ coordinate is read
220 m_ArcRadius = 0; // radius of arcs in circular interpol (given by A## command).
221 // in command like X##Y##A##
222 m_LastArcDataType = ARC_INFO_TYPE_NONE; // Extra coordinate info type for arcs
223 // (radius or IJ center coord)
224 m_LineNum = 0; // line number in file being read
225 m_Current_File = nullptr; // Gerber file to read
226 m_PolygonFillMode = false;
228 m_Selected_Tool = 0;
230 m_Exposure = false;
231
234}
235
236
237/* Function HasNegativeItems
238 * return true if at least one item must be drawn in background color
239 * used to optimize screen refresh
240 */
242{
243 if( m_hasNegativeItems < 0 ) // negative items are not yet searched: find them if any
244 {
245 if( m_ImageNegative ) // A negative layer is expected having always negative objects.
246 {
248 }
249 else
250 {
252
253 for( GERBER_DRAW_ITEM* item : GetItems() )
254 {
255 if( item->GetLayer() != m_GraphicLayer )
256 continue;
257
258 if( item->HasNegativeItems() )
259 {
261 break;
262 }
263 }
264 }
265 }
266
267 return m_hasNegativeItems == 1;
268}
269
271{
272 int count = 0;
273
274 for( unsigned ii = 0; ii < arrayDim( m_Aperture_List ); ii++ )
275 {
276 if( m_Aperture_List[ii] )
277 {
278 if( m_Aperture_List[ii]->m_InUse || m_Aperture_List[ii]->m_Defined )
279 ++count;
280 }
281
282 }
283
284 return count;
285}
286
287
297{
298 if( GetLayerParams().m_XRepeatCount < 2 && GetLayerParams().m_YRepeatCount < 2 )
299 return; // Nothing to repeat
300
301 // Duplicate item:
302 for( int ii = 0; ii < GetLayerParams().m_XRepeatCount; ii++ )
303 {
304 for( int jj = 0; jj < GetLayerParams().m_YRepeatCount; jj++ )
305 {
306 // the first gerber item already exists (this is the template)
307 // create duplicate only if ii or jj > 0
308 if( jj == 0 && ii == 0 )
309 continue;
310
311 GERBER_DRAW_ITEM* dupItem = new GERBER_DRAW_ITEM( aItem );
312 VECTOR2I move_vector;
313 move_vector.x = scaletoIU( ii * GetLayerParams().m_StepForRepeat.x,
314 GetLayerParams().m_StepForRepeatMetric );
315 move_vector.y = scaletoIU( jj * GetLayerParams().m_StepForRepeat.y,
316 GetLayerParams().m_StepForRepeatMetric );
317 dupItem->MoveXY( move_vector );
318 AddItemToList( dupItem );
319 }
320 }
321}
322
323
334{
335 wxString msg;
336
337 aMainFrame->ClearMsgPanel();
338
339 // Display the Gerber variant (X1 / X2
340 aMainFrame->AppendMsgPanel( _( "Format" ), m_IsX2_file ? wxT( "X2" ) : wxT( "X1" ) );
341
342 // Display Image name (Image specific). IM command (Image Name) is deprecated
343 // So non empty image name is very rare, probably never found
344 if( !m_ImageName.IsEmpty() )
345 aMainFrame->AppendMsgPanel( _( "Image name" ), m_ImageName );
346
347 // Display graphic layer number used to draw this Image
348 // (not a Gerber parameter but is also image specific)
349 msg.Printf( wxT( "%d" ), m_GraphicLayer + 1 );
350 aMainFrame->AppendMsgPanel( _( "Graphic layer" ), msg );
351
352 // Display Image rotation (Image specific)
353 msg.Printf( wxT( "%d" ), m_ImageRotation );
354 aMainFrame->AppendMsgPanel( _( "Img Rot." ), msg );
355
356 // Display Image polarity (Image specific)
357 msg = m_ImageNegative ? _("Negative") : _("Normal");
358 aMainFrame->AppendMsgPanel( _( "Polarity" ), msg );
359
360 // Display Image justification and offset for justification (Image specific)
361 msg = m_ImageJustifyXCenter ? _("Center") : _("Normal");
362 aMainFrame->AppendMsgPanel( _( "X Justify" ), msg );
363
364 msg = m_ImageJustifyYCenter ? _("Center") : _("Normal");
365 aMainFrame->AppendMsgPanel( _( "Y Justify" ), msg );
366
367 msg.Printf( wxT( "X=%s Y=%s" ),
370
371 aMainFrame->AppendMsgPanel( _( "Image Justify Offset" ), msg );
372}
373
374
376{
377 /* Called when a %TD command is found
378 * Remove the attribute specified by the %TD command.
379 * is no attribute, all current attributes specified by the %TO and the %TA
380 * commands are cleared.
381 * if a attribute name is specified (for instance %TD.CN*%) is specified,
382 * only this attribute is cleared
383 */
384 wxString cmd = aAttribute.GetPrm( 0 );
386
387 if( cmd.IsEmpty() || cmd == wxT( ".AperFunction" ) )
388 m_AperFunction.Clear();
389}
390
391
393 const std::vector<KICAD_T>& aScanTypes )
394{
395 for( KICAD_T scanType : aScanTypes )
396 {
397 if( scanType == GERBER_DRAW_ITEM_T )
398 {
399 if( IterateForward( GetItems(), inspector, testData, { scanType } ) == INSPECT_RESULT::QUIT )
400 return INSPECT_RESULT::QUIT;
401 }
402 }
403
404 return INSPECT_RESULT::CONTINUE;
405}
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
constexpr EDA_IU_SCALE gerbIUScale
Definition: base_units.h:107
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition: box2.h:990
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:89
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:296
void ClearAttribute(const wxString *aName)
Remove the net attribute specified by aName.
void MoveXY(const VECTOR2I &aMoveVector)
Move this object.
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.
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_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
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) const
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:48
#define FIRST_DCODE
Definition: dcode.h:69
#define _(s)
static constexpr EDA_ANGLE ANGLE_0
Definition: eda_angle.h:401
INSPECT_RESULT
Definition: eda_item.h:43
const INSPECTOR_FUNC & INSPECTOR
Definition: eda_item.h:82
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.
const double IU_PER_MM
Definition: base_units.h:76
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:209
@ GERBER_IMAGE_T
Definition: typeinfo.h:210
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:691