forked from pcb2gcode/pcb2gcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngc_exporter.cpp
421 lines (357 loc) · 16.2 KB
/
ngc_exporter.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
/*
* This file is part of pcb2gcode.
*
* Copyright (C) 2009, 2010 Patrick Birnzain <[email protected]> and others
* Copyright (C) 2010 Bernhard Kubicek <[email protected]>
* Copyright (C) 2013 Erik Schuster <[email protected]>
* Copyright (C) 2014, 2015 Nicola Corna <[email protected]>
*
* 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/>.
*/
#include "ngc_exporter.hpp"
#include "options.hpp"
#include <boost/algorithm/string.hpp>
#include "bg_operators.hpp"
#include <iostream>
using std::cerr;
using std::flush;
using std::ios_base;
using std::left;
#include <string>
using std::to_string;
using std::string;
using std::cout;
using std::endl;
#include <vector>
using std::vector;
#include <utility>
using std::pair;
#include <cmath>
using std::ceil;
#include <memory>
using std::shared_ptr;
using std::dynamic_pointer_cast;
#include <iomanip>
#include <boost/format.hpp>
using boost::format;
#include "units.hpp"
NGC_Exporter::NGC_Exporter(shared_ptr<Board> board)
: board(board), ocodes(1), globalVars(100) {}
/******************************************************************************/
/*
*/
/******************************************************************************/
void NGC_Exporter::add_header(string header)
{
this->header.push_back(header);
}
/******************************************************************************/
/*
*/
/******************************************************************************/
void NGC_Exporter::export_all(boost::program_options::variables_map& options)
{
bMetricinput = options["metric"].as<bool>(); //set flag for metric input
bMetricoutput = options["metricoutput"].as<bool>(); //set flag for metric output
bZchangeG53 = options["zchange-absolute"].as<bool>();
nom6 = options["nom6"].as<bool>();
string outputdir = options["output-dir"].as<string>();
//set imperial/metric conversion factor for output coordinates depending on metricoutput option
cfactor = bMetricoutput ? 25.4 : 1;
tileInfo = Tiling::generateTileInfo( options, board->get_height(), board->get_width() );
for ( string layername : board->list_layers() )
{
if (options["zero-start"].as<bool>()) {
xoffset = board->get_bounding_box().min_corner().x();
yoffset = board->get_bounding_box().min_corner().y();
} else {
xoffset = 0;
yoffset = 0;
}
xoffset -= options["x-offset"].as<Length>().asInch(bMetricinput ? 1.0/25.4 : 1);
yoffset -= options["y-offset"].as<Length>().asInch(bMetricinput ? 1.0/25.4 : 1);
if (layername == "back" ||
(layername == "outline" && !workSide(options, "cut"))) {
if (options["mirror-yaxis"].as<bool>()) {
yoffset = -yoffset + tileInfo.boardHeight*(tileInfo.tileY-1);
yoffset -= 2 * options["mirror-axis"].as<Length>().asInch(bMetricinput ? 1.0/25.4 : 1);
} else {
xoffset = -xoffset + tileInfo.boardWidth*(tileInfo.tileX-1);
xoffset -= 2 * options["mirror-axis"].as<Length>().asInch(bMetricinput ? 1.0/25.4 : 1);
}
}
boost::optional<autoleveller> leveller = boost::none;
//if ((options["al-front"].as<bool>() && layername == "front") ||
// (options["al-back"].as<bool>() && layername == "back")) {
// leveller.emplace(options, &ocodes, &globalVars,
// xoffset, yoffset, tileInfo);
//}
std::stringstream option_name;
option_name << layername << "-output";
string of_name = build_filename(outputdir, options[option_name.str()].as<string>());
cout << "Exporting " << layername << "... " << flush;
export_layer(board->get_layer(layername), of_name, leveller);
cout << "DONE." << " (Height: " << board->get_height() * cfactor
<< (bMetricoutput ? "mm" : "in") << " Width: "
<< board->get_width() * cfactor << (bMetricoutput ? "mm" : "in")
<< ")";
if (layername == "outline")
cout << " The board should be cut from the " << ( workSide(options, "cut") ? "FRONT" : "BACK" ) << " side. ";
cout << endl;
}
}
/* Assume that we start at a safe height above the first point in path. Cut
* around the path, handling bridges where needed. The bridges are identified
* by where the bridges begins. So the bridges is from points with indecies x
* to x+1 for each element in the bridges vector. We can always assume that the
* bridge segment and the segments on either side form a straight line. */
void NGC_Exporter::cutter_milling(std::ofstream& of, shared_ptr<Cutter> cutter, const linestring_type_fp& path,
const vector<size_t>& bridges, const double xoffsetTot, const double yoffsetTot) {
const unsigned int steps_num = ceil(-cutter->zwork / cutter->stepsize);
for (unsigned int i = 0; i < steps_num; i++) {
const double z = cutter->zwork / steps_num * (i + 1);
/* Lift between steps if this is not the first pass and the path
is not a closed loop. */
if (i > 0 && path.front() != path.back()) {
of << "G00 Z" << cutter->zsafe * cfactor << " ; retract\n";
of << "G00 X" << ( path.begin()->x() - xoffsetTot ) * cfactor << " Y"
<< ( path.begin()->y() - yoffsetTot ) * cfactor << " ; rapid move to begin.\n";
}
of << "G01 Z" << z * cfactor << " F" << cutter->vertfeed * cfactor << " ; plunge.\n";
of << "G04 P0 ; dwell for no time -- G64 should not smooth over this point\n";
of << "G01 F" << cutter->feed * cfactor << "\n";
auto current_bridge = bridges.cbegin();
bool in_bridge = false;
// Start at 1 because the caller already moved to the start.
for (size_t current = 1; current < path.size(); current++) {
while (current_bridge != bridges.cend() && *current_bridge < current - 1) {
current_bridge++;
}
// We are now cutting to current.
// Is this a bridge cut?
auto is_bridge_cut = current_bridge != bridges.cend() && *current_bridge == current-1;
if (is_bridge_cut && z < cutter->bridges_height && !in_bridge) {
// We're about to make a bridge cut so we need to go up.
of << "G00 Z" << cutter->bridges_height * cfactor << '\n';
in_bridge = true;
} else if (!is_bridge_cut && in_bridge) {
// Now plunge back down if needed.
of << "G01 Z" << z * cfactor << " F" << cutter->vertfeed * cfactor << '\n';
of << "G01 F" << cutter->feed * cfactor << '\n';
in_bridge = false;
}
// Now cut horizontally.
of << "G01 X" << (path.at(current).x() - xoffsetTot) * cfactor
<< " Y" << (path.at(current).y() - yoffsetTot) * cfactor << '\n';
}
}
}
void NGC_Exporter::isolation_milling(std::ofstream& of, shared_ptr<RoutingMill> mill, const linestring_type_fp& path,
boost::optional<autoleveller>& leveller, const double xoffsetTot, const double yoffsetTot) {
of << "G01 F" << mill->vertfeed * cfactor << '\n';
if (!mill->pre_milling_gcode.empty()) {
of << "; begin pre-milling-gcode\n";
of << mill->pre_milling_gcode << "\n";
of << "; end pre-milling-gcode\n";
}
const unsigned int steps_num = ceil(-mill->zwork / mill->stepsize);
for (unsigned int i = 0; i < steps_num; i++) {
const double z = mill->zwork / steps_num * (i + 1);
linestring_type_fp::const_iterator iter = path.cbegin();
of << "; Mill infeed pass " << i+1 << "/" << steps_num << "\n";
/* Lift between steps if this is not the first pass and the path
is not a closed loop. */
if (i > 0 && path.front() != path.back()) {
of << "G00 Z" << mill->zsafe * cfactor << " ; retract\n";
of << "G00 X" << ( path.begin()->x() - xoffsetTot ) * cfactor << " Y"
<< ( path.begin()->y() - yoffsetTot ) * cfactor << " ; rapid move to begin.\n";
}
//if (leveller) {
// leveller->setLastChainPoint(point_type_fp((path.begin()->x() - xoffsetTot) * cfactor,
// (path.begin()->y() - yoffsetTot) * cfactor));
// of << leveller->g01Corrected(point_type_fp((path.begin()->x() - xoffsetTot) * cfactor,
// (path.begin()->y() - yoffsetTot) * cfactor),
// z * cfactor);
//} else {
of << "G01 Z" << z * cfactor << "\n";
//}
of << "G04 P0 ; dwell for no time -- G64 should not smooth over this point\n";
of << "G01 F" << mill->feed * cfactor << '\n';
while (iter != path.cend()) {
//if (leveller) {
// of << leveller->addChainPoint(point_type_fp((iter->x() - xoffsetTot) * cfactor,
// (iter->y() - yoffsetTot) * cfactor),
// z * cfactor);
//} else {
of << "G01 X" << (iter->x() - xoffsetTot) * cfactor << " Y"
<< (iter->y() - yoffsetTot) * cfactor << '\n';
//}
++iter;
}
}
if (!mill->post_milling_gcode.empty()) {
of << "; begin post-milling-gcode\n";
of << mill->post_milling_gcode << "\n";
of << "; end post-milling-gcode\n";
}
}
void NGC_Exporter::export_layer(shared_ptr<Layer> layer, string of_name, boost::optional<autoleveller> leveller) {
string layername = layer->get_name();
shared_ptr<RoutingMill> mill = layer->get_manufacturer();
vector<pair<coordinate_type_fp, multi_linestring_type_fp>> all_toolpaths = layer->get_toolpaths();
if (all_toolpaths.size() < 1) {
return; // Nothing to do.
}
globalVars.getUniqueCode();
globalVars.getUniqueCode();
// open output file
std::ofstream of;
of.open(of_name);
if (!of.is_open()) {
std::stringstream error_message;
error_message << "Can't open for writing: " << of_name;
throw std::invalid_argument(error_message.str());
}
// write header to .mpf file
for ( string s : header )
{
of << "; " << s << endl;
}
//if( leveller || ( tileInfo.enabled && tileInfo.software != Software::CUSTOM ) )
// of << "; Gcode for " << tileInfo.software << "\n";
//else
of << "; Gcode for Sinumerik controls" << endl;
of.setf(ios_base::fixed); //write floating-point values in fixed-point notation
of.precision(5); //Set floating-point decimal precision
of << endl
<< preamble; //insert external preamble
if (bMetricoutput) {
//of << "G710 ; Units = Millimeters & Feed = Millimeters per minute.\n\n";
} else {
of << "; WARNING: NON-METRIC OUTPUT CONFIGURED!" << endl
<< "G700 ; Units = Inches & Feed = Inches per minute." << endl
<< endl;
}
//of << "G90 ; Absolute coordinates.\n";
//if (mill->explicit_tolerance) {
// of << "G641 ADIS=" << mill->tolerance * cfactor << " ; set maximum deviation from commanded toolpath\n";
//}
//of << "G01 F" << mill->feed * cfactor << " ; Feedrate.\n\n";
of << "G17 G90 G40 G64" << endl
<< "FGROUP(X1,Y1,Z1)" << endl
<< "FFWON" << endl
<< "CFIN" << endl
<< "TRANS X+0 Y+0 Z+100 ; comment this out after checking program on machine" << endl;
//if (leveller) {
// leveller->prepareWorkarea(all_toolpaths);
// leveller->header(of);
//}
shared_ptr<Cutter> cutter = dynamic_pointer_cast<Cutter>(mill);
shared_ptr<Isolator> isolator = dynamic_pointer_cast<Isolator>(mill);
// One list of bridges for each path.
vector<vector<size_t>> all_bridges;
if (cutter) {
for (auto& path : all_toolpaths[0].second) { // Cutter layer can only have one tool_diameter.
auto bridges = layer->get_bridges(path);
all_bridges.push_back(bridges);
}
}
uniqueCodes main_sub_ocodes(200);
for (size_t toolpaths_index = 0; toolpaths_index < all_toolpaths.size(); toolpaths_index++) {
const auto& toolpaths = all_toolpaths[toolpaths_index].second;
if (toolpaths.size() < 1) {
continue; // Nothing to do for this mill size.
}
Tiling tiling(tileInfo, cfactor, main_sub_ocodes.getUniqueCode());
if (toolpaths_index == all_toolpaths.size() - 1) {
// this is last tool, set file footer
std::ostringstream end_gcode;
end_gcode << endl
<< "G04 P0 ; dwell for no time -- G64 should not smooth over this point" << endl
<< (bZchangeG53 ? "G53 " : "") << "G00 Z" << str( format("%.6f") % ( mill->zchange * cfactor ) ) << " ; retract" << endl
<< endl
<< postamble
<< "M5 ; Spindle off." << endl
<< "G04 P" + to_string(mill->spindown_time) << endl;
tiling.setGCodeEnd(end_gcode.str());
}
// tool change
const auto& tool_diameter = all_toolpaths[toolpaths_index].first;
std::ostringstream tool_string;
tool_string << (cutter ? "EM" : "IM") << tool_diameter * (bMetricoutput ? 25.4 : 1);
of << endl
<< "MSG(\"" << tool_string.str() << "\") ; Set tool name as screen message." << endl
<< "T=\"" << tool_string.str() << "\" ; Select tool by name." << endl
<< "M6 ; Do tool change." << endl
<< endl
<< "M1 ; Optional machine stop." << endl
<< "M3 S" << left << mill->speed << " ; Spindle on clockwise with set RPM." << endl
<< "M7 ; Air blast cooling on." << endl;
tiling.header( of );
for( unsigned int i = 0; i < tileInfo.forYNum; i++ ) {
double yoffsetTot = yoffset - i * tileInfo.boardHeight;
for( unsigned int j = 0; j < tileInfo.forXNum; j++ ) {
double xoffsetTot = xoffset - ( i % 2 ? tileInfo.forXNum - j - 1 : j ) * tileInfo.boardWidth;
if( tileInfo.enabled && tileInfo.software == Software::CUSTOM )
of << "; Piece #" << j + 1 + i * tileInfo.forXNum << ", position [" << j << ";" << i << "]" << endl
<< endl;
// contours
for(size_t path_index = 0; path_index < toolpaths.size(); path_index++) {
const linestring_type_fp& path = toolpaths[path_index];
if (path.size() < 1) {
continue; // Empty path.
}
// retract, move to the starting point of the next contour
of << "G04 P0 ; dwell for no time -- G64 should not smooth over this point" << endl
<< "G00 Z" << mill->zsafe * cfactor << " ; retract" << endl
<< endl
<< "G00 X" << ( path.begin()->x() - xoffsetTot ) * cfactor << " Y"
<< ( path.begin()->y() - yoffsetTot ) * cfactor << " ; rapid move to begin." << endl;
/* if we're cutting, perhaps do it in multiple steps, but do isolations just once.
* i know this is partially repetitive, but this way it's easier to read
*/
if (cutter) {
cutter_milling(of, cutter, path, all_bridges[path_index], xoffsetTot, yoffsetTot);
} else {
isolation_milling(of, mill, path, leveller, xoffsetTot, yoffsetTot);
}
}
}
}
of << "M09 ; Coolant off." << endl;
tiling.footer( of );
}
//if (leveller) {
// leveller->footer(of);
//}
of << "M30 ; Program end." << endl << endl;
of.close();
}
/******************************************************************************/
/*
*/
/******************************************************************************/
void NGC_Exporter::set_preamble(string _preamble)
{
preamble = _preamble;
}
/******************************************************************************/
/*
*/
/******************************************************************************/
void NGC_Exporter::set_postamble(string _postamble)
{
postamble = _postamble;
}
/* vim: set tabstop=2 : */