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>
50 #include <reporter.h>
51 
52 // Comment/uncomment this to write or not a comment
53 // in drill file when PTH and NPTH are merged to flag
54 // tools used for PTH and tools used for NPTH
55 // #define WRITE_PTH_NPTH_COMMENT
56 
57 // Oblong holes can be drilled by a "canned slot" command (G85) or a routing command
58 // a linear routing command (G01) is perhaps more usual for drill files
59 // Comment this next line to use a canned slot hole (old way)
60 // Uncomment this next line to use a linear routed hole (new way)
61 #define USE_ROUTING_MODE_FOR_OBLONG_HOLE
62 
64  : GENDRILL_WRITER_BASE( aPcb )
65 {
66  m_file = NULL;
68  m_conversionUnits = 0.0001;
69  m_mirror = false;
70  m_merge_PTH_NPTH = false;
71  m_minimalHeader = false;
73 }
74 
75 
76 void EXCELLON_WRITER::CreateDrillandMapFilesSet( const wxString& aPlotDirectory,
77  bool aGenDrill, bool aGenMap,
78  REPORTER * aReporter )
79 {
80  wxFileName fn;
81  wxString msg;
82 
83  std::vector<DRILL_LAYER_PAIR> hole_sets = getUniqueLayerPairs();
84 
85  // append a pair representing the NPTH set of holes, for separate drill files.
86  if( !m_merge_PTH_NPTH )
87  hole_sets.push_back( DRILL_LAYER_PAIR( F_Cu, B_Cu ) );
88 
89  for( std::vector<DRILL_LAYER_PAIR>::const_iterator it = hole_sets.begin();
90  it != hole_sets.end(); ++it )
91  {
92  DRILL_LAYER_PAIR pair = *it;
93  // For separate drill files, the last layer pair is the NPTH drill file.
94  bool doing_npth = m_merge_PTH_NPTH ? false : ( it == hole_sets.end() - 1 );
95 
96  buildHolesList( pair, doing_npth );
97 
98  // The file is created if it has holes, or if it is the non plated drill file
99  // to be sure the NPTH file is up to date in separate files mode.
100  if( getHolesCount() > 0 || doing_npth )
101  {
102  fn = getDrillFileName( pair, doing_npth, m_merge_PTH_NPTH );
103  fn.SetPath( aPlotDirectory );
104 
105  if( aGenDrill )
106  {
107  wxString fullFilename = fn.GetFullPath();
108 
109  FILE* file = wxFopen( fullFilename, wxT( "w" ) );
110 
111  if( file == NULL )
112  {
113  if( aReporter )
114  {
115  msg.Printf( _( "** Unable to create %s **\n" ), GetChars( fullFilename ) );
116  aReporter->Report( msg );
117  }
118  break;
119  }
120  else
121  {
122  if( aReporter )
123  {
124  msg.Printf( _( "Create file %s\n" ), GetChars( fullFilename ) );
125  aReporter->Report( msg );
126  }
127  }
128 
129  createDrillFile( file );
130  }
131  }
132  }
133 
134  if( aGenMap )
135  CreateMapFilesSet( aPlotDirectory, aReporter );
136 }
137 
138 
140 {
141  m_file = aFile;
142 
143  int diam, holes_count;
144  int x0, y0, xf, yf, xc, yc;
145  double xt, yt;
146  char line[1024];
147 
148  LOCALE_IO dummy; // Use the standard notation for double numbers
149 
151 
152  holes_count = 0;
153 
154 #ifdef WRITE_PTH_NPTH_COMMENT
155  // if PTH_ and NPTH are merged write a comment in drill file at the
156  // beginning of NPTH section
157  bool writePTHcomment = m_merge_PTH_NPTH;
158  bool writeNPTHcomment = m_merge_PTH_NPTH;
159 #endif
160 
161  /* Write the tool list */
162  for( unsigned ii = 0; ii < m_toolListBuffer.size(); ii++ )
163  {
164  DRILL_TOOL& tool_descr = m_toolListBuffer[ii];
165 
166 #ifdef WRITE_PTH_NPTH_COMMENT
167  if( writePTHcomment && !tool_descr.m_Hole_NotPlated )
168  {
169  writePTHcomment = false;
170  fprintf( m_file, ";TYPE=PLATED\n" );
171  }
172 
173  if( writeNPTHcomment && tool_descr.m_Hole_NotPlated )
174  {
175  writeNPTHcomment = false;
176  fprintf( m_file, ";TYPE=NON_PLATED\n" );
177  }
178 #endif
179 
180  if( m_unitsMetric ) // if units are mm, the resolution is 0.001 mm (3 digits in mantissa)
181  fprintf( m_file, "T%dC%.3f\n", ii + 1, tool_descr.m_Diameter * m_conversionUnits );
182  else // if units are inches, the resolution is 0.1 mil (4 digits in mantissa)
183  fprintf( m_file, "T%dC%.4f\n", ii + 1, tool_descr.m_Diameter * m_conversionUnits );
184  }
185 
186  fputs( "%\n", m_file ); // End of header info
187  fputs( "G90\n", m_file ); // Absolute mode
188  fputs( "G05\n", m_file ); // Drill mode
189 
190  /* Read the hole file and generate lines for normal holes (oblong
191  * holes will be created later) */
192  int tool_reference = -2;
193 
194  for( unsigned ii = 0; ii < m_holeListBuffer.size(); ii++ )
195  {
196  HOLE_INFO& hole_descr = m_holeListBuffer[ii];
197 
198  if( hole_descr.m_Hole_Shape )
199  continue; // oblong holes will be created later
200 
201  if( tool_reference != hole_descr.m_Tool_Reference )
202  {
203  tool_reference = hole_descr.m_Tool_Reference;
204  fprintf( m_file, "T%d\n", tool_reference );
205  }
206 
207  x0 = hole_descr.m_Hole_Pos.x - m_offset.x;
208  y0 = hole_descr.m_Hole_Pos.y - m_offset.y;
209 
210  if( !m_mirror )
211  y0 *= -1;
212 
213  xt = x0 * m_conversionUnits;
214  yt = y0 * m_conversionUnits;
215  writeCoordinates( line, xt, yt );
216 
217  fputs( line, m_file );
218  holes_count++;
219  }
220 
221  /* Read the hole file and generate lines for normal holes (oblong holes
222  * will be created later) */
223  tool_reference = -2; // set to a value not used for
224  // m_holeListBuffer[ii].m_Tool_Reference
225  for( unsigned ii = 0; ii < m_holeListBuffer.size(); ii++ )
226  {
227  HOLE_INFO& hole_descr = m_holeListBuffer[ii];
228 
229  if( hole_descr.m_Hole_Shape == 0 )
230  continue; // wait for oblong holes
231 
232  if( tool_reference != hole_descr.m_Tool_Reference )
233  {
234  tool_reference = hole_descr.m_Tool_Reference;
235  fprintf( m_file, "T%d\n", tool_reference );
236  }
237 
238  diam = std::min( hole_descr.m_Hole_Size.x, hole_descr.m_Hole_Size.y );
239 
240  if( diam == 0 )
241  continue;
242 
243  /* Compute the hole coordinates: */
244  xc = x0 = xf = hole_descr.m_Hole_Pos.x - m_offset.x;
245  yc = y0 = yf = hole_descr.m_Hole_Pos.y - m_offset.y;
246 
247  /* Compute the start and end coordinates for the shape */
248  if( hole_descr.m_Hole_Size.x < hole_descr.m_Hole_Size.y )
249  {
250  int delta = ( hole_descr.m_Hole_Size.y - hole_descr.m_Hole_Size.x ) / 2;
251  y0 -= delta;
252  yf += delta;
253  }
254  else
255  {
256  int delta = ( hole_descr.m_Hole_Size.x - hole_descr.m_Hole_Size.y ) / 2;
257  x0 -= delta;
258  xf += delta;
259  }
260 
261  RotatePoint( &x0, &y0, xc, yc, hole_descr.m_Hole_Orient );
262  RotatePoint( &xf, &yf, xc, yc, hole_descr.m_Hole_Orient );
263 
264  if( !m_mirror )
265  {
266  y0 *= -1;
267  yf *= -1;
268  }
269 
270  xt = x0 * m_conversionUnits;
271  yt = y0 * m_conversionUnits;
272 #ifdef USE_ROUTING_MODE_FOR_OBLONG_HOLE
273  fputs( "G00", m_file ); // Select the routing mode
274 #endif
275  writeCoordinates( line, xt, yt );
276 
277 #ifndef USE_ROUTING_MODE_FOR_OBLONG_HOLE
278  /* remove the '\n' from end of line, because we must add the "G85"
279  * command to the line: */
280  for( int kk = 0; line[kk] != 0; kk++ )
281  {
282  if( line[kk] < ' ' )
283  line[kk] = 0;
284  }
285 
286  fputs( line, m_file );
287  fputs( "G85", m_file ); // add the "G85" command
288 #else
289  fputs( line, m_file );
290  fputs( "M15\nG01", m_file ); // tool down and linear routing from last coordinates
291 #endif
292  xt = xf * m_conversionUnits;
293  yt = yf * m_conversionUnits;
294  writeCoordinates( line, xt, yt );
295 
296  fputs( line, m_file );
297 #ifdef USE_ROUTING_MODE_FOR_OBLONG_HOLE
298  fputs( "M16\n", m_file ); // Tool up (end routing)
299 #endif
300  fputs( "G05\n", m_file ); // Select drill mode
301  holes_count++;
302  }
303 
305 
306  return holes_count;
307 }
308 
309 
310 void EXCELLON_WRITER::SetFormat( bool aMetric,
311  ZEROS_FMT aZerosFmt,
312  int aLeftDigits,
313  int aRightDigits )
314 {
315  m_unitsMetric = aMetric;
316  m_zeroFormat = aZerosFmt;
317 
318  /* Set conversion scale depending on drill file units */
319  if( m_unitsMetric )
320  m_conversionUnits = 1.0 / IU_PER_MM; // EXCELLON units = mm
321  else
322  m_conversionUnits = 0.001 / IU_PER_MILS; // EXCELLON units = INCHES
323 
324  // Set the zero counts. if aZerosFmt == DECIMAL_FORMAT, these values
325  // will be set, but not used.
326  if( aLeftDigits <= 0 )
327  aLeftDigits = m_unitsMetric ? 3 : 2;
328 
329  if( aRightDigits <= 0 )
330  aRightDigits = m_unitsMetric ? 3 : 4;
331 
332  m_precision.m_lhs = aLeftDigits;
333  m_precision.m_rhs = aRightDigits;
334 }
335 
336 
337 void EXCELLON_WRITER::writeCoordinates( char* aLine, double aCoordX, double aCoordY )
338 {
339  wxString xs, ys;
340  int xpad = m_precision.m_lhs + m_precision.m_rhs;
341  int ypad = xpad;
342 
343  switch( m_zeroFormat )
344  {
345  default:
346  case DECIMAL_FORMAT:
347  /* In Excellon files, resolution is 1/1000 mm or 1/10000 inch (0.1 mil)
348  * Although in decimal format, Excellon specifications do not specify
349  * clearly the resolution. However it seems to be 1/1000mm or 0.1 mil
350  * like in non decimal formats, so we trunk coordinates to 3 or 4 digits in mantissa
351  * Decimal format just prohibit useless leading 0:
352  * 0.45 or .45 is right, but 00.54 is incorrect.
353  */
354  if( m_unitsMetric )
355  {
356  // resolution is 1/1000 mm
357  xs.Printf( wxT( "%.3f" ), aCoordX );
358  ys.Printf( wxT( "%.3f" ), aCoordY );
359  }
360  else
361  {
362  // resolution is 1/10000 inch
363  xs.Printf( wxT( "%.4f" ), aCoordX );
364  ys.Printf( wxT( "%.4f" ), aCoordY );
365  }
366 
367  //Remove useless trailing 0
368  while( xs.Last() == '0' )
369  xs.RemoveLast();
370 
371  if( xs.Last() == '.' ) // however keep a trailing 0 after the floating point separator
372  xs << '0';
373 
374  while( ys.Last() == '0' )
375  ys.RemoveLast();
376 
377  if( ys.Last() == '.' )
378  ys << '0';
379 
380  sprintf( aLine, "X%sY%s\n", TO_UTF8( xs ), TO_UTF8( ys ) );
381  break;
382 
383  case SUPPRESS_LEADING:
384  for( int i = 0; i< m_precision.m_rhs; i++ )
385  {
386  aCoordX *= 10; aCoordY *= 10;
387  }
388 
389  sprintf( aLine, "X%dY%d\n", KiROUND( aCoordX ), KiROUND( aCoordY ) );
390  break;
391 
392  case SUPPRESS_TRAILING:
393  {
394  for( int i = 0; i < m_precision.m_rhs; i++ )
395  {
396  aCoordX *= 10;
397  aCoordY *= 10;
398  }
399 
400  if( aCoordX < 0 )
401  xpad++;
402 
403  if( aCoordY < 0 )
404  ypad++;
405 
406  xs.Printf( wxT( "%0*d" ), xpad, KiROUND( aCoordX ) );
407  ys.Printf( wxT( "%0*d" ), ypad, KiROUND( aCoordY ) );
408 
409  size_t j = xs.Len() - 1;
410 
411  while( xs[j] == '0' && j )
412  xs.Truncate( j-- );
413 
414  j = ys.Len() - 1;
415 
416  while( ys[j] == '0' && j )
417  ys.Truncate( j-- );
418 
419  sprintf( aLine, "X%sY%s\n", TO_UTF8( xs ), TO_UTF8( ys ) );
420  break;
421  }
422 
423  case KEEP_ZEROS:
424  for( int i = 0; i< m_precision.m_rhs; i++ )
425  {
426  aCoordX *= 10; aCoordY *= 10;
427  }
428 
429  if( aCoordX < 0 )
430  xpad++;
431 
432  if( aCoordY < 0 )
433  ypad++;
434 
435  xs.Printf( wxT( "%0*d" ), xpad, KiROUND( aCoordX ) );
436  ys.Printf( wxT( "%0*d" ), ypad, KiROUND( aCoordY ) );
437  sprintf( aLine, "X%sY%s\n", TO_UTF8( xs ), TO_UTF8( ys ) );
438  break;
439  }
440 }
441 
442 
444 {
445  fputs( "M48\n", m_file ); // The beginning of a header
446 
447  if( !m_minimalHeader )
448  {
449  // The next 2 lines in EXCELLON files are comments:
450  wxString msg;
451  msg << wxT("KiCad") << wxT( " " ) << GetBuildVersion();
452 
453  fprintf( m_file, ";DRILL file {%s} date %s\n", TO_UTF8( msg ), TO_UTF8( DateAndTime() ) );
454  msg = wxT( ";FORMAT={" );
455 
456  // Print precision:
457  // Note in decimal format the precision is not used.
458  // the floating point notation has higher priority than the precision.
461  else
462  msg << wxT( "-:-" ); // in decimal format the precision is irrelevant
463 
464  msg << wxT( "/ absolute / " );
465  msg << ( m_unitsMetric ? wxT( "metric" ) : wxT( "inch" ) );
466 
467  /* Adding numbers notation format.
468  * this is same as m_Choice_Zeros_Format strings, but NOT translated
469  * because some EXCELLON parsers do not like non ASCII values
470  * so we use ONLY English (ASCII) strings.
471  * if new options are added in m_Choice_Zeros_Format, they must also
472  * be added here
473  */
474  msg << wxT( " / " );
475 
476  const wxString zero_fmt[4] =
477  {
478  wxT( "decimal" ),
479  wxT( "suppress leading zeros" ),
480  wxT( "suppress trailing zeros" ),
481  wxT( "keep zeros" )
482  };
483 
484  msg << zero_fmt[m_zeroFormat] << wxT( "}\n" );
485  fputs( TO_UTF8( msg ), m_file );
486  fputs( "FMAT,2\n", m_file ); // Use Format 2 commands (version used since 1979)
487  }
488 
489  fputs( m_unitsMetric ? "METRIC" : "INCH", m_file );
490 
491  switch( m_zeroFormat )
492  {
493  case DECIMAL_FORMAT:
494  fputs( "\n", m_file );
495  break;
496 
497  case SUPPRESS_LEADING:
498  fputs( ",TZ\n", m_file );
499  break;
500 
501  case SUPPRESS_TRAILING:
502  fputs( ",LZ\n", m_file );
503  break;
504 
505  case KEEP_ZEROS:
506  // write nothing, but TZ is acceptable when all zeros are kept
507  fputs( "\n", m_file );
508  break;
509  }
510 }
511 
512 
514 {
515  //add if minimal here
516  fputs( "T0\nM30\n", m_file );
517  fclose( m_file );
518 }
static int KiROUND(double v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: common.h:120
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown...
Definition: common.h:179
virtual const wxString getDrillFileName(DRILL_LAYER_PAIR aPair, bool aNPTH, bool aMerge_PTH_NPTH) const
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
int createDrillFile(FILE *aFile)
Function CreateDrillFile Creates an Excellon drill file.
void writeCoordinates(char *aLine, double aCoordX, double aCoordY)
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.
The common library.
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:170
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 ...