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, similar 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  std::unique_ptr<EXCELLON_IMAGE> drill_layer_uptr = std::make_unique<EXCELLON_IMAGE>( layerId );
256 
257  // Read the Excellon drill file:
258  bool success = drill_layer_uptr->LoadFile( aFullFileName );
259 
260  if( !success )
261  {
262  drill_layer_uptr.reset();
263  msg.Printf( _( "File %s not found." ), aFullFileName );
264  ShowInfoBarError( msg );
265  return false;
266  }
267 
268  EXCELLON_IMAGE* drill_layer = drill_layer_uptr.release();
269 
270  layerId = images->AddGbrImage( drill_layer, layerId );
271 
272  if( layerId < 0 )
273  {
274  delete drill_layer;
275  ShowInfoBarError( _( "No empty layers to load file into." ) );
276  return false;
277  }
278 
279  // Display errors list
280  if( drill_layer->GetMessages().size() > 0 )
281  {
282  HTML_MESSAGE_BOX dlg( this, _( "Error reading EXCELLON drill file" ) );
283  dlg.ListSet( drill_layer->GetMessages() );
284  dlg.ShowModal();
285  }
286 
287  if( GetCanvas() )
288  {
289  for( GERBER_DRAW_ITEM* item : drill_layer->GetItems() )
290  GetCanvas()->GetView()->Add( (KIGFX::VIEW_ITEM*) item );
291  }
292 
293  return success;
294 }
295 
296 /*
297  * Read a EXCELLON file.
298  * Gerber classes are used because there is likeness between Gerber files
299  * and Excellon files
300  * DCode can easily store T code (tool size) as round (or oval) shape
301  * Drill commands are similar to flashed gerber items
302  * Routing commands are similar to Gerber polygons
303  * coordinates have the same format as Gerber, can be given in:
304  * decimal format (i.i. floating notation format)
305  * integer 2.4 format in imperial units,
306  * integer 3.2 or 3.3 format (metric units).
307  */
308 
309 bool EXCELLON_IMAGE::LoadFile( const wxString & aFullFileName )
310 {
311  // Set the default parameter values:
314 
315  m_Current_File = wxFopen( aFullFileName, "rt" );
316 
317  if( m_Current_File == nullptr )
318  return false;
319 
320  wxString msg;
321  m_FileName = aFullFileName;
322 
323  LOCALE_IO toggleIo;
324 
325  // FILE_LINE_READER will close the file.
326  FILE_LINE_READER excellonReader( m_Current_File, m_FileName );
327 
328  while( true )
329  {
330  if( excellonReader.ReadLine() == nullptr )
331  break;
332 
333  char* line = excellonReader.Line();
334  char* text = StrPurge( line );
335 
336  if( *text == ';' || *text == 0 ) // comment: skip line or empty malformed line
337  continue;
338 
340  {
342  }
343  else
344  {
345  switch( *text )
346  {
347  case 'M':
349  break;
350 
351  case 'G': // Line type Gxx : command
353  break;
354 
355  case 'X':
356  case 'Y': // command like X12550Y19250
358  break;
359 
360  case 'I':
361  case 'J': /* Auxiliary Move command */
362  m_IJPos = ReadIJCoord( text );
363  if( *text == '*' ) // command like X35142Y15945J504
364  {
366  }
367  break;
368 
369  case 'T': // Tool command
370  Select_Tool( text );
371  break;
372 
373  case '%':
374  break;
375 
376  default:
377  msg.Printf( "Unexpected symbol 0x%2.2X &lt;%c&gt;", *text, *text );
378  AddMessageToList( msg );
379  break;
380  } // End switch
381  }
382  }
383 
384  // Add our file attribute, to identify the drill file
386  char* text = (char*)file_attribute;
387  int dummyline = 0;
388  dummy.ParseAttribCmd( nullptr, nullptr, 0, text, dummyline );
389  delete m_FileFunction;
391 
392  m_InUse = true;
393 
394  return true;
395 }
396 
397 
399 {
400  EXCELLON_CMD* cmd = nullptr;
401  wxString msg;
402 
403  // Search command in list
404  for( unsigned ii = 0; ; ii++ )
405  {
406  EXCELLON_CMD* candidate = &excellonHeaderCmdList[ii];
407  int len = candidate->m_Name.size();
408 
409  if( len == 0 ) // End of list reached
410  break;
411 
412  if( candidate->m_Name.compare( 0, len, text, len ) == 0 ) // found.
413  {
414  cmd = candidate;
415  text += len;
416  break;
417  }
418  }
419 
420  if( !cmd )
421  {
422  msg.Printf( _( "Unknown Excellon command &lt;%s&gt;" ), text );
423  AddMessageToList( msg );
424  while( *text )
425  text++;
426 
427  return false;
428  }
429 
430  // Execute command
431  // some do nothing
432  switch( cmd->m_Code )
433  {
434  case DRILL_SKIP:
435  case DRILL_M_UNKNOWN:
436  break;
437 
438  case DRILL_M_END:
439  case DRILL_M_ENDFILE:
440  // if a route command is in progress, finish it
441  if( m_RouteModeOn )
443 
444  break;
445 
446  case DRILL_M_MESSAGE:
447  break;
448 
449  case DRILL_M_LONGMESSAGE:
450  break;
451 
452  case DRILL_M_HEADER:
454  break;
455 
456  case DRILL_M_ENDHEADER:
458  break;
459 
460  case DRILL_REWIND_STOP: // End of header. No action in a viewer
462  break;
463 
464  case DRILL_M_METRIC:
465  SelectUnits( true );
466  break;
467 
468  case DRILL_IMPERIAL_HEADER: // command like INCH,TZ or INCH,LZ
469  case DRILL_METRIC_HEADER: // command like METRIC,TZ or METRIC,LZ
470  SelectUnits( cmd->m_Code == DRILL_METRIC_HEADER ? true : false );
471 
472  if( *text != ',' )
473  {
474  // No TZ or LZ specified. Should be a decimal format
475  // but this is not always the case. Use default TZ setting as default
476  m_NoTrailingZeros = false;
477  break;
478  }
479 
480  text++; // skip separator
481  if( *text == 'T' )
482  m_NoTrailingZeros = false;
483  else
484  m_NoTrailingZeros = true;
485  break;
486 
488  break;
489 
490  case DRILL_M_ENDPATTERN:
491  break;
492 
493  case DRILL_M_CANNEDTEXT:
494  break;
495 
496  case DRILL_M_TIPCHECK:
497  break;
498 
499  case DRILL_DETECT_BROKEN:
500  break;
501 
503  if( *text != ',' )
504  {
505  AddMessageToList( "ICI command has no parameter" );
506  break;
507  }
508  text++; // skip separator
509  // Parameter should be ON or OFF
510  if( strncasecmp( text, "OFF", 3 ) == 0 )
511  m_Relative = false;
512  else if( strncasecmp( text, "ON", 2 ) == 0 )
513  m_Relative = true;
514  else
515  AddMessageToList( "ICI command has incorrect parameter" );
516  break;
517 
519  break;
520 
522  break;
523 
524  case DRILL_AXIS_VERSION:
525  break;
526 
527  case DRILL_RESET_CMD:
528  break;
529 
531  break;
532 
533  case DRILL_FMT:
534  break;
535 
538  break;
539 
540  case DRILL_M_TOOL_DOWN: // tool down (starting a routed hole or polyline)
541  // Only the last position is useful:
542  if( m_RoutePositions.size() > 1 )
543  m_RoutePositions.erase( m_RoutePositions.begin(), m_RoutePositions.begin() + m_RoutePositions.size() - 1 );
544 
545  break;
546 
547  case DRILL_M_TOOL_UP: // tool up (ending a routed polyline)
549  break;
550  }
551 
552  while( *text )
553  text++;
554 
555  return true;
556 }
557 
558 
560 {
561  // Read a tool definition like T1C0.02 or T1F00S00C0.02 or T1C0.02F00S00
562  // and enter the TCODE param in list (using the D_CODE param management, which
563  // is similar to TCODE params.
564  if( *aText == 'T' ) // This is the beginning of the definition
565  aText++;
566 
567  // Read tool number:
568  int iprm = ReadInt( aText, false );
569 
570  // Skip Feed rate and Spindle speed, if any here
571  while( *aText && ( *aText == 'F' || *aText == 'S' ) )
572  {
573  aText++;
574  ReadInt( aText, false );
575  }
576 
577  // Read tool shape
578  if( ! *aText )
580  _( "Tool definition shape not found" ) ) );
581  else if( *aText != 'C' )
583  _( "Tool definition '%c' not supported" ), *aText ) );
584  if( *aText )
585  aText++;
586 
587  //read tool diameter:
588  double dprm = ReadDouble( aText, false );
589  m_Has_DCode = true;
590 
591  // Initialize Dcode to handle this Tool
592  // Remember: dcodes are >= FIRST_DCODE
593  D_CODE* dcode = GetDCODEOrCreate( iprm + FIRST_DCODE );
594 
595  if( dcode == nullptr )
596  return false;
597 
598  // conv_scale = scaling factor from inch to Internal Unit
599  double conv_scale = IU_PER_MILS * 1000;
600 
601  if( m_GerbMetric )
602  conv_scale /= 25.4;
603 
604  dcode->m_Size.x = dcode->m_Size.y = KiROUND( dprm * conv_scale );
605  dcode->m_Shape = APT_CIRCLE;
606  dcode->m_Defined = true;
607 
608  return true;
609 }
610 
611 
613 {
614  D_CODE* tool;
615  GERBER_DRAW_ITEM * gbritem;
616 
617  while( true )
618  {
619  switch( *text )
620  {
621  case 'X':
622  case 'Y':
623  ReadXYCoord( text, true );
624 
625  if( *text == 'I' || *text == 'J' )
626  ReadIJCoord( text );
627 
628  break;
629 
630  case 'G': // G85 is found here for oval holes
633  break;
634 
635  case 0: // E.O.L: execute command
636  if( m_RouteModeOn )
637  {
638  // We are in routing mode, and this is an intermediate point.
639  // So just store it
640  int rmode = 0; // linear routing.
641 
643  rmode = ROUTE_CW;
645  rmode = ROUTE_CCW;
646 
648  {
649  EXCELLON_ROUTE_COORD point( m_CurrentPos, m_IJPos, rmode );
650  m_RoutePositions.push_back( point );
651  }
652  else
653  {
655  m_RoutePositions.push_back( point );
656  }
657  return true;
658  }
659 
660  tool = GetDCODE( m_Current_Tool );
661  if( !tool )
662  {
663  wxString msg;
664  msg.Printf( _( "Tool %d not defined" ), m_Current_Tool );
665  AddMessageToList( msg );
666  return false;
667  }
668 
669  gbritem = new GERBER_DRAW_ITEM( this );
670  AddItemToList( gbritem );
671 
672  if( m_SlotOn ) // Oblong hole
673  {
674  fillLineGBRITEM( gbritem, tool->m_Num_Dcode,
676  tool->m_Size, false );
677  // the hole is made: reset the slot on command (G85)
678  // (it is needed for each oblong hole)
679  m_SlotOn = false;
680  }
681  else
682  {
683  fillFlashedGBRITEM( gbritem, tool->m_Shape, tool->m_Num_Dcode,
684  m_CurrentPos, tool->m_Size, false );
685  }
686 
687  StepAndRepeatItem( *gbritem );
689  return true;
690  break;
691 
692  default:
693  text++;
694  break;
695  }
696  }
697 
698  return true;
699 }
700 
701 
702 bool EXCELLON_IMAGE::Select_Tool( char*& text )
703 {
704  // Select the tool from the command line Tn, with n = 1 ... TOOLS_MAX_COUNT - 1
705  // Because some drill file have an embedded TCODE definition (like T1C.008F0S0)
706  // in tool selection command, if the tool is not defined in list,
707  // and the definition is embedded, it will be entered in list
708  char * startline = text; // the tool id starts here.
709  int tool_id = TCodeNumber( text );
710 
711  // T0 is legal, but is not a selection tool. it is a special command
712  if( tool_id >= 0 )
713  {
714  int dcode_id = tool_id + FIRST_DCODE; // Remember: dcodes are >= FIRST_DCODE
715 
716  if( dcode_id > (TOOLS_MAX_COUNT - 1) )
717  dcode_id = TOOLS_MAX_COUNT - 1;
718 
719  m_Current_Tool = dcode_id;
720  D_CODE* currDcode = GetDCODEOrCreate( dcode_id, true );
721 
722  if( currDcode == nullptr && tool_id > 0 ) // if the definition is embedded, enter it
723  {
724  text = startline; // text starts at the beginning of the command
726  currDcode = GetDCODE( dcode_id );
727  }
728 
729  if( currDcode )
730  currDcode->m_InUse = true;
731  }
732 
733  while( *text )
734  text++;
735 
736  return tool_id >= 0;
737 }
738 
739 
741 {
742  EXCELLON_CMD* cmd = nullptr;
743  bool success = false;
744  int id = DRILL_G_UNKNOWN;
745 
746  // Search command in list
747  EXCELLON_CMD* candidate;
748  char * gcmd = text; // gcmd points the G command, for error messages.
749 
750  for( unsigned ii = 0; ; ii++ )
751  {
752  candidate = &excellon_G_CmdList[ii];
753  int len = candidate->m_Name.size();
754  if( len == 0 ) // End of list reached
755  break;
756  if( candidate->m_Name.compare( 0, len, text, len ) == 0 ) // found.
757  {
758  cmd = candidate;
759  text += len;
760  success = true;
761  id = cmd->m_Code;
762  break;
763  }
764  }
765 
766  switch( id )
767  {
768  case DRILL_G_ZERO_SET:
769  ReadXYCoord( text, true );
771  break;
772 
773  case DRILL_G_ROUT:
774  m_SlotOn = false;
775 
776  if( m_RouteModeOn )
778 
779  m_RouteModeOn = true;
780  m_RoutePositions.clear();
782  ReadXYCoord( text, true );
783  // This is the first point (starting point) of routing
784  m_RoutePositions.emplace_back( m_CurrentPos );
785  break;
786 
787  case DRILL_G_DRILL:
788  m_SlotOn = false;
789 
790  if( m_RouteModeOn )
792 
793  m_RouteModeOn = false;
794  m_RoutePositions.clear();
796  break;
797 
798  case DRILL_G_SLOT:
799  m_SlotOn = true;
800  break;
801 
802  case DRILL_G_LINEARMOVE:
805  ReadXYCoord( text, true );
806  m_RoutePositions.emplace_back( m_CurrentPos );
807  break;
808 
809  case DRILL_G_CWMOVE:
811  ReadXYCoord( text, true );
812 
813  if( *text == 'I' || *text == 'J' )
814  ReadIJCoord( text );
815 
817  m_RoutePositions.emplace_back( m_CurrentPos, m_IJPos, ROUTE_CW );
818  else
820  break;
821 
822  case DRILL_G_CCWMOVE:
824  ReadXYCoord( text, true );
825 
826  if( *text == 'I' || *text == 'J' )
827  ReadIJCoord( text );
828 
831  else
833  break;
834 
835  case DRILL_G_ABSOLUTE:
836  m_Relative = false; // false = absolute coord
837  break;
838 
839  case DRILL_G_INCREMENTAL:
840  m_Relative = true; // true = relative coord
841  break;
842 
843  case DRILL_G_UNKNOWN:
844  default:
845  AddMessageToList( wxString::Format( _( "Unknown Excellon G Code: &lt;%s&gt;" ), FROM_UTF8(gcmd) ) );
846  while( *text )
847  text++;
848  return false;
849  }
850 
851  return success;
852 }
853 
854 void EXCELLON_IMAGE::SelectUnits( bool aMetric )
855 {
856  /* Coordinates are measured either in inch or metric (millimeters).
857  * Inch coordinates are in six digits (00.0000) with increments
858  * as small as 0.0001 (1/10,000).
859  * Metric coordinates can be measured in microns (thousandths of a millimeter)
860  * in one of the following three ways:
861  * Five digit 10 micron resolution (000.00)
862  * Six digit 10 micron resolution (0000.00)
863  * Six digit micron resolution (000.000)
864  *
865  * Inches: Default fmt = 2.4 for X and Y axis: 6 digits with 0.0001 resolution
866  * metric: Default fmt = 3.3 for X and Y axis: 6 digits, 1 micron resolution
867  */
868  if( aMetric )
869  {
870  m_GerbMetric = true;
871  // number of digits in mantissa
873  // number of digits (mantissa+integer)
875  }
876  else
877  {
878  m_GerbMetric = false;
881  }
882 }
883 
884 
886 {
887  // Ends a route command started by M15 ot G01, G02 or G03 command
888  // if a route command is not in progress, do nothing
889 
890  if( !m_RouteModeOn )
891  return;
892 
893  D_CODE* tool = GetDCODE( m_Current_Tool );
894 
895  if( !tool )
896  {
897  AddMessageToList( wxString::Format( "Unknown tool code %d", m_Current_Tool ) );
898  return;
899  }
900 
901  for( size_t ii = 1; ii < m_RoutePositions.size(); ii++ )
902  {
903  GERBER_DRAW_ITEM* gbritem = new GERBER_DRAW_ITEM( this );
904 
905  if( m_RoutePositions[ii].m_rmode == 0 ) // linear routing
906  {
907  fillLineGBRITEM( gbritem, tool->m_Num_Dcode,
908  m_RoutePositions[ii-1].GetPos(), m_RoutePositions[ii].GetPos(),
909  tool->m_Size, false );
910  }
911  else // circular (cw or ccw) routing
912  {
913  bool rot_ccw = m_RoutePositions[ii].m_rmode == ROUTE_CW;
914  int radius = m_RoutePositions[ii].m_radius; // Can be adjusted by computeCenter.
915  wxPoint center;
916 
917  if( m_RoutePositions[ii].m_arc_type_info == ARC_INFO_TYPE_CENTER )
918  center = wxPoint( m_RoutePositions[ii].m_cx, m_RoutePositions[ii].m_cy );
919  else
920  center = computeCenter( m_RoutePositions[ii-1].GetPos(),
921  m_RoutePositions[ii].GetPos(), radius, rot_ccw );
922 
923  fillArcGBRITEM( gbritem, tool->m_Num_Dcode,
924  m_RoutePositions[ii-1].GetPos(), m_RoutePositions[ii].GetPos(),
925  center - m_RoutePositions[ii-1].GetPos(),
926  tool->m_Size, not rot_ccw , true,
927  false );
928  }
929 
930  AddItemToList( gbritem );
931 
932  StepAndRepeatItem( *gbritem );
933  }
934 
935  m_RoutePositions.clear();
936  m_RouteModeOn = false;
937 }
static EXCELLON_CMD excellon_G_CmdList[]
X2_ATTRIBUTE_FILEFUNCTION ( from TF.FileFunction in Gerber file) Example file function: TF....
void AddMessageToList(const wxString &aMessage)
Add a message to the message list.
X2_ATTRIBUTE_FILEFUNCTION * m_FileFunction
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition: richio.cpp:198
char * Line() const
Return a pointer to the last line that was read in.
Definition: richio.h:117
The attribute value consists of a number of substrings separated by a comma.
static wxString FROM_UTF8(const char *cstring)
Convert a UTF8 encoded C string to a wxString for all wxWidgets build modes.
Definition: macros.h:110
char * StrPurge(char *text)
Remove leading and training spaces, tabs and end of line chars in text.
Definition: string.cpp:463
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:188
wxPoint ReadIJCoord(char *&Text)
Return the current coordinate type pointed to by InnJnn Text (InnnnJmmmm)
bool m_InUse
false if the aperture (previously defined) is not used to draw something
Definition: dcode.h:197
#define ROUTE_CCW
APERTURE_T m_Shape
shape ( Line, rectangle, circle , oval .. )
Definition: dcode.h:189
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
Return a pointer to the D_CODE within this GERBER for the given aDCODE.
wxSize m_FmtLen
Image rotation (0, 90, 180, 270 only) in degrees.
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()
Clear the message list.
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.
An abstract base class for deriving all objects that can be added to a VIEW.
Definition: view_item.h:81
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...
wxSize m_FmtScale
< Fmt 2.3: m_FmtScale = 3, fmt 3.4: m_FmtScale = 4.
This file contains miscellaneous commonly used macros and functions.
int m_ArcRadius
Identifier for arc data type (IJ (center) or A## (radius)).
static LIB_SYMBOL * dummy()
Used to draw a dummy shape when a LIB_SYMBOL is not found in library.
Definition: sch_symbol.cpp:70
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:199
A LINE_READER that reads from an open file.
Definition: richio.h:172
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)
VECTOR2< double > VECTOR2D
Definition: vector2d.h:622
GERBER_FILE_IMAGE * GetGbrImage(int aIdx)
#define FIRST_DCODE
Definition: dcode.h:70
APERTURE_T
The set of all gerber aperture types allowed, according to page 16 of http://gerbv....
Definition: dcode.h:49
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:190
static const int fmtIntegerMM
bool Execute_Drill_Command(char *&text)
#define _(s)
virtual KIGFX::VIEW * GetView() const
Return a pointer to the #VIEW instance used in the panel.
#define ROUTE_CW
bool m_Relative
< false = absolute Coord, true = relative Coord.
std::string m_Name
HTML_MESSAGE_BOX.
double Angle() const
Compute the angle of the vector.
Definition: vector2d.h:307
wxPoint ReadXYCoord(char *&aText, bool aExcellonMode=false)
Return the current coordinate type pointed to by XnnYnn Text (XnnnnYmmmm).
excellon_state m_State
GBR_LAYOUT * GetGerberLayout() const
bool m_Has_DCode
< True if has DCodes in file or false if no DCodes found. Perhaps deprecated RS274D file.
static const char file_attribute[]
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
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
Return the active layer.
VECTOR2< T > Rotate(double aAngle) const
Rotate the vector by a given angle.
Definition: vector2d.h:371
LAST_EXTRA_ARC_DATA_TYPE m_LastArcDataType
A gerber DCODE (also called Aperture) definition.
Definition: dcode.h:80
Handle a drill image.
void FinishRouteCommand()
End 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:72
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:70
void ShowInfoBarError(const wxString &aErrorMsg, bool aShowCloseButton=false)
Show the WX_INFOBAR displayed on the top of the canvas with a message and an error icon on the left o...
std::vector< EXCELLON_ROUTE_COORD > m_RoutePositions
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition: vector2d.h:293
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Add a VIEW_ITEM to the view.
Definition: view.cpp:322
bool Execute_EXCELLON_G_Command(char *&text)
void SelectUnits(bool aMetric)
void StepAndRepeatItem(const GERBER_DRAW_ITEM &aItem)
Gerber format has a command Step an Repeat.
D_CODE * GetDCODEOrCreate(int aDCODE, bool aCreateIfNoExist=true)
Return 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.