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