-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
glcanon.py
2025 lines (1775 loc) · 76 KB
/
glcanon.py
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
from rs274 import Translated, ArcsToSegmentsMixin
from OpenGL.GL import *
from OpenGL.GLU import *
import math
import hershey
import linuxcnc
import array
import gcode
import os
import re
from functools import reduce
def minmax(*args):
return min(*args), max(*args)
allhomedicon = array.array('B',
[0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x08, 0x20,
0x08, 0x20,
0x08, 0x20,
0x08, 0x20,
0x08, 0x20,
0x0f, 0xe0,
0x08, 0x20,
0x08, 0x20,
0x08, 0x20,
0x08, 0x20,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00])
somelimiticon = array.array('B',
[0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x0f, 0xc0,
0x08, 0x00,
0x08, 0x00,
0x08, 0x00,
0x08, 0x00,
0x08, 0x00,
0x08, 0x00,
0x08, 0x00,
0x08, 0x00,
0x08, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00])
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])
# Axis Views
X = 0
Y = 1
Z = 2
A = 3
B = 4
C = 5
U = 6
V = 7
W = 8
R = 9
# View ports coordinates
VX = 0
VY = 1
VZ = 2
VP = 3
class GLCanon(Translated, ArcsToSegmentsMixin):
lineno = -1
def __init__(self, colors, geometry, is_foam=0, foam_w=1.5, foam_z=0.0):
# traverse list of tuples - [(line number, (start position), (end position), (tlo x, tlo y, tlo z))]
self.traverse = []
# feed list of tuples - [(line number, (start position), (end position), feedrate, (tlo x, tlo y, tlo z))]
self.feed = []
# arcfeed list of tuples - [(line number, (start position), (end position), feedrate, (tlo x, tlo y, tlo z))]
self.arcfeed = []
# dwell list - [line number, color, pos x, pos y, pos z, plane]
self.dwells = []
self.tool_list = []
# preview list - combines the unrotated points of the lists: self.traverse, self.feed, self.arcfeed
self.preview_zero_rxy = []
self.choice = None
self.feedrate = 1
self.lo = (0,) * 9
self.first_move = True
self.geometry = geometry
# min and max extents - the largest bounding box around the currently displayed preview
# bounding box is parallel to the machine axes
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]
# min and max extents at zero rotation - the largest bounding box around the preview
# after unrotating it by the amount of current g5x offset XY rotation
# bounding box is parallel to the machine axes. If the box is rotated by the g5x offset XY rotation amount
# it can be used to give a more accurate visual of where the cut will occur
self.min_extents_zero_rxy = [9e99,9e99,9e99]
self.max_extents_zero_rxy = [-9e99,-9e99,-9e99]
self.min_extents_notool_zero_rxy = [9e99,9e99,9e99]
self.max_extents_notool_zero_rxy = [-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 = foam_z
self.foam_w = foam_w
self.notify = 0
self.notify_message = ""
self.highlight_line = None
def comment(self, arg):
if arg.startswith("AXIS,") or arg.startswith("PREVIEW,"):
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):
# in the event of a "blank" gcode file (M2 only for example) this sets each of the extents to [0,0,0]
# to prevent passing the very large [9e99,9e99,9e99] values and populating the gcode properties with
# unusably large values. Some screens use the extents information to set the view distance so 0 values are preferred.
if not self.arcfeed and not self.feed and not self.traverse:
self.min_extents = \
self.max_extents = \
self.min_extents_notool = \
self.max_extents_notool = \
self.min_extents_zero_rxy = \
self.max_extents_zero_rxy = \
self.min_extents_notool_zero_rxy = \
self.max_extents_notool_zero_rxy = [0,0,0]
return
self.min_extents, self.max_extents, self.min_extents_notool, self.max_extents_notool = gcode.calc_extents(self.arcfeed, self.feed, self.traverse)
self.unrotate_preview()
self.min_extents_zero_rxy, self.max_extents_zero_rxy, self.min_extents_notool_zero_rxy, self.max_extents_notool_zero_rxy = gcode.calc_extents(self.preview_zero_rxy)
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
# unrotates the current preview points defined by self.feed, self.arcfeed, self.traverse
# by the current rotation_xy amount and populates self.preview_zero_rxy. Because this is
# only used to calculate the extents and not to draw to the screen, this can all be contained in the same list.
def unrotate_preview(self):
angle = math.radians(-self.rotation_xy)
cos = math.cos(angle)
sin = math.sin(angle)
g5x_x = self.g5x_offset_x
g5x_y = self.g5x_offset_y
for movelist in self.feed, self.arcfeed:
for linenum, start, end, feed, tooloffset in movelist:
tsx = start[0] - g5x_x
tsy = start[1] - g5x_y
tex = end[0] - g5x_x
tey = end[1] - g5x_y
rsx = (tsx * cos) - (tsy * sin) + g5x_x
rsy = (tsx * sin) + (tsy * cos) + g5x_y
rex = (tex * cos) - (tey * sin) + g5x_x
rey = (tex * sin) + (tey * cos) + g5x_y
self.preview_zero_rxy.append((linenum, (rsx, rsy) + start[2:], (rex, rey) + end[2:], feed, tooloffset))
for linenum, start, end, tooloffset in self.traverse:
tsx = start[0] - g5x_x
tsy = start[1] - g5x_y
tex = end[0] - g5x_x
tey = end[1] - g5x_y
rsx = (tsx * cos) - (tsy * sin) + g5x_x
rsy = (tsx * sin) + (tsy * cos) + g5x_y
rex = (tex * cos) - (tey * sin) + g5x_x
rey = (tex * sin) + (tey * cos) + g5x_y
self.preview_zero_rxy.append((linenum, (rsx, rsy) + start[2:], (rex, rey) + end[2:], tooloffset))
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.ao = 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
try:
self.tool_list.append(arg)
except Exception as e:
print(e)
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
def straight_probe(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
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], int(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], int(self.state.plane/10-17)))
def highlight(self, lineno, geometry):
glLineWidth(3)
glColor3f(*self.colors['selected'])
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], self.colors['selected']) + line[2:]], 2, 0)
coords.append(line[2:5])
glLineWidth(1)
if coords:
x = reduce(lambda _x, _y: _x+_y, [p[0] for p in coords]) / len(coords)
y = reduce(lambda _x, _y: _x+_y, [p[1] for p in coords]) / len(coords)
z = reduce(lambda _x, _y: _x+_y, [p[2] for p in coords]) / len(coords)
else:
x = (self.min_extents[X] + self.max_extents[X])/2
y = (self.min_extents[Y] + self.max_extents[Y])/2
z = (self.min_extents[Z] + self.max_extents[Z])/2
return x, y, z
def color_with_alpha(self, colorname):
glColor4f(*(self.colors[colorname] + (self.colors.get(colorname+'_alpha', 1/3.),)))
def color(self, colorname):
glColor3f(*self.colors[colorname])
def draw(self, for_selection=0, no_traverse=True):
if not no_traverse:
self.colored_lines('traverse', self.traverse, for_selection)
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, int(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),
'limits': (1.0, 0.0, 0.0),
}
def __init__(self, s=None, lp=None, 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
self.no_joint_display = False
self.kinsmodule = "UNKNOWN"
self.trajcoordinates = "unknown"
self.dro_in = "% 9.4f"
self.dro_mm = "% 9.3f"
self.show_overlay = False
self.enable_dro = True
self.cone_basesize = .5
self.show_small_origin = True
self.foam_w_height = 1.5
self.foam_z_height = 0
self.hide_icons = False
try:
system_memory_bytes = os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES')
except Exception as e:
system_memory_bytes = 4
print("Error: Unable to determine system memory, defaulting to 4 GB")
system_memory_gb = system_memory_bytes / (1024 ** 3)
# Set to -1 to disable the file size limit.
# The file size limit is set to 20MB or 1/4 of the system memory, whichever is smaller.
# TODO I don't see any calculation for 1/4 of system_memory_gb ? CMorley 2024
self.max_file_size = min(system_memory_gb, 20) * 1024 * 1024
try:
if os.environ["INI_FILE_NAME"]:
self.inifile = linuxcnc.ini(os.environ["INI_FILE_NAME"])
if self.inifile.find("DISPLAY", "DRO_FORMAT_IN"):
temp = self.inifile.find("DISPLAY", "DRO_FORMAT_IN")
try:
test = temp % 1.234
except:
print("Error: invalid [DISPLAY] DRO_FORMAT_IN in INI file")
else:
self.dro_in = temp
if self.inifile.find("DISPLAY", "DRO_FORMAT_MM"):
temp = self.inifile.find("DISPLAY", "DRO_FORMAT_MM")
try:
test = temp % 1.234
except:
print("Error: invalid [DISPLAY] DRO_FORMAT_MM in INI file")
else:
self.dro_mm = temp
self.dro_in = temp
self.foam_w_height = float(self.inifile.find("[DISPLAY]", "FOAM_W") or 1.5)
self.foam_z_height = float(self.inifile.find("[DISPLAY]", "FOAM_Z") or 0)
size = (self.inifile.find("DISPLAY", "CONE_BASESIZE") or None)
if size is not None:
self.set_cone_basesize(float(size))
# set maximum file size before showing boundary box instead
temp = self.inifile.find("DISPLAY", "GRAPHICAL_MAX_FILE_SIZE")
if not temp is None:
self.max_file_size = int(temp) * 1024 * 1024
except:
# Probably started in an editor so no INI
pass
def set_cone_basesize(self, size):
if size > 2 or size < .025:
size = 0.5
print("Invalid Cone Base size resetting to 0.5")
self.cone_basesize = size
self._redraw()
def init_glcanondraw(self,trajcoordinates="XYZABCUVW",kinsmodule="trivkins",msg=""):
self.trajcoordinates = trajcoordinates.upper().replace(" ","")
self.kinsmodule = kinsmodule
self.no_joint_display = self.stat.kinematics_type == linuxcnc.KINEMATICS_IDENTITY
if (msg != ""):
print("init_glcanondraw %s coords=%s kinsmodule=%s no_joint_display=%d"%(
msg,self.trajcoordinates,self.kinsmodule,self.no_joint_display))
g = self.get_geometry().upper()
linuxcnc.gui_respect_offsets(self.trajcoordinates,int('!' in g))
geometry_chars = "XYZABCUVW-!;"
dupchars = []; badchars = []
for ch in g:
if g.count(ch) >1: dupchars.append(ch)
if not ch in geometry_chars: badchars.append(ch)
if dupchars:
print("Warning: duplicate chars %s in geometry: %s"%(dupchars,g))
if badchars:
print("Warning: unknown chars %s in geometry: %s"%(badchars,g))
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
self.canon.foam_z = self.foam_z_height
self.canon.foam_w = self.foam_w_height
@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_view, y_view):
if self.canon is None: return
pmatrix = glGetDoublev(GL_PROJECTION_MATRIX)
glMatrixMode(GL_PROJECTION)
glPushMatrix()
glLoadIdentity()
vport = glGetIntegerv(GL_VIEWPORT)
gluPickMatrix(x_view, vport[3]-y_view, 5, 5, vport)
glMultMatrixd(pmatrix)
glMatrixMode(GL_MODELVIEW)
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 = glRenderMode(GL_RENDER)
except:
buffer = []
if buffer:
min_depth, max_depth, names = (buffer[0].near, buffer[0].far, buffer[0].names)
for point in buffer:
if min_depth < point.near:
min_depth, max_depth, names = (point.near, point.far, point.names)
self.set_highlight_line(names[0])
else:
self.set_highlight_line(None)
glMatrixMode(GL_PROJECTION)
glPopMatrix()
glMatrixMode(GL_MODELVIEW)
def dlist(self, listname, n=1, gen=lambda n: None):
if listname not in self._dlists:
base = glGenLists(n)
self._dlists[listname] = base, n
gen(base)
return self._dlists[listname][0]
def stale_dlist(self, listname):
if listname not in self._dlists: return
base, count = self._dlists.pop(listname)
glDeleteLists(base, count)
def __del__(self):
for base, count in list(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[X] + self.canon.max_extents[X])/2
y = (self.canon.min_extents[Y] + self.canon.max_extents[Y])/2
z = (self.canon.min_extents[Z] + self.canon.max_extents[Z])/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
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 == VZ or view == VP:
z_pos = g.min_extents[VZ]
zdashwidth = 0
else:
z_pos = g.min_extents[VZ] - pullback
zdashwidth = dashwidth
#draw dimension lines
self.color_limit(0)
glBegin(GL_LINES)
# x dimension
if view != VX and g.max_extents[X] > g.min_extents[X]:
y_pos = g.min_extents[Y] - pullback
#dimension line
glVertex3f(g.min_extents[X], y_pos, z_pos)
glVertex3f(g.max_extents[X], y_pos, z_pos)
#line perpendicular to dimension line at min extent
glVertex3f(g.min_extents[X], y_pos - dashwidth, z_pos - zdashwidth)
glVertex3f(g.min_extents[X], y_pos + dashwidth, z_pos + zdashwidth)
#line perpendicular to dimension line at max extent
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 != VY and g.max_extents[Y] > g.min_extents[Y]:
x_pos = g.min_extents[X] - pullback
#dimension line
glVertex3f(x_pos, g.min_extents[Y], z_pos)
glVertex3f(x_pos, g.max_extents[Y], z_pos)
#line perpendicular to dimension line at min extent
glVertex3f(x_pos - dashwidth, g.min_extents[Y], z_pos - zdashwidth)
glVertex3f(x_pos + dashwidth, g.min_extents[Y], z_pos + zdashwidth)
#line perpendicular to dimension line at max extent
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 != VZ and g.max_extents[Z] > g.min_extents[Z]:
x_pos = g.min_extents[X] - pullback
y_pos = g.min_extents[Y] - pullback
#dimension line
glVertex3f(x_pos, y_pos, g.min_extents[Z])
glVertex3f(x_pos, y_pos, g.max_extents[Z])
#line perpendicular to dimension line at min extent
glVertex3f(x_pos - dashwidth, y_pos - zdashwidth, g.min_extents[Z])
glVertex3f(x_pos + dashwidth, y_pos + zdashwidth, g.min_extents[Z])
#line perpendicular to dimension line at max extent
glVertex3f(x_pos - dashwidth, y_pos - zdashwidth, g.max_extents[Z])
glVertex3f(x_pos + dashwidth, y_pos + zdashwidth, g.max_extents[Z])
glEnd()
# Labels
# get_show_relative == True calculates extents from the local origin
# get_show_relative == False calculates extents from the machine origin
if self.get_show_relative():
offset = self.to_internal_units(s.g5x_offset + s.g92_offset)
else:
offset = 0, 0, 0
#Z extent labels
if view != VZ and g.max_extents[Z] > g.min_extents[Z]:
if view == VX:
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
#Z MIN extent
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 != VX:
glRotatef(-90, 0, 1, 0)
self.hershey.plot_string(f, 0, bbox)
glPopMatrix()
#Z MAX extent
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 != VX:
glRotatef(-90, 0, 1, 0)
self.hershey.plot_string(f, 0, bbox)
glPopMatrix()
self.color_limit(0)
glPushMatrix()
#Z Midpoint
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 != VX:
glRotatef(-90, 0, 0, 1)
glRotatef(-90, 0, 1, 0)
self.hershey.plot_string(f, .5, bbox)
glPopMatrix()
#Y extent labels
if view != VY and g.max_extents[Y] > g.min_extents[Y]:
x_pos = g.min_extents[X] - 6.0*dashwidth
#Y MIN extent
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 == VX:
glRotatef(90, 0, 1, 0)
glTranslatef(dashwidth*1.5, 0, 0)
glScalef(charsize, charsize, charsize)
self.hershey.plot_string(f, 0, bbox)
glPopMatrix()
#Y MAX extent
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 == VX:
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()
#Y midpoint
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 == VX:
glRotatef(-90, 1, 0, 0)
glTranslatef(0, halfchar, 0)
glScalef(charsize, charsize, charsize)
self.hershey.plot_string(f, .5)
glPopMatrix()
#X extent labels
if view != VX and g.max_extents[X] > g.min_extents[X]:
y_pos = g.min_extents[Y] - 6.0*dashwidth
#X MIN extent
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 == VY:
glRotatef(90, 0, 1, 0)
glTranslatef(dashwidth*1.5, 0, 0)
glScalef(charsize, charsize, charsize)
self.hershey.plot_string(f, 0, bbox)
glPopMatrix()
#X MAX extent
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 == VY:
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()
#X midpoint
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 == VY:
glRotatef(-90, 1, 0, 0)
glTranslatef(0, halfchar, 0)
glScalef(charsize, charsize, charsize)
self.hershey.plot_string(f, .5)
glPopMatrix()
def draw_cube(self, min_extents, max_extents, color=(1, 1, 1)):
"""
Draw a cube
:param min_extents: Tuple of X,Y,Z Minimum Limits
:param max_extents: Tuple of X,Y,Z Maximum Limits
:param color: Tuple of RGB color values
"""
glColor3f(color[0], color[1], color[2])
glBegin(GL_LINES)
# Bottom of part bounding box
glVertex3f(min_extents[X], min_extents[Y], min_extents[Z])
glVertex3f(max_extents[X], min_extents[Y], min_extents[Z])
glVertex3f(max_extents[X], min_extents[Y], min_extents[Z])
glVertex3f(max_extents[X], max_extents[Y], min_extents[Z])
glVertex3f(max_extents[X], max_extents[Y], min_extents[Z])
glVertex3f(min_extents[X], max_extents[Y], min_extents[Z])