-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathglcanon.py
More file actions
1669 lines (1450 loc) · 62 KB
/
glcanon.py
File metadata and controls
1669 lines (1450 loc) · 62 KB
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This is a component of AXIS, a front-end for emc
# Copyright 2004, 2005, 2006 Jeff Epler <jepler@unpythonic.net>
#
# This program 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 2 of the License, or
# (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
from rs274 import Translated, ArcsToSegmentsMixin, OpenGLTk
from minigl import *
import math
import glnav
import hershey
import linuxcnc
import array
import gcode
def minmax(*args):
return min(*args), max(*args)
homeicon = array.array('B',
[0x2, 0x00, 0x02, 0x00, 0x02, 0x00, 0x0f, 0x80,
0x1e, 0x40, 0x3e, 0x20, 0x3e, 0x20, 0x3e, 0x20,
0xff, 0xf8, 0x23, 0xe0, 0x23, 0xe0, 0x23, 0xe0,
0x13, 0xc0, 0x0f, 0x80, 0x02, 0x00, 0x02, 0x00])
limiticon = array.array('B',
[ 0, 0, 128, 0, 134, 0, 140, 0, 152, 0, 176, 0, 255, 255,
255, 255, 176, 0, 152, 0, 140, 0, 134, 0, 128, 0, 0, 0,
0, 0, 0, 0])
class GLCanon(Translated, ArcsToSegmentsMixin):
lineno = -1
def __init__(self, colors, geometry, is_foam=0):
# traverse list - [line number, [start position], [end position], [tlo x, tlo y, tlo z]]
self.traverse = []; self.traverse_append = self.traverse.append
# feed list - [line number, [start position], [end position], feedrate, [tlo x, tlo y, tlo z]]
self.feed = []; self.feed_append = self.feed.append
# arcfeed list - [line number, [start position], [end position], feedrate, [tlo x, tlo y, tlo z]]
self.arcfeed = []; self.arcfeed_append = self.arcfeed.append
# dwell list - [line number, color, pos x, pos y, pos z, plane]
self.dwells = []; self.dwells_append = self.dwells.append
# spindle-synched feed list - (line number, (position deltas), S, fpr)
self.feed_synched = []
self.choice = None
self.feedrate = 1
self.lo = (0,) * 9
self.first_move = True
self.geometry = geometry
self.min_extents = [9e99,9e99,9e99]
self.max_extents = [-9e99,-9e99,-9e99]
self.min_extents_notool = [9e99,9e99,9e99]
self.max_extents_notool = [-9e99,-9e99,-9e99]
self.colors = colors
self.in_arc = 0
self.xo = self.yo = self.zo = self.ao = self.bo = self.co = self.uo = self.vo = self.wo = 0
self.dwell_time = 0
self.suppress = 0
self.g92_offset_x = 0.0
self.g92_offset_y = 0.0
self.g92_offset_z = 0.0
self.g92_offset_a = 0.0
self.g92_offset_b = 0.0
self.g92_offset_c = 0.0
self.g92_offset_u = 0.0
self.g92_offset_v = 0.0
self.g92_offset_w = 0.0
self.g5x_index = 1
self.g5x_offset_x = 0.0
self.g5x_offset_y = 0.0
self.g5x_offset_z = 0.0
self.g5x_offset_a = 0.0
self.g5x_offset_b = 0.0
self.g5x_offset_c = 0.0
self.g5x_offset_u = 0.0
self.g5x_offset_v = 0.0
self.g5x_offset_w = 0.0
self.is_foam = is_foam
self.foam_z = 0
self.foam_w = 1.5
self.notify = 0
self.notify_message = ""
self.highlight_line = None
def comment(self, arg):
if arg.startswith("AXIS,"):
parts = arg.split(",")
command = parts[1]
if command == "stop": raise KeyboardInterrupt
if command == "hide": self.suppress += 1
if command == "show": self.suppress -= 1
if command == "XY_Z_POS":
if len(parts) > 2 :
try:
self.foam_z = float(parts[2])
if 210 in self.state.gcodes:
self.foam_z = self.foam_z / 25.4
except:
self.foam_z = 5.0/25.4
if command == "UV_Z_POS":
if len(parts) > 2 :
try:
self.foam_w = float(parts[2])
if 210 in self.state.gcodes:
self.foam_w = self.foam_w / 25.4
except:
self.foam_w = 30.0
if command == "notify":
self.notify = self.notify + 1
self.notify_message = "(AXIS,notify):" + str(self.notify)
if len(parts) > 2:
if len(parts[2]): self.notify_message = parts[2]
def message(self, message): pass
def check_abort(self): pass
def next_line(self, st):
self.state = st
self.lineno = self.state.sequence_number
def draw_lines(self, lines, for_selection, j=0, geometry=None):
return linuxcnc.draw_lines(geometry or self.geometry, lines, for_selection)
def colored_lines(self, color, lines, for_selection, j=0):
if self.is_foam:
if not for_selection:
self.color_with_alpha(color + "_xy")
glPushMatrix()
glTranslatef(0, 0, self.foam_z)
self.draw_lines(lines, for_selection, 2*j, 'XY')
glPopMatrix()
if not for_selection:
self.color_with_alpha(color + "_uv")
glPushMatrix()
glTranslatef(0, 0, self.foam_w)
self.draw_lines(lines, for_selection, 2*j+len(lines), 'UV')
glPopMatrix()
else:
if not for_selection:
self.color_with_alpha(color)
self.draw_lines(lines, for_selection, j)
def draw_dwells(self, dwells, alpha, for_selection, j0=0):
return linuxcnc.draw_dwells(self.geometry, dwells, alpha, for_selection, self.is_lathe())
def calc_extents(self):
self.min_extents, self.max_extents, self.min_extents_notool, self.max_extents_notool = gcode.calc_extents(self.arcfeed, self.feed, self.traverse)
if self.is_foam:
min_z = min(self.foam_z, self.foam_w)
max_z = max(self.foam_z, self.foam_w)
self.min_extents = self.min_extents[0], self.min_extents[1], min_z
self.max_extents = self.max_extents[0], self.max_extents[1], max_z
self.min_extents_notool = \
self.min_extents_notool[0], self.min_extents_notool[1], min_z
self.max_extents_notool = \
self.max_extents_notool[0], self.max_extents_notool[1], max_z
def calc_velocity(self, delta, axis_max_vel):
"""Using techniques from getStraightVelocity() in emccanon.cc, given 9
axis deltas and velocity limits, calculate max velocity of a
straight move; deltas should be absolute; invalid axes should be 0
"""
# Clean up tiny values
delta = tuple([(0.0 if i<1e-7 else i) for i in delta])
# Fastest time of coordinated move is the maximum time of any
# one axis to perform move at axis max velocity
tmax = max([(i[0]/i[1] if i[1] else 0.0)
for i in zip(delta, axis_max_vel)])
# Total distance is the hypotenuse of a set of three axes;
# which set depends on the type of move
if sum(delta[0:3]) > 0:
# Linear XYZ with or without ABC or UVW
dtot = math.sqrt(sum(i*i for i in delta[0:3]))
elif sum(delta[6:9]) > 0:
# Linear UVW without XYZ and with or without ABC
dtot = math.sqrt(sum(i*i for i in delta[6:9]))
else:
# Angular-only
dtot = math.sqrt(sum(i*i for i in delta[3:6]))
# Max velocity = total distance / fastest time
max_vel = dtot/tmax
return max_vel
def tool_offset(self, xo, yo, zo, ao, bo, co, uo, vo, wo):
self.first_move = True
x, y, z, a, b, c, u, v, w = self.lo
self.lo = (x - xo + self.xo, y - yo + self.yo, z - zo + self.zo, a - ao + self.ao, b - bo + self.bo, c - bo + self.bo,
u - uo + self.uo, v - vo + self.vo, w - wo + self.wo)
self.xo = xo
self.yo = yo
self.zo = zo
self.so = ao
self.bo = bo
self.co = co
self.uo = uo
self.vo = vo
self.wo = wo
def set_spindle_rate(self, arg): pass
def set_feed_rate(self, arg): self.feedrate = arg / 60.
def select_plane(self, arg): pass
def change_tool(self, arg):
self.first_move = True
def straight_traverse(self, x,y,z, a,b,c, u, v, w):
if self.suppress > 0: return
l = self.rotate_and_translate(x,y,z,a,b,c,u,v,w)
if not self.first_move:
self.traverse_append((self.lineno, self.lo, l, [self.xo, self.yo, self.zo]))
self.lo = l
def rigid_tap(self, x, y, z):
if self.suppress > 0: return
self.first_move = False
l = self.rotate_and_translate(x,y,z,0,0,0,0,0,0)[:3]
l += [self.lo[3], self.lo[4], self.lo[5],
self.lo[6], self.lo[7], self.lo[8]]
self.feed_append((self.lineno, self.lo, l, self.feedrate, [self.xo, self.yo, self.zo]))
# self.dwells_append((self.lineno, self.colors['dwell'], x + self.offset_x, y + self.offset_y, z + self.offset_z, 0))
self.feed_append((self.lineno, l, self.lo, self.feedrate, [self.xo, self.yo, self.zo]))
def arc_feed(self, *args):
if self.suppress > 0: return
self.first_move = False
self.in_arc = True
try:
ArcsToSegmentsMixin.arc_feed(self, *args)
finally:
self.in_arc = False
def straight_arcsegments(self, segs):
self.first_move = False
lo = self.lo
lineno = self.lineno
feedrate = self.feedrate
to = [self.xo, self.yo, self.zo]
append = self.arcfeed_append
for l in segs:
append((lineno, lo, l, feedrate, to))
lo = l
self.lo = lo
def straight_feed(self, x,y,z, a,b,c, u, v, w):
if self.suppress > 0: return
self.first_move = False
l = self.rotate_and_translate(x,y,z,a,b,c,u,v,w)
self.feed_append((self.lineno, self.lo, l, self.feedrate, [self.xo, self.yo, self.zo]))
self.lo = l
straight_probe = straight_feed
def straight_feed_synched(self, lineno, x,y,z, a,b,c, u,v,w, s, fpr):
"""For spindle-synched straight feeds, also collect data needed to
check if the commanded spindle rate and feed per revolution
will violate any axis MAX_VELOCITY constraints"""
if self.suppress > 0: return
# save segment start and record straight feed segment
lo = self.lo
self.straight_feed(x,y,z, a,b,c, u, v, w)
# record axis distances, spindle speed and feed per revolution
delta = tuple([abs(i[0]-i[1]) for i in zip(lo, self.lo)])
self.feed_synched.append((lineno, delta, s, fpr))
def user_defined_function(self, i, p, q):
if self.suppress > 0: return
color = self.colors['m1xx']
self.dwells_append((self.lineno, color, self.lo[0], self.lo[1], self.lo[2], self.state.plane/10-17))
def dwell(self, arg):
if self.suppress > 0: return
self.dwell_time += arg
color = self.colors['dwell']
self.dwells_append((self.lineno, color, self.lo[0], self.lo[1], self.lo[2], self.state.plane/10-17))
def highlight(self, lineno, geometry):
glLineWidth(3)
c = self.colors['selected']
glColor3f(*c)
glBegin(GL_LINES)
coords = []
for line in self.traverse:
if line[0] != lineno: continue
linuxcnc.line9(geometry, line[1], line[2])
coords.append(line[1][:3])
coords.append(line[2][:3])
for line in self.arcfeed:
if line[0] != lineno: continue
linuxcnc.line9(geometry, line[1], line[2])
coords.append(line[1][:3])
coords.append(line[2][:3])
for line in self.feed:
if line[0] != lineno: continue
linuxcnc.line9(geometry, line[1], line[2])
coords.append(line[1][:3])
coords.append(line[2][:3])
glEnd()
for line in self.dwells:
if line[0] != lineno: continue
self.draw_dwells([(line[0], c) + line[2:]], 2, 0)
coords.append(line[2:5])
glLineWidth(1)
if coords:
x = reduce(lambda x,y:x+y, [c[0] for c in coords]) / len(coords)
y = reduce(lambda x,y:x+y, [c[1] for c in coords]) / len(coords)
z = reduce(lambda x,y:x+y, [c[2] for c in coords]) / len(coords)
else:
x = (self.min_extents[0] + self.max_extents[0])/2
y = (self.min_extents[1] + self.max_extents[1])/2
z = (self.min_extents[2] + self.max_extents[2])/2
return x, y, z
def color_with_alpha(self, name):
glColor4f(*(self.colors[name] + (self.colors.get(name+'_alpha', 1/3.),)))
def color(self, name):
glColor3f(*self.colors[name])
def draw(self, for_selection=0, no_traverse=True):
if not no_traverse:
glEnable(GL_LINE_STIPPLE)
self.colored_lines('traverse', self.traverse, for_selection)
glDisable(GL_LINE_STIPPLE)
else:
self.colored_lines('straight_feed', self.feed, for_selection, len(self.traverse))
self.colored_lines('arc_feed', self.arcfeed, for_selection, len(self.traverse) + len(self.feed))
glLineWidth(2)
self.draw_dwells(self.dwells, self.colors.get('dwell_alpha', 1/3.), for_selection, len(self.traverse) + len(self.feed) + len(self.arcfeed))
glLineWidth(1)
def with_context(f):
def inner(self, *args, **kw):
self.activate()
try:
return f(self, *args, **kw)
finally:
self.deactivate()
return inner
def with_context_swap(f):
def inner(self, *args, **kw):
self.activate()
try:
return f(self, *args, **kw)
finally:
self.swapbuffers()
self.deactivate()
return inner
class GlCanonDraw:
colors = {
'traverse': (0.30, 0.50, 0.50),
'traverse_alpha': 1/3.,
'traverse_xy': (0.30, 0.50, 0.50),
'traverse_alpha_xy': 1/3.,
'traverse_uv': (0.30, 0.50, 0.50),
'traverse_alpha_uv': 1/3.,
'backplotprobing_alpha': 0.75,
'backplotprobing': (0.63, 0.13, 0.94),
'backplottraverse': (0.30, 0.50, 0.50),
'label_ok': (1.00, 0.51, 0.53),
'backplotjog_alpha': 0.75,
'tool_diffuse': (0.60, 0.60, 0.60),
'backplotfeed': (0.75, 0.25, 0.25),
'back': (0.00, 0.00, 0.00),
'lathetool_alpha': 0.10,
'axis_x': (0.20, 1.00, 0.20),
'cone': (1.00, 1.00, 1.00),
'cone_xy': (0.00, 1.00, 0.00),
'cone_uv': (0.00, 0.00, 1.00),
'axis_z': (0.20, 0.20, 1.00),
'label_limit': (1.00, 0.21, 0.23),
'backplotjog': (1.00, 1.00, 0.00),
'selected': (0.00, 1.00, 1.00),
'lathetool': (0.80, 0.80, 0.80),
'dwell': (1.00, 0.50, 0.50),
'overlay_foreground': (1.00, 1.00, 1.00),
'overlay_background': (0.00, 0.00, 0.00),
'straight_feed': (1.00, 1.00, 1.00),
'straight_feed_alpha': 1/3.,
'straight_feed_xy': (0.20, 1.00, 0.20),
'straight_feed_alpha_xy': 1/3.,
'straight_feed_uv': (0.20, 0.20, 1.00),
'straight_feed_alpha_uv': 1/3.,
'small_origin': (0.00, 1.00, 1.00),
'backplottoolchange_alpha': 0.25,
'backplottraverse_alpha': 0.25,
'overlay_alpha': 0.75,
'tool_ambient': (0.40, 0.40, 0.40),
'tool_alpha': 0.20,
'backplottoolchange': (1.00, 0.65, 0.00),
'backplotarc': (0.75, 0.25, 0.50),
'm1xx': (0.50, 0.50, 1.00),
'backplotfeed_alpha': 0.75,
'backplotarc_alpha': 0.75,
'arc_feed': (1.00, 1.00, 1.00),
'arc_feed_alpha': .5,
'arc_feed_xy': (0.20, 1.00, 0.20),
'arc_feed_alpha_xy': 1/3.,
'arc_feed_uv': (0.20, 0.20, 1.00),
'arc_feed_alpha_uv': 1/3.,
'axis_y': (1.00, 0.20, 0.20),
'grid': (0.15, 0.15, 0.15),
}
def __init__(self, s, lp, g=None):
self.stat = s
self.lp = lp
self.canon = g
self._dlists = {}
self.select_buffer_size = 100
self.cached_tool = -1
self.initialised = 0
def realize(self):
self.hershey = hershey.Hershey()
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
self.basic_lighting()
self.initialised = 1
def set_canon(self, canon):
self.canon = canon
@with_context
def basic_lighting(self):
glLightfv(GL_LIGHT0, GL_POSITION, (1, -1, 1, 0))
glLightfv(GL_LIGHT0, GL_AMBIENT, self.colors['tool_ambient'] + (0,))
glLightfv(GL_LIGHT0, GL_DIFFUSE, self.colors['tool_diffuse'] + (0,))
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, (1,1,1,0))
glEnable(GL_LIGHTING)
glEnable(GL_LIGHT0)
glDepthFunc(GL_LESS)
glEnable(GL_DEPTH_TEST)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
def select(self, x, y):
if self.canon is None: return
pmatrix = glGetDoublev(GL_PROJECTION_MATRIX)
glMatrixMode(GL_PROJECTION)
glPushMatrix()
glLoadIdentity()
vport = glGetIntegerv(GL_VIEWPORT)
gluPickMatrix(x, vport[3]-y, 5, 5, vport)
glMultMatrixd(pmatrix)
glMatrixMode(GL_MODELVIEW)
while 1:
glSelectBuffer(self.select_buffer_size)
glRenderMode(GL_SELECT)
glInitNames()
glPushName(0)
if self.get_show_rapids():
glCallList(self.dlist('select_rapids', gen=self.make_selection_list))
glCallList(self.dlist('select_norapids', gen=self.make_selection_list))
try:
buffer = list(glRenderMode(GL_RENDER))
except OverflowError:
self.select_buffer_size *= 2
continue
break
if buffer:
min_depth, max_depth, names = min(buffer)
self.set_highlight_line(names[0])
else:
self.set_highlight_line(None)
glMatrixMode(GL_PROJECTION)
glPopMatrix()
glMatrixMode(GL_MODELVIEW)
def dlist(self, name, n=1, gen=lambda n: None):
if name not in self._dlists:
base = glGenLists(n)
self._dlists[name] = base, n
gen(base)
return self._dlists[name][0]
def stale_dlist(self, name):
if name not in self._dlists: return
base, count = self._dlists.pop(name)
glDeleteLists(base, count)
def __del__(self):
for base, count in self._dlists.values():
glDeleteLists(base, count)
def update_highlight_variable(self,line):
self.highlight_line = line
def set_current_line(self, line): pass
def set_highlight_line(self, line):
if line == self.get_highlight_line(): return
self.update_highlight_variable(line)
highlight = self.dlist('highlight')
glNewList(highlight, GL_COMPILE)
if line is not None and self.canon is not None:
if self.is_foam():
glPushMatrix()
glTranslatef(0, 0, self.get_foam_z())
x, y, z = self.canon.highlight(line, "XY")
glTranslatef(0, 0, self.get_foam_w()-self.get_foam_z())
u, v, w = self.canon.highlight(line, "UV")
glPopMatrix()
x = (x+u)/2
y = (y+v)/2
z = (self.get_foam_z() + self.get_foam_w())/2
else:
x, y, z = self.canon.highlight(line, self.get_geometry())
elif self.canon is not None:
x = (self.canon.min_extents[0] + self.canon.max_extents[0])/2
y = (self.canon.min_extents[1] + self.canon.max_extents[1])/2
z = (self.canon.min_extents[2] + self.canon.max_extents[2])/2
else:
x, y, z = 0.0, 0.0, 0.0
glEndList()
self.set_centerpoint(x, y, z)
@with_context_swap
def redraw_perspective(self):
w = self.winfo_width()
h = self.winfo_height()
glViewport(0, 0, w, h)
# Clear the background and depth buffer.
glClearColor(*(self.colors['back'] + (0,)))
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(self.fovy, float(w)/float(h), self.near, self.far + self.distance)
gluLookAt(0, 0, self.distance,
0, 0, 0,
0., 1., 0.)
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
try:
self.redraw()
finally:
glFlush() # Tidy up
glPopMatrix() # Restore the matrix
@with_context_swap
def redraw_ortho(self):
if not self.initialised: return
w = self.winfo_width()
h = self.winfo_height()
glViewport(0, 0, w, h)
# Clear the background and depth buffer.
glClearColor(*(self.colors['back'] + (0,)))
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
ztran = self.distance
k = (abs(ztran or 1)) ** .55555
l = k * h / w
glOrtho(-k, k, -l, l, -1000, 1000.)
gluLookAt(0, 0, 1,
0, 0, 0,
0., 1., 0.)
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
try:
self.redraw()
finally:
glFlush() # Tidy up
glPopMatrix() # Restore the matrix
def color_limit(self, cond):
if cond:
glColor3f(*self.colors['label_limit'])
else:
glColor3f(*self.colors['label_ok'])
return cond
def show_extents(self):
s = self.stat
g = self.canon
if g is None: return
# Dimensions
x,y,z,p = 0,1,2,3
view = self.get_view()
is_metric = self.get_show_metric()
dimscale = is_metric and 25.4 or 1.0
fmt = is_metric and "%.1f" or "%.2f"
machine_limit_min, machine_limit_max = self.soft_limits()
pullback = max(g.max_extents[x] - g.min_extents[x],
g.max_extents[y] - g.min_extents[y],
g.max_extents[z] - g.min_extents[z],
2 ) * .1
dashwidth = pullback/4
charsize = dashwidth * 1.5
halfchar = charsize * .5
if view == z or view == p:
z_pos = g.min_extents[z]
zdashwidth = 0
else:
z_pos = g.min_extents[z] - pullback
zdashwidth = dashwidth
# x dimension
self.color_limit(0)
glBegin(GL_LINES)
if view != x and g.max_extents[x] > g.min_extents[x]:
y_pos = g.min_extents[y] - pullback
glVertex3f(g.min_extents[x], y_pos, z_pos)
glVertex3f(g.max_extents[x], y_pos, z_pos)
glVertex3f(g.min_extents[x], y_pos - dashwidth, z_pos - zdashwidth)
glVertex3f(g.min_extents[x], y_pos + dashwidth, z_pos + zdashwidth)
glVertex3f(g.max_extents[x], y_pos - dashwidth, z_pos - zdashwidth)
glVertex3f(g.max_extents[x], y_pos + dashwidth, z_pos + zdashwidth)
# y dimension
if view != y and g.max_extents[y] > g.min_extents[y]:
x_pos = g.min_extents[x] - pullback
glVertex3f(x_pos, g.min_extents[y], z_pos)
glVertex3f(x_pos, g.max_extents[y], z_pos)
glVertex3f(x_pos - dashwidth, g.min_extents[y], z_pos - zdashwidth)
glVertex3f(x_pos + dashwidth, g.min_extents[y], z_pos + zdashwidth)
glVertex3f(x_pos - dashwidth, g.max_extents[y], z_pos - zdashwidth)
glVertex3f(x_pos + dashwidth, g.max_extents[y], z_pos + zdashwidth)
# z dimension
if view != z and g.max_extents[z] > g.min_extents[z]:
x_pos = g.min_extents[x] - pullback
y_pos = g.min_extents[y] - pullback
glVertex3f(x_pos, y_pos, g.min_extents[z])
glVertex3f(x_pos, y_pos, g.max_extents[z])
glVertex3f(x_pos - dashwidth, y_pos - zdashwidth, g.min_extents[z])
glVertex3f(x_pos + dashwidth, y_pos + zdashwidth, g.min_extents[z])
glVertex3f(x_pos - dashwidth, y_pos - zdashwidth, g.max_extents[z])
glVertex3f(x_pos + dashwidth, y_pos + zdashwidth, g.max_extents[z])
glEnd()
# Labels
if self.get_show_relative():
offset = self.to_internal_units(s.g5x_offset + s.g92_offset)
else:
offset = 0, 0, 0
if view != z and g.max_extents[z] > g.min_extents[z]:
if view == x:
x_pos = g.min_extents[x] - pullback
y_pos = g.min_extents[y] - 6.0*dashwidth
else:
x_pos = g.min_extents[x] - 6.0*dashwidth
y_pos = g.min_extents[y] - pullback
bbox = self.color_limit(g.min_extents_notool[z] < machine_limit_min[z])
glPushMatrix()
f = fmt % ((g.min_extents[z]-offset[z]) * dimscale)
glTranslatef(x_pos, y_pos, g.min_extents[z] - halfchar)
glScalef(charsize, charsize, charsize)
glRotatef(-90, 0, 1, 0)
glRotatef(-90, 0, 0, 1)
if view != x:
glRotatef(-90, 0, 1, 0)
self.hershey.plot_string(f, 0, bbox)
glPopMatrix()
bbox = self.color_limit(g.max_extents_notool[z] > machine_limit_max[z])
glPushMatrix()
f = fmt % ((g.max_extents[z]-offset[z]) * dimscale)
glTranslatef(x_pos, y_pos, g.max_extents[z] - halfchar)
glScalef(charsize, charsize, charsize)
glRotatef(-90, 0, 1, 0)
glRotatef(-90, 0, 0, 1)
if view != x:
glRotatef(-90, 0, 1, 0)
self.hershey.plot_string(f, 0, bbox)
glPopMatrix()
self.color_limit(0)
glPushMatrix()
f = fmt % ((g.max_extents[z] - g.min_extents[z]) * dimscale)
glTranslatef(x_pos, y_pos, (g.max_extents[z] + g.min_extents[z])/2)
glScalef(charsize, charsize, charsize)
if view != x:
glRotatef(-90, 0, 0, 1)
glRotatef(-90, 0, 1, 0)
self.hershey.plot_string(f, .5, bbox)
glPopMatrix()
if view != y and g.max_extents[y] > g.min_extents[y]:
x_pos = g.min_extents[x] - 6.0*dashwidth
bbox = self.color_limit(g.min_extents_notool[y] < machine_limit_min[y])
glPushMatrix()
f = fmt % ((g.min_extents[y] - offset[y]) * dimscale)
glTranslatef(x_pos, g.min_extents[y] + halfchar, z_pos)
glRotatef(-90, 0, 0, 1)
glRotatef(-90, 0, 0, 1)
if view == x:
glRotatef(90, 0, 1, 0)
glTranslatef(dashwidth*1.5, 0, 0)
glScalef(charsize, charsize, charsize)
self.hershey.plot_string(f, 0, bbox)
glPopMatrix()
bbox = self.color_limit(g.max_extents_notool[y] > machine_limit_max[y])
glPushMatrix()
f = fmt % ((g.max_extents[y] - offset[y]) * dimscale)
glTranslatef(x_pos, g.max_extents[y] + halfchar, z_pos)
glRotatef(-90, 0, 0, 1)
glRotatef(-90, 0, 0, 1)
if view == x:
glRotatef(90, 0, 1, 0)
glTranslatef(dashwidth*1.5, 0, 0)
glScalef(charsize, charsize, charsize)
self.hershey.plot_string(f, 0, bbox)
glPopMatrix()
self.color_limit(0)
glPushMatrix()
f = fmt % ((g.max_extents[y] - g.min_extents[y]) * dimscale)
glTranslatef(x_pos, (g.max_extents[y] + g.min_extents[y])/2,
z_pos)
glRotatef(-90, 0, 0, 1)
if view == x:
glRotatef(-90, 1, 0, 0)
glTranslatef(0, halfchar, 0)
glScalef(charsize, charsize, charsize)
self.hershey.plot_string(f, .5)
glPopMatrix()
if view != x and g.max_extents[x] > g.min_extents[x]:
y_pos = g.min_extents[y] - 6.0*dashwidth
bbox = self.color_limit(g.min_extents_notool[x] < machine_limit_min[x])
glPushMatrix()
f = fmt % ((g.min_extents[x] - offset[x]) * dimscale)
glTranslatef(g.min_extents[x] - halfchar, y_pos, z_pos)
glRotatef(-90, 0, 0, 1)
if view == y:
glRotatef(90, 0, 1, 0)
glTranslatef(dashwidth*1.5, 0, 0)
glScalef(charsize, charsize, charsize)
self.hershey.plot_string(f, 0, bbox)
glPopMatrix()
bbox = self.color_limit(g.max_extents_notool[x] > machine_limit_max[x])
glPushMatrix()
f = fmt % ((g.max_extents[x] - offset[x]) * dimscale)
glTranslatef(g.max_extents[x] - halfchar, y_pos, z_pos)
glRotatef(-90, 0, 0, 1)
if view == y:
glRotatef(90, 0, 1, 0)
glTranslatef(dashwidth*1.5, 0, 0)
glScalef(charsize, charsize, charsize)
self.hershey.plot_string(f, 0, bbox)
glPopMatrix()
self.color_limit(0)
glPushMatrix()
f = fmt % ((g.max_extents[x] - g.min_extents[x]) * dimscale)
glTranslatef((g.max_extents[x] + g.min_extents[x])/2, y_pos,
z_pos)
if view == y:
glRotatef(-90, 1, 0, 0)
glTranslatef(0, halfchar, 0)
glScalef(charsize, charsize, charsize)
self.hershey.plot_string(f, .5)
glPopMatrix()
def to_internal_linear_unit(self, v, unit=None):
if unit is None:
unit = self.stat.linear_units
lu = (unit or 1) * 25.4
return v/lu
def to_internal_units(self, pos, unit=None):
if unit is None:
unit = self.stat.linear_units
lu = (unit or 1) * 25.4
lus = [lu, lu, lu, 1, 1, 1, lu, lu, lu]
return [a/b for a, b in zip(pos, lus)]
def soft_limits(self):
def fudge(x):
if abs(x) > 1e30: return 0
return x
ax = self.stat.axis
return (
self.to_internal_units([fudge(ax[i]['min_position_limit'])
for i in range(3)]),
self.to_internal_units([fudge(ax[i]['max_position_limit'])
for i in range(3)]))
def get_foam_z(self):
if self.canon: return self.canon.foam_z
return 0
def get_foam_w(self):
if self.canon: return self.canon.foam_w
return 1.5
def get_grid(self):
if self.canon and self.canon.grid: return self.canon.grid
return 5./25.4
def comp(self, (sx, sy), (cx, cy)):
return -(sx*cx + sy*cy) / (sx*sx + sy*sy)
def param(self, (x1, y1), (dx1, dy1), (x3, y3), (dx3, dy3)):
den = (dy3)*(dx1) - (dx3)*(dy1)
if den == 0: return 0
num = (dx3)*(y1-y3) - (dy3)*(x1-x3)
return num * 1. / den
def draw_grid_lines(self, space, (ox, oy), (dx, dy), lim_min, lim_max,
inverse_permutation):
# draw a series of line segments of the form
# dx(x-ox) + dy(y-oy) + k*space = 0
# for integers k that intersect the AABB [lim_min, lim_max]
lim_pts = [
(lim_min[0], lim_min[1]),
(lim_max[0], lim_min[1]),
(lim_min[0], lim_max[1]),
(lim_max[0], lim_max[1])]
od = self.comp((dy, -dx), (ox, oy))
d0, d1 = minmax(*(self.comp((dy, -dx), i)-od for i in lim_pts))
k0 = int(math.ceil(d0/space))
k1 = int(math.floor(d1/space))
delta = (dx, dy)
for k in range(k0, k1+1):
d = k*space
# Now we're drawing the line dx(x-ox) + dx(y-oy) + d = 0
p0 = (ox - dy * d, oy + dx * d)
# which is the same as the line p0 + u * delta
# but we only want the part that's inside the box lim_pts...
if dx and dy:
times = [
self.param(p0, delta, lim_min[:2], (0, 1)),
self.param(p0, delta, lim_min[:2], (1, 0)),
self.param(p0, delta, lim_max[:2], (0, 1)),
self.param(p0, delta, lim_max[:2], (1, 0))]
times.sort()
t0, t1 = times[1], times[2] # Take the middle two times
elif dx:
times = [
self.param(p0, delta, lim_min[:2], (0, 1)),
self.param(p0, delta, lim_max[:2], (0, 1))]
times.sort()
t0, t1 = times[0], times[1] # Take the only two times
else:
times = [
self.param(p0, delta, lim_min[:2], (1, 0)),
self.param(p0, delta, lim_max[:2], (1, 0))]
times.sort()
t0, t1 = times[0], times[1] # Take the only two times
x0, y0 = p0[0] + delta[0]*t0, p0[1] + delta[1]*t0
x1, y1 = p0[0] + delta[0]*t1, p0[1] + delta[1]*t1
xm, ym = (x0+x1)/2, (y0+y1)/2
# The computation of k0 and k1 above should mean that
# the lines are always in the limits, but I observed
# that this wasn't always the case...
#if xm < lim_min[0] or xm > lim_max[0]: continue
#if ym < lim_min[1] or ym > lim_max[1]: continue
glVertex3f(*inverse_permutation((x0, y0, lim_min[2])))
glVertex3f(*inverse_permutation((x1, y1, lim_min[2])))
def draw_grid_permuted(self, rotation, permutation, inverse_permutation):
grid_size=self.get_grid_size()
if not grid_size: return
glLineWidth(1)
glColor3f(*self.colors['grid'])
s = self.stat
tlo_offset = permutation(self.to_internal_units(s.tool_offset)[:3])
g5x_offset = permutation(self.to_internal_units(s.g5x_offset)[:3])[:2]
g92_offset = permutation(self.to_internal_units(s.g92_offset)[:3])[:2]
lim_min, lim_max = self.soft_limits()
lim_min = permutation(lim_min)
lim_max = permutation(lim_max)
lim_min = tuple(a-b for a,b in zip(lim_min, tlo_offset))
lim_max = tuple(a-b for a,b in zip(lim_max, tlo_offset))
lim_pts = (
(lim_min[0], lim_min[1]),
(lim_max[0], lim_min[1]),
(lim_min[0], lim_max[1]),
(lim_max[0], lim_max[1]))
if self.get_show_relative():
cos_rot = math.cos(rotation)
sin_rot = math.sin(rotation)
offset = (
g5x_offset[0] + g92_offset[0] * cos_rot
- g92_offset[1] * sin_rot,
g5x_offset[1] + g92_offset[0] * sin_rot
+ g92_offset[1] * cos_rot)
else:
offset = 0., 0.
cos_rot = 1.
sin_rot = 0.
glDepthMask(False)
glBegin(GL_LINES)
self.draw_grid_lines(grid_size, offset, (cos_rot, sin_rot),
lim_min, lim_max, inverse_permutation)
self.draw_grid_lines(grid_size, offset, (sin_rot, -cos_rot),
lim_min, lim_max, inverse_permutation)
glEnd()
glDepthMask(True)
def draw_grid(self):
x,y,z,p = 0,1,2,3
view = self.get_view()
if view == p: return
rotation = math.radians(self.stat.rotation_xy % 90)
if rotation != 0 and view != z and self.get_show_relative(): return
permutations = [
lambda (x, y, z): (z, y, x), # YZ X
lambda (x, y, z): (z, x, y), # ZX Y
lambda (x, y, z): (x, y, z), # XY Z
]
inverse_permutations = [
lambda (z, y, x): (x, y, z), # YZ X
lambda (z, x, y): (x, y, z), # ZX Y
lambda (x, y, z): (x, y, z), # XY Z
]
self.draw_grid_permuted(rotation, permutations[view],
inverse_permutations[view])
def redraw(self):
s = self.stat
s.poll()
machine_limit_min, machine_limit_max = self.soft_limits()
glDisable(GL_LIGHTING)
glMatrixMode(GL_MODELVIEW)
self.draw_grid()
if self.get_show_program():
if self.get_program_alpha():
glDisable(GL_DEPTH_TEST)
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
if self.get_show_rapids():
glCallList(self.dlist('program_rapids', gen=self.make_main_list))
glCallList(self.dlist('program_norapids', gen=self.make_main_list))
glCallList(self.dlist('highlight'))
if self.get_program_alpha():
glDisable(GL_BLEND)
glEnable(GL_DEPTH_TEST)
if self.get_show_extents():
self.show_extents()
if self.get_show_live_plot() or self.get_show_program():
alist = self.dlist(('axes', self.get_view()), gen=self.draw_axes)
glPushMatrix()
if self.get_show_relative() and (s.g5x_offset[0] or s.g5x_offset[1] or s.g5x_offset[2] or
s.g92_offset[0] or s.g92_offset[1] or s.g92_offset[2] or
s.rotation_xy):
olist = self.dlist('draw_small_origin',
gen=self.draw_small_origin)
glCallList(olist)