forked from FMMT666/yagv-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyagv-ng
executable file
·629 lines (474 loc) · 16.4 KB
/
yagv-ng
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
#!/usr/bin/env python
YAGV_VERSION = "0.5"
import pyglet
# Disable error checking for increased performance
pyglet.options['debug_gl'] = False
from pyglet import clock
from pyglet.gl import *
from pyglet.window import key
from pyglet.window import mouse
from gcodeParser import *
import os.path
import time
class App:
def __init__(self):
# default file to load
self.DEFAULT_FILE_NAME = "data/einhornkatzchen_min.gcode"
# viewport variables
self.RX = 0.0
self.RZ = 0.0
self.zoom = 1.0
self.viewZ = 0.0
self.viewX = 0.0
# flags and variables for mouse operation "rotate"
self.rotateDrag = False # dragging in progess
self.rotateDragStartRX = None # not necessary, only here for visibility
self.rotateDragStartRZ = None
self.rotateDragStartX = None
self.rotateDragStartY = None
# flags and variables for mouse operation "layer change"
self.layerDrag = False # dragging in progess
self.layerDragStartLayer = None # not necessary, only here for visibility
self.layerDragStartX = None
self.layerDragStartY = None
# flags for keyboard modifiers
self.pressedShift = False
self.pressedCtrl = False
self.pressedAlt = False # Command for Mac ('Alt' has wrong key code in OS X pyglet)
def main(self):
#### MAIN CODE ####
print "Yet Another GCode Viewer v%s"%YAGV_VERSION
import sys
if len(sys.argv) > 1:
path = sys.argv[1]
else:
# get the real path to the script
script_path = os.path.realpath(__file__)
# get the containing folder
script_dir = os.path.dirname(script_path)
# default to hana
path = os.path.join(script_dir, self.DEFAULT_FILE_NAME)
self.load(path)
# default to the middle layer
self.layerIdx = len(self.model.layers)/2
self.window = MyWindow(self, caption="Yet Another GCode Viewer v%s"%YAGV_VERSION, resizable=True)
# debug: log all events
# self.window.push_handlers(pyglet.window.event.WindowEventLogger())
pyglet.app.run()
def reload(self):
self.load(self.path)
def load(self, path):
print "loading file %s ..."%repr(path)
t1 = time.time()
print
print "Parsing '%s'..."%path
print
self.path = path
parser = GcodeParser()
self.model = parser.parseFile(path)
print
print "Done! %s"%self.model
print
# render the model
print "rendering vertices..."
self.renderVertices()
print "rendering indexed colors..."
self.renderIndexedColors()
print "rendering true colors..."
self.renderColors()
print "generating graphics..."
self.generateGraphics()
print "Done"
t2 = time.time()
print "loaded file in %0.3f ms" % ((t2-t1)*1000.0, )
def renderVertices(self):
t1 = time.time()
self.vertices = []
for layer in self.model.layers:
layer_vertices = []
x = layer.start["X"]
y = layer.start["Y"]
z = layer.start["Z"]
for seg in layer.segments:
layer_vertices.append(x)
layer_vertices.append(y)
layer_vertices.append(z)
x = seg.coords["X"]
y = seg.coords["Y"]
z = seg.coords["Z"]
layer_vertices.append(x)
layer_vertices.append(y)
layer_vertices.append(z)
self.vertices.append(layer_vertices)
t2 = time.time()
print "end renderColors in %0.3f ms" % ((t2-t1)*1000.0, )
def renderIndexedColors(self):
t1 = time.time()
# pre-render segments to colors in the index
styleToColoridx = {
"extrude" : 0,
"fly" : 1,
"retract" : 2,
"restore" : 3
}
# all the styles for all layers
self.vertex_indexed_colors = []
# for all layers
for layer in self.model.layers:
# index for this layer
layer_vertex_indexed_colors = []
for seg in layer.segments:
# get color index for this segment
styleCol = styleToColoridx[seg.style]
# append color twice (once per end)
layer_vertex_indexed_colors.extend((styleCol, styleCol))
# append layer to all layers
self.vertex_indexed_colors.append(layer_vertex_indexed_colors)
t2 = time.time()
print "end renderIndexedColors in %0.3f ms" % ((t2-t1)*1000.0, )
def renderColors(self):
t1 = time.time()
self.vertex_colors = [[],[],[]]
# render color index to real colors
colorMap = [
# 0: old layer
[[255,255,255, 40],[255, 0, 0, 20],[ 0, 255, 0, 32],[ 0, 0, 255, 32]], # extrude, fly, retract, restore
# 1: current layer
[[255,255,255,255],[255, 0, 0,128],[ 0, 255, 0,128],[ 0, 0, 255,128]], # extrude, fly, retract, restore
# 2: limbo layer
[[255,255,255, 10],[255, 0, 0, 10],[ 0, 255, 0, 10],[ 0, 0, 255, 10]] # extrude, fly, retract, restore
]
# for all 3 types
for display_type in xrange(3):
type_color_map = colorMap[display_type]
# for all preindexed layer colors
for indexes in self.vertex_indexed_colors:
# render color indexes to colors
colors = map(lambda e: type_color_map[e], indexes)
# flatten color values
fcolors = []
map(fcolors.extend, colors)
# push colors to vertex list
self.vertex_colors[display_type].append(fcolors)
t2 = time.time()
print "end renderColors in %0.3f ms" % ((t2-t1)*1000.0, )
def generateGraphics(self):
t1 = time.time()
self.graphics_old = []
self.graphics_current = []
self.graphics_limbo = []
for layer_idx in xrange(len(self.vertices)):
nb_layer_vertices = len(self.vertices[layer_idx])/3
vertex_list = pyglet.graphics.vertex_list(nb_layer_vertices,
('v3f/static', self.vertices[layer_idx]),
('c4B/static', self.vertex_colors[0][layer_idx])
)
self.graphics_old.append(vertex_list)
vertex_list = pyglet.graphics.vertex_list(nb_layer_vertices,
('v3f/static', self.vertices[layer_idx]),
('c4B/static', self.vertex_colors[1][layer_idx])
)
self.graphics_current.append(vertex_list)
vertex_list = pyglet.graphics.vertex_list(nb_layer_vertices,
('v3f/static', self.vertices[layer_idx]),
('c4B/static', self.vertex_colors[2][layer_idx])
)
self.graphics_limbo.append(vertex_list)
# print nb_layer_vertices, len(self.vertices[layer_idx]), len(self.colors[0][layer_idx])
t2 = time.time()
print "end generateGraphics in %0.3f ms" % ((t2-t1)*1000.0, )
def rotate_drag_do(self, x, y, dx, dy, buttons, modifiers):
if self.rotateDrag:
# deltas
deltaX = x - self.rotateDragStartX
deltaY = y - self.rotateDragStartY
# rotate!
self.RZ = self.rotateDragStartRZ + deltaX/5.0 # mouse X bound to model Z
self.RX = self.rotateDragStartRX + deltaY/5.0 # mouse Y bound to model X
else:
self.rotateDragStartRX = self.RX
self.rotateDragStartRZ = self.RZ
self.rotateDragStartX = x
self.rotateDragStartY = y
self.rotateDrag = True
def rotate_drag_end(self, x, y, button, modifiers):
self.rotateDrag = False
def layer_next_up(self, delta = 1):
self.layerIdx = max(min(self.layerIdx + delta, self.model.topLayer), 0)
def layer_next_down(self, delta = 1):
self.layerIdx = max(min(self.layerIdx - delta, self.model.topLayer), 0)
def layer_drag_do(self, x, y, dx, dy, buttons, modifiers):
if self.layerDrag:
# sum x & y
delta = x - self.layerDragStartX + y - self.layerDragStartY
# new theoretical layer
self.layerIdx = int(self.layerDragStartLayer + delta/5)
# clamp layer to 0-max
self.layerIdx = max(min(self.layerIdx, self.model.topLayer), 0)
self.window.layerLabel.text = "layer %d"%self.layerIdx
else:
self.layerDragStartLayer = self.layerIdx
self.layerDragStartX = x
self.layerDragStartY = y
self.layerDrag = True
def layer_drag_end(self, x, y, button, modifiers):
self.layerDrag = False
def move_view_x(self, dx):
self.viewX += dx
def move_view_z(self, dz):
self.viewZ += dz
class MyWindow(pyglet.window.Window):
# constructor
def __init__(self, app, **kwargs):
pyglet.window.Window.__init__(self, **kwargs)
self.app = app
self.hud()
# hud info
def hud(self):
# HUD labels
self.blLabels = []
self.brLabels = []
self.tlLabels = []
self.trLabels = []
# help
self.helpText = [
"reload - ctrl + r",
"layer - mouse left + alt/command; scroll + alt",
"zoom - mouse left + ctrl; scroll + ctrl",
"up/down - mouse left + shift; scroll + shift",
"rotate - mouse left"]
for txt in self.helpText:
self.blLabels.append( pyglet.text.Label( txt, font_size=10, font_name="courier" ) )
# statistics
## model stats
self.statsLabel = pyglet.text.Label( "", font_size=12, anchor_y='top' )
filename = os.path.basename(self.app.path)
self.statsLabel.text = "%s: %d segments, %d layers."%(filename, len(self.app.model.segments), len(self.app.model.layers))
## fps counter
self.fpsLabel = pyglet.text.Label( "",
font_size=12,
anchor_y='top')
self.tlLabels.append(self.statsLabel)
self.tlLabels.append(self.fpsLabel)
# status
## current Layer
self.layerLabel = pyglet.text.Label( "layer %d"%self.app.layerIdx,
font_size=12,
anchor_x='right', anchor_y='top')
self.trLabels.append(self.layerLabel)
# layout the labels in the window's corners
self.placeLabels(self.width, self.height)
# events
def on_resize(self, width, height):
glViewport(0, 0, width, height)
self.placeLabels(width, height)
#self.render(width, height)
return pyglet.event.EVENT_HANDLED
def on_mouse_press(self, x, y, button, modifiers):
#print "on_mouse_press(x=%d, y=%d, button=%s, modifiers=%s)"%(x, y, button, modifiers)
# ASkr: removed both of these to have a "mouse click" for other stuff.
#if button & mouse.LEFT:
# self.app.rotate_drag_start(x, y, button, modifiers)
#if button & mouse.RIGHT:
# self.app.layer_drag_start(x, y, button, modifiers)
pass
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
# ASkr: Suppress unwanted modifiers. Aka.: NUM_LOCK might ruin everything :)
modifiers &= ( key.MOD_SHIFT | key.MOD_CTRL | key.MOD_ALT | key.MOD_COMMAND )
# --- NO MODIFIERS
if modifiers == 0:
# left drag rotates view
if buttons & mouse.LEFT:
self.app.rotate_drag_do(x, y, dx, dy, buttons, modifiers)
# right drag moves view up/down
if buttons & mouse.RIGHT:
self.app.move_view_z( -dy / 10.0 )
self.app.move_view_x( -dx / 10.0 )
# --- SHIFT
elif modifiers == key.MOD_SHIFT:
# left drag + SHIFT moves view up/down
if buttons & mouse.LEFT:
self.app.move_view_z( -dy / 10.0 )
# --- CTRL
elif modifiers == key.MOD_CTRL:
# left drag + CTRL zoom
if buttons & mouse.LEFT:
z = 1.1 if dy>0 else 1/1.1
self.app.zoom = max(1.0, self.app.zoom * z)
# --- ALT/COMMAND
# Little annoying under Linux because Alt+drag moves windows (o_O),
# but the mouse's scroll wheel function coveres that...
elif modifiers == key.MOD_ALT or modifiers == key.MOD_COMMAND:
# left drag + ALT selects layer
if buttons & mouse.LEFT:
if dy > 0:
self.app.layer_next_up()
else:
self.app.layer_next_down()
def on_mouse_release(self, x, y, button, modifiers):
#print "on_mouse_release(x=%d, y=%d, button=%s, modifiers=%s)"%(x, y, button, modifiers)
if button & mouse.LEFT:
if self.app.rotateDrag == True:
self.app.rotate_drag_end(x, y, button, modifiers)
if button & mouse.RIGHT:
self.app.layer_drag_end(x, y, button, modifiers)
def on_key_press(self, symbol, modifiers):
if symbol == key.LSHIFT or symbol == key.RSHIFT:
print("SHIFT")
self.app.pressedShift = True;
if symbol == key.LCTRL or symbol == key.RCTRL:
print("CTRL")
self.app.pressedCtrl = True;
if symbol == key.LALT or symbol == key.RALT or symbol == key.LCOMMAND or symbol == key.RCOMMAND:
print("ALT")
self.app.pressedAlt = True;
def on_key_release(self, symbol, modifiers):
print "pressed key: %s, mod: %s"%(symbol, modifiers)
#print "pressed key: %s, mod: %s"%(pyglet.window.key.R, pyglet.window.key.MOD_CTRL)
if symbol == key.LSHIFT or symbol == key.RSHIFT:
# This, of course, won't work if the windows loses the focus...
self.app.pressedShift = False;
if symbol == key.LCTRL or symbol == key.RCTRL:
# same focus problem like above
self.app.pressedCtrl = False;
if symbol == key.LALT or symbol == key.RALT or symbol == key.LCOMMAND or symbol == key.RCOMMAND:
# same here
self.app.pressedAlt = False;
if symbol==key.R and modifiers & key.MOD_CTRL:
self.app.reload()
def placeLabels(self, width, height):
x = 5
y = 5
for label in self.blLabels:
label.x = x
label.y = y
y += 20
x = width - 5
y = 5
for label in self.brLabels:
label.x = x
label.y = y
y += 20
x = 5
y = height - 5
for label in self.tlLabels:
label.x = x
label.y = y
y -= 20
x = width - 5
y = height - 5
for label in self.trLabels:
label.x = x
label.y = y
y -= 20
def on_mouse_scroll(self, x, y, dx, dy):
delta = dx + dy
# scroll with SHIFT only: translate height
if self.app.pressedShift and not self.app.pressedCtrl and not self.app.pressedAlt:
if delta > 0:
self.app.move_view_z( 2.0 )
elif delta < 0:
self.app.move_view_z( -2.0 )
# scroll with ALT/COMMAND only: select next layer (slow)
elif not self.app.pressedShift and not self.app.pressedCtrl and self.app.pressedAlt:
if delta > 0:
self.app.layer_next_up()
elif delta < 0:
self.app.layer_next_down()
# scroll with ALT/COMMAND and SHIFT: select next layer (fast)
elif self.app.pressedShift and not self.app.pressedCtrl and self.app.pressedAlt:
if delta > 0:
self.app.layer_next_up( 10 )
elif delta < 0:
self.app.layer_next_down( 10 )
# scroll: zoom in/out
else:
z = 1.2 if delta>0 else 1/1.2
self.app.zoom = max(1.0, self.app.zoom * z)
def on_draw(self):
#print "draw"
# Clear buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
# setup projection
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(65, self.width / float(self.height), 0.1, 1000)
# setup camera
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
gluLookAt( 0, 1.5, 2, 0 ,0 ,0 , 0 ,1 ,0 )
# fit & user zoom model
max_width = max(
self.app.model.bbox.dx(),
self.app.model.bbox.dy(),
self.app.model.bbox.dz()
)
scale = self.app.zoom / max_width
glScaled(scale, scale, scale)
# adjust camera height
# ASkr TESTING
gluLookAt( 0, self.app.viewZ, 2, 0 ,self.app.viewZ ,0 , 0 ,1 , 0 )
# gluLookAt( self.app.viewX, self.app.viewZ, 2,
# self.app.viewX, self.app.viewZ, 0,
# 0 ,1 , 0 )
# enable alpha blending
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
# rotate axes to match reprap style
glRotated(-90, 1,0,0)
# ASkr TODO: also "reverse translate" to viewport x/y offset (#1)
glTranslated( 0, 0, self.app.viewZ )
# user rotate model
glRotated(-self.app.RX, 1,0,0)
glRotated(self.app.RZ, 0,0,1)
# modify the height
# ASkr TODO: and translate x/y back (#2)
glTranslated( 0, 0, -self.app.viewZ )
glTranslated(-self.app.model.bbox.cx(), -self.app.model.bbox.cy(), -self.app.model.bbox.cz())
# draw axes
glBegin(GL_LINES)
glColor3f(1,0,0)
glVertex3f(0,0,0); glVertex3f(1,0,0); glVertex3f(1,0,0); glVertex3f(1,0.1,0)
glVertex3f(1,0,0); glVertex3f(self.app.model.bbox.xmax,0,0)
glColor3f(0,1,0)
glVertex3f(0,0,0); glVertex3f(0,1,0); glVertex3f(0,1,0); glVertex3f(0,1,0.1)
glVertex3f(0,1,0); glVertex3f(0,self.app.model.bbox.ymax,0)
glColor3f(0,0,1)
glVertex3f(0,0,0); glVertex3f(0,0,1); glVertex3f(0,0,1); glVertex3f(0.1,0,1)
glVertex3f(0,0,1); glVertex3f(0,0,self.app.model.bbox.zmax)
glEnd()
glLineWidth(1)
# Draw the model layers
# lower layers
for graphic in self.app.graphics_old[0:self.app.layerIdx]:
graphic.draw(GL_LINES)
glLineWidth(2)
# highlighted layer
graphic = self.app.graphics_current[self.app.layerIdx]
graphic.draw(GL_LINES)
glLineWidth(1)
# limbo layers
for graphic in self.app.graphics_limbo[self.app.layerIdx+1:]:
graphic.draw(GL_LINES)
# disable depth for HUD
glDisable(GL_DEPTH_TEST)
glDepthMask(0)
#Set your camera up for 2d, draw 2d scene
glMatrixMode(GL_PROJECTION)
glLoadIdentity();
glOrtho(0, self.width, 0, self.height, -1, 1)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
self.fpsLabel.text = "%d fps"%int(round(pyglet.clock.get_fps()))
for label in self.blLabels:
label.draw()
for label in self.brLabels:
label.draw()
for label in self.tlLabels:
label.draw()
for label in self.trLabels:
label.draw()
# reenable depth for next model display
glEnable(GL_DEPTH_TEST)
glDepthMask(1)
App().main()