-
Notifications
You must be signed in to change notification settings - Fork 4
/
g_code_library.py
2071 lines (1808 loc) · 84.3 KB
/
g_code_library.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
#!/usr/bin/python
"""
g_code_library
Copyright (C) <2017> <Scorch>
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 3 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, see <http://www.gnu.org/licenses/>.
"""
import sys
from math import *
import os
import re
import binascii
import getopt
import webbrowser
#################
### START LIB ###
#################
############################################################################
class G_Code_Rip:
def __init__(self):
self.Zero = 0.0000001
self.g_code_data = []
self.scaled_trans = []
self.right_side = []
self.left_side = []
self.probe_gcode = []
self.probe_coords = []
self.arc_angle = 2
self.accuracy = .001
self.units = "in"
################################################################################
# Function for outputting messages to different locations #
# depending on what options are enabled #
################################################################################
def fmessage(self,text,newline=True):
if newline==True:
try:
sys.stdout.write(text)
sys.stdout.write("\n")
except:
pass
else:
try:
sys.stdout.write(text)
except:
pass
def Read_G_Code(self,filename, XYarc2line = False, arc_angle=2, units="in", Accuracy=""):
self.g_code_data = []
self.scaled_trans = []
self.right_side = []
self.left_side = []
self.probe_gcode = []
self.probe_coords = []
self.arc_angle = arc_angle
self.units = units
if Accuracy == "":
if units == "in":
self.accuracy = .001
else:
self.accuracy = .025
else:
self.accuracy = float(Accuracy)
READ_MSG = []
# Try to open file for reading
try:
fin = open(filename,'r')
except:
READ_MSG.append("Unable to open file: %s" %(filename))
return READ_MSG
scale = 1
variables = []
line_number = 0
xind=0
yind=1
zind=2
mode_arc = "incremental" # "absolute"
mode_pos = "absolute" # "incremental"
mvtype = 1 # G0 (Rapid), G1 (linear), G2 (clockwise arc) or G3 (counterclockwise arc).
plane = "17" # G17 (Z-axis, XY-plane), G18 (Y-axis, XZ-plane), or G19 (X-axis, YZ-plane)
pos =['','','']
pos_last=['','','']
POS =[complex(0,1),complex(0,1),complex(0,1)]
feed = 0
spindle = 0
#########################
for line in fin:
line_number = line_number + 1
#print line_number
line = line.replace("\n","")
line = line.replace("\r","")
code_line=[]
#####################
### FIND COMMENTS ###
#####################
if line.find("(") != -1:
s = line.find("(")
while s != -1:
e = line.find(")")
code_line.append([ ";", line[s:e+1] ])
line = self.rm_text(line,s,e)
s = line.find("(")
if line.find(";") != -1:
s = line.find(";")
e = len(line)
code_line.append([ ";", line[s:e] ])
line = self.rm_text(line,s,e)
# If comment exists write it to output
if code_line!= []:
for comment in code_line:
self.g_code_data.append(comment)
code_line=[]
# Switch remaining non comment data to upper case
# and remove spaces
line = line.upper()
line = line.replace(" ","")
#####################################################
# Find # chars and check for a variable definition #
#####################################################
if line.find("#") != -1:
s = line.rfind("#")
while s != -1:
if line[s+1] == '<':
e = s+2
while line[e] != '>' and e <= len(line):
e = e+1
e = e+1
vname = line[s:e].lower()
else:
vname = re.findall(r'[-+]?\d+',line[s:])[0]
e = s + 1 + len(vname)
vname = line[s:e]
DEFINE = False
if e < len(line):
if line[e]=="=":
DEFINE = True
if DEFINE:
try:
vval = "%.4f" %(float(line[e+1:]))
line = ''
except:
try:
vval = self.EXPRESSION_EVAL(line[e+1:])
line = ''
except:
READ_MSG.append(str(sys.exc_info()[1]))
return READ_MSG
variables.append([vname,vval])
line = self.rm_text(line,s,e-1)
else:
line = self.rm_text(line,s,e-1)
VALUE = ''
for V in variables:
if V[0] == vname:
VALUE = V[1]
line = self.insert_text(line,VALUE,s)
s = line.rfind("#")
#########################
### FIND MATH REGIONS ###
#########################
if line.find("[") != -1 and line.find("[") != 0:
############################
s = line.find("[")
while s != -1:
e = s + 1
val = 1
while val > 0:
if e >= len(line):
MSG = "ERROR: Unable to evaluate expression: G-Code Line %d" %(line_number)
raise ValueError(MSG)
if line[e]=="[":
val = val + 1
elif line[e] == "]":
val = val - 1
e = e + 1
new_val = self.EXPRESSION_EVAL(line[s:e])
line = self.rm_text(line,s,e-1)
line = self.insert_text(line,new_val,s)
s = line.find("[")
#############################
####################################
### FIND FULLY UNSUPPORTED CODES ###
####################################
# D Tool radius compensation number
# E ...
# L ...
# O ... Subroutines
# Q Feed increment in G73, G83 canned cycles
# A A axis of machine
# B B axis of machine
# C C axis of machine
# U U axis of machine
# V V axis of machine
# W W axis of machine
UCODES = ("A","B","C","D","E","L","O","Q","U","V","W")
skip = False
for code in UCODES:
if line.find(code) != -1:
READ_MSG.append("Warning: %s Codes are not supported ( G-Code File Line: %d )" %(code,line_number))
skip = True
if skip:
continue
##############################
### FIND ALL CODES ###
##############################
# F Feed rate
# G General function
# I X offset for arcs and G87 canned cycles
# J Y offset for arcs and G87 canned cycles
# K Z offset for arcs and G87 canned cycles. Spindle-Motion Ratio for G33 synchronized movements.
# M Miscellaneous function (See table Modal Groups)
# P Dwell time in canned cycles and with G4. Key used with G10. Used with G2/G3.
# R Arc radius or canned cycle plane
# S Spindle speed
# T Tool selection
# X X axis of machine
# Y Y axis of machine
# Z Z axis of machine
ALL = ("A","B","C","D","E","F","G","H","I","J",\
"K","L","M","N","O","P","Q","R","S","T",\
"U","V","W","X","Y","Z","#","=")
temp = []
line = line.replace(" ","")
for code in ALL:
index=-1
index = line.find(code,index+1)
while index != -1:
temp.append([code,index])
index = line.find(code,index+1)
temp.sort(key=lambda a:a[1])
code_line=[]
if temp != []:
x = 0
while x <= len(temp)-1:
s = temp[x][1]+1
if x == len(temp)-1:
e = len(line)
else:
e = temp[x+1][1]
CODE = temp[x][0]
VALUE = line[s:e]
code_line.append([ CODE, VALUE ])
x = x + 1
#################################
mv_flag = 0
POS_LAST = POS[:]
#CENTER = ['','','']
CENTER = POS_LAST[:]
passthru = ""
for com in code_line:
if com[0] == "G":
Gnum = "%g" %(float(com[1]))
if Gnum == "0" or Gnum == "1":
mvtype = int(Gnum)
elif Gnum == "2" or Gnum == "3":
mvtype = int(Gnum)
#CENTER = POS_LAST[:]
elif Gnum == "17":
plane = Gnum
elif Gnum == "18":
plane = Gnum
elif Gnum == "19":
plane = Gnum
elif Gnum == "20":
if units == "in":
scale = 1
else:
scale = 25.4
elif Gnum == "21":
if units == "mm":
scale = 1
else:
scale = 1.0/25.4
elif Gnum == "81":
READ_MSG.append("Warning: G%s Codes are not supported ( G-Code File Line: %d )" %(Gnum,line_number))
elif Gnum == "90.1":
mode_arc = "absolute"
elif Gnum == "90":
mode_pos = "absolute"
elif Gnum == "91":
mode_pos = "incremental"
elif Gnum == "91.1":
mode_arc = "incremental"
elif Gnum == "92":
#READ_MSG.append("Aborting G-Code Reading: G%s Codes are not supported" %(Gnum))
READ_MSG.append("Warning: G%s Codes are not supported ( G-Code File Line: %d )" %(Gnum,line_number))
#return READ_MSG
elif Gnum == "38.2":
READ_MSG.append("Warning: G%s Codes are not supported ( G-Code File Line: %d )" %(Gnum,line_number))
#READ_MSG.append("Aborting G-Code Reading: G%s Codes are not supported" %(Gnum))
#return READ_MSG
else:
passthru = passthru + "%s%s " %(com[0],com[1])
elif com[0] == "X":
if mode_pos == "absolute":
POS[xind] = float(com[1])*scale
else:
POS[xind] = float(com[1])*scale + POS_LAST[xind]
mv_flag = 1
elif com[0] == "Y":
if mode_pos == "absolute":
POS[yind] = float(com[1])*scale
else:
POS[yind] = float(com[1])*scale + POS_LAST[yind]
mv_flag = 1
elif com[0] == "Z":
if mode_pos == "absolute":
POS[zind] = float(com[1])*scale
else:
POS[zind] = float(com[1])*scale + POS_LAST[zind]
mv_flag = 1
###################
elif com[0] == "I":
if mode_arc == "absolute":
CENTER[xind] = float(com[1])*scale
else:
CENTER[xind] = float(com[1])*scale + POS_LAST[xind]
if (mvtype==2 or mvtype==3):
mv_flag = 1
elif com[0] == "J":
if mode_arc == "absolute":
CENTER[yind] = float(com[1])*scale
else:
CENTER[yind] = float(com[1])*scale + POS_LAST[yind]
if (mvtype==2 or mvtype==3):
mv_flag = 1
elif com[0] == "K":
if mode_arc == "absolute":
CENTER[zind] = float(com[1])*scale
else:
CENTER[zind] = float(com[1])*scale + POS_LAST[zind]
if (mvtype==2 or mvtype==3):
mv_flag = 1
elif com[0] == "R":
Rin= float(com[1])*scale
CENTER = self.get_center(POS,POS_LAST,Rin,mvtype,plane)
###################
elif com[0] == "F":
feed = float(com[1]) * scale
elif com[0] == "S":
spindle = float(com[1])
elif com[0] == ";":
passthru = passthru + "%s " %(com[1])
elif com[0] == "P" and mv_flag == 1 and mvtype > 1:
READ_MSG.append("Aborting G-Code Reading: P word specifying the number of full or partial turns of arc are not supported")
return READ_MSG
elif com[0] == "M":
Mnum = "%g" %(float(com[1]))
if Mnum == "2":
self.g_code_data.append([ "M2", "(END PROGRAM)" ])
passthru = passthru + "%s%s " %(com[0],com[1])
elif com[0] == "N":
pass
#print "Ignoring Line Number %g" %(float(com[1]))
else:
passthru = passthru + "%s%s " %(com[0],com[1])
pos = POS[:]
pos_last = POS_LAST[:]
center = CENTER[:]
# Most command on a line are executed prior to a move so
# we will write the passthru commands on the line before we found them
# only "M0, M1, M2, M30 and M60" are executed after the move commands
# there is a risk that one of these commands could stop the program before
# the move is completed
if passthru != '':
self.g_code_data.append("%s" %(passthru))
###############################################################################
if mv_flag == 1:
if mvtype == 0:
self.g_code_data.append([mvtype,pos_last[:],pos[:]])
if mvtype == 1:
self.g_code_data.append([mvtype,pos_last[:],pos[:],feed,spindle])
if mvtype == 2 or mvtype == 3:
if plane == "17":
if XYarc2line == False:
self.g_code_data.append([mvtype,pos_last[:],pos[:],center[:],feed,spindle])
else:
data = self.arc2lines(pos_last[:],pos[:],center[:], mvtype, plane)
for line in data:
XY=line
self.g_code_data.append([1,XY[:3],XY[3:],feed,spindle])
elif plane == "18":
data = self.arc2lines(pos_last[:],pos[:],center[:], mvtype, plane)
for line in data:
XY=line
self.g_code_data.append([1,XY[:3],XY[3:],feed,spindle])
elif plane == "19":
data = self.arc2lines(pos_last[:],pos[:],center[:], mvtype, plane)
for line in data:
XY=line
self.g_code_data.append([1,XY[:3],XY[3:],feed,spindle])
###############################################################################
#################################
fin.close()
## Post process the g-code data to remove complex numbers
cnt = 0
firstx = complex(0,1)
firsty = complex(0,1)
firstz = complex(0,1)
first_sum = firstx + firsty + firstz
while ((cnt < len(self.g_code_data)) and (isinstance(first_sum, complex))):
line = self.g_code_data[cnt]
if line[0] == 0 or line[0] == 1 or line[0] == 2 or line[0] == 3:
if (isinstance(firstx, complex)): firstx = line[2][0]
if (isinstance(firsty, complex)): firsty = line[2][1]
if (isinstance(firstz, complex)): firstz = line[2][2]
cnt=cnt+1
first_sum = firstx + firsty + firstz
max_cnt = cnt
cnt = 0
ambiguousX = False
ambiguousY = False
ambiguousZ = False
while (cnt < max_cnt):
line = self.g_code_data[cnt]
if line[0] == 1 or line[0] == 2 or line[0] == 3:
# X Values
if (isinstance(line[1][0], complex)):
line[1][0] = firstx
ambiguousX = True
if (isinstance(line[2][0], complex)):
line[2][0] = firstx
ambiguousX = True
# Y values
if (isinstance(line[1][1], complex)):
line[1][1] = firsty
ambiguousY = True
if (isinstance(line[2][1], complex)):
line[2][1] = firsty
ambiguousY = True
# Z values
if (isinstance(line[1][2], complex)):
line[1][2] = firstz
ambiguousZ = True
if (isinstance(line[2][2], complex)):
line[2][2] = firstz
ambiguousZ = True
cnt=cnt+1
#if (ambiguousX or ambiguousY or ambiguousZ):
# MSG = "Ambiguous G-Code start location:\n"
# if (ambiguousX): MSG = MSG + "X position is not set by a G0(rapid) move prior to a G1,G2 or G3 move.\n"
# if (ambiguousY): MSG = MSG + "Y position is not set by a G0(rapid) move prior to a G1,G2 or G3 move.\n"
# if (ambiguousZ): MSG = MSG + "Z position is not set by a G0(rapid) move prior to a G1,G2 or G3 move.\n"
# MSG = MSG + "!! Review output files carefully !!"
# READ_MSG.append(MSG)
return READ_MSG
def get_center(self,POS,POS_LAST,Rin,mvtype,plane="17"):
if plane == "18":
xind=2
yind=0
zind=1
elif plane == "19":
xind=1
yind=2
zind=0
elif plane == "17":
xind=0
yind=1
zind=2
CENTER=["","",""]
cord = sqrt( (POS[xind]-POS_LAST[xind])**2 + (POS[yind]-POS_LAST[yind])**2 )
v1 = cord/2.0
#print "rin=%f v1=%f (Rin**2 - v1**2)=%f" %(Rin,v1,(Rin**2 - v1**2))
v2_sq = Rin**2 - v1**2
if v2_sq<0.0:
v2_sq = 0.0
v2 = sqrt( v2_sq )
theta = self.Get_Angle2(POS[xind]-POS_LAST[xind],POS[yind]-POS_LAST[yind])
if mvtype == 3:
dxc,dyc = self.Transform(-v2,v1,radians(theta-90))
elif mvtype == 2:
dxc,dyc = self.Transform(v2,v1,radians(theta-90))
else:
return "Center Error"
xcenter = POS_LAST[xind] + dxc
ycenter = POS_LAST[yind] + dyc
CENTER[xind] = xcenter
CENTER[yind] = ycenter
CENTER[zind] = POS_LAST[zind]
return CENTER
#######################################
def split_code(self,code2split,shift=[0,0,0],angle=0.0):
xsplit=0.0
mvtype = -1 # G0 (Rapid), G1 (linear), G2 (clockwise arc) or G3 (counterclockwise arc).
passthru = ""
POS =[0,0,0]
feed = 0
spindle = 0
self.right_side = []
self.left_side = []
L = 0
R = 1
for line in code2split:
if line[0] == 1:
mvtype = line[0]
POS_LAST = line[1][:]
POS = line[2][:]
CENTER = ['','','']
feed = line[3]
spindle = line[4]
elif line[0] == 3 or line[0] == 2:
mvtype = line[0]
POS_LAST = line[1][:]
POS = line[2][:]
CENTER = line[3][:]
feed = line[4]
spindle = line[5]
else:
mvtype = -1
passthru = line
###############################################################################
if mvtype >= 1 and mvtype <= 3:
pos = self.coordop(POS,shift,angle)
pos_last = self.coordop(POS_LAST,shift,angle)
if CENTER[0]!='' and CENTER[1]!='':
center = self.coordop(CENTER,shift,angle)
else:
center = CENTER
this=""
other=""
if pos_last[0] > xsplit+self.Zero:
flag_side = R
elif pos_last[0] < xsplit-self.Zero:
flag_side = L
else:
if mvtype == 1:
if pos[0] >= xsplit:
flag_side = R
else:
flag_side = L
elif mvtype == 2:
if abs(pos_last[1]-center[1]) < self.Zero:
if center[0] > xsplit:
flag_side = R
else:
flag_side = L
else:
if pos_last[1] >= center[1]:
flag_side = R
else:
flag_side = L
else: #(mvtype == 3)
if abs(pos_last[1]-center[1]) < self.Zero:
if center[0] > xsplit:
flag_side = R
else:
flag_side = L
else:
if pos_last[1] >= center[1]:
flag_side = L
else:
flag_side = R
if flag_side == R:
this = 1
other = 0
else:
this = 0
other = 1
app=[self.apright, self.apleft]
#############################
if mvtype == 0:
pass
if mvtype == 1:
A = self.coordunop(pos_last[:],shift,angle)
C = self.coordunop(pos[:] ,shift,angle)
cross = self.get_line_intersect(pos_last, pos, xsplit)
if len(cross) > 0: ### Line crosses boundary ###
B = self.coordunop(cross[0] ,shift,angle)
app[this] ( [mvtype,A,B,feed,spindle] )
app[other]( [mvtype,B,C,feed,spindle] )
else:
app[this] ( [mvtype,A,C,feed,spindle] )
if mvtype == 2 or mvtype == 3:
A = self.coordunop(pos_last[:],shift,angle)
C = self.coordunop(pos[:] ,shift,angle)
D = self.coordunop(center ,shift,angle)
cross = self.get_arc_intersects(pos_last[:], pos[:], xsplit, center[:], "G%d" %(mvtype))
if len(cross) > 0: ### Arc crosses boundary at least once ###
B = self.coordunop(cross[0] ,shift,angle)
#Check length of arc before writing
if sqrt((A[0]-B[0])**2 + (A[1]-B[1])**2) > self.accuracy:
app[this]( [mvtype,A,B,D,feed,spindle])
if len(cross) == 1: ### Arc crosses boundary only once ###
#Check length of arc before writing
if sqrt((B[0]-C[0])**2 + (B[1]-C[1])**2) > self.accuracy:
app[other]([ mvtype,B,C,D, feed,spindle] )
if len(cross) == 2: ### Arc crosses boundary twice ###
E = self.coordunop(cross[1],shift,angle)
#Check length of arc before writing
if sqrt((B[0]-E[0])**2 + (B[1]-E[1])**2) > self.accuracy:
app[other]([ mvtype,B,E,D, feed,spindle] )
#Check length of arc before writing
if sqrt((E[0]-C[0])**2 + (E[1]-C[1])**2) > self.accuracy:
app[this] ([ mvtype,E,C,D, feed,spindle] )
else: ### Arc does not cross boundary ###
app[this]([ mvtype,A,C,D, feed,spindle])
###############################################################################
else:
if passthru != '':
self.apboth(passthru)
#######################################
def probe_code(self,code2probe,nX,nY,probe_istep,minx,miny,xPartitionLength,yPartitionLength): #,Xoffset,Yoffset):
#def probe_code(self,code2probe,nX,nY,probe_istep,minx,miny,xPartitionLength,yPartitionLength,Xoffset,Yoffset,Zoffset):
#print "nX,nY =",nX,nY
probe_coords = []
BPN=500
POINT_LIST = [False for i in range(int((nY)*(nX)))]
if code2probe == []:
return
mvtype = -1 # G0 (Rapid), G1 (linear), G2 (clockwise arc) or G3 (counterclockwise arc).
passthru = ""
POS = [0,0,0]
feed = 0
spindle = 0
out = []
min_length = min(xPartitionLength,yPartitionLength) / probe_istep
if (min_length < self.Zero):
min_length = max(xPartitionLength,yPartitionLength) / probe_istep
if (min_length < self.Zero):
min_length = 1
for line in code2probe:
if line[0] == 0 or line[0] == 1:
mvtype = line[0]
POS_LAST = line[1][:]
POS = line[2][:]
CENTER = ['','','']
if line[0] == 1:
feed = line[3]
spindle = line[4]
elif line[0] == 3 or line[0] == 2:
mvtype = line[0]
POS_LAST = line[1][:]
POS = line[2][:]
CENTER = line[3][:]
feed = line[4]
spindle = line[5]
else:
mvtype = -1
passthru = line
###############################################################################
if mvtype >= 0 and mvtype <=3:
pos = POS[:]
pos_last = POS_LAST[:]
center = CENTER[:]
#############################
if mvtype == 0:
out.append( [mvtype,pos_last,pos] )
if mvtype == 1:
dx = pos[0]-pos_last[0]
dy = pos[1]-pos_last[1]
dz = pos[2]-pos_last[2]
length = sqrt(dx*dx + dy*dy)
if (length <= min_length):
out.append( [mvtype,pos_last,pos,feed,spindle] )
else:
Lsteps = max(2,int(ceil(length / min_length)))
xstp0 = float(pos_last[0])
ystp0 = float(pos_last[1])
zstp0 = float(pos_last[2])
for n in range(1,Lsteps+1):
xstp1 = n/float(Lsteps)*dx + pos_last[0]
ystp1 = n/float(Lsteps)*dy + pos_last[1]
zstp1 = n/float(Lsteps)*dz + pos_last[2]
out.append( [mvtype,[xstp0,ystp0,zstp0],[xstp1,ystp1,zstp1],feed,spindle] )
xstp0 = float(xstp1)
ystp0 = float(ystp1)
zstp0 = float(zstp1)
if mvtype == 2 or mvtype == 3:
out.append( [ mvtype,pos_last,pos,center, feed,spindle] )
###############################################################################
else:
if passthru != '':
out.append(passthru)
################################
## Loop through output to ##
## find needed probe points ##
################################
for i in range(len(out)):
line = out[i]
if line[0] == 0 or line[0] == 1:
mvtype = line[0]
POS_LAST = line[1][:]
POS = line[2][:]
CENTER = ['','','']
if line[0] == 1:
feed = line[3]
spindle = line[4]
elif line[0] == 3 or line[0] == 2:
mvtype = line[0]
POS_LAST = line[1][:]
POS = line[2][:]
CENTER = line[3][:]
feed = line[4]
spindle = line[5]
else:
mvtype = -1
passthru = line
if mvtype >= 1 and mvtype <=3:
pos = POS[:]
pos_last = POS_LAST[:]
center = CENTER[:]
#### ADD ADDITIONAL DATA TO POS_LAST DATA ####
i_x,i_y = self.get_ix_iy((pos_last[0]-minx),(pos_last[1]-miny),xPartitionLength,yPartitionLength)
#i_x = i_x+Xoffset
#i_y = i_y+Yoffset
if i_x < 0:
i_x=0
if i_y < 0:
i_y=0
if (i_x+1 >= nX):
i_x = nX-2
#i_x = i_x-1 #commented 02/22
#print "adjust i_x POS_LAST"
i_x2 = i_x+1
if (i_y+1 >= nY):
i_y = nY-2
#i_y = i_y-1 #commented 02/22
#print "adjust i_y POS_LAST"
i_y2 = i_y+1
p_index_A = int(i_y* nX + i_x )
p_index_B = int(i_y2*nX + i_x )
p_index_C = int(i_y *nX + i_x2)
p_index_D = int(i_y2*nX + i_x2)
Xfraction=((pos_last[0]-minx)-(i_x*xPartitionLength))/xPartitionLength
Yfraction=((pos_last[1]-miny)-(i_y*yPartitionLength))/yPartitionLength
if Xfraction>1.0:
#print "ERROR POS_LAST: Xfraction = ", Xfraction
Xfraction = 1.0
if Xfraction <0.0:
#print "ERROR POS_LAST: Xfraction = ", Xfraction
Xfraction = 0.0
if Yfraction > 1.0:
#print "ERROR POS_LAST: Yfraction = ", Yfraction
Yfraction = 1.0
if Yfraction<0.0:
#print "ERROR POS_LAST: Yfraction = ", Yfraction
Yfraction = 0.0
BPN=500
out[i][1].append(p_index_A+BPN)
out[i][1].append(p_index_B+BPN)
out[i][1].append(p_index_C+BPN)
out[i][1].append(p_index_D+BPN)
out[i][1].append(Xfraction)
out[i][1].append(Yfraction)
try:
POINT_LIST[p_index_A ] = True
POINT_LIST[p_index_B ] = True
POINT_LIST[p_index_C ] = True
POINT_LIST[p_index_D ] = True
except:
pass
#### ADD ADDITIONAL DATA TO POS_LAST DATA ####
i_x,i_y = self.get_ix_iy((pos[0]-minx),(pos[1]-miny),xPartitionLength,yPartitionLength)
#i_x = i_x+Xoffset
#i_y = i_y+Yoffset
if i_x < 0:
i_x=0
if i_y < 0:
i_y=0
if (i_x+1 >= nX):
i_x = nX-2
#i_x = i_x-1 #commented 02/22
#print "adjust i_x POS"
i_x2 = i_x+1
if (i_y+1 >= nY):
i_y = nY-2
#i_y = i_y-1#commented 02/22
#print "adjust i_y POS"
i_y2 = i_y+1
p_index_A = int(i_y* nX + i_x )
p_index_B = int(i_y2*nX + i_x )
p_index_C = int(i_y *nX + i_x2)
p_index_D = int(i_y2*nX + i_x2)
Xfraction=((pos[0]-minx)-(i_x*xPartitionLength))/xPartitionLength
Yfraction=((pos[1]-miny)-(i_y*yPartitionLength))/yPartitionLength
if Xfraction>1.0:
Xfraction = 1.0
#print "ERROR POS: Xfraction = ", Xfraction
if Xfraction <0.0:
Xfraction = 0.0
#print "ERROR POS: Xfraction = ", Xfraction
if Yfraction > 1.0:
Yfraction = 1.0
#print "ERROR POS: Yfraction = ", Yfraction
if Yfraction<0.0:
Yfraction = 0.0
#print "ERROR POS: Yfraction = ", Yfraction
out[i][2].append(p_index_A+BPN)
out[i][2].append(p_index_B+BPN)
out[i][2].append(p_index_C+BPN)
out[i][2].append(p_index_D+BPN)
out[i][2].append(Xfraction)
out[i][2].append(Yfraction)
try:
POINT_LIST[p_index_A ] = True
POINT_LIST[p_index_B ] = True
POINT_LIST[p_index_C ] = True
POINT_LIST[p_index_D ] = True
except:
pass
self.probe_gcode = out
#for line in out:
# print line
################################
## Generate Probing Code ##
## For needed points ##
################################
for i in range(len(POINT_LIST)):
i_x = i % nX
i_y = int(i / nX)
xp = i_x * xPartitionLength + minx
yp = i_y * yPartitionLength + miny
probe_coords.append([POINT_LIST[i],i+BPN,xp,yp])
self.probe_coords = probe_coords
return
def get_ix_iy(self,x,y,xPartitionLength,yPartitionLength):
i_x=int(x/xPartitionLength)
i_y=int(y/yPartitionLength)
return i_x,i_y
#######################################
def scale_rotate_code(self,code2scale,scale=[1.0,1.0,1.0,1.0],angle=0.0):
if code2scale == []:
return code2scale,0,0,0,0,0,0
minx = 99999
maxx = -99999
miny = 99999
maxy = -99999
minz = 99999
maxz = -99999
mvtype = -1 # G0 (Rapid), G1 (linear), G2 (clockwise arc) or G3 (counterclockwise arc).
passthru = ""
POS =[0,0,0]
feed = 0
spindle = 0
out = []
L = 0
R = 1
flag_side = 1
for line in code2scale:
if line[0] == 0 or line[0] == 1:
mvtype = line[0]
POS_LAST = line[1][:]
POS = line[2][:]
CENTER = ['','','']
if line[0] == 1:
feed = line[3] * scale[3]
spindle = line[4]
elif line[0] == 3 or line[0] == 2:
mvtype = line[0]
POS_LAST = line[1][:]
POS = line[2][:]
CENTER = line[3][:]
feed = line[4] * scale[3]
spindle = line[5]
else:
mvtype = -1
passthru = line
###############################################################################
if mvtype >= 0 and mvtype <=3:
pos = self.scale_rot_coords(POS,scale,angle)
pos_last = self.scale_rot_coords(POS_LAST,scale,angle)
if CENTER[0]!='' and CENTER[1]!='':
center = self.scale_rot_coords(CENTER,scale,angle)
else: