forked from pcb2gcode/pcb2gcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngc_exporter.cpp
416 lines (356 loc) · 16 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
/*
* 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 <boost/foreach.hpp>
#include <boost/algorithm/string.hpp>
#include <iostream>
using std::cerr;
using std::ios_base;
using std::left;
#include <iomanip>
#include <glibmm/miscutils.h>
using Glib::build_filename;
#include <boost/format.hpp>
using boost::format;
/******************************************************************************/
/*
*/
/******************************************************************************/
NGC_Exporter::NGC_Exporter(shared_ptr<Board> board)
: Exporter(board), dpi(board->get_dpi()),
quantization_error( 2.0 / dpi ), ocodes(1), globalVars(100)
{
this->board = board;
bDoSVG = false;
}
/******************************************************************************/
/*
*/
/******************************************************************************/
void NGC_Exporter::set_svg_exporter(shared_ptr<SVG_Exporter> svgexpo)
{
this->svgexpo = svgexpo;
bDoSVG = true;
}
/******************************************************************************/
/*
*/
/******************************************************************************/
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
bFrontAutoleveller = options["al-front"].as<bool>();
bBackAutoleveller = options["al-back"].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;
if( options["zero-start"].as<bool>() )
{
xoffset = board->get_min_x();
yoffset = board->get_min_y();
}
else
{
xoffset = 0;
yoffset = 0;
}
tileInfo = Tiling::generateTileInfo( options, ocodes, board->get_height(), board->get_width() );
if( options.count("g64") )
g64 = options["g64"].as<double>();
else
g64 = quantization_error * cfactor; // set maximum deviation to 2 pixels to ensure smooth movement
if( bFrontAutoleveller || bBackAutoleveller )
leveller = new autoleveller ( options, &ocodes, &globalVars, quantization_error,
xoffset, yoffset, tileInfo );
if (options["bridges"].as<double>() > 0 && options["bridgesnum"].as<unsigned int>() > 0)
bBridges = true;
else
bBridges = false;
BOOST_FOREACH( string layername, board->list_layers() )
{
std::stringstream option_name;
option_name << layername << "-output";
string of_name = build_filename(outputdir, options[option_name.str()].as<string>());
cerr << "Exporting " << layername << "... ";
export_layer(board->get_layer(layername), of_name);
cerr << "DONE." << " (Height: " << board->get_height() * cfactor
<< (bMetricoutput ? "mm" : "in") << " Width: "
<< board->get_width() * cfactor << (bMetricoutput ? "mm" : "in")
<< ")";
if (layername == "outline")
cerr << " The board should be cut from the " << ( workSide(options, "cut") ? "FRONT" : "BACK" ) << " side. ";
cerr << endl;
}
}
/******************************************************************************/
/*
*/
/******************************************************************************/
void NGC_Exporter::export_layer(shared_ptr<Layer> layer, string of_name)
{
string layername = layer->get_name();
shared_ptr<RoutingMill> mill = layer->get_manufacturer();
bool bSvgOnce = TRUE;
bool bAutolevelNow;
vector<shared_ptr<icoords> > toolpaths = layer->get_toolpaths();
vector<unsigned int> bridges;
vector<unsigned int>::const_iterator currentBridge;
static const unsigned int repeatVar = ocodes.getUniqueCode();
double xoffsetTot;
double yoffsetTot;
Tiling tiling( tileInfo, cfactor );
tiling.setGCodeEnd( "\nG04 P0 ( dwell for no time -- G64 should not smooth over this point )\n"
"G00 Z" + str( format("%.3f") % ( mill->zchange * cfactor ) ) +
" ( retract )\n\n" + postamble + "M5 ( Spindle off. )\nM9 ( Coolant off. )\n"
"M2 ( Program end. )\n\n" );
tiling.initialXOffsetVar = globalVars.getUniqueCode();
tiling.initialYOffsetVar = globalVars.getUniqueCode();
// open output file
std::ofstream of;
of.open(of_name.c_str());
// write header to .ngc file
BOOST_FOREACH( string s, header )
{
of << "( " << s << " )\n";
}
if( ( bFrontAutoleveller && layername == "front" ) ||
( bBackAutoleveller && layername == "back" ) )
bAutolevelNow = true;
else
bAutolevelNow = false;
if( bAutolevelNow || ( tileInfo.enabled && tileInfo.software != CUSTOM ) )
of << "( Gcode for " << getSoftwareString(tileInfo.software) << " )\n";
else
of << "( Software-independent Gcode )\n";
of.setf(ios_base::fixed); //write floating-point values in fixed-point notation
of.precision(5); //Set floating-point decimal precision
of << "\n" << preamble; //insert external preamble
if (bMetricoutput)
{
of << "G94 ( Millimeters per minute feed rate. )\n"
<< "G21 ( Units == Millimeters. )\n\n";
}
else
{
of << "G94 ( Inches per minute feed rate. )\n"
<< "G20 ( Units == INCHES. )\n\n";
}
of << "G90 ( Absolute coordinates. )\n"
<< "S" << left << mill->speed << " ( RPM spindle speed. )\n"
<< "G64 P" << g64 << " ( set maximum deviation from commanded toolpath )\n"
<< "F" << mill->feed * cfactor << " ( Feedrate. )\n\n";
if( bAutolevelNow )
{
if( !leveller->prepareWorkarea( toolpaths ) )
{
std::cerr << "Required number of probe points (" << leveller->requiredProbePoints() <<
") exceeds the maximum number (" << leveller->maxProbePoints() << "). "
"Reduce either al-x or al-y." << std::endl;
exit(EXIT_FAILURE);
}
leveller->header( of );
}
of << "F" << mill->feed * cfactor << " ( Feedrate. )\n"
<< "M3 ( Spindle on clockwise. )\n";
tiling.header( of );
//SVG EXPORTER
if (bDoSVG)
{
//choose a color
svgexpo->set_rand_color();
}
for( unsigned int i = 0; i < tileInfo.forYNum; i++ )
{
yoffsetTot = yoffset - i * tileInfo.boardHeight;
for( unsigned int j = 0; j < tileInfo.forXNum; j++ )
{
xoffsetTot = xoffset - ( i % 2 ? tileInfo.forXNum - j - 1 : j ) * tileInfo.boardWidth;
if( tileInfo.enabled && tileInfo.software == CUSTOM )
of << "( Piece #" << j + 1 + i * tileInfo.forXNum << ", position [" << j << ";" << i << "] )\n\n";
// contours
BOOST_FOREACH( shared_ptr<icoords> path, toolpaths )
{
// retract, move to the starting point of the next contour
of << "G04 P0 ( dwell for no time -- G64 should not smooth over this point )\n";
of << "G00 Z" << mill->zsafe * cfactor << " ( retract )\n\n";
of << "G00 X" << ( path->begin()->first - xoffsetTot ) * cfactor << " Y"
<< ( path->begin()->second - yoffsetTot ) * cfactor << " ( rapid move to begin. )\n";
//SVG EXPORTER
if (bDoSVG)
{
svgexpo->move_to(path->begin()->first, path->begin()->second);
bSvgOnce = TRUE;
}
/* 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
*/
shared_ptr<Cutter> cutter = boost::dynamic_pointer_cast<Cutter>(mill);
if (cutter && cutter->do_steps)
{
//--------------------------------------------------------------------
//cutting (outline)
double z_step = cutter->stepsize;
double z = mill->zwork + z_step * abs(int(mill->zwork / z_step));
if( bBridges )
if( i == 0 && j == 0 ) //Compute the bridges only the 1st time
bridges = layer->get_bridges( path );
while (z >= mill->zwork)
{
of << "G01 Z" << z * cfactor << " F" << mill->vertfeed * cfactor << " ( plunge. )\n";
of << "G04 P0 ( dwell for no time -- G64 should not smooth over this point )\n";
of << "F" << mill->feed * cfactor << "\n";
icoords::iterator iter = path->begin();
icoords::iterator last = path->end(); // initializing to quick & dirty sentinel value
icoords::iterator peek;
if (bBridges)
currentBridge = bridges.begin();
while (iter != path->end())
{
peek = iter + 1;
if (mill->optimise //Already optimised (also includes the bridge case)
|| last == path->end() //First
|| peek == path->end() //Last
|| !aligned(last, iter, peek) ) //Not aligned
{
of << "X" << ( iter->first - xoffsetTot ) * cfactor << " Y"
<< ( iter->second - yoffsetTot ) * cfactor << endl;
if (bDoSVG)
{
if (bSvgOnce)
svgexpo->line_to(iter->first, iter->second);
}
if( bBridges && currentBridge != bridges.end() )
{
double bridges_depth = cutter->bridges_height >= 0 ?
cutter->bridges_height : cutter->bridges_height * z / mill->zwork;
if( *currentBridge == iter - path->begin() )
of << "Z" << bridges_depth * cfactor << endl;
else if( *currentBridge == last - path->begin() )
{
of << "Z" << z * cfactor << " F" << cutter->vertfeed * cfactor << endl;
of << "F" << cutter->feed * cfactor;
++currentBridge;
}
}
}
last = iter;
++iter;
}
//SVG EXPORTER
if (bDoSVG)
{
svgexpo->close_path();
bSvgOnce = FALSE;
}
z -= z_step;
}
}
else
{
//--------------------------------------------------------------------
// isolating (front/backside)
of << "F" << mill->vertfeed * cfactor << endl;
if( bAutolevelNow )
{
leveller->setLastChainPoint( icoordpair( ( path->begin()->first - xoffsetTot ) * cfactor,
( path->begin()->second - yoffsetTot ) * cfactor ) );
of << leveller->g01Corrected( icoordpair( ( path->begin()->first - xoffsetTot ) * cfactor,
( path->begin()->second - yoffsetTot ) * cfactor ) );
}
else
of << "G01 Z" << mill->zwork * cfactor << "\n";
of << "G04 P0 ( dwell for no time -- G64 should not smooth over this point )\n";
of << "F" << mill->feed * cfactor << endl;
icoords::iterator iter = path->begin();
icoords::iterator last = path->end(); // initializing to quick & dirty sentinel value
icoords::iterator peek;
while (iter != path->end())
{
peek = iter + 1;
if (mill->optimise //When simplifypath is performed, no further optimisation is required
|| last == path->end() //First
|| peek == path->end() //Last
|| !aligned(last, iter, peek) ) //Not aligned
{
/* no need to check for "they are on one axis but iter is outside of last and peek"
because that's impossible from how they are generated */
if( bAutolevelNow )
of << leveller->addChainPoint( icoordpair( ( iter->first - xoffsetTot ) * cfactor,
( iter->second - yoffsetTot ) * cfactor ) );
else
of << "X" << ( iter->first - xoffsetTot ) * cfactor << " Y"
<< ( iter->second - yoffsetTot ) * cfactor << endl;
//SVG EXPORTER
if (bDoSVG)
if (bSvgOnce)
svgexpo->line_to(iter->first, iter->second);
}
last = iter;
++iter;
}
//SVG EXPORTER
if (bDoSVG)
{
svgexpo->close_path();
bSvgOnce = FALSE;
}
}
}
}
}
tiling.footer( of );
if( bAutolevelNow )
{
leveller->footer( of );
}
of.close();
//SVG EXPORTER
if (bDoSVG)
{
svgexpo->stroke();
}
}
/******************************************************************************/
/*
*/
/******************************************************************************/
void NGC_Exporter::set_preamble(string _preamble)
{
preamble = _preamble;
}
/******************************************************************************/
/*
*/
/******************************************************************************/
void NGC_Exporter::set_postamble(string _postamble)
{
postamble = _postamble;
}