-
Notifications
You must be signed in to change notification settings - Fork 45
/
gcode.py
996 lines (816 loc) · 37.1 KB
/
gcode.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
# -*- coding: utf-8 -*-
__author__ = 'Tibor Vavra'
import time
#from fastnumbers import fast_float
from collections import defaultdict
from copy import deepcopy
from pprint import pprint
import os
import numpy as np
from PyQt4.QtCore import QFile
from PyQt4.QtCore import QIODevice
from PyQt4.QtCore import QObject
from PyQt4.QtCore import QTextStream
from PyQt4.QtCore import QThread
from PyQt4.QtCore import pyqtSignal
DEBUG = False
class GCode(object):
def __init__(self, filename, controller, done_loading_callback, done_writing_callback):
self.controller = controller
self.done_loading_callback = done_loading_callback
self.writing_done_callback = done_writing_callback
self.data = defaultdict(list)
self.all_data = []
self.data_keys = set()
self.color_change_data = []
self.actual_z = '0.0'
self.speed = 0.0
self.z_hop = False
self.last_point = np.array([0.0, 0.0, 0.0])
self.actual_point = [0.0, 0.0, 0.0]
self.printing_time = 0.0
self.filament_length = 0.0
#print("Filename type: " + str(type(filename)))
#print("Filename: " + filename)
#if type(filename)==:
#self.filename = u'c:\\models\\super mega testovací Jindřich šložka čěýáéůú\\anubis_PLA_OPTIMAL.gcode'
self.filename = filename
self.is_loaded = False
self.gcode_parser = GcodeParserRunner(controller, self.filename)
self.gcode_parser_thread = QThread()
self.gcode_copy = GcodeCopyRunner(self.filename, "", color_change_lst=self.color_change_data)
self.gcode_copy_thread = QThread()
def set_running_variable(self, variable):
if self.gcode_parser:
self.gcode_parser.is_running = variable
def cancel_parsing_gcode(self):
print("Cancel presset")
if self.gcode_parser and self.gcode_parser_thread and self.gcode_parser_thread.isRunning():
self.gcode_parser.is_running = False
self.gcode_parser_thread.quit()
self.gcode_parser_thread.wait()
self.is_loaded = False
self.data = {}
self.all_data = []
self.data_keys = []
elif self.gcode_parser and self.gcode_parser.is_running==True:
self.gcode_parser.is_running = False
self.controller.set_progress_bar(0)
def cancel_writing_gcode(self):
print("Cancel writing gcode")
if self.gcode_copy and self.gcode_copy_thread and self.gcode_copy_thread.isRunning():
self.gcode_copy.quit()
self.gcode_copy_thread.wait()
def get_first_extruding_line_number_of_gcode_for_layers(self, layers_keys_lst):
lines_number = []
for i in layers_keys_lst:
line = self.data[i]
for o in line:
_a, _b, type, _speed, _extr, _extruder, line_n = o
if type >= 1.0:
lines_number.append(line_n)
break
return lines_number
def read_in_thread(self, update_progressbar_function, after_done_function):
print("reading in thread")
self.gcode_parser.moveToThread(self.gcode_parser_thread)
self.done_loading_callback = after_done_function
# connect all signals to thread class
self.gcode_parser_thread.started.connect(self.gcode_parser.load_gcode_file)
# connect all signals to parser class
self.gcode_parser.finished.connect(self.set_finished_read)
self.gcode_parser.update_progressbar=True
self.gcode_parser.set_update_progress.connect(update_progressbar_function)
self.gcode_parser.set_data_keys.connect(self.set_data_keys)
self.gcode_parser.set_data.connect(self.set_data)
self.gcode_parser.set_all_data.connect(self.set_all_data)
self.gcode_parser.set_printing_time.connect(self.set_printig_time)
self.gcode_parser_thread.start()
def read_in_realtime(self, update_progressbar=False, progressbar_func=None):
self.gcode_parser.set_data_keys.connect(self.set_data_keys)
self.gcode_parser.set_data.connect(self.set_data)
self.gcode_parser.set_all_data.connect(self.set_all_data)
self.gcode_parser.set_printing_time.connect(self.set_printig_time)
if update_progressbar and progressbar_func:
self.gcode_parser.update_progressbar = update_progressbar
self.gcode_parser.set_update_progress.connect(progressbar_func)
if self.gcode_parser.load_gcode_file():
self.is_loaded = True
return True
else:
return False
def set_printig_time(self, time):
self.printing_time = time
def set_data_keys(self, data_keys):
self.data_keys = data_keys
def set_all_data(self, all_data):
self.all_data = all_data
def set_data(self, data):
self.data = data
def set_finished_read(self):
self.gcode_parser_thread.quit()
self.is_loaded = True
self.done_loading_callback()
#self.controller.set_gcode()
def set_finished_copy(self):
self.gcode_copy_thread.quit()
#print(str(self.writing_done_callback))
self.writing_done_callback()
def set_color_change_data(self, data):
self.color_change_data = data
def write_with_changes_in_thread(self, filename_in, filename_out, update_function):
self.gcode_copy.filename_in = filename_in
self.gcode_copy.filename_out = filename_out
self.gcode_copy.color_change_lst = self.color_change_data
self.gcode_copy.moveToThread(self.gcode_copy_thread)
self.gcode_copy_thread.started.connect(self.gcode_copy.write_file)
self.gcode_copy.finished.connect(self.set_finished_copy)
self.gcode_copy.set_update_progress.connect(update_function)
self.gcode_copy_thread.start()
class GcodeCopyRunner(QObject):
finished = pyqtSignal()
set_update_progress = pyqtSignal(int)
def __init__(self, filename_in, filename_out, color_change_lst):
super(GcodeCopyRunner, self).__init__()
self.filename_in = filename_in
self.filename_out = filename_out
self.color_change_lst = color_change_lst
self.is_running = True
def write_file(self):
if self.color_change_lst:
self.copy_file_with_progress_and_color_changes(self.filename_in, self.filename_out)
else:
self.copy_file_with_progress(self.filename_in, self.filename_out)
def copy_file_with_progress_and_color_changes(self, filename_in, filename_out, length=16*1024):
f_src = open(filename_in, 'r')
f_dst = open(filename_out, 'w')
fsrc_size = os.fstat(f_src.fileno()).st_size
copied = 0
line_number = 0
while self.is_running is True:
buf = f_src.readline()
line_number += 1
if not buf:
self.finished.emit()
break
if line_number in self.color_change_lst:
f_dst.write("M600\n")
f_dst.write(buf)
copied += len(buf)
self.set_update_progress.emit((copied * 1. / fsrc_size * 1.)*100)
#progress_callback((copied * 1. / fsrc_size * 1.))
def copy_file_with_progress(self, filename_in, filename_out, length=16*1024):
f_src = open(filename_in, 'r')
f_dst = open(filename_out, 'w')
fsrc_size = os.fstat(f_src.fileno()).st_size
copied = 0
while self.is_running is True:
buf = f_src.read(length)
if not buf:
self.finished.emit()
break
f_dst.write(buf)
copied += len(buf)
self.set_update_progress.emit((copied * 1. / fsrc_size * 1.)*100)
#progress_callback((copied * 1. / fsrc_size * 1.))
class GcodeParserRunner(QObject):
finished = pyqtSignal()
set_data_keys = pyqtSignal(list)
set_data = pyqtSignal(dict)
set_all_data = pyqtSignal(list)
set_printing_time = pyqtSignal(float)
set_update_progress = pyqtSignal(int)
def __init__(self, controller, filename):
super(GcodeParserRunner, self).__init__()
self.is_running = True
self.controller = controller
self.filename = filename
self.update_progressbar=False
self.data = defaultdict(list)
self.all_data = []
self.sleep_data = []
self.tool_change_data = []
self.data_keys = set()
self.actual_z = '0.0'
self.actual_tool = 0
self.speed = 1500.0
self.extrusion = 0.0
self.z_hop = False
self.last_point = np.array([0.0, 0.0, 0.0])
self.actual_point = np.array([0.0, 0.0, 0.0])
self.absolute_coordinates = True
self.printing_time = 0.0
self.filament_length = 0.0
def cancel_parsing(self):
self.is_running = False
def load_gcode_file(self):
file = QFile(self.filename)
file.open(QIODevice.ReadOnly | QIODevice.Text)
in_stream = QTextStream(file)
file_size = file.size()
counter = 0
line = 0
line_number = 0
while not in_stream.atEnd():
if self.is_running == False:
return False
if self.update_progressbar:
if counter==10000:
#in_stream.pos() je hodne pomala funkce takze na ni pozor!!!
progress = (in_stream.pos()*1./file_size*1.) * 100.
self.set_update_progress.emit(int(progress))
counter=0
else:
counter+=1
line = in_stream.readLine()
bits = line.split(';', 1)
bits_len = len(bits)
if bits[0] == '':
line_number+=1
if bits_len > 1:
if bits[0] == '' and bits[1] == "END gcode for filament":
break
continue
if 'G1 ' in bits[0]:
self.parse_g1_line_new(bits, line_number)
elif 'G4' in bits[0]:
self.parse_g4_line(bits, line_number)
elif 'T0' in bits[0] or 'T1' in bits[0] or 'T2' in bits[0] or 'T3' in bits[0]:
self.parse_t_line(bits, line_number)
elif 'G90' in bits[0]:
self.absolute_coordinates = True
elif 'G91' in bits[0]:
self.absolute_coordinates = False
elif 'G92' in bits[0]:
self.parse_g92_line(bits, line_number)
else:
if DEBUG:
print("Nezpracovano: " + str(bits))
line_number += 1
continue
line_number += 1
if self.is_running is False and self.update_progressbar is True:
self.set_update_progress.emit(0)
return False
self.printing_time = self.calculate_time_of_print()
self.filament_length = 0.0 # self.calculate_length_of_filament()
###
self.non_extruding_layers = []
for i in self.data:
layer_flag = 'M'
for l in self.data[i]:
_start, _end, flag, _speed, _extrusion, _extruder, _line = l
if flag >= 1.0:
layer_flag = 'E'
break
if layer_flag == 'M':
self.non_extruding_layers.append(i)
for i in self.non_extruding_layers:
self.data.pop(i, None)
#print("data after:")
#pprint(len(self.data))
self.data_keys = set()
self.data_keys = set(self.data)
self.data_keys = sorted(self.data_keys, key=float)
self.set_data_keys.emit(self.data_keys)
self.set_data.emit(self.data)
self.set_all_data.emit(self.all_data)
self.set_printing_time.emit(self.printing_time)
self.finished.emit()
return True
def calculate_time_of_print(self):
time_of_print = 0.0
all_data = self.all_data
number_of_tool_change = len(self.tool_change_data)
#vectorization speed up
a_vect = np.array([i[0] for i in all_data])
b_vect = np.array([i[1] for i in all_data])
speed_vect = np.array([i[3] for i in all_data])
vect_vect = b_vect - a_vect
#print("vect_vect: " + str(vect_vect.tolist()))
leng_vect = np.linalg.norm(vect_vect, axis=1)
#print("leng_vect: " + str(leng_vect.tolist()))
time_vect = np.divide(leng_vect, speed_vect)
time_of_print = np.sum(time_vect)
print("Time of print0: " + str(time_of_print))
#Magic constant :-)
time_of_print *= 1.1
print("Time of print1: " + str(time_of_print))
sum_of_sleep = np.sum(self.sleep_data)
print("Sum of sleep: " + str(sum_of_sleep))
time_of_print += sum_of_sleep
print("Time of print2: " +str(time_of_print))
#speed is in mm/min => mm/sec
return time_of_print*60.
def calculate_length_of_filament(self):
length = 0.0
for line in self.all_data:
if line[2]=='M':
continue
a = np.array(line[0])
b = np.array(line[1])
vect = b-a
leng = np.linalg.norm(vect)
length+=leng
return length
def set_print_info_text(self, string):
print("Info: " + string)
#only T lines
def parse_t_line(self, data, line_number):
if len(data)>1:
text = data[0]
comment = data[1]
else:
text = data[0]
comment = ""
line = text.split(' ')
line = list(filter(None, line))
line_len = len(line)
comment_line = comment.split(' ')
comment_line = list(filter(None, comment_line))
if 'T?' in line[0]:
return
else:
self.tool_change_data.append(int(line[0][1:]))
self.actual_tool = int(line[0][1:])
#only G4 lines
def parse_g4_line(self, data, line_number):
if len(data)>1:
text = data[0]
comment = data[1]
else:
text = data[0]
comment = ""
line = text.split(' ')
line = list(filter(None, line))
line_len = len(line)
comment_line = comment.split(' ')
comment_line = list(filter(None, comment_line))
if line_len > 1:
if 'S' in line[1]:
self.sleep_data.append(float(line[1][1:]))
#set sleep
'''
#only G1 lines
def parse_g1_line(self, data, line_number):
if len(data)>1:
text = data[0]
comment = data[1]
else:
text = data[0]
comment = ""
line = text.split(' ')
line = list(filter(None, line))
#print(comment)
comment_line = comment.split(' ')
comment_line = list(filter(None, comment_line))
if len(comment_line)==0:
# Uncomented gcode
if 'Z' in line[1]:
# Set of Z axis
new_z = float(line[1][1:])
self.actual_z = "%.2f" % new_z
self.last_point[2] = new_z
return
elif 'F' in line[1]:
self.speed = float(line[1][1:])
elif 'X' in line[1] and 'Y' in line[2] and not ('E' in line[3]) and 'F' in line[3]:
# elif 'X' in line[1] and 'Y' in line[2] and not ('E' in line[3]) and 'F' in line[3]:
# Move point
self.actual_point = np.array([np.float(line[1][1:]), np.float(line[2][1:]), np.float(self.actual_z)])
if self.last_point.any():
self.add_line(self.last_point, self.actual_point, self.actual_z, 'M', np.float(line[3][1:]), self.actual_tool, line_number=line_number)
self.last_point = np.array(self.actual_point)
else:
self.last_point = np.array(self.actual_point)
elif 'X' in line[1] and 'Y' in line[2] and 'E' in line[3]:
# elif 'X' in line[1] and 'Y' in line[2] and 'E' in line[3]:
# Extrusion point
self.actual_point = np.array([np.float(line[1][1:]), np.float(line[2][1:]), np.float(self.actual_z)])
self.extrusion = np.float(line[3][1:])
if self.last_point.any():
if np.float(line[3][1:]) > 0.:
if len(comment_line) > 0:
if 'infill' in comment_line[0]:
type = 'E-i'
elif 'perimeter' in comment_line[0]:
type = 'E-p'
elif 'support' in comment_line[0] and 'material' in comment_line[1]:
type = 'E-su'
elif 'skirt' in comment_line[0]:
type = 'E-sk'
else:
type = 'E'
else:
type = 'E'
else:
type = 'M'
self.add_line(self.last_point, self.actual_point, self.actual_z, type, self.speed, self.extrusion, self.actual_tool, line_number=line_number)
self.last_point = np.array(self.actual_point)
else:
self.last_point = np.array(self.actual_point)
elif 'X' in line[1] and 'E' in line[2] and 'F' in line[3]:
# elif 'X' in line[1] and 'E' in line[2] and 'F' in line[3]:
# Extrusion point
self.actual_point[0] = np.float(line[1][1:])
self.extrusion = np.float(line[2][1:])
if self.last_point.any():
if np.float(line[2][1:]) > 0.:
if len(comment_line) > 0:
if 'infill' in comment_line[0]:
type = 'E-i'
elif 'perimeter' in comment_line[0]:
type = 'E-p'
elif 'support' in comment_line[0] and 'material' in comment_line[1]:
type = 'E-su'
elif 'skirt' in comment_line[0]:
type = 'E-sk'
else:
type = 'E'
else:
type = 'E'
else:
type = 'M'
self.add_line(self.last_point, self.actual_point, self.actual_z, type, np.float(line[3][1:]), self.extrusion, self.actual_tool, line_number=line_number)
self.last_point = np.array(self.actual_point)
else:
self.last_point = np.array(self.actual_point)
elif 'Y' in line[1] and 'F' in line[2]:
# elif 'Y' in line[1] and 'F' in line[2]:
# Move point
self.actual_point[1] = np.float(line[1][1:])
if self.last_point:
self.add_line(self.last_point, self.actual_point, self.actual_z, 'M', np.float(line[2][1:]), self.actual_tool, line_number=line_number)
self.last_point = np.array(self.actual_point)
else:
self.last_point = np.array(self.actual_point)
elif 'Y' in line[1] and 'E' in line[2]:
# G1 Y199.750 E0.3154 F2400
# G1 Y199.750 E0.3154
speed = 0.
if len(line) > 3:
if 'F' in line[3]:
speed = np.float(line[3][1:])
self.actual_point[1] = np.float(line[1][1:])
self.extrusion = np.float(line[2][1:])
self.add_line(self.last_point, self.actual_point, self.actual_z, 'E', speed, self.extrusion, self.actual_tool, line_number=line_number)
self.last_point = np.array(self.actual_point)
elif 'X' in line[1]:
print("Zpracovavam: " + str(line))
# G1 X179.750 F7000
# G1 X240.250 E1.9299
speed = 0.
if len(line) > 2:
if 'F' in line[2]:
speed = np.float(line[2][1:])
elif 'E' in line[2]:
self.extrusion = np.float(line[2][1:])
self.actual_point[0] = np.float(line[1][1:])
self.add_line(self.last_point, self.actual_point, self.actual_z, 'E', speed, self.extrusion, self.actual_tool, line_number=line_number)
self.last_point = np.array(self.actual_point)
elif DEBUG:
print("Nezpracovano: " + str(line) + ' ' + str(comment_line))
else:
#Comented gcode
if 'Z' in line[1]:
# G1 Z0.350 F7200.000 ; move to next layer (1)
# Set of Z axis
new_z = np.float(line[1][1:])
self.actual_z = "%.2f" % new_z
self.last_point[2] = new_z
return
elif 'F' in line[1]:
# G1 F5760
self.speed = np.float(line[1][1:])
elif 'X' in line[1] and 'Y' in line[2] and not ('E' in line[3]) and 'F' in line[3] and not (
'intro' in comment_line[0] and 'line' in comment_line[1]):
# elif 'X' in line[1] and 'Y' in line[2] and not ('E' in line[3]) and 'F' in line[3]:
# Move point
#
self.actual_point = np.array([np.float(line[1][1:]), np.float(line[2][1:]), np.float(self.actual_z)])
if self.last_point.any():
self.add_line(self.last_point, self.actual_point, self.actual_z, 'M', np.float(line[3][1:]), self.actual_tool, line_number=line_number)
self.last_point = np.array(self.actual_point)
else:
self.last_point = np.array(self.actual_point)
elif 'X' in line[1] and 'Y' in line[2] and 'E' in line[3] and not (
'intro' in comment_line[0] and 'line' in comment_line[1]):
# G1 X122.438 Y106.154 E0.01540 ; infill
# elif 'X' in line[1] and 'Y' in line[2] and 'E' in line[3]:
# Extrusion point
self.actual_point = np.array([np.float(line[1][1:]), np.float(line[2][1:]), np.float(self.actual_z)])
self.extrusion = np.float(line[3][1:])
if self.last_point.any():
if np.float(line[3][1:]) > 0.:
if len(comment_line) > 0:
if 'infill' in comment_line[0]:
type = 'E-i'
elif 'perimeter' in comment_line[0]:
type = 'E-p'
elif 'support' in comment_line[0] and 'material' in comment_line[1]:
type = 'E-su'
elif 'skirt' in comment_line[0]:
type = 'E-sk'
else:
type = 'E'
else:
type = 'E'
else:
type = 'M'
self.add_line(self.last_point, self.actual_point, self.actual_z, type, self.speed, self.extrusion, self.actual_tool, line_number=line_number)
self.last_point = np.array(self.actual_point)
else:
self.last_point = np.array(self.actual_point)
elif 'X' in line[1] and 'E' in line[2] and 'F' in line[3] and not (
'intro' in comment_line[0] and 'line' in comment_line[1]):
# elif 'X' in line[1] and 'E' in line[2] and 'F' in line[3]:
# Extrusion point
self.actual_point[0] = np.float(line[1][1:])
self.extrusion = np.float(line[2][1:])
if self.last_point.any():
if np.float(line[2][1:]) > 0.:
if len(comment_line) > 0:
if 'infill' in comment_line[0]:
type = 'E-i'
elif 'perimeter' in comment_line[0]:
type = 'E-p'
elif 'support' in comment_line[0] and 'material' in comment_line[1]:
type = 'E-su'
elif 'skirt' in comment_line[0]:
type = 'E-sk'
else:
type = 'E'
else:
type = 'E'
else:
type = 'M'
self.add_line(self.last_point, self.actual_point, self.actual_z, type, np.float(line[3][1:]), self.extrusion, self.actual_tool, line_number=line_number)
self.last_point = np.array(self.actual_point)
else:
self.last_point = np.array(self.actual_point)
elif 'Y' in line[1] and 'F' in line[2] and not ('go' in comment_line[0] and 'outside' in comment_line[1]):
# elif 'Y' in line[1] and 'F' in line[2]:
# Move point
self.actual_point[1] = np.float(line[1][1:])
if self.last_point.any():
self.add_line(self.last_point, self.actual_point, self.actual_z, 'M', np.float(line[2][1:]), self.actual_tool, line_number=line_number)
self.last_point = np.array(self.actual_point)
else:
self.last_point = np.array(self.actual_point)
elif 'Y' in line[1] and 'E' in line[2]:
# G1 Y199.750 E0.3154 F2400
# G1 Y199.750 E0.3154
speed = 0.
if len(line) > 3:
if 'F' in line[3]:
speed = np.float(line[3][1:])
self.actual_point[1] = np.float(line[1][1:])
self.extrusion = np.float(line[2][1:])
self.add_line(self.last_point, self.actual_point, self.actual_z, 'E', speed, self.extrusion, self.actual_tool, line_number=line_number)
self.last_point = np.array(self.actual_point)
elif 'X' in line[1]:
#print("Zpracovavam: " + str(line))
# G1 X179.750 F7000
# G1 X240.250 E1.9299
speed = 0.
if len(line) > 2:
if 'F' in line[2]:
speed = np.float(line[2][1:])
elif 'E' in line[2]:
self.extrusion = np.float(line[2][1:])
self.actual_point[0] = np.float(line[1][1:])
self.add_line(self.last_point, self.actual_point, self.actual_z, 'E', speed, self.extrusion, self.actual_tool,
line_number=line_number)
self.last_point = np.array(self.actual_point)
elif DEBUG:
print("Nezpracovano: " + str(line) +' ' +str(comment_line))
return
'''
@staticmethod
def type_convert(type):
if type == 0.0:
type_out = "M" #Move type
elif type == 1.0:
type_out = "E" # Extrusion type
elif type == 2.0:
type_out = "E-i" # Extrusion-infill type
elif type == 3.0:
type_out = "E-p" # Extrusion-perimeter type
elif type == 4.0:
type_out = "E-su" # Extrusion-support type
elif type == 5.0:
type_out = "E-sk" # Extrusion-support type
else:
type_out = "E"
return type_out
def parse_g1_line_new(self, data, line_number):
# get raw line data and line_number to know position in file
# data is list from line from file devided by ;
# [0] data and [1] is comment
if len(data)>1:
text = data[0]
comment = data[1]
else:
text = data[0]
comment = ""
line = text.split(' ')
line = list(filter(None, line))
line_len = len(line)
#print(comment)
comment_line = comment.split(' ')
comment_line = list(filter(None, comment_line))
comment_line_len = len(comment_line)
if 'Z' in line[1]:
# Set of Z axis
# G1 Z1.850 F7200.000 ; lift Z
new_z = float(line[1][1:])
if self.absolute_coordinates:
self.actual_z = "%.2f" % new_z
self.last_point = np.array([self.last_point[0], self.last_point[1], new_z])
else:
self.actual_z = "%.2f" % (self.last_point[2] + new_z)
self.last_point = np.array([self.last_point[0], self.last_point[1], self.last_point[2] + new_z])
return
elif 'F' in line[1]:
# Set of feed rate(speed mm/m)
# G1 F5760
self.speed = np.float(line[1][1:])
elif 'X' in line[1]:
# Set move point(possible extrusion)
# G1 X119.784 Y109.613 E0.00507 ; perimeter
# G1 X119.731 Y110.014 F7200.000 ; move to first perimeter point
# G1 X118.109 Y101.483 E0.03127 ; infill
# G1 X121.899 Y107.591 E-0.97707 ; wipe and retract
# G1 X179.750 F7000
# G1 X240.250 E1.9299
# G1 X60 Y60 Z1 F1000.0
if line_len == 5:
if 'X' in line[1] and 'Y' in line[2] and 'Z' in line[3] and 'F' in line[4]:
self.speed = np.float(line[4][1:])
self.actual_point = np.array([np.float(line[1][1:]), self.actual_point[1], np.float(self.actual_z)])
else:
if DEBUG:
print("Nezpracovano: " + str(line) + ' ' + str(comment_line))
elif line_len == 4:
if 'E' in line[2] and 'F' in line[3]:
# G1 X181.500 E0.0217 F2900
self.extrusion = np.float(line[2][1:])
self.speed = np.float(line[3][1:])
self.actual_point = np.array([np.float(line[1][1:]), self.actual_point[1], np.float(self.actual_z)])
elif 'E' in line[3]:
# G1 X119.784 Y109.613 E0.00507 ; perimeter
# G1 X118.109 Y101.483 E0.03127 ; infill
# G1 X121.899 Y107.591 E-0.97707 ; wipe and retract
self.extrusion = np.float(line[3][1:])
self.actual_point = np.array(
[np.float(line[1][1:]), np.float(line[2][1:]), np.float(self.actual_z)])
elif 'F' in line[3]:
# G1 X119.731 Y110.014 F7200.000 ; move to first perimeter point
self.speed = np.float(line[3][1:])
self.extrusion = 0.
self.actual_point = np.array([np.float(line[1][1:]), np.float(line[2][1:]), np.float(self.actual_z)])
elif line_len == 3:
# G1 X179.750 F7000
# G1 X240.250 E1.9299
if 'E' in line[2]:
self.extrusion = np.float(line[2][1:])
elif 'F' in line[2]:
self.speed = np.float(line[2][1:])
self.actual_point = np.array([np.float(line[1][1:]), self.actual_point[1], self.actual_point[2]])
if self.extrusion > 0.:
if comment_line_len > 0:
if 'infill' in comment_line[0]:
type = 2.0
elif 'perimeter' in comment_line[0]:
type = 3.0
elif 'support' in comment_line[0] and 'material' in comment_line[1]:
type = 4.0
elif 'skirt' in comment_line[0]:
type = 5.0
else:
type = 1.0
else:
type = 1.0
else:
type = 0.0
if self.last_point.any():
self.add_line(self.last_point, self.actual_point, self.actual_z, type, self.speed, self.extrusion,
self.actual_tool, line_number=line_number)
self.last_point = np.array(self.actual_point)
else:
self.last_point = np.array(self.actual_point)
elif 'Y' in line[1]:
# Set move point(possible extrusion)
# G1 Y199.750 E0.3154 F2400
# G1 Y185.250 E0.3154
if line_len == 4:
if 'F' in line[3]:
# G1 Y199.750 E0.3154 F2400
self.speed = np.float(line[3][1:])
self.extrusion = np.float(line[2][1:])
self.actual_point = np.array([self.actual_point[0], np.float(line[1][1:]), self.actual_point[2]])
if self.extrusion > 0.:
type = 1.0
else:
type = 0.0
if self.last_point.any():
self.add_line(self.last_point, self.actual_point, self.actual_z, type, self.speed, self.extrusion,
self.actual_tool, line_number=line_number)
self.last_point = np.array(self.actual_point)
else:
self.last_point = np.array(self.actual_point)
elif 'E' in line[1] and 'F' in line[2]:
# G1 E-15.0000 F5000
self.extrusion = np.float(line[1][1:])
self.speed = np.float(line[2][1:])
else:
if DEBUG:
print("Nezpracovano: " + str(line) + ' ' + str(comment_line))
return
def parse_g92_line(self, data, line_number):
# get raw line data and line_number to know position in file
# data is list from line from file devided by ;
# [0] data and [1] is comment
if len(data)>1:
text = data[0]
comment = data[1]
else:
text = data[0]
comment = ""
line = text.split(' ')
line = list(filter(None, line))
line_len = len(line)
#print(comment)
comment_line = comment.split(' ')
comment_line = list(filter(None, comment_line))
comment_line_len = len(comment_line)
if 'E' in line[1]:
self.extrusion = np.float(line[1][1:])
def add_line(self, first_point, second_point, actual_z, type, speed=0., extrusion=0., extruder=0, line_number = -1):
#print("Add line: " + str(first_point) + ' ' + str(second_point) + ' type: ' + str(type) + ' ' + str(line_number))
#print("actual data:")
#print(self.data_keys)
key = deepcopy(actual_z)
'''
if key in self.data_keys:
self.data[key].append([deepcopy(first_point),
deepcopy(second_point),
deepcopy(type),
deepcopy(speed),
deepcopy(extrusion),
deepcopy(extruder),
deepcopy(line_number)])
self.all_data.append([deepcopy(first_point),
deepcopy(second_point),
deepcopy(type),
deepcopy(speed),
deepcopy(extrusion),
deepcopy(extruder),
deepcopy(line_number)])
else:
self.data_keys.add(key)
self.data[key] = []
self.data[key].append([deepcopy(first_point),
deepcopy(second_point),
deepcopy(type),
deepcopy(speed),
deepcopy(extrusion),
deepcopy(extruder),
deepcopy(line_number)])
self.all_data.append([deepcopy(first_point),
deepcopy(second_point),
deepcopy(type),
deepcopy(speed),
deepcopy(extrusion),
deepcopy(extruder),
deepcopy(line_number)])
'''
if not key in self.data_keys:
self.data_keys.add(key)
self.data[key] = []
self.data[key].append(np.array([first_point,
second_point,
type,
speed,
extrusion,
extruder,
line_number]))
self.all_data.append(np.array([first_point,
second_point,
type,
speed,
extrusion,
extruder,
line_number]))
'''
def add_point(self, x, y, z, actual_z):
key = actual_z
if key in self.data_keys:
self.data[key].append([x, y, z])
self.all_data.append([x, y, z])
else:
self.data_keys.add(key)
self.data[key] = []
self.data[key].append([x, y, z])
self.all_data.append([x, y, z])
'''