-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgcodeExaminer.pde
337 lines (267 loc) · 8.72 KB
/
gcodeExaminer.pde
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
// gcodeExaminer by Whosawhatsis
// based on gcode3b by peter jansen
// g-code visualizer for toolpaths expored from skeinforge for the reprap/makerbot.
// please feel free (and encouraged) to modify. if you do, please post your modifications
// as a fork on Github.
// https://github.com/whosawhatsis/gcodeExaminer
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
MouseWheelEventDemo wheel;
import processing.opengl.*;
import processing.video.*;
MovieMaker mm;
int width = 640;
int height = 480;
float pi = 3.1415926;
float arrowSize = 5;
float depth = 400;
color boxFill;
int speed = 1;
boolean demo = true;
String[] inputfile;
Points[] allpoints;
int numpoints;
int maxpoints = 500000;
float cur_z = 0;
float max_z = 0;
int cur_index = 0;
float rotationX = 0;
float rotationY = 0;
float current_height = 0;
float current_rot_x = 0;
float max_f = 0;
float max_e = 0;
void setup() {
// size(640, 360, P3D);
size(640, 480, P3D);
// size(width, height, OPENGL); hint(DISABLE_OPENGL_2X_SMOOTH);
// size(1280, 720, P3D);
noStroke();
colorMode(HSB, 255);
PFont font;
font = loadFont("Monospaced-12.vlw");
hint(ENABLE_NATIVE_FONTS);
textFont(font);
// inputfile = loadStrings("horseshoe-clip_export.gcode");
// inputfile = loadStrings("rocket_export.gcode");
String filename = selectInput();
println(filename);
inputfile = loadStrings(filename);
allpoints = new Points[maxpoints];
wheel = new MouseWheelEventDemo();
numpoints = 0;
// Cycle through all lines
float e = 0;
for (int i=0; i<inputfile.length; i++) {
String pieces[] = split(inputfile[i], ' '); // deliminate data by spaces
// Check for G1 code
if (pieces.length > 2) {
if (pieces[0].equals("G1")) {
// Add new point
if (numpoints < maxpoints) {
allpoints[numpoints] = new Points(pieces);
allpoints[numpoints].l = i;
// find maximum Z point for animation
if (allpoints[numpoints].z > max_z) {
max_z = allpoints[numpoints].z;
}
// find maximum F for colour
if (allpoints[numpoints].f > max_f) {
max_f = allpoints[numpoints].f;
}
numpoints ++;
}
}
}
}
// Start Recording Animation
// mm = new MovieMaker(this, width, height, "drawing1.mov");
// mm = new MovieMaker(this, width, height, "test6_640_480.mov", 15, MovieMaker.H263, MovieMaker.LOSSLESS);
// mm = new MovieMaker(this, width, height, "test6_640_480.mov", 15, MovieMaker.JPEG, MovieMaker.LOSSLESS);
}
public class MouseWheelEventDemo implements MouseWheelListener {
public MouseWheelEventDemo() {
addMouseWheelListener(this);
}
public void mouseWheelMoved(MouseWheelEvent e) {
demo = false;
depth += e.getWheelRotation() * 5;
}
}
void draw() {
background(255);
float s = 20.0;
float initial_speed = 0.002;
float overhead_height = 0.0;
for(int i = allpoints[cur_index].l; i < inputfile.length && i < allpoints[cur_index].l + 40; i++) {
if (i == allpoints[cur_index].l) fill(0, 0, 0, 255);
else fill(0, 0, 0, 64);
text(inputfile[i], 2, 2 + 12 * (i - allpoints[cur_index].l), 2000, 476);
}
// println(allpoints[cur_index].z);
if(mousePressed){
demo = false;
rotationY -= (mouseX-pmouseX) * 0.01;
rotationX -= (mouseY-pmouseY) * 0.01;
} else if (demo) {
rotationY += .02;
rotationX -= (rotationX - pi / 2) / 100;
}
translate(width/2, height/2 + allpoints[cur_index].z * sin(rotationX) * 10, - depth - (allpoints[cur_index].z * 20) - (allpoints[cur_index].z) * (cos(rotationX) * 10));
rotateX(rotationX);
rotateZ(rotationY);
if (false)
// Center and spin grid
if ((frameCount * initial_speed) < pi/2) {
translate(width/2, height/2, -depth);
rotateX(frameCount * initial_speed);
rotateZ(-frameCount * initial_speed);
} else {
if (current_height < (s * allpoints[cur_index].z) + overhead_height) {
current_height += 0.85;
}
if ((frameCount * initial_speed) > pi) {
if (current_rot_x > -pi/12) {
current_rot_x -= 0.001;
}
}
translate(width/2, height/2 + (current_height) , -depth );
rotateX(pi/2 + current_rot_x);
rotateZ(-frameCount * initial_speed);
// rotateZ(0);
}
scale(1, -1, 1); // Right-handed coordinate system, please. Thank you.
// Build grid using multiple translations
for (int i=0; i < numpoints-1; i++){
// boxFill = color(abs(i), abs(j), abs(k), 50 );
// boxFill = color(0, 0, 100 + round(i/numpoints), 50 );
// Colour
float alpha_value = 255;
// if (i == 1) println(alpha_value);
if(i > cur_index) {
if (allpoints[i].z == allpoints[cur_index].z) alpha_value = 20;
else alpha_value = 1;
} else if (i < cur_index) {
alpha_value = 255 - (cur_index - i) * 255 / (60 + speed);
if (alpha_value < 20) alpha_value = 20;
if (allpoints[i].z == allpoints[cur_index].z && alpha_value < 70) alpha_value = 50;
}
/*
if ( false &&(i + speed * 2 + 30) < cur_index || alpha_value < 20) {
if (cur_index > i) {
// current layer is below cur_z, so the alpha can be less transparent
alpha_value = 20;
} else {
alpha_value = 1;
}
}
if (alpha_value > 128) {
// alpha_value = 2;
}*/
if (allpoints[i].z == allpoints[cur_index].z) boxFill = color(255, 255, 100 + 100*round(allpoints[i].f / max_f), alpha_value );
else boxFill = color(150, 100, 100 + 100*round(allpoints[i].f / max_f), alpha_value);
float basex0 = s * allpoints[i].x;
float basey0 = s * allpoints[i].y;
float basez0 = s * allpoints[i].z;
float basex1 = s * allpoints[i+1].x;
float basey1 = s * allpoints[i+1].y;
float basez1 = s * allpoints[i+1].z;
// pushMatrix();
// translate(s * allpoints[i].x + (a*difx), s * allpoints[i].y + (a*dify), s * allpoints[i].z + (a*difz));
fill(boxFill);
float en = 5.0f;
// draw a triangle
beginShape(TRIANGLE_STRIP);
vertex(basex0, basey0, basez0);
vertex(basex1, basey1, basez1);
vertex(basex0 + en, basey0, basez0);
vertex(basex1 + en, basey1, basez1);
vertex(basex0 + en, basey0, basez0 + en);
vertex(basex1 + en, basey1, basez1 + en);
vertex(basex0, basey0, basez0 + en);
vertex(basex1, basey1, basez1 + en);
endShape();
//box(boxSize, boxSize, boxSize);
// popMatrix();
}
if (speed != 0) {
// Alpha animation update
cur_index += speed;
if (cur_index >= numpoints - speed + 1) {
cur_z = 0;
cur_index = numpoints - 1;
// cur_index = numpoints - speed + 1;
} else speed++;
}
// Add window's pixels to movie
// mm.addFrame();
/*
// Build grid using multiple translations
for (float i =- depth/2+margin; i <= depth/2-margin; i += boxSize){
pushMatrix();
for (float j =- height+margin; j <= height-margin; j += boxSize){
pushMatrix();
for (float k =- width+margin; k <= width-margin; k += boxSize){
// Base fill color on counter values, abs function
// ensures values stay within legal range
boxFill = color(abs(i), abs(j), abs(k), 50 );
pushMatrix();
translate(k, j, i);
fill(boxFill);
// draw a triangle
beginShape(TRIANGLES);
vertex(0, 0, 0);
vertex(boxSize, 0, 0);
vertex(boxSize/2, boxSize, 0);
endShape();
//box(boxSize, boxSize, boxSize);
popMatrix();
}
popMatrix();
}
popMatrix();
}
*/
}
void keyPressed() {
print(cur_index);
print(' ');
if (keyCode == RIGHT) {
speed = 0;
++cur_index;
} else if (keyCode == LEFT) {
speed = 0;
--cur_index;
} else if (keyCode == UP) {
speed = 0;
++cur_index;
while(cur_index < numpoints - 1 && allpoints[cur_index].z == allpoints[++cur_index].z);
--cur_index;
} else if (keyCode == DOWN) {
speed = 0;
--cur_index;
while(cur_index > 0 && allpoints[cur_index].z == allpoints[--cur_index].z);
//++cur_index;
} else if (key == ' ') {
// Finish the movie if space bar is pressed
mm.finish();
// Quit running the sketch once the file is written
exit();
}
if (cur_index < 1) cur_index = 1;
else if (cur_index >= numpoints) cur_index = numpoints - 1;
println(cur_index);
}
class Points {
float x;
float y;
float z;
float f;
int l;
public Points(String[] pieces) {
x = float(pieces[1].substring(1));
y = float(pieces[2].substring(1));
z = float(pieces[3].substring(1));
f = float(pieces[4].substring(1));
}
}