KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 <[email protected]>
6 * Copyright The 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
30
31
37
38#include <plotters/plotter.h>
39#include <string_utils.h>
40#include <macros.h>
41#include <pcb_edit_frame.h>
42#include <build_version.h>
43#include <math/util.h> // for KiROUND
44#include <trigo.h>
45
46#include <pcbplot.h>
47#include <board.h>
50#include <reporter.h>
51#include <gbr_metadata.h>
52
53#include <fmt/format.h>
54
55
56// Oblong holes can be drilled by a "canned slot" command (G85) or a routing command
57// a linear routing command (G01) is perhaps more usual for drill files
58//
59// set m_useRouteModeForOval to false to use a canned slot hole (old way)
60// set m_useRouteModeForOval to true (preferred mode) to use a linear routed hole (new way)
61
62
64 : GENDRILL_WRITER_BASE( aPcb )
65{
66 m_file = nullptr;
68 m_conversionUnits = 0.0001;
69 m_mirror = false;
70 m_merge_PTH_NPTH = false;
71 m_minimalHeader = false;
74 m_mantissaLenght = 3; // suitable to print coordinates in mm
75}
76
77
78bool EXCELLON_WRITER::CreateDrillandMapFilesSet( const wxString& aPlotDirectory, bool aGenDrill,
79 bool aGenMap, REPORTER * aReporter )
80{
81 bool success = true;
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.emplace_back( 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 to be
101 // sure the NPTH file is up to date in separate files mode.
102 // Also a PTH drill/map file is always created, to be sure at least one plated hole
103 // drill file is created (do not create any PTH drill file can be seen as not working
104 // drill generator).
105 if( getHolesCount() > 0 || doing_npth || pair == DRILL_LAYER_PAIR( F_Cu, B_Cu ) )
106 {
107 fn = getDrillFileName( pair, doing_npth, m_merge_PTH_NPTH );
108 fn.SetPath( aPlotDirectory );
109
110 if( aGenDrill )
111 {
112 wxString fullFilename = fn.GetFullPath();
113
114 FILE* file = wxFopen( fullFilename, wxT( "w" ) );
115
116 if( file == nullptr )
117 {
118 if( aReporter )
119 {
120 msg.Printf( _( "Failed to create file '%s'." ), fullFilename );
121 aReporter->Report( msg, RPT_SEVERITY_ERROR );
122 success = false;
123 }
124
125 break;
126 }
127 else
128 {
129 if( aReporter )
130 {
131 msg.Printf( _( "Created file '%s'" ), fullFilename );
132 aReporter->Report( msg, RPT_SEVERITY_ACTION );
133 }
134 }
135
136 TYPE_FILE file_type = TYPE_FILE::PTH_FILE;
137
138 // Only external layer pair can have non plated hole
139 // internal layers have only plated via holes
140 if( pair == DRILL_LAYER_PAIR( F_Cu, B_Cu ) )
141 {
142 if( m_merge_PTH_NPTH )
143 file_type = TYPE_FILE::MIXED_FILE;
144 else if( doing_npth )
145 file_type = TYPE_FILE::NPTH_FILE;
146 }
147
148 try
149 {
150 createDrillFile( file, pair, file_type );
151 }
152 catch( ... ) // Capture fmt::print exception on write issues
153 {
154 fclose( file );
155 msg.Printf( _( "Failed to write file '%s'." ), fullFilename );
156 aReporter->Report( msg, RPT_SEVERITY_ERROR );
157 success = false;
158 }
159 }
160 }
161 }
162
163 if( aGenMap )
164 success &= CreateMapFilesSet( aPlotDirectory, aReporter );
165
166 if( aReporter )
167 aReporter->ReportTail( _( "Done." ), RPT_SEVERITY_INFO );
168
169 return success;
170}
171
172
174{
175 // Hole attributes are comments (lines starting by ';') in the drill files
176 // For tools (file header), they are similar to X2 apertures attributes.
177 // for attributes added in coordinate list, they are just comments.
178 if( !m_minimalHeader )
179 {
180 switch( aAttribute )
181 {
183 fmt::print( m_file, "{}", "; #@! TA.AperFunction,Plated,PTH,ViaDrill\n" );
184 break;
185
187 fmt::print( m_file, "{}", "; #@! TA.AperFunction,Plated,Buried,ViaDrill\n" );
188 break;
189
191 //case HOLE_ATTRIBUTE::HOLE_PAD_CASTELLATED:
192 fmt::print( m_file, "{}", "; #@! TA.AperFunction,Plated,PTH,ComponentDrill\n" );
193 break;
194
196 fmt::print( m_file, "{}", "; #@! TA.AperFunction,Plated,PTH,CastelletedDrill\n" );
197 break;
198
200 fmt::print( m_file, "{}", "; #@! TA.AperFunction,Plated,PTH,ComponentDrill,PressFit\n" );
201 break;
202
204 fmt::print( m_file, "{}", "; #@! TA.AperFunction,NonPlated,NPTH,ComponentDrill\n" );
205 break;
206
208 fmt::print( m_file, "{}", "; #@! TD\n" );
209 break;
210 }
211 }
212}
213
214
216 TYPE_FILE aHolesType )
217{
218 // if units are mm, the resolution is 0.001 mm (3 digits in mantissa)
219 // if units are inches, the resolution is 0.1 mil (4 digits in mantissa)
221
222 m_file = aFile;
223
224 int diam, holes_count;
225 int x0, y0, xf, yf, xc, yc;
226 double xt, yt;
227 char line[1024];
228
229 writeEXCELLONHeader( aLayerPair, aHolesType );
230
231 holes_count = 0;
232
233 /* Write the tool list */
234 for( unsigned ii = 0; ii < m_toolListBuffer.size(); ii++ )
235 {
236 DRILL_TOOL& tool_descr = m_toolListBuffer[ii];
237
238#if USE_ATTRIB_FOR_HOLES
240#endif
241 fmt::print( m_file, "T{}C{:.{}f}\n", ii + 1, tool_descr.m_Diameter * m_conversionUnits, m_mantissaLenght );
242 }
243
244 fmt::print( m_file, "{}", "%\n" ); // End of header info
245 fmt::print( m_file, "{}", "G90\n" ); // Absolute mode
246 fmt::print( m_file, "{}", "G05\n" ); // Drill mode
247
248 /* Read the hole list and generate data for normal holes (oblong
249 * holes will be created later) */
250 int tool_reference = -2;
251
252 for( unsigned ii = 0; ii < m_holeListBuffer.size(); ii++ )
253 {
254 HOLE_INFO& hole_descr = m_holeListBuffer[ii];
255
256 if( hole_descr.m_Hole_Shape )
257 continue; // oblong holes will be created later
258
259 if( tool_reference != hole_descr.m_Tool_Reference )
260 {
261 tool_reference = hole_descr.m_Tool_Reference;
262 fmt::print( m_file, "T{}\n", tool_reference );
263 }
264
265 x0 = hole_descr.m_Hole_Pos.x - m_offset.x;
266 y0 = hole_descr.m_Hole_Pos.y - m_offset.y;
267
268 if( !m_mirror )
269 y0 *= -1;
270
271 xt = x0 * m_conversionUnits;
272 yt = y0 * m_conversionUnits;
273 writeCoordinates( line, sizeof( line ), xt, yt );
274
275 fmt::print( m_file, "{}", line );
276 holes_count++;
277 }
278
279 /* Read the hole list and generate data for oblong holes
280 */
281 tool_reference = -2; // set to a value not used for
282 // m_holeListBuffer[ii].m_Tool_Reference
283
284 for( unsigned ii = 0; ii < m_holeListBuffer.size(); ii++ )
285 {
286 HOLE_INFO& hole_descr = m_holeListBuffer[ii];
287
288 if( hole_descr.m_Hole_Shape == 0 )
289 continue; // wait for oblong holes
290
291 if( tool_reference != hole_descr.m_Tool_Reference )
292 {
293 tool_reference = hole_descr.m_Tool_Reference;
294 fmt::print( m_file, "T{}\n", tool_reference );
295 }
296
297 diam = std::min( hole_descr.m_Hole_Size.x, hole_descr.m_Hole_Size.y );
298
299 if( diam == 0 )
300 continue;
301
302 /* Compute the hole coordinates: */
303 xc = x0 = xf = hole_descr.m_Hole_Pos.x - m_offset.x;
304 yc = y0 = yf = hole_descr.m_Hole_Pos.y - m_offset.y;
305
306 /* Compute the start and end coordinates for the shape */
307 if( hole_descr.m_Hole_Size.x < hole_descr.m_Hole_Size.y )
308 {
309 int delta = ( hole_descr.m_Hole_Size.y - hole_descr.m_Hole_Size.x ) / 2;
310 y0 -= delta;
311 yf += delta;
312 }
313 else
314 {
315 int delta = ( hole_descr.m_Hole_Size.x - hole_descr.m_Hole_Size.y ) / 2;
316 x0 -= delta;
317 xf += delta;
318 }
319
320 RotatePoint( &x0, &y0, xc, yc, hole_descr.m_Hole_Orient );
321 RotatePoint( &xf, &yf, xc, yc, hole_descr.m_Hole_Orient );
322
323 if( !m_mirror )
324 {
325 y0 *= -1;
326 yf *= -1;
327 }
328
329 xt = x0 * m_conversionUnits;
330 yt = y0 * m_conversionUnits;
331
333 fmt::print( m_file, "{}", "G00" ); // Select the routing mode
334
335 writeCoordinates( line, sizeof( line ), xt, yt );
336
338 {
339 /* remove the '\n' from end of line, because we must add the "G85"
340 * command to the line: */
341 for( int kk = 0; line[kk] != 0; kk++ )
342 {
343 if( line[kk] < ' ' )
344 line[kk] = 0;
345 }
346
347 fmt::print( m_file, "{}", line );
348 fmt::print( m_file, "{}", "G85" ); // add the "G85" command
349 }
350 else
351 {
352 fmt::print( m_file, "{}", line );
353 fmt::print( m_file, "{}", "M15\nG01" ); // tool down and linear routing from last coordinates
354 }
355
356 xt = xf * m_conversionUnits;
357 yt = yf * m_conversionUnits;
358 writeCoordinates( line, sizeof( line ), xt, yt );
359
360 fmt::print( m_file, "{}",line );
361
363 fmt::print( m_file, "{}", "M16\n" ); // Tool up (end routing)
364
365 fmt::print( m_file, "{}", "G05\n" ); // Select drill mode
366 holes_count++;
367 }
368
370
371 return holes_count;
372}
373
374
375void EXCELLON_WRITER::SetFormat( bool aMetric, ZEROS_FMT aZerosFmt, int aLeftDigits,
376 int aRightDigits )
377{
378 m_unitsMetric = aMetric;
379 m_zeroFormat = aZerosFmt;
380
381 /* Set conversion scale depending on drill file units */
382 if( m_unitsMetric )
383 m_conversionUnits = 1.0 / pcbIUScale.IU_PER_MM; // EXCELLON units = mm
384 else
385 m_conversionUnits = 0.001 / pcbIUScale.IU_PER_MILS; // EXCELLON units = in
386
387 // Set the zero counts. if aZerosFmt == DECIMAL_FORMAT, these values
388 // will be set, but not used.
389 if( aLeftDigits <= 0 )
390 aLeftDigits = m_unitsMetric ? 3 : 2;
391
392 if( aRightDigits <= 0 )
393 aRightDigits = m_unitsMetric ? 3 : 4;
394
395 m_precision.m_Lhs = aLeftDigits;
396 m_precision.m_Rhs = aRightDigits;
397}
398
399
400void EXCELLON_WRITER::writeCoordinates( char* aLine, size_t aLineSize, double aCoordX,
401 double aCoordY )
402{
403 wxString xs, ys;
404 int xpad = m_precision.m_Lhs + m_precision.m_Rhs;
405 int ypad = xpad;
406
407 // if units are mm, the resolution is 0.001 mm (3 digits in mantissa)
408 // if units are inches, the resolution is 0.1 mil (4 digits in mantissa)
409 // in DECIMAL_FORMAT we could use more digits.
410
411 switch( m_zeroFormat )
412 {
413 default:
414 case DECIMAL_FORMAT:
415 /* In Excellon files, resolution is 1/1000 mm or 1/10000 inch (0.1 mil)
416 * Although in decimal format, Excellon specifications do not specify
417 * clearly the resolution. However it seems to be usually 1/1000mm or 0.1 mil
418 * like in non decimal formats, so we trunk coordinates to m_mantissaLenght in mantissa
419 * Decimal format just prohibit useless leading 0:
420 * 0.45 or .45 is right, but 00.54 is incorrect.
421 */
422 xs = fmt::format( "{:.{}f}", aCoordX, m_mantissaLenght );
423 ys = fmt::format( "{:.{}f}", aCoordY, m_mantissaLenght );
424
425 //Remove useless trailing 0
426 while( xs.Last() == '0' )
427 xs.RemoveLast();
428
429 if( xs.Last() == '.' ) // however keep a trailing 0 after the floating point separator
430 xs << '0';
431
432 while( ys.Last() == '0' )
433 ys.RemoveLast();
434
435 if( ys.Last() == '.' )
436 ys << '0';
437
438 std::snprintf( aLine, aLineSize, "X%sY%s\n", TO_UTF8( xs ), TO_UTF8( ys ) );
439 break;
440
441 case SUPPRESS_LEADING:
442 for( int i = 0; i< m_precision.m_Rhs; i++ )
443 {
444 aCoordX *= 10; aCoordY *= 10;
445 }
446
447 std::snprintf( aLine, aLineSize, "X%dY%d\n", KiROUND( aCoordX ), KiROUND( aCoordY ) );
448 break;
449
451 {
452 for( int i = 0; i < m_precision.m_Rhs; i++ )
453 {
454 aCoordX *= 10;
455 aCoordY *= 10;
456 }
457
458 if( aCoordX < 0 )
459 xpad++;
460
461 if( aCoordY < 0 )
462 ypad++;
463
464 xs.Printf( wxT( "%0*d" ), xpad, KiROUND( aCoordX ) );
465 ys.Printf( wxT( "%0*d" ), ypad, KiROUND( aCoordY ) );
466
467 size_t j = xs.Len() - 1;
468
469 while( xs[j] == '0' && j )
470 xs.Truncate( j-- );
471
472 j = ys.Len() - 1;
473
474 while( ys[j] == '0' && j )
475 ys.Truncate( j-- );
476
477 std::snprintf( aLine, aLineSize, "X%sY%s\n", TO_UTF8( xs ), TO_UTF8( ys ) );
478 break;
479 }
480
481 case KEEP_ZEROS:
482 for( int i = 0; i< m_precision.m_Rhs; i++ )
483 {
484 aCoordX *= 10; aCoordY *= 10;
485 }
486
487 if( aCoordX < 0 )
488 xpad++;
489
490 if( aCoordY < 0 )
491 ypad++;
492
493 xs.Printf( wxT( "%0*d" ), xpad, KiROUND( aCoordX ) );
494 ys.Printf( wxT( "%0*d" ), ypad, KiROUND( aCoordY ) );
495 std::snprintf( aLine, aLineSize, "X%sY%s\n", TO_UTF8( xs ), TO_UTF8( ys ) );
496 break;
497 }
498}
499
500
502{
503 fmt::print( m_file, "{}", "M48\n" ); // The beginning of a header
504
505 if( !m_minimalHeader )
506 {
507 // The next lines in EXCELLON files are comments:
508 wxString msg;
509 msg << wxT( "KiCad " ) << GetBuildVersion();
510
511 fmt::print( m_file, "; DRILL file {} date {}\n",
513 msg = wxT( "; FORMAT={" );
514
515 // Print precision:
516 // Note in decimal format the precision is not used.
517 // the floating point notation has higher priority than the precision.
519 msg << m_precision.GetPrecisionString();
520 else
521 msg << wxT( "-:-" ); // in decimal format the precision is irrelevant
522
523 msg << wxT( "/ absolute / " );
524 msg << ( m_unitsMetric ? wxT( "metric" ) : wxT( "inch" ) );
525
526 /* Adding numbers notation format.
527 * this is same as m_Choice_Zeros_Format strings, but NOT translated
528 * because some EXCELLON parsers do not like non ASCII values
529 * so we use ONLY English (ASCII) strings.
530 * if new options are added in m_Choice_Zeros_Format, they must also
531 * be added here
532 */
533 msg << wxT( " / " );
534
535 const wxString zero_fmt[4] =
536 {
537 wxT( "decimal" ),
538 wxT( "suppress leading zeros" ),
539 wxT( "suppress trailing zeros" ),
540 wxT( "keep zeros" )
541 };
542
543 msg << zero_fmt[m_zeroFormat] << wxT( "}\n" );
544 fmt::print( m_file, "{}", TO_UTF8( msg ) );
545
546 // add the structured comment TF.CreationDate:
547 // The attribute value must conform to the full version of the ISO 8601
549 fmt::print( m_file, "{}", TO_UTF8( msg ) );
550
551 // Add the application name that created the drill file
552 msg = wxT( "; #@! TF.GenerationSoftware,Kicad,Pcbnew," );
553 msg << GetBuildVersion() << wxT( "\n" );
554 fmt::print( m_file, "{}", TO_UTF8( msg ) );
555
556 // Add the standard X2 FileFunction for drill files
557 // TF.FileFunction,Plated[NonPlated],layer1num,layer2num,PTH[NPTH]
558 msg = BuildFileFunctionAttributeString( aLayerPair, aHolesType , true ) + wxT( "\n" );
559 fmt::print( m_file, "{}", TO_UTF8( msg ) );
560
561 fmt::print( m_file, "{}", "FMAT,2\n" ); // Use Format 2 commands (version used since 1979)
562 }
563
564 fmt::print( m_file, "{}", m_unitsMetric ? "METRIC" : "INCH" );
565
566 switch( m_zeroFormat )
567 {
568 case DECIMAL_FORMAT:
569 fmt::print( m_file, "{}", "\n" );
570 break;
571
572 case SUPPRESS_LEADING:
573 fmt::print( m_file, "{}", ",TZ\n" );
574 break;
575
577 fmt::print( m_file, "{}", ",LZ\n" );
578 break;
579
580 case KEEP_ZEROS:
581 // write nothing, but TZ is acceptable when all zeros are kept
582 fmt::print( m_file, "{}", "\n" );
583 break;
584 }
585}
586
587
589{
590 // add if minimal here
591 fmt::print( m_file, "{}", "M30\n" );
592 fclose( m_file );
593}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:112
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:990
wxString GetBuildVersion()
Get the full KiCad version string.
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:317
HOLE_ATTRIBUTE m_HoleAttribute
void SetFormat(bool aMetric, ZEROS_FMT aZerosFmt=DECIMAL_FORMAT, int aLeftDigits=0, int aRightDigits=0)
Initialize internal parameters to match the given format.
void writeCoordinates(char *aLine, size_t aLineSize, double aCoordX, double aCoordY)
Create a line like according to the selected format.
void writeEXCELLONHeader(DRILL_LAYER_PAIR aLayerPair, TYPE_FILE aHolesType)
Print the DRILL file header.
void writeHoleAttribute(HOLE_ATTRIBUTE aAttribute)
Write a comment string giving the hole attribute.
bool CreateDrillandMapFilesSet(const wxString &aPlotDirectory, bool aGenDrill, bool aGenMap, REPORTER *aReporter=nullptr)
Create the full set of Excellon drill file for the board.
int createDrillFile(FILE *aFile, DRILL_LAYER_PAIR aLayerPair, TYPE_FILE aHolesType)
Create an Excellon drill file.
std::vector< DRILL_LAYER_PAIR > getUniqueLayerPairs() const
Get unique layer pairs by examining the micro and blind_buried vias.
virtual const wxString getDrillFileName(DRILL_LAYER_PAIR aPair, bool aNPTH, bool aMerge_PTH_NPTH) const
void buildHolesList(DRILL_LAYER_PAIR aLayerPair, bool aGenerateNPTH_list)
Create the list of holes and tools for a given board.
const wxString BuildFileFunctionAttributeString(DRILL_LAYER_PAIR aLayerPair, TYPE_FILE aHoleType, bool aCompatNCdrill=false) const
std::vector< HOLE_INFO > m_holeListBuffer
std::vector< DRILL_TOOL > m_toolListBuffer
bool CreateMapFilesSet(const wxString &aPlotDirectory, REPORTER *aReporter=nullptr)
Create the full set of map files for the board, in PS, PDF ... format (use SetMapFileFormat() to sele...
Handle hole which must be drilled (diameter, position and layers).
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:73
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
Definition reporter.h:102
virtual REPORTER & ReportTail(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Places the report at the end of the list, for objects that support report ordering.
Definition reporter.h:112
#define _(s)
wxString GbrMakeCreationDateAttributeString(GBR_NC_STRING_FORMAT aFormat)
Handle special data (items attributes) during plot.
@ GBR_NC_STRING_FORMAT_NCDRILL
Classes used in drill files, map files and report files generation.
std::pair< PCB_LAYER_ID, PCB_LAYER_ID > DRILL_LAYER_PAIR
static const std::string DrillFileExtension
@ B_Cu
Definition layer_ids.h:65
@ F_Cu
Definition layer_ids.h:64
This file contains miscellaneous commonly used macros and functions.
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_INFO
@ RPT_SEVERITY_ACTION
wxString GetISO8601CurrentDateTime()
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
int delta
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Calculate the new point of coord coord pX, pY, for a rotation center 0, 0.
Definition trigo.cpp:229
Definition of file extensions used in Kicad.