-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcode.cpp
170 lines (135 loc) · 3.55 KB
/
gcode.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
#include <iostream>
#include <fstream>
#include <vector>
#include <cstdlib>
#include <sstream>
using namespace std;
#include "point.h"
double atof(string myString) {
return atof(myString.c_str());
}
template<class T>
class gCode {
public:
gCode() {
}
void read(string filename) {
read(filename.c_str());
}
void read(const char * filename) {
fstream gCode;
gCode.open(filename, fstream::in);
if (!gCode.is_open()) {
cout << "couldn't open " << filename << endl;
return;
}
string line;
//int counter = 0;
T x = 0.0, z = 0.0;
bool stateG0 = false;
bool firstRun = true;
int g0index = 0, g1index = 0;
g0.reserve(10);
g1.reserve(10);
g0.push_back(vector<point<T> >());
g1.push_back(vector<point<T> >());
while (!gCode.eof()) {
gCode >> line;
//cout << line << endl;
if (line[0] == 'g' || line[0] == 'G') {
if (line[1] == '0') {
if (firstRun) {
firstRun = false;
}
else {
g0[g0index].push_back(point<T>(z, x));
//cout << "g0 " << g0[g0index].back() << endl;
if (!stateG0) {
g0index++;
g0.push_back(vector<point<T> >());
g1[g1index].push_back(point<T>(z, x));
//cout << "g1 " << g1[g1index].back() << endl;
}
}
stateG0 = true;
}
if (line[1] == '1') {
if (firstRun) {
firstRun = false;
}
else {
g1[g1index].push_back(point<T>(z, x));
//cout << "g1 " << g1[g1index].back() << endl;
if (stateG0) {
g1index++;
g1.push_back(vector<point<T> >());
g0[g0index].push_back(point<T>(z, x));
//cout << "g0 " << g0[g0index].back() << endl;
}
}
stateG0 = false;
}
}
if (line[0] == 'z' || line[0] == 'Z') {
z = atof(line.substr(1));
}
if (line[0] == 'x' || line[0] == 'X') {
x = atof(line.substr(1));
}
maximum.setMax(point<T>(z, x));
minimum.setMin(point<T>(z, x));
line.clear();
}
gCode.close();
}
void gnuplot() {
fstream plot;
plot.open("g1.data", fstream::out);
if (!plot.is_open()) {
cout << "couldn't open g1.data" << endl;
return;
}
gnuplot(plot, g1);
plot.close();
plot.open("g0.data", fstream::out);
if (!plot.is_open()) {
cout << "couldn't open g0.data" << endl;
return;
}
gnuplot(plot, g0);
plot.close();
stringstream command;
command << "gnuplot -e \"set terminal svg;"
<< "set output 'gcode.svg';"
<< "set xrange[" << minimum.z << ":" << maximum.z << "];"
<< "set yrange[" << maximum.x << ":" << minimum.z << "];"
<< "plot 'g1.data' title 'G1' with lines, 'g0.data' title 'G0' with lines;\"";
system(command.str().c_str());
}
/*!
Generate code for drawing a bunch of open lines using gnuplot
*/
void gnuplot(ostream& out, vector<vector<point<T> > >& lines) {
for (class vector<vector<point<T> > >::iterator it = lines.begin(); it != lines.end(); it++) {
gnuplot(out, (*it));
}
}
/*!
Generate code for drawing a bunch of points as an open line using gnuplot.
*/
void gnuplot(ostream& out, vector<point<T> >& points) {
for (class vector<point<T> >::iterator it = points.begin(); it != points.end(); it++) {
out << (*it) << endl;
}
out << endl;
}
vector<vector<point<T> > > g0;
vector<vector<point<T> > > g1;
point<T> maximum, minimum;
};
int main(void) {
gCode<double> gc;
gc.read("gcode.ngc");
gc.gnuplot();
return 0;
}