KiCad PCB EDA Suite
gerber_file_image.cpp
Go to the documentation of this file.
1 
6 /*
7  * This program source code file is part of KiCad, a free EDA CAD application.
8  *
9  * Copyright (C) 1992-2016 Jean-Pierre Charras jp.charras at wanadoo.fr
10  * Copyright (C) 1992-2016 KiCad Developers, see AUTHORS.txt for contributors.
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, you may find one here:
24  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
25  * or you may search the http://www.gnu.org website for the version 2 license,
26  * or you may write to the Free Software Foundation, Inc.,
27  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
28  */
29 
30 #include <fctsys.h>
31 #include <common.h>
32 #include <class_drawpanel.h>
33 #include <macros.h>
34 #include <convert_to_biu.h>
35 
36 #include <gerbview.h>
37 #include <gerbview_frame.h>
38 #include <gerber_file_image.h>
39 #include <X2_gerber_attributes.h>
40 
41 #include <algorithm>
42 #include <map>
43 
44 
49 extern int scaletoIU( double aCoord, bool isMetric ); // defined it rs274d_read_XY_and_IJ_coordiantes.cpp
50 
51 /* Format Gerber: NOTES:
52  * Tools and D_CODES
53  * tool number (identification of shapes)
54  * 1 to 999
55  *
56  * D_CODES:
57  * D01 ... D9 = action codes:
58  * D01 = activating light (lower pen) when di placement
59  * D02 = light extinction (lift pen) when di placement
60  * D03 Flash
61  * D09 = VAPE Flash
62  * D10 ... = Indentification Tool (Opening)
63  *
64  * For tools:
65  * DCode min = D10
66  * DCode max = 999
67  */
68 
69 
71 {
73 }
74 
75 
77 {
78 }
79 
80 
82 {
83  m_LayerName = wxT( "no name" ); // Layer name from the LN command
84  m_LayerNegative = false; // true = Negative Layer
85  m_StepForRepeat.x = m_StepForRepeat.y = 0; // X and Y offsets for Step and Repeat command
86  m_XRepeatCount = 1; // The repeat count on X axis
87  m_YRepeatCount = 1; // The repeat count on Y axis
88  m_StepForRepeatMetric = false; // false = Inches, true = metric
89 }
90 
91 
94 {
95  m_GraphicLayer = aLayer; // Graphic layer Number
96  m_IsVisible = true; // must be drawn
97  m_PositiveDrawColor = WHITE; // The color used to draw positive items for this image
98 
99  m_Selected_Tool = 0;
100  m_FileFunction = NULL; // file function parameters
101 
103 
104  for( unsigned ii = 0; ii < arrayDim( m_Aperture_List ); ii++ )
105  m_Aperture_List[ii] = 0;
106 }
107 
108 
110 {
112 
113  for( unsigned ii = 0; ii < arrayDim( m_Aperture_List ); ii++ )
114  {
115  delete m_Aperture_List[ii];
116  }
117 
118  delete m_FileFunction;
119 }
120 
121 /*
122  * Function GetItemsList
123  * returns the first GERBER_DRAW_ITEM * item of the items list
124  */
126 {
127  return m_Drawings;
128 }
129 
130 
131 D_CODE* GERBER_FILE_IMAGE::GetDCODEOrCreate( int aDCODE, bool aCreateIfNoExist )
132 {
133  unsigned ndx = aDCODE - FIRST_DCODE;
134 
135  if( ndx < (unsigned) arrayDim( m_Aperture_List ) )
136  {
137  // lazily create the D_CODE if it does not exist.
138  if( aCreateIfNoExist )
139  {
140  if( m_Aperture_List[ndx] == NULL )
141  m_Aperture_List[ndx] = new D_CODE( ndx + FIRST_DCODE );
142  }
143 
144  return m_Aperture_List[ndx];
145  }
146 
147  return NULL;
148 }
149 
150 
152 {
153  unsigned ndx = aDCODE - FIRST_DCODE;
154 
155  if( ndx < (unsigned) arrayDim( m_Aperture_List ) )
156  {
157  return m_Aperture_List[ndx];
158  }
159 
160  return NULL;
161 }
162 
163 
165 {
166  APERTURE_MACRO_SET::iterator iter = m_aperture_macros.find( aLookup );
167 
168  if( iter != m_aperture_macros.end() )
169  {
170  APERTURE_MACRO* pam = (APERTURE_MACRO*) &(*iter);
171  return pam;
172  }
173 
174  return NULL; // not found
175 }
176 
177 
179 {
180  m_InUse = false;
182  m_FileName.Empty();
183  m_ImageName = wxT( "no name" ); // Image name from the IN command
184  m_ImageNegative = false; // true = Negative image
185  m_IsX2_file = false; // true only if a %TF, %TA or %TD command
186  delete m_FileFunction; // file function parameters
187  m_FileFunction = NULL;
188  m_MD5_value.Empty(); // MD5 value found in a %TF.MD5 command
189  m_PartString.Empty(); // string found in a %TF.Part command
190  m_hasNegativeItems = -1; // set to uninitialized
191  m_ImageJustifyOffset = wxPoint(0,0); // Image justify Offset
192  m_ImageJustifyXCenter = false; // Image Justify Center on X axis (default = false)
193  m_ImageJustifyYCenter = false; // Image Justify Center on Y axis (default = false)
194  m_GerbMetric = false; // false = Inches (default), true = metric
195  m_Relative = false; // false = absolute Coord,
196  // true = relative Coord
197  m_NoTrailingZeros = false; // true: trailing zeros deleted
198  m_ImageOffset.x = m_ImageOffset.y = 0; // Coord Offset, from IO command
199  m_ImageRotation = 0; // Allowed 0, 90, 180, 270 (in degree)
200  m_LocalRotation = 0.0; // Layer totation from RO command (in 0.1 degree)
201  m_Offset.x = 0;
202  m_Offset.y = 0; // Coord Offset, from OF command
203  m_Scale.x = m_Scale.y = 1.0; // scale (A and B) this layer
204  m_MirrorA = false; // true: miror / axe A (default = X)
205  m_MirrorB = false; // true: miror / axe B (default = Y)
206  m_SwapAxis = false; // false if A = X, B = Y; true if A =Y, B = Y
207  m_Has_DCode = false; // true = DCodes in file
208  // false = no DCode->
209  // search for separate DCode file
210  m_FmtScale.x = m_FmtScale.y = 4; // Initialize default format to 3.4 => 4
211  m_FmtLen.x = m_FmtLen.y = 3 + 4; // Initialize default format len = 3+4
212 
213  m_Iterpolation = GERB_INTERPOL_LINEAR_1X; // Linear, 90 arc, Circ.
214  m_360Arc_enbl = false; // 360 deg circular
215  // interpolation disable
216  m_Current_Tool = 0; // Current Dcode selected
217  m_CommandState = 0; // State of the current command
218  m_CurrentPos.x = m_CurrentPos.y = 0; // current specified coord
219  m_PreviousPos.x = m_PreviousPos.y = 0; // last specified coord
220  m_IJPos.x = m_IJPos.y = 0; // current centre coord for
221  // plot arcs & circles
222  m_LastCoordIsIJPos = false; // True only after a IJ coordinate is read
223  m_ArcRadius = 0; // radius of arcs in circular interpol (given by A## command).
224  // in command like X##Y##A##
225  m_LastArcDataType = ARC_INFO_TYPE_NONE; // Extra coordinate info type for arcs
226  // (radius or IJ center coord)
227  m_LineNum = 0; // line number in file being read
228  m_Current_File = NULL; // Gerber file to read
229  m_PolygonFillMode = false;
231  m_Selected_Tool = 0;
232  m_Last_Pen_Command = 0;
233  m_Exposure = false;
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  m_hasNegativeItems = 1;
247  else
248  {
249  m_hasNegativeItems = 0;
250  for( GERBER_DRAW_ITEM* item = GetItemsList(); item; item = item->Next() )
251  {
252  if( item->GetLayer() != m_GraphicLayer )
253  continue;
254  if( item->HasNegativeItems() )
255  {
256  m_hasNegativeItems = 1;
257  break;
258  }
259  }
260  }
261  }
262  return m_hasNegativeItems == 1;
263 }
264 
266 {
267  int count = 0;
268 
269  for( unsigned ii = 0; ii < arrayDim( m_Aperture_List ); ii++ )
270  {
271  if( m_Aperture_List[ii] )
272  if( m_Aperture_List[ii]->m_InUse || m_Aperture_List[ii]->m_Defined )
273  ++count;
274  }
275 
276  return count;
277 }
278 
279 
281 {
282  for( int count = 0; count < TOOLS_MAX_COUNT; count++ )
283  {
284  if( m_Aperture_List[count] == NULL )
285  continue;
286 
287  m_Aperture_List[count]->m_Num_Dcode = count + FIRST_DCODE;
289  }
290 
291  m_aperture_macros.clear();
292 }
293 
294 
304 {
305  if( GetLayerParams().m_XRepeatCount < 2 &&
306  GetLayerParams().m_YRepeatCount < 2 )
307  return; // Nothing to repeat
308 
309  // Duplicate item:
310  for( int ii = 0; ii < GetLayerParams().m_XRepeatCount; ii++ )
311  {
312  for( int jj = 0; jj < GetLayerParams().m_YRepeatCount; jj++ )
313  {
314  // the first gerber item already exists (this is the template)
315  // create duplicate only if ii or jj > 0
316  if( jj == 0 && ii == 0 )
317  continue;
318  GERBER_DRAW_ITEM* dupItem = new GERBER_DRAW_ITEM( aItem );
319  wxPoint move_vector;
320  move_vector.x = scaletoIU( ii * GetLayerParams().m_StepForRepeat.x,
321  GetLayerParams().m_StepForRepeatMetric );
322  move_vector.y = scaletoIU( jj * GetLayerParams().m_StepForRepeat.y,
323  GetLayerParams().m_StepForRepeatMetric );
324  dupItem->MoveXY( move_vector );
325  m_Drawings.Append( dupItem );
326  }
327  }
328 }
329 
330 
340 {
341  wxString msg;
342 
343  aMainFrame->ClearMsgPanel();
344 
345  // Display Image name (Image specific)
346  aMainFrame->AppendMsgPanel( _( "Image name" ), m_ImageName, CYAN );
347 
348  // Display graphic layer number used to draw this Image
349  // (not a Gerber parameter but is also image specific)
350  msg.Printf( wxT( "%d" ), m_GraphicLayer + 1 );
351  aMainFrame->AppendMsgPanel( _( "Graphic layer" ), msg, BROWN );
352 
353  // Display Image rotation (Image specific)
354  msg.Printf( wxT( "%d" ), m_ImageRotation );
355  aMainFrame->AppendMsgPanel( _( "Img Rot." ), msg, CYAN );
356 
357  // Display Image polarity (Image specific)
358  msg = m_ImageNegative ? _("Negative") : _("Normal");
359  aMainFrame->AppendMsgPanel( _( "Polarity" ), msg, BROWN );
360 
361  // Display Image justification and offset for justification (Image specific)
362  msg = m_ImageJustifyXCenter ? _("Center") : _("Normal");
363  aMainFrame->AppendMsgPanel( _( "X Justify" ), msg, DARKRED );
364 
365  msg = m_ImageJustifyYCenter ? _("Center") : _("Normal");
366  aMainFrame->AppendMsgPanel( _( "Y Justify" ), msg, DARKRED );
367 
368  if( aMainFrame->GetUserUnits() == INCHES )
369  msg.Printf( wxT( "X=%f Y=%f" ), Iu2Mils( m_ImageJustifyOffset.x ) / 1000.0,
370  Iu2Mils( m_ImageJustifyOffset.y ) / 1000.0 );
371  else
372  msg.Printf( wxT( "X=%f Y=%f" ), Iu2Millimeter( m_ImageJustifyOffset.x ),
373  Iu2Millimeter( m_ImageJustifyOffset.y ) );
374 
375  aMainFrame->AppendMsgPanel( _( "Image Justify Offset" ), msg, DARKRED );
376 }
377 
378 
380 {
381  /* Called when a %TD command is found
382  * Remove the attribute specified by the %TD command.
383  * is no attribute, all current attributes specified by the %TO and the %TA
384  * commands are cleared.
385  * if a attribute name is specified (for instance %TD.CN*%) is specified,
386  * only this attribute is cleared
387  */
388  m_NetAttributeDict.ClearAttribute( &aAttribute.GetPrm( 1 ) );
389 
390  if( aAttribute.GetPrm( 1 ).IsEmpty() || aAttribute.GetPrm( 1 ) == ".AperFunction" )
391  m_AperFunction.Clear();
392 }
393 
394 
395 SEARCH_RESULT GERBER_FILE_IMAGE::Visit( INSPECTOR inspector, void* testData, const KICAD_T scanTypes[] )
396 {
397  KICAD_T stype;
399  const KICAD_T* p = scanTypes;
400  bool done = false;
401 
402 #if 0 && defined(DEBUG)
403  std::cout << GetClass().mb_str() << ' ';
404 #endif
405 
406  while( !done )
407  {
408  stype = *p;
409 
410  switch( stype )
411  {
412  case GERBER_IMAGE_T:
413  case GERBER_IMAGE_LIST_T:
414  ++p;
415  break;
416 
417  case GERBER_DRAW_ITEM_T:
418  result = IterateForward( &m_Drawings[0], inspector, testData, p );
419  ++p;
420  break;
421 
422  default: // catch EOT or ANY OTHER type here and return.
423  done = true;
424  break;
425  }
426 
427  if( result == SEARCH_QUIT )
428  break;
429  }
430 
431  return result;
432 }
APERTURE_MACRO_SET m_aperture_macros
a collection of APERTURE_MACROS, sorted by name
wxString m_LayerName
X2_ATTRIBUTE_FILEFUNCTION * m_FileFunction
static SEARCH_RESULT IterateForward(EDA_ITEM *listStart, INSPECTOR inspector, void *testData, const KICAD_T scanTypes[])
Function IterateForward walks through the object tree calling the inspector() on each object type req...
bool HasNegativeItems()
Function HasNegativeItems.
class X2_ATTRIBUTE The attribute value consists of a number of substrings separated by a comma
wxString GetClass() const override
Function GetClass returns the class name.
void Append(T *aNewElement)
Function Append adds aNewElement to the end of the list.
Definition: dlist.h:177
wxRealPoint m_StepForRepeat
GERBER_FILE_IMAGE(int layer)
D_CODE * GetDCODE(int aDCODE) const
Function GetDCODE returns a pointer to the D_CODE within this GERBER for the given aDCODE.
int scaletoIU(double aCoord, bool isMetric)
Function scaletoIU converts a distance given in floating point to our internal units.
const wxString & GetPrm(int aIdx)
void DeleteAll()
Function DeleteAll deletes all items on the list and leaves the list empty.
Definition: dlist.cpp:44
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:78
Definition: colors.h:54
This file contains miscellaneous commonly used macros and functions.
D_CODE * m_Aperture_List[TOOLS_MAX_COUNT]
Dcode (Aperture) List for this layer (max 999)
const INSPECTOR_FUNC & INSPECTOR
Definition: base_struct.h:102
void DisplayImageInfo(GERBVIEW_FRAME *aMainFrame)
Function DisplayImageInfo has knowledge about the frame and how and where to put status information a...
GERBER_LAYER m_GBRLayerParams
#define FIRST_DCODE
Definition: dcode.h:71
EDA_UNITS_T GetUserUnits() const override
Return the user units currently in use.
Definition: draw_frame.h:291
Definition: common.h:161
int m_Num_Dcode
D code value ( >= 10 )
Definition: dcode.h:96
Definition: colors.h:59
APERTURE_MACRO * FindApertureMacro(const APERTURE_MACRO &aLookup)
Function FindApertureMacro looks up a previously read in aperture macro.
bool m_StepForRepeatMetric
void RemoveAttribute(X2_ATTRIBUTE &aAttribute)
Function RemoveAttribute.
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Definition: macros.h:99
GERBER_DRAW_ITEM * Next() const
void AppendMsgPanel(const wxString &textUpper, const wxString &textLower, COLOR4D color, int pad=6)
Append a message to the message panel.
bool m_Exposure
whether an aperture macro tool is flashed on or off
LAST_EXTRA_ARC_DATA_TYPE m_LastArcDataType
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:154
The common library.
Definition: colors.h:49
void ClearAttribute(const wxString *aName)
remove the net attribute specified by aName If aName == NULL or empty, remove all attributes
Struct APERTURE_MACRO helps support the "aperture macro" defined within standard RS274X.
Definition: am_primitive.h:163
void InitToolTable()
Function InitToolTable.
SEARCH_RESULT
Definition: base_struct.h:66
void MoveXY(const wxPoint &aMoveVector)
Function MoveXY move this object.
void StepAndRepeatItem(const GERBER_DRAW_ITEM &aItem)
Function StepAndRepeatItem Gerber format has a command Step an Repeat This function must be called wh...
void Clear_D_CODE_Data()
Definition: dcode.cpp:79
D_CODE * GetDCODEOrCreate(int aDCODE, bool aCreateIfNoExist=true)
Function GetDCODEOrCreate returns a pointer to the D_CODE within this GERBER for the given aDCODE.
void ClearMsgPanel(void)
Clear all messages from the message panel.
GERBER_LAYER & GetLayerParams()
Function GetLayerParams.
virtual void ResetDefaultValues()
Definition: colors.h:62