KiCad PCB EDA Suite
excellon_read_drill_file.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-2016 Jean-Pierre Charras <jp.charras at wanadoo.fr>
5  * Copyright (C) 1992-2018 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 /*
26  * Here is a sample of drill files created by Pcbnew, in decimal format:
27  * (Note: coordinates formats are same as Gerber, and T commands are near Gerber D commands).
28  * M48
29  * ;DRILL file {PCBnew (2011-03-14 BZR 2894)-testing} date 15/03/2011 14:23:22
30  * ;FORMAT={-:-/ absolute / inch / decimal}
31  * FMAT,2
32  * INCH,TZ
33  * T1C0.02
34  * T2C0.032
35  * %
36  * G90
37  * G05
38  * M72
39  * T1
40  * X1.580Y-1.360
41  * X1.580Y-4.860
42  * X8.680Y-1.360
43  * X8.680Y-4.860
44  * T2
45  * X2.930Y-3.560
46  * X5.280Y-2.535
47  * X5.405Y-2.610
48  * X5.620Y-2.900
49  * T0
50  * M30
51  */
52  /*
53  * Note there are some variant of tool definition:
54  * T1F00S00C0.2 or T1C0.02F00S00 ... Feed Rate and Spindle Speed of Tool 1
55  * Feed Rate and Spindle Speed are just skipped because they are not used in a viewer
56  */
57 
65 #include <math/util.h> // for KiROUND
66 
67 #include <gerbview.h>
68 #include <gerbview_frame.h>
69 #include <gerber_file_image.h>
70 #include <gerber_file_image_list.h>
71 #include <excellon_image.h>
72 #include <macros.h>
73 #include <kicad_string.h>
74 #include <locale_io.h>
75 #include <X2_gerber_attributes.h>
76 #include <view/view.h>
77 
78 #include <cmath>
79 
81 
82 // Default format for dimensions: they are the default values, not the actual values
83 // number of digits in mantissa:
84 static const int fmtMantissaMM = 3;
85 static const int fmtMantissaInch = 4;
86 // number of digits, integer part:
87 static const int fmtIntegerMM = 3;
88 static const int fmtIntegerInch = 2;
89 
90 // A helper function to calculate the arc center of an arc
91 // known by 2 end points, the radius, and the angle direction (CW or CCW)
92 // Arc angles are <= 180 degrees in circular interpol.
93 static wxPoint computeCenter(wxPoint aStart, wxPoint aEnd, int& aRadius, bool aRotCCW )
94 {
95  wxPoint center;
96  VECTOR2D end;
97  end.x = double(aEnd.x - aStart.x);
98  end.y = double(aEnd.y - aStart.y);
99 
100  // Be sure aRadius/2 > dist between aStart and aEnd
101  double min_radius = end.EuclideanNorm() * 2;
102 
103  if( min_radius <= aRadius )
104  {
105  // Adjust the radius and the arc center for a 180 deg arc between end points
106  aRadius = KiROUND( min_radius );
107  center.x = ( aStart.x + aEnd.x + 1 ) / 2;
108  center.y = ( aStart.y + aEnd.y + 1 ) / 2;
109  return center;
110  }
111 
112  /* to compute the centers position easily:
113  * rotate the segment (0,0 to end.x,end.y) to make it horizontal (end.y = 0).
114  * the X center position is end.x/2
115  * the Y center positions are on the vertical line starting at end.x/2, 0
116  * and solve aRadius^2 = X^2 + Y^2 (2 values)
117  */
118  double seg_angle = end.Angle(); //in radian
119  VECTOR2D h_segm = end.Rotate( - seg_angle );
120  double cX = h_segm.x/2;
121  double cY1 = sqrt( (double)aRadius*aRadius - cX*cX );
122  double cY2 = -cY1;
123  VECTOR2D center1( cX, cY1 );
124  center1 = center1.Rotate( seg_angle );
125  double arc_angle1 = (end - center1).Angle() - (VECTOR2D(0.0,0.0) - center1).Angle();
126  VECTOR2D center2( cX, cY2 );
127  center2 = center2.Rotate( seg_angle );
128  double arc_angle2 = (end - center2).Angle() - (VECTOR2D(0.0,0.0) - center2).Angle();
129 
130  if( !aRotCCW )
131  {
132  if( arc_angle1 < 0.0 )
133  arc_angle1 += 2*M_PI;
134 
135  if( arc_angle2 < 0.0 )
136  arc_angle2 += 2*M_PI;
137  }
138  else
139  {
140  if( arc_angle1 > 0.0 )
141  arc_angle1 -= 2*M_PI;
142 
143  if( arc_angle2 > 0.0 )
144  arc_angle2 -= 2*M_PI;
145  }
146 
147  // Arc angle must be <= 180.0 degrees.
148  // So choose the center that create a arc angle <= 180.0
149  if( std::abs( arc_angle1 ) <= M_PI )
150  {
151  center.x = KiROUND( center1.x );
152  center.y = KiROUND( center1.y );
153  }
154  else
155  {
156  center.x = KiROUND( center2.x );
157  center.y = KiROUND( center2.y );
158  }
159 
160  return center+aStart;
161 }
162 
163 extern int ReadInt( char*& text, bool aSkipSeparator = true );
164 extern double ReadDouble( char*& text, bool aSkipSeparator = true );
165 
166 // See rs274d.cpp:
167 extern void fillFlashedGBRITEM( GERBER_DRAW_ITEM* aGbrItem,
168  APERTURE_T aAperture,
169  int Dcode_index,
170  const wxPoint& aPos,
171  wxSize aSize,
172  bool aLayerNegative );
173 
174 extern void fillLineGBRITEM( GERBER_DRAW_ITEM* aGbrItem,
175  int Dcode_index,
176  const wxPoint& aStart,
177  const wxPoint& aEnd,
178  wxSize aPenSize,
179  bool aLayerNegative );
180 
181 extern void fillArcGBRITEM( GERBER_DRAW_ITEM* aGbrItem, int Dcode_index,
182  const wxPoint& aStart, const wxPoint& aEnd,
183  const wxPoint& aRelCenter, wxSize aPenSize,
184  bool aClockwise, bool aMultiquadrant,
185  bool aLayerNegative );
186 
187 // Gerber X2 files have a file attribute which specify the type of image
188 // (copper, solder paste ... and sides tpo, bottom or inner copper layers)
189 // Excellon drill files do not have attributes, so, just to identify the image
190 // In gerbview, we add this attribute, similat to a Gerber drill file
191 static const char file_attribute[] = ".FileFunction,Other,Drill*";
192 
194 {
195  { "M0", DRILL_M_END, -1 }, // End of Program - No Rewind
196  { "M00", DRILL_M_END, -1 }, // End of Program - No Rewind
197  { "M15", DRILL_M_TOOL_DOWN, 0 }, // tool down (starting a routed hole)
198  { "M16", DRILL_M_TOOL_UP, 0 }, // tool up (ending a routed hole)
199  { "M17", DRILL_M_TOOL_UP, 0 }, // tool up similar to M16 for a viewer
200  { "M30", DRILL_M_ENDFILE, -1 }, // End of File (last line of NC drill)
201  { "M47", DRILL_M_MESSAGE, -1 }, // Operator Message
202  { "M45", DRILL_M_LONGMESSAGE, -1 }, // Long Operator message (use more than one line)
203  { "M48", DRILL_M_HEADER, 0 }, // beginning of a header
204  { "M95", DRILL_M_ENDHEADER, 0 }, // End of the header
205  { "METRIC", DRILL_METRIC_HEADER, 1 },
206  { "INCH", DRILL_IMPERIAL_HEADER, 1 },
207  { "M71", DRILL_M_METRIC, 1 },
208  { "M72", DRILL_M_IMPERIAL, 1 },
209  { "M25", DRILL_M_BEGINPATTERN, 0 }, // Beginning of Pattern
210  { "M01", DRILL_M_ENDPATTERN, 0 }, // End of Pattern
211  { "M97", DRILL_M_CANNEDTEXT, -1 },
212  { "M98", DRILL_M_CANNEDTEXT, -1 },
213  { "DETECT", DRILL_DETECT_BROKEN, -1 },
214  { "ICI", DRILL_INCREMENTALHEADER, 1 },
215  { "FMAT", DRILL_FMT, 1 }, // Use Format command
216  { "ATC", DRILL_AUTOMATIC_TOOL_CHANGE, 0 },
217  { "TCST", DRILL_TOOL_CHANGE_STOP, 0 }, // Tool Change Stop
218  { "AFS", DRILL_AUTOMATIC_SPEED, 0 }, // Automatic Feeds and Speeds
219  { "VER", DRILL_AXIS_VERSION, 1 }, // Selection of X and Y Axis Version
220  { "R", DRILL_RESET_CMD, -1 }, // Reset commands
221  { "%", DRILL_REWIND_STOP, -1 }, // Rewind stop. End of the header
222  { "/", DRILL_SKIP, -1 }, // Clear Tool Linking. End of the header
223  // Keep this item after all commands starting by 'T':
224  { "T", DRILL_TOOL_INFORMATION, 0 }, // Tool Information
225  { "", DRILL_M_UNKNOWN, 0 } // last item in list
226 };
227 
229 {
230  { "G90", DRILL_G_ABSOLUTE, 0 }, // Absolute Mode
231  { "G91", DRILL_G_INCREMENTAL, 0 }, // Incremental Input Mode
232  { "G90", DRILL_G_ZEROSET, 0 }, // Absolute Mode
233  { "G00", DRILL_G_ROUT, 1 }, // Route Mode
234  { "G05", DRILL_G_DRILL, 0 }, // Drill Mode
235  { "G85", DRILL_G_SLOT, 0 }, // Canned Mode slot (oval holes)
236  { "G01", DRILL_G_LINEARMOVE, 1 }, // Linear (Straight Line) routing Mode
237  { "G02", DRILL_G_CWMOVE, 1 }, // Circular CW Mode
238  { "G03", DRILL_G_CCWMOVE, 1 }, // Circular CCW Mode
239  { "G93", DRILL_G_ZERO_SET, 1 }, // Zero Set (XnnYmm and coordinates origin)
240  { "", DRILL_G_UNKNOWN, 0 }, // last item in list
241 };
242 
243 
244 bool GERBVIEW_FRAME::Read_EXCELLON_File( const wxString& aFullFileName )
245 {
246  wxString msg;
247  int layerId = GetActiveLayer(); // current layer used in GerbView
249  auto gerber_layer = images->GetGbrImage( layerId );
250 
251  // OIf the active layer contains old gerber or nc drill data, remove it
252  if( gerber_layer )
253  Erase_Current_DrawLayer( false );
254 
255  EXCELLON_IMAGE* drill_layer = new EXCELLON_IMAGE( layerId );
256 
257  // Read the Excellon drill file:
258  bool success = drill_layer->LoadFile( aFullFileName );
259 
260  if( !success )
261  {
262  delete drill_layer;
263  msg.Printf( _( "File %s not found." ), aFullFileName );
264  ShowInfoBarError( msg );
265  return false;
266  }
267 
268  layerId = images->AddGbrImage( drill_layer, layerId );
269 
270  if( layerId < 0 )
271  {
272  delete drill_layer;
273  ShowInfoBarError( _( "No empty layers to load file into." ) );
274  return false;
275  }
276 
277  // Display errors list
278  if( drill_layer->GetMessages().size() > 0 )
279  {
280  HTML_MESSAGE_BOX dlg( this, _( "Error reading EXCELLON drill file" ) );
281  dlg.ListSet( drill_layer->GetMessages() );
282  dlg.ShowModal();
283  }
284 
285  if( GetCanvas() )
286  {
287  for( GERBER_DRAW_ITEM* item : drill_layer->GetItems() )
288  GetCanvas()->GetView()->Add( (KIGFX::VIEW_ITEM*) item );
289  }
290 
291  return success;
292 }
293 
294 /*
295  * Read a EXCELLON file.
296  * Gerber classes are used because there is likeness between Gerber files
297  * and Excellon files
298  * DCode can easily store T code (tool size) as round (or oval) shape
299  * Drill commands are similar to flashed gerber items
300  * Routing commands are similar to Gerber polygons
301  * coordinates have the same format as Gerber, can be given in:
302  * decimal format (i.i. floating notation format)
303  * integer 2.4 format in imperial units,
304  * integer 3.2 or 3.3 format (metric units).
305  */
306 
307 bool EXCELLON_IMAGE::LoadFile( const wxString & aFullFileName )
308 {
309  // Set the default parmeter values:
312 
313  m_Current_File = wxFopen( aFullFileName, "rt" );
314 
315  if( m_Current_File == NULL )
316  return false;
317 
318  wxString msg;
319  m_FileName = aFullFileName;
320 
321  LOCALE_IO toggleIo;
322 
323  // FILE_LINE_READER will close the file.
324  FILE_LINE_READER excellonReader( m_Current_File, m_FileName );
325 
326  while( true )
327  {
328  if( excellonReader.ReadLine() == 0 )
329  break;
330 
331  char* line = excellonReader.Line();
332  char* text = StrPurge( line );
333 
334  if( *text == ';' || *text == 0 ) // comment: skip line or empty malformed line
335  continue;
336 
338  {
340  }
341  else
342  {
343  switch( *text )
344  {
345  case 'M':
347  break;
348 
349  case 'G': // Line type Gxx : command
351  break;
352 
353  case 'X':
354  case 'Y': // command like X12550Y19250
355  Execute_Drill_Command(text);
356  break;
357 
358  case 'I':
359  case 'J': /* Auxiliary Move command */
360  m_IJPos = ReadIJCoord( text );
361  if( *text == '*' ) // command like X35142Y15945J504
362  {
363  Execute_Drill_Command( text);
364  }
365  break;
366 
367  case 'T': // Tool command
368  Select_Tool( text );
369  break;
370 
371  case '%':
372  break;
373 
374  default:
375  msg.Printf( "Unexpected symbol 0x%2.2X &lt;%c&gt;", *text, *text );
376  AddMessageToList( msg );
377  break;
378  } // End switch
379  }
380  }
381 
382  // Add our file attribute, to identify the drill file
384  char* text = (char*)file_attribute;
385  int dummyline = 0;
386  dummy.ParseAttribCmd( NULL, NULL, 0, text, dummyline );
387  delete m_FileFunction;
389 
390  m_InUse = true;
391 
392  return true;
393 }
394 
395 
397 {
398  EXCELLON_CMD* cmd = NULL;
399  wxString msg;
400 
401  // Search command in list
402  for( unsigned ii = 0; ; ii++ )
403  {
404  EXCELLON_CMD* candidate = &excellonHeaderCmdList[ii];
405  int len = candidate->m_Name.size();
406 
407  if( len == 0 ) // End of list reached
408  break;
409 
410  if( candidate->m_Name.compare( 0, len, text, len ) == 0 ) // found.
411  {
412  cmd = candidate;
413  text += len;
414  break;
415  }
416  }
417 
418  if( !cmd )
419  {
420  msg.Printf( _( "Unknown Excellon command &lt;%s&gt;" ), text );
421  AddMessageToList( msg );
422  while( *text )
423  text++;
424 
425  return false;
426  }
427 
428  // Execute command
429  // some do nothing
430  switch( cmd->m_Code )
431  {
432  case DRILL_SKIP:
433  case DRILL_M_UNKNOWN:
434  break;
435 
436  case DRILL_M_END:
437  case DRILL_M_ENDFILE:
438  // if a route command is in progress, finish it
439  if( m_RouteModeOn )
441 
442  break;
443 
444  case DRILL_M_MESSAGE:
445  break;
446 
447  case DRILL_M_LONGMESSAGE:
448  break;
449 
450  case DRILL_M_HEADER:
452  break;
453 
454  case DRILL_M_ENDHEADER:
456  break;
457 
458  case DRILL_REWIND_STOP: // End of header. No action in a viewer
460  break;
461 
462  case DRILL_M_METRIC:
463  SelectUnits( true );
464  break;
465 
466  case DRILL_IMPERIAL_HEADER: // command like INCH,TZ or INCH,LZ
467  case DRILL_METRIC_HEADER: // command like METRIC,TZ or METRIC,LZ
468  SelectUnits( cmd->m_Code == DRILL_METRIC_HEADER ? true : false );
469 
470  if( *text != ',' )
471  {
472  // No TZ or LZ specified. Should be a decimal format
473  // but this is not always the case. Use default TZ setting as default
474  m_NoTrailingZeros = false;
475  break;
476  }
477 
478  text++; // skip separator
479  if( *text == 'T' )
480  m_NoTrailingZeros = false;
481  else
482  m_NoTrailingZeros = true;
483  break;
484 
486  break;
487 
488  case DRILL_M_ENDPATTERN:
489  break;
490 
491  case DRILL_M_CANNEDTEXT:
492  break;
493 
494  case DRILL_M_TIPCHECK:
495  break;
496 
497  case DRILL_DETECT_BROKEN:
498  break;
499 
501  if( *text != ',' )
502  {
503  AddMessageToList( "ICI command has no parameter" );
504  break;
505  }
506  text++; // skip separator
507  // Parameter should be ON or OFF
508  if( strncasecmp( text, "OFF", 3 ) == 0 )
509  m_Relative = false;
510  else if( strncasecmp( text, "ON", 2 ) == 0 )
511  m_Relative = true;
512  else
513  AddMessageToList( "ICI command has incorrect parameter" );
514  break;
515 
517  break;
518 
520  break;
521 
522  case DRILL_AXIS_VERSION:
523  break;
524 
525  case DRILL_RESET_CMD:
526  break;
527 
529  break;
530 
531  case DRILL_FMT:
532  break;
533 
535  readToolInformation( text );
536  break;
537 
538  case DRILL_M_TOOL_DOWN: // tool down (starting a routed hole or polyline)
539  // Only the last position is usefull:
540  if( m_RoutePositions.size() > 1 )
541  m_RoutePositions.erase( m_RoutePositions.begin(), m_RoutePositions.begin() + m_RoutePositions.size() - 1 );
542 
543  break;
544 
545  case DRILL_M_TOOL_UP: // tool up (ending a routed polyline)
547  break;
548  }
549 
550  while( *text )
551  text++;
552 
553  return true;
554 }
555 
556 
558 {
559  // Read a tool definition like T1C0.02 or T1F00S00C0.02 or T1C0.02F00S00
560  // and enter the TCODE param in list (using the D_CODE param management, which
561  // is similar to TCODE params.
562  if( *aText == 'T' ) // This is the beginning of the definition
563  aText++;
564 
565  // Read tool number:
566  int iprm = ReadInt( aText, false );
567 
568  // Skip Feed rate and Spindle speed, if any here
569  while( *aText && ( *aText == 'F' || *aText == 'S' ) )
570  {
571  aText++;
572  ReadInt( aText, false );
573  }
574 
575  // Read tool shape
576  if( ! *aText )
578  _( "Tool definition shape not found" ) ) );
579  else if( *aText != 'C' )
581  _( "Tool definition '%c' not supported" ), *aText ) );
582  if( *aText )
583  aText++;
584 
585  //read tool diameter:
586  double dprm = ReadDouble( aText, false );
587  m_Has_DCode = true;
588 
589  // Initialize Dcode to handle this Tool
590  // Remember: dcodes are >= FIRST_DCODE
591  D_CODE* dcode = GetDCODEOrCreate( iprm + FIRST_DCODE );
592 
593  if( dcode == NULL )
594  return false;
595 
596  // conv_scale = scaling factor from inch to Internal Unit
597  double conv_scale = IU_PER_MILS * 1000;
598 
599  if( m_GerbMetric )
600  conv_scale /= 25.4;
601 
602  dcode->m_Size.x = dcode->m_Size.y = KiROUND( dprm * conv_scale );
603  dcode->m_Shape = APT_CIRCLE;
604  dcode->m_Defined = true;
605 
606  return true;
607 }
608 
609 
611 {
612  D_CODE* tool;
613  GERBER_DRAW_ITEM * gbritem;
614 
615  while( true )
616  {
617  switch( *text )
618  {
619  case 'X':
620  case 'Y':
621  ReadXYCoord( text, true );
622 
623  if( *text == 'I' || *text == 'J' )
624  ReadIJCoord( text );
625 
626  break;
627 
628  case 'G': // G85 is found here for oval holes
631  break;
632 
633  case 0: // E.O.L: execute command
634  if( m_RouteModeOn )
635  {
636  // We are in routing mode, and this is an intermediate point.
637  // So just store it
638  int rmode = 0; // linear routing.
639 
641  rmode = ROUTE_CW;
643  rmode = ROUTE_CCW;
644 
646  {
647  EXCELLON_ROUTE_COORD point( m_CurrentPos, m_IJPos, rmode );
648  m_RoutePositions.push_back( point );
649  }
650  else
651  {
653  m_RoutePositions.push_back( point );
654  }
655  return true;
656  }
657 
658  tool = GetDCODE( m_Current_Tool );
659  if( !tool )
660  {
661  wxString msg;
662  msg.Printf( _( "Tool %d not defined" ), m_Current_Tool );
663  AddMessageToList( msg );
664  return false;
665  }
666 
667  gbritem = new GERBER_DRAW_ITEM( this );
668  AddItemToList( gbritem );
669 
670  if( m_SlotOn ) // Oblong hole
671  {
672  fillLineGBRITEM( gbritem, tool->m_Num_Dcode,
674  tool->m_Size, false );
675  // the hole is made: reset the slot on command (G85)
676  // (it is needed for each oblong hole)
677  m_SlotOn = false;
678  }
679  else
680  {
681  fillFlashedGBRITEM( gbritem, tool->m_Shape, tool->m_Num_Dcode,
682  m_CurrentPos, tool->m_Size, false );
683  }
684 
685  StepAndRepeatItem( *gbritem );
687  return true;
688  break;
689 
690  default:
691  text++;
692  break;
693  }
694  }
695 
696  return true;
697 }
698 
699 
700 bool EXCELLON_IMAGE::Select_Tool( char*& text )
701 {
702  // Select the tool from the command line Tn, with n = 1 ... TOOLS_MAX_COUNT - 1
703  // Because some drill file have an embedded TCODE definition (like T1C.008F0S0)
704  // in tool selection command, if the tool is not defined in list,
705  // and the definition is embedded, it will be entered in list
706  char * startline = text; // the tool id starts here.
707  int tool_id = TCodeNumber( text );
708 
709  // T0 is legal, but is not a selection tool. it is a special command
710  if( tool_id >= 0 )
711  {
712  int dcode_id = tool_id + FIRST_DCODE; // Remember: dcodes are >= FIRST_DCODE
713 
714  if( dcode_id > (TOOLS_MAX_COUNT - 1) )
715  dcode_id = TOOLS_MAX_COUNT - 1;
716 
717  m_Current_Tool = dcode_id;
718  D_CODE* currDcode = GetDCODEOrCreate( dcode_id, true );
719 
720  if( currDcode == NULL && tool_id > 0 ) // if the definition is embedded, enter it
721  {
722  text = startline; // text starts at the beginning of the command
723  readToolInformation( text );
724  currDcode = GetDCODE( dcode_id );
725  }
726 
727  if( currDcode )
728  currDcode->m_InUse = true;
729  }
730 
731  while( *text )
732  text++;
733 
734  return tool_id >= 0;
735 }
736 
737 
739 {
740  EXCELLON_CMD* cmd = NULL;
741  bool success = false;
742  int id = DRILL_G_UNKNOWN;
743 
744  // Search command in list
745  EXCELLON_CMD* candidate;
746  char * gcmd = text; // gcmd points the G command, for error messages.
747 
748  for( unsigned ii = 0; ; ii++ )
749  {
750  candidate = &excellon_G_CmdList[ii];
751  int len = candidate->m_Name.size();
752  if( len == 0 ) // End of list reached
753  break;
754  if( candidate->m_Name.compare( 0, len, text, len ) == 0 ) // found.
755  {
756  cmd = candidate;
757  text += len;
758  success = true;
759  id = cmd->m_Code;
760  break;
761  }
762  }
763 
764  switch( id )
765  {
766  case DRILL_G_ZERO_SET:
767  ReadXYCoord( text, true );
769  break;
770 
771  case DRILL_G_ROUT:
772  m_SlotOn = false;
773 
774  if( m_RouteModeOn )
776 
777  m_RouteModeOn = true;
778  m_RoutePositions.clear();
780  ReadXYCoord( text, true );
781  // This is the first point (starting point) of routing
782  m_RoutePositions.emplace_back( m_CurrentPos );
783  break;
784 
785  case DRILL_G_DRILL:
786  m_SlotOn = false;
787 
788  if( m_RouteModeOn )
790 
791  m_RouteModeOn = false;
792  m_RoutePositions.clear();
794  break;
795 
796  case DRILL_G_SLOT:
797  m_SlotOn = true;
798  break;
799 
800  case DRILL_G_LINEARMOVE:
803  ReadXYCoord( text, true );
804  m_RoutePositions.emplace_back( m_CurrentPos );
805  break;
806 
807  case DRILL_G_CWMOVE:
809  ReadXYCoord( text, true );
810 
811  if( *text == 'I' || *text == 'J' )
812  ReadIJCoord( text );
813 
815  m_RoutePositions.emplace_back( m_CurrentPos, m_IJPos, ROUTE_CW );
816  else
818  break;
819 
820  case DRILL_G_CCWMOVE:
822  ReadXYCoord( text, true );
823 
824  if( *text == 'I' || *text == 'J' )
825  ReadIJCoord( text );
826 
829  else
831  break;
832 
833  case DRILL_G_ABSOLUTE:
834  m_Relative = false; // false = absolute coord
835  break;
836 
837  case DRILL_G_INCREMENTAL:
838  m_Relative = true; // true = relative coord
839  break;
840 
841  case DRILL_G_UNKNOWN:
842  default:
843  AddMessageToList( wxString::Format( _( "Unknown Excellon G Code: &lt;%s&gt;" ), FROM_UTF8(gcmd) ) );
844  while( *text )
845  text++;
846  return false;
847  }
848 
849  return success;
850 }
851 
852 void EXCELLON_IMAGE::SelectUnits( bool aMetric )
853 {
854  /* Coordinates are measured either in inch or metric (millimeters).
855  * Inch coordinates are in six digits (00.0000) with increments
856  * as small as 0.0001 (1/10,000).
857  * Metric coordinates can be measured in microns (thousandths of a millimeter)
858  * in one of the following three ways:
859  * Five digit 10 micron resolution (000.00)
860  * Six digit 10 micron resolution (0000.00)
861  * Six digit micron resolution (000.000)
862  *
863  * Inches: Default fmt = 2.4 for X and Y axis: 6 digits with 0.0001 resolution
864  * metric: Default fmt = 3.3 for X and Y axis: 6 digits, 1 micron resolution
865  */
866  if( aMetric )
867  {
868  m_GerbMetric = true;
869  // number of digits in mantissa
871  // number of digits (mantissa+interger)
873  }
874  else
875  {
876  m_GerbMetric = false;
879  }
880 }
881 
882 
884 {
885  // Ends a route command started by M15 ot G01, G02 or G03 command
886  // if a route command is not in progress, do nothing
887 
888  if( !m_RouteModeOn )
889  return;
890 
891  D_CODE* tool = GetDCODE( m_Current_Tool );
892 
893  if( !tool )
894  {
895  AddMessageToList( wxString::Format( "Unknown tool code %d", m_Current_Tool ) );
896  return;
897  }
898 
899  for( size_t ii = 1; ii < m_RoutePositions.size(); ii++ )
900  {
901  GERBER_DRAW_ITEM* gbritem = new GERBER_DRAW_ITEM( this );
902 
903  if( m_RoutePositions[ii].m_rmode == 0 ) // linear routing
904  {
905  fillLineGBRITEM( gbritem, tool->m_Num_Dcode,
906  m_RoutePositions[ii-1].GetPos(), m_RoutePositions[ii].GetPos(),
907  tool->m_Size, false );
908  }
909  else // circular (cw or ccw) routing
910  {
911  bool rot_ccw = m_RoutePositions[ii].m_rmode == ROUTE_CW;
912  int radius = m_RoutePositions[ii].m_radius; // Can be adjusted by computeCenter.
913  wxPoint center;
914 
915  if( m_RoutePositions[ii].m_arc_type_info == ARC_INFO_TYPE_CENTER )
916  center = wxPoint( m_RoutePositions[ii].m_cx, m_RoutePositions[ii].m_cy );
917  else
918  center = computeCenter( m_RoutePositions[ii-1].GetPos(),
919  m_RoutePositions[ii].GetPos(), radius, rot_ccw );
920 
921  fillArcGBRITEM( gbritem, tool->m_Num_Dcode,
922  m_RoutePositions[ii-1].GetPos(), m_RoutePositions[ii].GetPos(),
923  center - m_RoutePositions[ii-1].GetPos(),
924  tool->m_Size, not rot_ccw , true,
925  false );
926  }
927 
928  AddItemToList( gbritem );
929 
930  StepAndRepeatItem( *gbritem );
931  }
932 
933  m_RoutePositions.clear();
934  m_RouteModeOn = false;
935 }
static EXCELLON_CMD excellon_G_CmdList[]
X2_ATTRIBUTE_FILEFUNCTION ( from TF.FileFunction in Gerber file) Example file function: TF....
void AddMessageToList(const wxString &aMessage)
Function AddMessageToList Add a message to the message list.
X2_ATTRIBUTE_FILEFUNCTION * m_FileFunction
char * ReadLine() override
Function ReadLine reads a line of text into the buffer and increments the line number counter.
Definition: richio.cpp:198
char * Line() const
Function Line returns a pointer to the last line that was read in.
Definition: richio.h:140
X2_ATTRIBUTE The attribute value consists of a number of substrings separated by a comma.
static wxString FROM_UTF8(const char *cstring)
function FROM_UTF8 converts a UTF8 encoded C string to a wxString for all wxWidgets build modes.
Definition: macros.h:109
char * StrPurge(char *text)
Remove leading and training spaces, tabs and end of line chars in text.
Definition: string.cpp:365
virtual void ResetDefaultValues() override
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:40
GERBER_DRAW_ITEMS & GetItems()
wxSize m_Size
Horizontal and vertical dimensions.
Definition: dcode.h:94
wxPoint ReadIJCoord(char *&Text)
Returns the current coordinate type pointed to by InnJnn Text (InnnnJmmmm) These coordinates are rela...
bool m_InUse
false if the aperure (previously defined) is not used to draw something
Definition: dcode.h:102
#define ROUTE_CCW
APERTURE_T m_Shape
shape ( Line, rectangle, circle , oval .. )
Definition: dcode.h:95
static EXCELLON_CMD excellonHeaderCmdList[]
static wxPoint computeCenter(wxPoint aStart, wxPoint aEnd, int &aRadius, bool aRotCCW)
static const int fmtMantissaInch
virtual EDA_DRAW_PANEL_GAL * GetCanvas() const
Return a pointer to GAL-based canvas of given EDA draw frame.
static const int fmtIntegerInch
D_CODE * GetDCODE(int aDCODE) const
Function GetDCODE returns a pointer to the D_CODE within this GERBER for the given aDCODE.
static const int fmtMantissaMM
void fillFlashedGBRITEM(GERBER_DRAW_ITEM *aGbrItem, APERTURE_T aAperture, int Dcode_index, const wxPoint &aPos, wxSize aSize, bool aLayerNegative)
Function fillFlashedGBRITEM initializes a given GBRITEM so that it can draw a circle which is filled ...
Definition: rs274d.cpp:100
void ClearMessageList()
Function ClearMessageList Clear the message list Call it before reading a Gerber file.
bool Select_Tool(char *&text)
int AddGbrImage(GERBER_FILE_IMAGE *aGbrImage, int aIdx)
Add a GERBER_FILE_IMAGE* at index aIdx or at the first free location if aIdx < 0.
VIEW_ITEM - is an abstract base class for deriving all objects that can be added to a VIEW.
Definition: view_item.h:85
void Erase_Current_DrawLayer(bool query)
GERBER_FILE_IMAGE_LIST is a helper class to handle a list of GERBER_FILE_IMAGE files which are loaded...
This file contains miscellaneous commonly used macros and functions.
bool LoadFile(const wxString &aFullFileName)
Read and load a drill (EXCELLON format) file.
bool m_Defined
false if the aperture is not defined in the header
Definition: dcode.h:104
FILE_LINE_READER is a LINE_READER that reads from an open file.
Definition: richio.h:181
void AddItemToList(GERBER_DRAW_ITEM *aItem)
Add a new GERBER_DRAW_ITEM item to the drawings list.
bool Execute_HEADER_And_M_Command(char *&text)
#define NULL
VECTOR2< double > VECTOR2D
Definition: vector2d.h:593
GERBER_FILE_IMAGE * GetGbrImage(int aIdx)
#define FIRST_DCODE
Definition: dcode.h:71
APERTURE_T
Enum APERTURE_T is the set of all gerber aperture types allowed, according to page 16 of http://gerbv...
Definition: dcode.h:50
const wxArrayString & GetMessages() const
void ListSet(const wxString &aList)
Add a list of items.
int m_Num_Dcode
D code value ( >= 10 )
Definition: dcode.h:96
static const int fmtIntegerMM
bool Execute_Drill_Command(char *&text)
virtual KIGFX::VIEW * GetView() const
Function GetView() Returns a pointer to the VIEW instance used in the panel.
#define ROUTE_CW
std::string m_Name
HTML_MESSAGE_BOX.
double Angle() const
Function Angle computes the angle of the vector.
Definition: vector2d.h:313
void ShowInfoBarError(const wxString &aErrorMsg)
wxPoint ReadXYCoord(char *&aText, bool aExcellonMode=false)
Function ReadXYCoord Returns the current coordinate type pointed to by XnnYnn Text (XnnnnYmmmm)
excellon_state m_State
GBR_LAYOUT * GetGerberLayout() const
static const char file_attribute[]
int TCodeNumber(char *&aText)
bool readToolInformation(char *&aText)
Read a tool definition like T1C0.02 or T1F00S00C0.02 or T1C0.02F00S00 and enter params in TCODE list.
int GetActiveLayer() const
Function SetActiveLayer returns the active layer.
VECTOR2< T > Rotate(double aAngle) const
Function Rotate rotates the vector by a given angle.
Definition: vector2d.h:377
LAST_EXTRA_ARC_DATA_TYPE m_LastArcDataType
D_CODE holds a gerber DCODE (also called Aperture) definition.
Definition: dcode.h:82
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:201
static LIB_PART * dummy()
Used to draw a dummy shape when a LIB_PART is not found in library.
#define _(s)
Definition: 3d_actions.cpp:33
void FinishRouteCommand()
Ends a route command started by M15 ot G01, G02 or G03 command.
double ReadDouble(char *&text, bool aSkipSeparator=true)
Function ReadDouble reads a double from an ASCII character buffer.
#define TOOLS_MAX_COUNT
Definition: dcode.h:73
GERBER_FILE_IMAGE_LIST * GetImagesList() const
Definition: gbr_layout.cpp:41
#define IU_PER_MILS
Definition: plotter.cpp:137
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:68
std::vector< EXCELLON_ROUTE_COORD > m_RoutePositions
T EuclideanNorm() const
Destructor.
Definition: vector2d.h:299
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Function Add() Adds a VIEW_ITEM to the view.
Definition: view.cpp:327
bool Execute_EXCELLON_G_Command(char *&text)
void SelectUnits(bool aMetric)
void StepAndRepeatItem(const GERBER_DRAW_ITEM &aItem)
Function StepAndRepeatItem Gerber format has a command Step an Repeat This function must be called wh...
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 fillArcGBRITEM(GERBER_DRAW_ITEM *aGbrItem, int Dcode_index, const wxPoint &aStart, const wxPoint &aEnd, const wxPoint &aRelCenter, wxSize aPenSize, bool aClockwise, bool aMultiquadrant, bool aLayerNegative)
Function fillArcGBRITEM initializes a given GBRITEM so that it can draw an arc G code.
Definition: rs274d.cpp:204
void fillLineGBRITEM(GERBER_DRAW_ITEM *aGbrItem, int Dcode_index, const wxPoint &aStart, const wxPoint &aEnd, wxSize aPenSize, bool aLayerNegative)
Function fillLineGBRITEM initializes a given GBRITEM so that it can draw a linear D code.
Definition: rs274d.cpp:155
bool Read_EXCELLON_File(const wxString &aFullFileName)
int ReadInt(char *&text, bool aSkipSeparator=true)
Function ReadInt reads an int from an ASCII character buffer.