File: ngc_exporter.cpp

package info (click to toggle)
pcb2gcode 1.1.4-git20120902-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,400 kB
  • ctags: 358
  • sloc: sh: 10,116; cpp: 2,335; python: 53; makefile: 43
file content (234 lines) | stat: -rw-r--r-- 6,832 bytes parent folder | download | duplicates (3)
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
/*
 * This file is part of pcb2gcode.
 * 
 * Copyright (C) 2009, 2010 Patrick Birnzain <pbirnzain@users.sourceforge.net>
 * 
 * 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 <iostream>
#include <iomanip>
using namespace std;

NGC_Exporter::NGC_Exporter( shared_ptr<Board> board ) : Exporter(board)
{
	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)
{
	BOOST_FOREACH( string layername, board->list_layers() ) {
		std::stringstream option_name;
		option_name << layername << "-output";
		string of_name = options[option_name.str()].as<string>();
		cerr << "Current Layer: " << layername << ", exporting to " << of_name << "." << endl;
		export_layer( board->get_layer(layername), of_name);
	}
}

double
NGC_Exporter::get_tolerance( void )
{
	// set maximum deviation to 5 pixels to ensure smooth movement
	return 5.0/this->board->get_dpi();
}

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;
	
	// open output file
	std::ofstream of; of.open( of_name.c_str() );

	// write header to .ngc file
        BOOST_FOREACH( string s, header )
        {
                of << "( " << s << " )" << endl;
        }
        of << endl;

        of.setf( ios_base::fixed );
        of.precision(5);
	of << setw(7);

	// preamble
	of << "G94     ( Inches per minute feed rate. )\n"
	   << "G20     ( Units == INCHES.             )\n"
	   << "G90     ( Absolute coordinates.        )\n"
	   << "S" << left << mill->speed << "  ( RPM spindle speed.           )\n"
	   << "M3      ( Spindle on clockwise.        )\n"
	   << endl;

	of << "G64 P" << get_tolerance() << " ( set maximum deviation from commanded toolpath )\n"
	   << endl;

	
	//SVG EXPORTER
	if (bDoSVG) {
		//choose a color
		svgexpo->set_rand_color();
	}
	
	
	// contours
 	BOOST_FOREACH( shared_ptr<icoords> path, layer->get_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 << " ( retract )\n" << endl;
                of << "G00 X" << path->begin()->first << " Y" << path->begin()->second << " ( 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
			double z_step = cutter->stepsize;
			double z = mill->zwork + z_step * abs( int( mill->zwork / z_step ) );

			while( z >= mill->zwork ) {
				of << "G01 Z" << z << " F" << mill->feed << " ( plunge. )\n";
				of << "G04 P0 ( dwell for no time -- G64 should not smooth over this point )\n";

				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( /* it's necessary to write the coordinates if... */
							last == path->end() || /* it's the beginning */
							peek == path->end() || /* it's the end */
							!( /* or if neither of the axis align */
								( last->first == iter->first && iter->first == peek->first ) || /* x axis aligns */
								( last->second == iter->second && iter->second == peek->second ) /* y axis aligns */
							)
							/* no need to check for "they are on one axis but iter is outside of last and peek" becaus that's impossible from how they are generated */
					  ) {
						of << "X" << iter->first << " Y" << iter->second << 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;
				}
			
				z -= z_step;
			}
		} else {
			// isolating
			of << "G01 Z" << mill->zwork << " F" << mill->feed << " ( plunge. )\n";
			of << "G04 P0 ( dwell for no time -- G64 should not smooth over this point )\n";

			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( /* it's necessary to write the coordinates if... */
						last == path->end() || /* it's the beginning */
						peek == path->end() || /* it's the end */
						!( /* or if neither of the axis align */
							( last->first == iter->first && iter->first == peek->first ) || /* x axis aligns */
							( last->second == iter->second && iter->second == peek->second ) /* y axis aligns */
						)
						/* no need to check for "they are on one axis but iter is outside of last and peek" becaus that's impossible from how they are generated */
				  ) {
					of << "X" << iter->first << " Y" << iter->second << 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;
			}

		}


		
        }

        of << endl;

	// retract, end
	of << "G04 P0 ( dwell for no time -- G64 should not smooth over this point )\n";
	of << "G00 Z" << mill->zchange << " ( retract )\n" << endl;

	of << "M9 ( Coolant off. )\n";
	of << "M2 ( Program end. )\n\n";

	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;
}