KiCad PCB EDA Suite
gendrill_Excellon_writer.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) 2018 Jean_Pierre Charras <jp.charras at wanadoo.fr>
5  * Copyright (C) 2015 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
6  * Copyright (C) 1992-2018 KiCad Developers, see AUTHORS.txt for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
38 #include <fctsys.h>
39 
40 #include <plotter.h>
41 #include <kicad_string.h>
42 #include <pcb_edit_frame.h>
43 #include <pgm_base.h>
44 #include <build_version.h>
45 
46 #include <pcbplot.h>
47 #include <pcbnew.h>
48 #include <class_board.h>
51 #include <reporter.h>
52 #include <gbr_metadata.h>
53 
54 // Comment/uncomment this to write or not a comment
55 // in drill file when PTH and NPTH are merged to flag
56 // tools used for PTH and tools used for NPTH
57 // #define WRITE_PTH_NPTH_COMMENT
58 
59 // Oblong holes can be drilled by a "canned slot" command (G85) or a routing command
60 // a linear routing command (G01) is perhaps more usual for drill files
61 // Comment this next line to use a canned slot hole (old way)
62 // Uncomment this next line to use a linear routed hole (new way)
63 #define USE_ROUTING_MODE_FOR_OBLONG_HOLE
64 
66  : GENDRILL_WRITER_BASE( aPcb )
67 {
68  m_file = NULL;
70  m_conversionUnits = 0.0001;
71  m_mirror = false;
72  m_merge_PTH_NPTH = false;
73  m_minimalHeader = false;
75 }
76 
77 
78 void EXCELLON_WRITER::CreateDrillandMapFilesSet( const wxString& aPlotDirectory,
79  bool aGenDrill, bool aGenMap,
80  REPORTER * aReporter )
81 {
82  wxFileName fn;
83  wxString msg;
84 
85  std::vector<DRILL_LAYER_PAIR> hole_sets = getUniqueLayerPairs();
86 
87  // append a pair representing the NPTH set of holes, for separate drill files.
88  if( !m_merge_PTH_NPTH )
89  hole_sets.push_back( DRILL_LAYER_PAIR( F_Cu, B_Cu ) );
90 
91  for( std::vector<DRILL_LAYER_PAIR>::const_iterator it = hole_sets.begin();
92  it != hole_sets.end(); ++it )
93  {
94  DRILL_LAYER_PAIR pair = *it;
95  // For separate drill files, the last layer pair is the NPTH drill file.
96  bool doing_npth = m_merge_PTH_NPTH ? false : ( it == hole_sets.end() - 1 );
97 
98  buildHolesList( pair, doing_npth );
99 
100  // The file is created if it has holes, or if it is the non plated drill file
101  // to be sure the NPTH file is up to date in separate files mode.
102  if( getHolesCount() > 0 || doing_npth )
103  {
104  fn = getDrillFileName( pair, doing_npth, m_merge_PTH_NPTH );
105  fn.SetPath( aPlotDirectory );
106 
107  if( aGenDrill )
108  {
109  wxString fullFilename = fn.GetFullPath();
110 
111  FILE* file = wxFopen( fullFilename, wxT( "w" ) );
112 
113  if( file == NULL )
114  {
115  if( aReporter )
116  {
117  msg.Printf( _( "** Unable to create %s **\n" ), GetChars( fullFilename ) );
118  aReporter->Report( msg );
119  }
120  break;
121  }
122  else
123  {
124  if( aReporter )
125  {
126  msg.Printf( _( "Create file %s\n" ), GetChars( fullFilename ) );
127  aReporter->Report( msg );
128  }
129  }
130 
131  createDrillFile( file, pair, doing_npth );
132  }
133  }
134  }
135 
136  if( aGenMap )
137  CreateMapFilesSet( aPlotDirectory, aReporter );
138 }
139 
140 
142  bool aGenerateNPTH_list )
143 {
144  m_file = aFile;
145 
146  int diam, holes_count;
147  int x0, y0, xf, yf, xc, yc;
148  double xt, yt;
149  char line[1024];
150 
151  LOCALE_IO dummy; // Use the standard notation for double numbers
152 
153  writeEXCELLONHeader( aLayerPair, aGenerateNPTH_list );
154 
155  holes_count = 0;
156 
157 #ifdef WRITE_PTH_NPTH_COMMENT
158  // if PTH_ and NPTH are merged write a comment in drill file at the
159  // beginning of NPTH section
160  bool writePTHcomment = m_merge_PTH_NPTH;
161  bool writeNPTHcomment = m_merge_PTH_NPTH;
162 #endif
163 
164  /* Write the tool list */
165  for( unsigned ii = 0; ii < m_toolListBuffer.size(); ii++ )
166  {
167  DRILL_TOOL& tool_descr = m_toolListBuffer[ii];
168 
169 #ifdef WRITE_PTH_NPTH_COMMENT
170  if( writePTHcomment && !tool_descr.m_Hole_NotPlated )
171  {
172  writePTHcomment = false;
173  fprintf( m_file, ";TYPE=PLATED\n" );
174  }
175 
176  if( writeNPTHcomment && tool_descr.m_Hole_NotPlated )
177  {
178  writeNPTHcomment = false;
179  fprintf( m_file, ";TYPE=NON_PLATED\n" );
180  }
181 #endif
182 
183  if( m_unitsMetric ) // if units are mm, the resolution is 0.001 mm (3 digits in mantissa)
184  fprintf( m_file, "T%dC%.3f\n", ii + 1, tool_descr.m_Diameter * m_conversionUnits );
185  else // if units are inches, the resolution is 0.1 mil (4 digits in mantissa)
186  fprintf( m_file, "T%dC%.4f\n", ii + 1, tool_descr.m_Diameter * m_conversionUnits );
187  }
188 
189  fputs( "%\n", m_file ); // End of header info
190  fputs( "G90\n", m_file ); // Absolute mode
191  fputs( "G05\n", m_file ); // Drill mode
192 
193  /* Read the hole file and generate lines for normal holes (oblong
194  * holes will be created later) */
195  int tool_reference = -2;
196 
197  for( unsigned ii = 0; ii < m_holeListBuffer.size(); ii++ )
198  {
199  HOLE_INFO& hole_descr = m_holeListBuffer[ii];
200 
201  if( hole_descr.m_Hole_Shape )
202  continue; // oblong holes will be created later
203 
204  if( tool_reference != hole_descr.m_Tool_Reference )
205  {
206  tool_reference = hole_descr.m_Tool_Reference;
207  fprintf( m_file, "T%d\n", tool_reference );
208  }
209 
210  x0 = hole_descr.m_Hole_Pos.x - m_offset.x;
211  y0 = hole_descr.m_Hole_Pos.y - m_offset.y;
212 
213  if( !m_mirror )
214  y0 *= -1;
215 
216  xt = x0 * m_conversionUnits;
217  yt = y0 * m_conversionUnits;
218  writeCoordinates( line, xt, yt );
219 
220  fputs( line, m_file );
221  holes_count++;
222  }
223 
224  /* Read the hole file and generate lines for normal holes (oblong holes
225  * will be created later) */
226  tool_reference = -2; // set to a value not used for
227  // m_holeListBuffer[ii].m_Tool_Reference
228  for( unsigned ii = 0; ii < m_holeListBuffer.size(); ii++ )
229  {
230  HOLE_INFO& hole_descr = m_holeListBuffer[ii];
231 
232  if( hole_descr.m_Hole_Shape == 0 )
233  continue; // wait for oblong holes
234 
235  if( tool_reference != hole_descr.m_Tool_Reference )
236  {
237  tool_reference = hole_descr.m_Tool_Reference;
238  fprintf( m_file, "T%d\n", tool_reference );
239  }
240 
241  diam = std::min( hole_descr.m_Hole_Size.x, hole_descr.m_Hole_Size.y );
242 
243  if( diam == 0 )
244  continue;
245 
246  /* Compute the hole coordinates: */
247  xc = x0 = xf = hole_descr.m_Hole_Pos.x - m_offset.x;
248  yc = y0 = yf = hole_descr.m_Hole_Pos.y - m_offset.y;
249 
250  /* Compute the start and end coordinates for the shape */
251  if( hole_descr.m_Hole_Size.x < hole_descr.m_Hole_Size.y )
252  {
253  int delta = ( hole_descr.m_Hole_Size.y - hole_descr.m_Hole_Size.x ) / 2;
254  y0 -= delta;
255  yf += delta;
256  }
257  else
258  {
259  int delta = ( hole_descr.m_Hole_Size.x - hole_descr.m_Hole_Size.y ) / 2;
260  x0 -= delta;
261  xf += delta;
262  }
263 
264  RotatePoint( &x0, &y0, xc, yc, hole_descr.m_Hole_Orient );
265  RotatePoint( &xf, &yf, xc, yc, hole_descr.m_Hole_Orient );
266 
267  if( !m_mirror )
268  {
269  y0 *= -1;
270  yf *= -1;
271  }
272 
273  xt = x0 * m_conversionUnits;
274  yt = y0 * m_conversionUnits;
275 #ifdef USE_ROUTING_MODE_FOR_OBLONG_HOLE
276  fputs( "G00", m_file ); // Select the routing mode
277 #endif
278  writeCoordinates( line, xt, yt );
279 
280 #ifndef USE_ROUTING_MODE_FOR_OBLONG_HOLE
281  /* remove the '\n' from end of line, because we must add the "G85"
282  * command to the line: */
283  for( int kk = 0; line[kk] != 0; kk++ )
284  {
285  if( line[kk] < ' ' )
286  line[kk] = 0;
287  }
288 
289  fputs( line, m_file );
290  fputs( "G85", m_file ); // add the "G85" command
291 #else
292  fputs( line, m_file );
293  fputs( "M15\nG01", m_file ); // tool down and linear routing from last coordinates
294 #endif
295  xt = xf * m_conversionUnits;
296  yt = yf * m_conversionUnits;
297  writeCoordinates( line, xt, yt );
298 
299  fputs( line, m_file );
300 #ifdef USE_ROUTING_MODE_FOR_OBLONG_HOLE
301  fputs( "M16\n", m_file ); // Tool up (end routing)
302 #endif
303  fputs( "G05\n", m_file ); // Select drill mode
304  holes_count++;
305  }
306 
308 
309  return holes_count;
310 }
311 
312 
313 void EXCELLON_WRITER::SetFormat( bool aMetric,
314  ZEROS_FMT aZerosFmt,
315  int aLeftDigits,
316  int aRightDigits )
317 {
318  m_unitsMetric = aMetric;
319  m_zeroFormat = aZerosFmt;
320 
321  /* Set conversion scale depending on drill file units */
322  if( m_unitsMetric )
323  m_conversionUnits = 1.0 / IU_PER_MM; // EXCELLON units = mm
324  else
325  m_conversionUnits = 0.001 / IU_PER_MILS; // EXCELLON units = INCHES
326 
327  // Set the zero counts. if aZerosFmt == DECIMAL_FORMAT, these values
328  // will be set, but not used.
329  if( aLeftDigits <= 0 )
330  aLeftDigits = m_unitsMetric ? 3 : 2;
331 
332  if( aRightDigits <= 0 )
333  aRightDigits = m_unitsMetric ? 3 : 4;
334 
335  m_precision.m_lhs = aLeftDigits;
336  m_precision.m_rhs = aRightDigits;
337 }
338 
339 
340 void EXCELLON_WRITER::writeCoordinates( char* aLine, double aCoordX, double aCoordY )
341 {
342  wxString xs, ys;
343  int xpad = m_precision.m_lhs + m_precision.m_rhs;
344  int ypad = xpad;
345 
346  switch( m_zeroFormat )
347  {
348  default:
349  case DECIMAL_FORMAT:
350  /* In Excellon files, resolution is 1/1000 mm or 1/10000 inch (0.1 mil)
351  * Although in decimal format, Excellon specifications do not specify
352  * clearly the resolution. However it seems to be 1/1000mm or 0.1 mil
353  * like in non decimal formats, so we trunk coordinates to 3 or 4 digits in mantissa
354  * Decimal format just prohibit useless leading 0:
355  * 0.45 or .45 is right, but 00.54 is incorrect.
356  */
357  if( m_unitsMetric )
358  {
359  // resolution is 1/1000 mm
360  xs.Printf( wxT( "%.3f" ), aCoordX );
361  ys.Printf( wxT( "%.3f" ), aCoordY );
362  }
363  else
364  {
365  // resolution is 1/10000 inch
366  xs.Printf( wxT( "%.4f" ), aCoordX );
367  ys.Printf( wxT( "%.4f" ), aCoordY );
368  }
369 
370  //Remove useless trailing 0
371  while( xs.Last() == '0' )
372  xs.RemoveLast();
373 
374  if( xs.Last() == '.' ) // however keep a trailing 0 after the floating point separator
375  xs << '0';
376 
377  while( ys.Last() == '0' )
378  ys.RemoveLast();
379 
380  if( ys.Last() == '.' )
381  ys << '0';
382 
383  sprintf( aLine, "X%sY%s\n", TO_UTF8( xs ), TO_UTF8( ys ) );
384  break;
385 
386  case SUPPRESS_LEADING:
387  for( int i = 0; i< m_precision.m_rhs; i++ )
388  {
389  aCoordX *= 10; aCoordY *= 10;
390  }
391 
392  sprintf( aLine, "X%dY%d\n", KiROUND( aCoordX ), KiROUND( aCoordY ) );
393  break;
394 
395  case SUPPRESS_TRAILING:
396  {
397  for( int i = 0; i < m_precision.m_rhs; i++ )
398  {
399  aCoordX *= 10;
400  aCoordY *= 10;
401  }
402 
403  if( aCoordX < 0 )
404  xpad++;
405 
406  if( aCoordY < 0 )
407  ypad++;
408 
409  xs.Printf( wxT( "%0*d" ), xpad, KiROUND( aCoordX ) );
410  ys.Printf( wxT( "%0*d" ), ypad, KiROUND( aCoordY ) );
411 
412  size_t j = xs.Len() - 1;
413 
414  while( xs[j] == '0' && j )
415  xs.Truncate( j-- );
416 
417  j = ys.Len() - 1;
418 
419  while( ys[j] == '0' && j )
420  ys.Truncate( j-- );
421 
422  sprintf( aLine, "X%sY%s\n", TO_UTF8( xs ), TO_UTF8( ys ) );
423  break;
424  }
425 
426  case KEEP_ZEROS:
427  for( int i = 0; i< m_precision.m_rhs; i++ )
428  {
429  aCoordX *= 10; aCoordY *= 10;
430  }
431 
432  if( aCoordX < 0 )
433  xpad++;
434 
435  if( aCoordY < 0 )
436  ypad++;
437 
438  xs.Printf( wxT( "%0*d" ), xpad, KiROUND( aCoordX ) );
439  ys.Printf( wxT( "%0*d" ), ypad, KiROUND( aCoordY ) );
440  sprintf( aLine, "X%sY%s\n", TO_UTF8( xs ), TO_UTF8( ys ) );
441  break;
442  }
443 }
444 
445 
447  bool aGenerateNPTH_list)
448 {
449  fputs( "M48\n", m_file ); // The beginning of a header
450 
451  if( !m_minimalHeader )
452  {
453  // The next lines in EXCELLON files are comments:
454  wxString msg;
455  msg << "KiCad " << GetBuildVersion();
456 
457  fprintf( m_file, "; DRILL file {%s} date %s\n", TO_UTF8( msg ), TO_UTF8( DateAndTime() ) );
458  msg = "; FORMAT={";
459 
460  // Print precision:
461  // Note in decimal format the precision is not used.
462  // the floating point notation has higher priority than the precision.
465  else
466  msg << "-:-"; // in decimal format the precision is irrelevant
467 
468  msg << "/ absolute / ";
469  msg << ( m_unitsMetric ? "metric" : "inch" );
470 
471  /* Adding numbers notation format.
472  * this is same as m_Choice_Zeros_Format strings, but NOT translated
473  * because some EXCELLON parsers do not like non ASCII values
474  * so we use ONLY English (ASCII) strings.
475  * if new options are added in m_Choice_Zeros_Format, they must also
476  * be added here
477  */
478  msg << wxT( " / " );
479 
480  const wxString zero_fmt[4] =
481  {
482  "decimal",
483  "suppress leading zeros",
484  "suppress trailing zeros",
485  "keep zeros"
486  };
487 
488  msg << zero_fmt[m_zeroFormat] << "}\n";
489  fputs( TO_UTF8( msg ), m_file );
490 
491  // add the structured comment TF.CreationDate:
492  // The attribute value must conform to the full version of the ISO 8601
494  fputs( TO_UTF8( msg ), m_file );
495 
496  // Add the application name that created the drill file
497  msg = "; #@! TF.GenerationSoftware,Kicad,Pcbnew,";
498  msg << GetBuildVersion() << "\n";
499  fputs( TO_UTF8( msg ), m_file );
500 
501  if( !m_merge_PTH_NPTH )
502  {
503  // Add the standard X2 FileFunction for drill files
504  // TF.FileFunction,Plated[NonPlated],layer1num,layer2num,PTH[NPTH]
505  msg = BuildFileFunctionAttributeString( aLayerPair, aGenerateNPTH_list, true )
506  + "\n";
507  fputs( TO_UTF8( msg ), m_file );
508  }
509 
510  fputs( "FMAT,2\n", m_file ); // Use Format 2 commands (version used since 1979)
511  }
512 
513  fputs( m_unitsMetric ? "METRIC" : "INCH", m_file );
514 
515  switch( m_zeroFormat )
516  {
517  case DECIMAL_FORMAT:
518  fputs( "\n", m_file );
519  break;
520 
521  case SUPPRESS_LEADING:
522  fputs( ",TZ\n", m_file );
523  break;
524 
525  case SUPPRESS_TRAILING:
526  fputs( ",LZ\n", m_file );
527  break;
528 
529  case KEEP_ZEROS:
530  // write nothing, but TZ is acceptable when all zeros are kept
531  fputs( "\n", m_file );
532  break;
533  }
534 }
535 
536 
538 {
539  //add if minimal here
540  fputs( "T0\nM30\n", m_file );
541  fclose( m_file );
542 }
a class to handle special data (items attributes) during plot.
static int KiROUND(double v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: common.h:121
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown...
Definition: common.h:180
Class BOARD to handle a board.
virtual const wxString getDrillFileName(DRILL_LAYER_PAIR aPair, bool aNPTH, bool aMerge_PTH_NPTH) const
int createDrillFile(FILE *aFile, DRILL_LAYER_PAIR aLayerPair, bool aGenerateNPTH_list)
Function CreateDrillFile Creates an Excellon drill file.
wxString GbrMakeCreationDateAttributeString(GBR_NC_STRING_FORMAT aFormat)
void writeEXCELLONHeader(DRILL_LAYER_PAIR aLayerPair, bool aGenerateNPTH_list)
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:216
Class REPORTER is a pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:61
void CreateMapFilesSet(const wxString &aPlotDirectory, REPORTER *aReporter=NULL)
Function CreateMapFilesSet Creates the full set of map files for the board, in PS, PDF ...
static const int delta[8][2]
Definition: solve.cpp:112
void SetFormat(bool aMetric, ZEROS_FMT aZerosFmt=DECIMAL_FORMAT, int aLeftDigits=0, int aRightDigits=0)
Function SetFormat Initialize internal parameters to match the given format.
Board plot function definition file.
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes...
Definition: macros.h:47
std::vector< DRILL_TOOL > m_toolListBuffer
void writeCoordinates(char *aLine, double aCoordX, double aCoordY)
const wxString BuildFileFunctionAttributeString(DRILL_LAYER_PAIR aLayerPair, bool aIsNpth, bool aCompatNCdrill=false) const
std::vector< DRILL_LAYER_PAIR > getUniqueLayerPairs() const
Get unique layer pairs by examining the micro and blind_buried vias.
wxString GetBuildVersion()
Function GetBuildVersion Return the build version string.
Definition of file extensions used in Kicad.
std::pair< PCB_LAYER_ID, PCB_LAYER_ID > DRILL_LAYER_PAIR
void buildHolesList(DRILL_LAYER_PAIR aLayerPair, bool aGenerateNPTH_list)
Function BuildHolesList Create the list of holes and tools for a given board The list is sorted by in...
void CreateDrillandMapFilesSet(const wxString &aPlotDirectory, bool aGenDrill, bool aGenMap, REPORTER *aReporter=NULL)
Function CreateDrillandMapFilesSet Creates the full set of Excellon drill file for the board filename...
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
see class PGM_BASE
static LIB_PART * dummy()
Used when a LIB_PART is not found in library to draw a dummy shape This component is a 400 mils squar...
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:171
size_t i
Definition: json11.cpp:597
#define IU_PER_MILS
Definition: plotter.cpp:134
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_UNDEFINED)=0
Function Report is a pure virtual function to override in the derived object.
Classes used in drill files, map files and report files generation.
std::vector< HOLE_INFO > m_holeListBuffer
wxString DateAndTime()
Function DateAndTime.
Definition: string.cpp:306
const std::string DrillFileExtension
#define min(a, b)
Definition: auxiliary.h:85
GENDRILL_WRITER_BASE is a class to create drill maps and drill report, and a helper class to created ...