forked from patkan/pcb2gcode
/
drill.cpp
518 lines (427 loc) · 18 KB
/
drill.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/*!\defgroup DRILL*/
/******************************************************************************/
/*!
\file drill.cpp
\brief Generate the g-code file for drilling.
\version
09.01.2015 - Nicola Corna - nicola@corna.info\n
- Added zero-start option
1.1.4dev 2013 - Erik Schuster - erik@muenchen-ist-toll.de\n
- Bugfix for postamble: The postamble is now inserted in the output file.
- Preamble: The optional external file does not substitute the internal preamble anymore.
- Changed x-coordinate calculation.
- Added onedrill option.
- Added metricoutput option.
- Prepared documenting the code with doxygen.
- Formatted the code.\n
\version
1.1.4 2009, 2010 Patrick Birnzain <pbirnzain@users.sourceforge.net> and others
\copyright
pcb2gcode is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
pcb2gcode is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with pcb2gcode. If not, see <http://www.gnu.org/licenses/>.
\bug The milldrill option does not produce usable output.
\bug Milldrill + mirror_absolute returns error #134.
\todo Sort the drill coordinates if the onedrill option is given to reduce the
time for the drilling process.
\ingroup DRILL
*/
/******************************************************************************/
#include <iostream>
using std::cout;
using std::endl;
#include <fstream>
#include <iomanip>
using namespace std;
#include "drill.hpp"
#include <cstring>
#include <boost/scoped_array.hpp>
#include <boost/foreach.hpp>
using std::pair;
/******************************************************************************/
/*
\brief Constructor
\param metricoutput : if true, ngc output in metric units
*/
/******************************************************************************/
ExcellonProcessor::ExcellonProcessor(string drillfile,
const ivalue_t board_width,
const ivalue_t board_center,
bool _metricoutput,
double xoffset,
double yoffset)
: board_center(board_center), board_width(board_width),
bMetricOutput(_metricoutput), xoffset(xoffset),
yoffset(yoffset) {
bDoSVG = false; //clear flag for SVG export
project = gerbv_create_project();
const char* cfilename = drillfile.c_str();
boost::scoped_array<char> filename(new char[strlen(cfilename) + 1]);
strcpy(filename.get(), cfilename);
gerbv_open_layer_from_filename(project, filename.get());
if (project->file[0] == NULL) {
throw drill_exception();
}
//set imperial/metric conversion factor for output coordinates depending on metricoutput option
cfactor = bMetricOutput ? 25.4 : 1;
calc_dimensions(); //calculate board dimensions
//set metric or imperial preambles
if (bMetricOutput) {
preamble = string("G94 (Millimeters per minute feed rate.)\n")
+ "G21 (Units == Millimeters.)\n";
} else {
preamble = string("G94 (Inches per minute feed rate.)\n")
+ "G20 (Units == INCHES.)\n";
}
preamble += "G90 (Absolute coordinates.)\n";
//set postamble
postamble = string("M5 (Spindle off.)\n") +
"M9 (Coolant off.)\n" +
"M2 (Program end.)\n";
}
/******************************************************************************/
/*
\brief Destructor
*/
/******************************************************************************/
ExcellonProcessor::~ExcellonProcessor() {
gerbv_destroy_project(project);
}
/******************************************************************************/
/*
*/
/******************************************************************************/
void ExcellonProcessor::set_svg_exporter(shared_ptr<SVG_Exporter> svgexpo) {
this->svgexpo = svgexpo;
bDoSVG = true;
}
/******************************************************************************/
/*
*/
/******************************************************************************/
void ExcellonProcessor::add_header(string header) {
this->header.push_back(header);
}
/******************************************************************************/
/*
\brief Calculate the board dimensions based on the drill file only
*/
/******************************************************************************/
void ExcellonProcessor::calc_dimensions(void) {
x_min = +INFINITY;
x_max = -INFINITY;
shared_ptr<const map<int, drillbit> > bits = get_bits();
shared_ptr<const map<int, icoords> > holes = get_holes();
for (map<int, drillbit>::const_iterator it = bits->begin();
it != bits->end(); it++) {
const icoords drill_coords = holes->at(it->first);
icoords::const_iterator coord_iter = drill_coords.begin();
x_min = (coord_iter->first < x_min) ? coord_iter->first : x_min;
x_max = (coord_iter->first > x_max) ? coord_iter->first : x_max;
++coord_iter;
while (coord_iter != drill_coords.end()) {
x_min = (coord_iter->first < x_min) ? coord_iter->first : x_min;
x_max = (coord_iter->first > x_max) ? coord_iter->first : x_max;
++coord_iter;
}
}
width = x_max - x_min;
x_center = x_min + width / 2;
}
/******************************************************************************/
/*
\brief Recalculates the x-coordinate based on drillfront and mirror_absolute
\param drillfront = drill from front side
\param mirror_absoulte = mirror back side on y-axis
\param xvalue = x-coordinate
\retval recalulated x-coordinate
*/
/******************************************************************************/
double ExcellonProcessor::get_xvalue(bool drillfront, bool mirror_absolute,
double xvalue) {
double retval;
if (drillfront) { //drill from the front, no calculation needed
retval = xvalue;
} else {
if (mirror_absolute) { //drill from back side, mirrored along y-axis
retval = xvalue * -1;
} else { //drill from back side, mirrored along board center
retval = (2 * board_center - xvalue);
}
}
return retval;
}
/******************************************************************************/
/*
\brief Exports the ngc file for drilling
\param of_name = output filename
\param driller = ...
\param drillfront = if true, use the coordinates from the front side instead of the back side (drill-front=true)
\param mirror_absolute = if true, mirror along the y axis instead of the board center
\param onedrill : if true, only the first drill bit is used, the others are skipped
\todo Optimise the implementation of onedrill by modifying the bits and
using the smallest bit only.
*/
/******************************************************************************/
void ExcellonProcessor::export_ngc(const string of_name,
shared_ptr<Driller> driller, bool drillfront,
bool mirror_absolute, bool onedrill, bool nog81) {
ivalue_t double_mirror_axis = mirror_absolute ? 0 : board_width;
//SVG EXPORTER
int rad = 1.;
cout << "Exporting dill... ";
//open output file
std::ofstream of;
of.open(of_name.c_str());
shared_ptr<const map<int, drillbit> > bits = get_bits();
shared_ptr<const map<int, icoords> > holes = get_holes();
//write header to .ngc file
BOOST_FOREACH (string s, header) {
of << "( " << s << " )" << "\n";
}
if (!onedrill) {
of << "\n( This file uses " << bits->size() << " drill bit sizes. )\n";
of << "( Bit sizes:";
for (map<int, drillbit>::const_iterator it = bits->begin();
it != bits->end(); it++) {
of << " [" << it->second.diameter << it->second.unit << "]";
}
of << " )\n\n";
} else {
of << "\n( This file uses only one drill bit. Forced by 'onedrill' option )\n\n";
}
of.setf(ios_base::fixed); //write floating-point values in fixed-point notation
of.precision(5); //Set floating-point decimal precision
of << setw(7); //Sets the field width to be used on output operations.
of << preamble_ext; //insert external preamble file
of << preamble; //insert internal preamble
of << "S" << left << driller->speed << " (RPM spindle speed.)\n" << "\n";
for (map<int, drillbit>::const_iterator it = bits->begin();
it != bits->end(); it++) {
//if the command line option "onedrill" is given, allow only the inital toolchange
if ((onedrill == true) && (it != bits->begin())) {
of << "(Drill change skipped. Forced by 'onedrill' option.)\n" << "\n";
} else {
of << "G00 Z" << driller->zchange * cfactor << " (Retract)\n" << "T"
<< it->first << "\n" << "M5 (Spindle stop.)\n"
<< "M6 (Tool change.)\n"
<< "(MSG, Change tool bit to drill size " << it->second.diameter
<< " " << it->second.unit << ")\n"
<< "M0 (Temporary machine stop.)\n"
<< "M3 (Spindle on clockwise.)\n" << "\n";
}
const icoords drill_coords = holes->at(it->first);
icoords::const_iterator coord_iter = drill_coords.begin();
//coord_iter->first = x-coorinate (top view)
//coord_iter->second =y-coordinate (top view)
//SVG EXPORTER
if (bDoSVG) {
svgexpo->set_rand_color(); //set a random color
svgexpo->circle((double_mirror_axis - coord_iter->first),
coord_iter->second, rad); //draw first circle
svgexpo->stroke();
}
if( nog81 )
of << "F" << driller->feed * cfactor << endl;
else {
of << "G81 R" << driller->zsafe * cfactor << " Z"
<< driller->zwork * cfactor << " F" << driller->feed * cfactor << " X"
<< ( get_xvalue(drillfront, mirror_absolute, coord_iter->first) - xoffset ) * cfactor
<< " Y" << ( ( coord_iter->second - yoffset ) * cfactor) << "\n";
++coord_iter;
}
while (coord_iter != drill_coords.end()) {
if( nog81 ) {
of << "G0 X"
<< ( get_xvalue(drillfront, mirror_absolute, coord_iter->first) - xoffset ) * cfactor
<< " Y" << ( ( coord_iter->second - yoffset ) * cfactor) << "\n";
of << "G1 Z" << driller->zwork * cfactor << endl;
of << "G1 Z" << driller->zsafe * cfactor << endl;
}
else {
of << "X"
<< ( get_xvalue(drillfront, mirror_absolute, coord_iter->first) - xoffset )
* cfactor
<< " Y" << ( ( coord_iter->second - yoffset ) * cfactor) << "\n";
}
//SVG EXPORTER
if (bDoSVG) {
svgexpo->circle((double_mirror_axis - coord_iter->first),
coord_iter->second, rad); //make a whole
svgexpo->stroke();
}
++coord_iter;
}
of << "\n";
}
of << "G00 Z" << driller->zchange * cfactor << " (All done -- retract)\n\n";
of << postamble_ext; //insert external postamble file
of << postamble << endl; //add internal postamble
of.close();
cout << "DONE." << endl;
}
/******************************************************************************/
/*
* \brief mill one circle
*/
/******************************************************************************/
void ExcellonProcessor::millhole(std::ofstream &of, double x, double y,
shared_ptr<Cutter> cutter,
double holediameter) {
g_assert(cutter);
double cutdiameter = cutter->tool_diameter;
cout << " C: " << cutdiameter << " H:" << holediameter << "\n";
if (cutdiameter * 1.001 >= holediameter) {
of << "G0 X" << x * cfactor << " Y" << y * cfactor << endl;
of << "G1 Z#50" << endl;
of << "G0 Z#51" << endl << endl;
} else {
double millr = (holediameter - cutdiameter) / 2.; //mill radius
of << "G0 X" << x * cfactor + millr << " Y" << y * cfactor << endl;
double z_step = cutter->stepsize;
double z = cutter->zwork + z_step * abs(int(cutter->zwork / z_step));
if (!cutter->do_steps) {
z = cutter->zwork;
z_step = 1; //dummy to exit the loop
}
int stepcount = abs(int(cutter->zwork / z_step));
while (z >= cutter->zwork) {
of << "G1 Z[#50+" << stepcount << "*#52]" << endl;
of << "G2 I" << -millr << " J0" << endl;
z -= z_step;
stepcount--;
}
of << "G0 Z" << cutter->zsafe * cfactor << endl << endl;
}
}
/******************************************************************************/
/*
\brief mill larger holes by using a smaller mill-head
\bug does not work properly.
*/
/******************************************************************************/
void ExcellonProcessor::export_ngc(const string outputname,
shared_ptr<Cutter> target, bool drillfront,
bool mirror_absolute, bool onedrill) {
g_assert(drillfront == true);
g_assert(mirror_absolute == false);
cerr << "Currently Drilling " << "\n";
// open output file
std::ofstream of;
of.open(outputname.c_str());
shared_ptr<const map<int, drillbit> > bits = get_bits();
shared_ptr<const map<int, icoords> > holes = get_holes();
// write header to .ngc file
BOOST_FOREACH (string s, header) {
of << "( " << s << " )" << "\n";
}
of << "\n";
of.setf(ios_base::fixed); //write floating-point values in fixed-point notation
of.precision(5); //Set floating-point decimal precision
of << setw(7); //Sets the field width to be used on output operations.
of << "( This file uses a mill head of " << (bMetricOutput ? (target->tool_diameter * 25.4) : target->tool_diameter)
<< (bMetricOutput ? "mm" : "inch") << " to drill the " << bits->size()
<< "bit sizes. )" << "\n";
of << "( Bit sizes:";
for (map<int, drillbit>::const_iterator it = bits->begin();
it != bits->end(); it++) {
of << " [" << it->second.diameter << "]";
}
of << " )\n\n";
//preamble
of << preamble_ext << preamble << "S" << left << target->speed
<< " (RPM spindle speed.)\n" << "F" << target->feed * cfactor
<< " (Feedrate)\n\n";
of << "#50=" << target->zwork * cfactor << " (zwork)" << "\n";
of << "#51=" << target->zsafe * cfactor << " (zsafe)" << "\n";
of << "#52=" << target->stepsize * cfactor << " (stepsize)" << "\n\n";
of << "G00 Z#51\n";
for (map<int, drillbit>::const_iterator it = bits->begin();
it != bits->end(); it++) {
double diameter = it->second.diameter;
const icoords drill_coords = holes->at(it->first);
icoords::const_iterator coord_iter = drill_coords.begin();
millhole(of, get_xvalue(!drillfront, mirror_absolute, coord_iter->first) - xoffset,
coord_iter->second - yoffset, target, diameter);
++coord_iter;
while (coord_iter != drill_coords.end()) {
millhole(of, get_xvalue(!drillfront, mirror_absolute, coord_iter->first) - xoffset,
coord_iter->second - yoffset, target, diameter);
++coord_iter;
}
}
// retract, end
of << "G00 Z" << target->zchange * cfactor << " ( All done -- retract )\n";
of << postamble_ext;
of << postamble << endl;
of.close();
}
/******************************************************************************/
/*
*/
/******************************************************************************/
void ExcellonProcessor::parse_bits() {
bits = shared_ptr<map<int, drillbit> >(new map<int, drillbit>());
for (gerbv_drill_list_t* currentDrill = project->file[0]->image->drill_stats
->drill_list; currentDrill; currentDrill = currentDrill->next) {
drillbit curBit;
curBit.diameter = currentDrill->drill_size;
curBit.unit = string(currentDrill->drill_unit);
curBit.drill_count = currentDrill->drill_count;
bits->insert(pair<int, drillbit>(currentDrill->drill_num, curBit));
}
}
/******************************************************************************/
/*
*/
/******************************************************************************/
void ExcellonProcessor::parse_holes() {
if (!bits)
parse_bits();
holes = shared_ptr<map<int, icoords> >(new map<int, icoords>());
for (gerbv_net_t* currentNet = project->file[0]->image->netlist; currentNet;
currentNet = currentNet->next) {
if (currentNet->aperture != 0)
(*holes)[currentNet->aperture].push_back(
icoordpair(currentNet->start_x, currentNet->start_y));
}
}
/******************************************************************************/
/*
*/
/******************************************************************************/
shared_ptr<const map<int, drillbit> > ExcellonProcessor::get_bits() {
if (!bits)
parse_bits();
return bits;
}
/******************************************************************************/
/*
*/
/******************************************************************************/
shared_ptr<const map<int, icoords> > ExcellonProcessor::get_holes() {
if (!holes)
parse_holes();
return holes;
}
/******************************************************************************/
/*
*/
/******************************************************************************/
void ExcellonProcessor::set_preamble(string _preamble) {
preamble_ext = _preamble;
}
/******************************************************************************/
/*
*/
/******************************************************************************/
void ExcellonProcessor::set_postamble(string _postamble) {
postamble_ext = _postamble;
}