forked from patkan/pcb2gcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drill.cpp
448 lines (359 loc) · 14.7 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
/*!\defgroup DRILL*/
/******************************************************************************/
/*!
\file drill.cpp
\brief This file is part of pcb2gcode.
\version
18.08.2013 - Erik Schuster - erik@muenchen-ist-toll.de\n
- Changed x-coordinate calculation.
\version
04.08.2013 - Erik Schuster - erik@muenchen-ist-toll.de\n
- Added onedrill option.
- Formatted the code with the Eclipse code styler (Style: K&R).
- Started documenting the code for doxygen processing.
\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/>.
\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) :
board_center(board_center), board_width(board_width) {
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 = metricoutput ? 25.4 : 1;
calc_dimensions();
//set metric or imperial preambles
if (metricoutput) {
preamble = string("G94 ( Millimeters per minute feed rate.)\n") + "G21 ( Units == Millimeters.)\n" + "G90 ( Absolute coordinates.)\n";
} else {
preamble = string("G94 ( Inches per minute feed rate. )\n") + "G20 ( Units == INCHES. )\n"
+ "G90 ( Absolute coordinates. )\n";
}
//set postamble
postamble = string("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);
//cout << "Center:" << board_center * 25.4 << " Xvalue:" << xvalue * 25.4 << endl;
}
}
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
*/
/******************************************************************************/
void ExcellonProcessor::export_ngc(const string of_name, shared_ptr<Driller> driller, bool drillfront, bool mirror_absolute, bool onedrill) {
ivalue_t double_mirror_axis = mirror_absolute ? 0 : board_width;
//SVG EXPORTER
int rad = 1.;
//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 << " )" << endl;
}
of << endl;
if (!onedrill) {
of << "( This file uses " << bits->size() << " drill bit sizes. )\n\n";
} else {
of << "( This file uses only one drill bit. )\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.
// preamble
of << preamble_ext;
of << preamble << "S" << left << driller->speed << " ( RPM spindle speed.)\n" << endl;
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 due to 'onedrill' parameter.)\n" << endl;
} else {
of << "G00 Z" << driller->zchange * cfactor << " ( Retract )\n" << "T" << it->first << endl << "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" << endl;
}
const icoords drill_coords = holes->at(it->first);
icoords::const_iterator coord_iter = drill_coords.begin();
//SVG EXPORTER
if (bDoSVG) {
//set a random color
svgexpo->set_rand_color();
//draw first circle
svgexpo->circle((double_mirror_axis - coord_iter->first), coord_iter->second, rad);
svgexpo->stroke();
}
//coord_iter->first = x-coorinate (top view)
//coord_iter->second =y-coordinate (top view)
of << "G81 R" << driller->zsafe * cfactor << " Z" << driller->zwork * cfactor << " F" << driller->feed * cfactor << " X"
<< get_xvalue(drillfront, mirror_absolute, coord_iter->first) * cfactor << " Y" << (coord_iter->second * cfactor) << endl << endl;
++coord_iter;
while (coord_iter != drill_coords.end()) {
of << " X" << get_xvalue(drillfront, mirror_absolute, coord_iter->first) * cfactor << " Y" << (coord_iter->second * cfactor) << endl;
//SVG EXPORTER
if (bDoSVG) {
//make a whole
svgexpo->circle((double_mirror_axis - coord_iter->first), coord_iter->second, rad);
svgexpo->stroke();
}
++coord_iter;
}
of << "\n";
}
// retract, end
of << "G00 Z" << driller->zchange * cfactor << " ( All done -- retract )\n" << endl;
of << postamble_ext;
of << postamble;
of.close();
}
/******************************************************************************/
/*
*/
/******************************************************************************/
void ExcellonProcessor::millhole(std::ofstream &of, float x, float y, shared_ptr<Cutter> cutter, float holediameter) {
g_assert(cutter);
double cutdiameter = cutter->tool_diameter;
if (cutdiameter * 1.001 >= holediameter) {
of << "G0 X" << x * cfactor << " Y" << y * cfactor << endl;
//of<<"G1 Z"<<cutter->zwork<<endl;
//of<<"G0 Z"<<cutter->zsafe<<endl<<endl;
of << "G1 Z#50" << endl;
of << "G0 Z#51" << endl << endl;
} else {
float millr = (holediameter - cutdiameter) / 2.;
of << "G0 X" << (x + millr) * cfactor << " Y" << y << endl;
double z_step = cutter->stepsize;
//z_step=0.01;
double z = cutter->zwork + z_step * abs(int(cutter->zwork / z_step));
if (!cutter->do_steps) {
z = cutter->zwork * cfactor;
z_step = 1; //dummy to exit the loop
}
int stepcount = abs(int(cutter->zwork * cfactor / z_step));
while (z >= cutter->zwork) {
//of<<"G1 Z"<<z<<endl;
of << "G1 Z[#50+" << stepcount << "*#52]" << endl;
of << "G2 I" << -millr * cfactor << " J0" << endl;
z -= z_step;
stepcount--;
}
of << "G0 Z" << cutter->zsafe * cfactor << endl << endl;
}
}
/******************************************************************************/
/*
\brief mill larger holes by using a smaller mill-head
*/
/******************************************************************************/
void ExcellonProcessor::export_ngc(const string outputname, shared_ptr<Cutter> target, bool mirrored, bool mirror_absolute, bool onedrill) {
g_assert(mirrored == true);
g_assert(mirror_absolute == false);
cerr << "Currently Drilling " << endl;
// 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 << " )" << endl;
}
of << endl;
//of << "( This file uses " << bits->size() << " drill bit sizes. )\n\n";
of << "( This file uses a mill head of " << target->tool_diameter << " to drill the " << bits->size() << "bit sizes. )\n\n";
of.setf(ios_base::fixed);
of.precision(5);
of << setw(7);
// preamble
of << preamble << "S" << left << target->speed << " ( RPM spindle speed. )\n" << endl;
of << "F" << target->feed << endl;
of << "#50=" << target->zwork * cfactor << " ; zwork" << endl;
of << "#51=" << target->zsafe * cfactor << " ; zsafe" << endl;
of << "#52=" << target->stepsize * cfactor << " ; stepsize" << endl;
for (map<int, drillbit>::const_iterator it = bits->begin(); it != bits->end(); it++) {
float diameter = it->second.diameter;
//cerr<<"bit:"<<diameter<<endl;
const icoords drill_coords = holes->at(it->first);
icoords::const_iterator coord_iter = drill_coords.begin();
millhole(of, board_width - coord_iter->first, coord_iter->second, target, diameter);
++coord_iter;
while (coord_iter != drill_coords.end()) {
millhole(of, board_width - coord_iter->first, coord_iter->second, target, diameter);
++coord_iter;
}
}
// retract, end
of << "G00 Z" << target->zchange * cfactor << " ( All done -- retract )\n" << 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;
}