aboutsummaryrefslogtreecommitdiffstats
path: root/src/emc/usr_intf/qtplasmac/qtplasmac_gcode.py
blob: ea1eb843e3140f397197aad58d918c93b18db7c6 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271

'''
plasmac_gcode.py

Copyright (C) 2019, 2020, 2021, 2022  Phillip A Carter
Copyright (C)       2020, 2021, 2022  Gregory D Carl

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.
'''

import os
import sys
import linuxcnc
import math
import shutil
import time
from subprocess import run as RUN
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QDialog, QScrollArea, QWidget, QVBoxLayout, QLabel, QPushButton, QStyle, QFrame

app = QApplication(sys.argv)
ini = linuxcnc.ini(os.environ['INI_FILE_NAME'])
cmd = linuxcnc.command()
inPath = sys.argv[1]
inFile = os.path.basename(inPath)
if inFile == 'rfl.ngc':
    with open(inPath, 'r') as inCode:
        for line in inCode:
            print(line.strip())
    sys.exit()
# assume gui to be qtplasmac unless a specific gui selected
if 'axis' in ini.find('DISPLAY', 'DISPLAY'):
    gui = 'plasmac'
    tmpPath = '/tmp/plasmac'
    cutTypePin = 'axisui.cut-type'
    matNumPin = 'axisui.material-change-number'
    convBlockPin = 'axisui.conv-block-loaded'
    matTmpPin = 'axisui.material-temp'
    matReloadPin = 'axisui.material-reload'
    fgColor = '#ffee00'
    bgColor = '#050505'
    bgAltColor = '#36362e'
    notice  = 'The line numbers in the original file may differ from what is shown below.\n\n'
else:
    gui = 'qtplasmac'
    tmpPath = '/tmp/qtplasmac'
    cutTypePin = 'qtplasmac.cut_type'
    matNumPin = 'qtplasmac.material_change_number'
    convBlockPin = 'qtplasmac.conv_block_loaded'
    matTmpPin = 'qtplasmac.material_temp'
    matReloadPin = 'qtplasmac.material_reload'
    response = RUN(['halcmd', 'getp', 'qtplasmac.color_fg'], capture_output = True)
    fgColor = str(hex(int(response.stdout.decode()))).replace('0x', '#')
    response = RUN(['halcmd', 'getp', 'qtplasmac.color_bg'], capture_output = True)
    bgColor = str(hex(int(response.stdout.decode()))).replace('0x', '#')
    response = RUN(['halcmd', 'getp', 'qtplasmac.color_bgalt'], capture_output = True)
    bgAltColor = str(hex(int(response.stdout.decode()))).replace('0x', '#')
    response = RUN(['halcmd', 'getp', 'plasmac.max-offset'], capture_output = True)
    notice  = 'If the G-code editor is used to resolve the following issues, the lines with errors\n'
    notice += 'will be highlighted. The line numbers may differ from what is shown below.\n\n'
filteredBkp = '{}/filtered_bkp.ngc'.format(tmpPath)
errorFile = '{}/gcode_errors.txt'.format(tmpPath)
materialFile = '{}_material.cfg'.format(ini.find('EMC', 'MACHINE'))
tmpMaterialFile = '{}/{}_material.gcode'.format(tmpPath, ini.find('EMC', 'MACHINE'))
tmpMatNum = 1000000
tmpMatNam = ''
prefsFile = ini.find('EMC', 'MACHINE') + '.prefs'
response = RUN(['halcmd', 'getp', '{}'.format(cutTypePin)], capture_output = True)
cutType = int(response.stdout.decode())
response = RUN(['halcmd', 'getp', '{}'.format(matNumPin)], capture_output = True)
currentMat = int(response.stdout.decode())
response = RUN(['halcmd', 'getp', 'plasmac.max-offset'], capture_output = True)
zMaxOffset = float(response.stdout.decode())
RUN(['halcmd', 'setp', '{}'.format(convBlockPin), '0'])
metric = ['mm', 4]
imperial = ['in', 6]
units, precision = imperial if ini.find('TRAJ', 'LINEAR_UNITS').lower() == 'inch' else metric
if units == 'mm':
    minDiameter = 32
    ocLength = 4
    unitsPerMm = 1
    blendTolerance = 0.1
else:
    minDiameter = 1.26
    ocLength = 0.157
    unitsPerMm = 0.03937
    blendTolerance = 0.004
unitMultiplier = 1
gcodeList = []
newMaterial = []
firstMaterial = ''
line = ''
rapidLine = ''
lastX = 0
lastY = 0
oBurnX = 0
oBurnY = 0
lineNum = 0
lineNumOrg = 0
distMode = 90 # absolute
arcDistMode = 91.1 # incremental
holeVelocity = 60
material = [0, False]
overCut = False
holeActive = False
holeEnable = False
arcEnable = False
customDia = False
customLen = False
torchEnable = True
pierceOnly = False
scribing = False
spotting = False
offsetG4x = False
zSetup = False
zBypass = False
pathBlend = False
convBlock = False
filtered = False
firstMove = False
codeError = False
errors  = 'The following errors will affect the process.\n'
errors += 'Errors must be fixed before reloading this file.\n'
errorMath = []
errorMissMat = []
errorNoMat = []
errorBadMat = []
errorTempMat = []
errorNewMat = []
errorEditMat = []
errorWriteMat = []
errorReadMat = []
errorCompMat = []
errorFirstMove = []
errorLines = []
codeWarn = False
warnings  = 'The following warnings may affect the quality of the process.\n'
warnings += 'It is recommended that all warnings are fixed before running this file.\n'
warnUnitsDep = []
warnPierceScribe = []
warnMatLoad = []
warnHoleDir = []
warnCompTorch = []
warnCompVel = []
warnFeed = []
warnChar = []

# feedback dialog
def dialog_box(title, icon, errorText, warningText):
    dlg = QDialog()
    scroll = QScrollArea(dlg)
    widget = QWidget()
    vbox = QVBoxLayout()
    labelN = QLabel(notice, objectName = 'labelN')
    lineE = QFrame(objectName = 'lineE')
    lineE.setFrameShape(QFrame.HLine)
    labelE1 = QLabel(objectName = 'labelE1')
    labelE2 = QLabel()
    lineW = QFrame(objectName = 'lineW')
    lineW.setFrameShape(QFrame.HLine)
    labelW1 = QLabel(objectName = 'labelW1')
    labelW2 = QLabel()
    vbox.addWidget(labelN)
    vbox.addWidget(lineE)
    vbox.addWidget(labelE1)
    vbox.addWidget(labelE2)
    vbox.addWidget(lineW)
    vbox.addWidget(labelW1)
    vbox.addWidget(labelW2)
    widget.setLayout(vbox)
    btn = QPushButton('OK', dlg)
    dlg.setWindowTitle(title)
    dlg.setWindowIcon(QIcon(dlg.style().standardIcon(icon)))
    dlg.setWindowFlags(Qt.WindowStaysOnTopHint)
    dlg.setModal(False)
    dlg.setFixedWidth(600)
    dlg.setFixedHeight(310)
    scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
    scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
    scroll.setWidgetResizable(True)
    scroll.setWidget(widget)
    scroll.setGeometry(5, 5, 590, 250)
    btn.move(270,260)
    btn.clicked.connect(lambda w: dlg_ok_clicked(dlg))
    if errorText:
        labelE1.setText(errors)
        labelE2.setText(errorText)
    else:
        lineE.hide()
        labelE1.hide()
        labelE2.hide()
    if warningText:
        labelW1.setText(warnings)
        labelW2.setText(warningText)
    else:
        lineW.hide()
        labelW1.hide()
        labelW2.hide()
    dlg.setStyleSheet(' \
                      * {{ color: {0}; background: {1}}} \
                      QScrollArea {{color:{0}; background:{1}; border:1px solid {0}; border-radius:4px; padding:4px}} \
                      QPushButton {{border:2px solid {0}; border-radius:4px; font:12pt; width:60px; height:40px}} \
                      QPushButton:pressed {{border:1px solid {0}}} \
                      QScrollBar:vertical {{background:{2}; border:0px; border-radius:4px; margin: 0px; width:20px}} \
                      QScrollBar::handle:vertical {{background:{0}; border:2px solid {0}; border-radius:4px; margin:2px; min-height:40px}} \
                      QScrollBar::add-line:vertical {{height:0px}} \
                      QScrollBar::sub-line:vertical {{height:0px}} \
                      QVboxLayout {{margin:100}} \
                      #labelN {{font-style:italic}} \
                      #lineE, #lineW {{border:1px solid {0}}} \
                      #labelE1, #labelW1 {{font-weight:bold}}'.format(fgColor, bgColor, bgAltColor))
    dlg.exec()

def dlg_ok_clicked(dlg):
    dlg.accept()

# set hole type
def set_hole_type(h):
    global holeEnable, overCut, arcEnable
    if h == 1:
        holeEnable = True
        arcEnable = False
        overCut = False
    elif h == 2:
        holeEnable = True
        arcEnable = False
        overCut = True
    elif h == 3:
        holeEnable = True
        arcEnable = True
        overCut = False
    elif h == 4:
        holeEnable = True
        arcEnable = True
        overCut = True
    else:
        holeEnable = False
        arcEnable = False
        overCut = False

# check if arc is a hole
def check_if_hole():
    global lastX, lastY, minDiameter
    I, J, isHole = 0, 0, 0
    if distMode == 91: # get absolute X & Y from incremental coordinates
        endX = lastX + get_axis_value('x') if 'x' in line else lastX
        endY = lastY + get_axis_value('y') if 'y' in line else lastY
    else: # get absolute X & Y
        endX = get_axis_value('x') if 'x' in line else lastX
        endY = get_axis_value('y') if 'y' in line else lastY
    if arcDistMode == 90.1: # convert I & J to incremental to make diameter calculations easier
        if 'i' in line: I = get_axis_value('i') - lastX
        if 'j' in line: J = get_axis_value('j') - lastY
    else: # get incremental I & J
        if 'i' in line: I = get_axis_value('i')
        if 'j' in line: J = get_axis_value('j')
    if lastX and lastY and lastX == endX and lastY == endY:
        isHole = True
    diameter = get_hole_diameter(I, J, isHole)
    gcodeList.append(line)
    if isHole and overCut and diameter <= minDiameter and ocLength:
        overburn(I, J, diameter / 2)
        return
    else:
        lastX = endX
        lastY = endY

# get hole diameter and set velocity percentage
def get_hole_diameter(I, J, isHole):
    global lineNum, lineNumOrg, errorLines, holeActive, codeWarn, warnCompVel, warnHoleDir
    if offsetG4x:
        diameter = math.sqrt((I ** 2) + (J ** 2)) * 2
    else:
        if material[0] in materialDict:
            kerfWidth = materialDict[material[0]][1] / 2 * unitMultiplier
        else:
            kerfWidth = 0
        diameter = (math.sqrt((I ** 2) + (J ** 2)) * 2) + kerfWidth
    # velocity reduction is required
    if diameter <= minDiameter and (isHole or arcEnable):
        if offsetG4x:
            lineNum += 1
            gcodeList.append(';m67 e3 q0 (inactive due to g41)')
            codeWarn = True
            warnCompVel.append(lineNum)
            errorLines.append(lineNumOrg)
        elif not holeActive:
            if diameter <= minDiameter:
                lineNum += 1
                gcodeList.append('m67 e3 q{0} (diameter:{1:0.3f}, velocity:{0}%)'.format(holeVelocity, diameter))
            holeActive = True
        if line.startswith('g2') and isHole:
            codeWarn = True
            warnHoleDir.append(lineNum)
            errorLines.append(lineNumOrg)
    # no velocity reduction required
    else:
        if holeActive:
            lineNum += 1
            gcodeList.append('m67 e3 q0 (arc complete, velocity 100%)')
            holeActive = False
    return diameter

# turn torch off and move 4mm (0.157) past hole end
def overburn(I, J, radius):
    global lineNum, lineNumOrg, errorLines, lastX, lastY, torchEnable, ocLength, warnCompTorch, arcDistMode, oBurnX, oBurnY, codeWarn
    centerX = lastX + I
    centerY = lastY + J
    cosA = math.cos(ocLength / radius)
    sinA = math.sin(ocLength / radius)
    cosB = ((lastX - centerX) / radius)
    sinB = ((lastY - centerY) / radius)
    lineNum += 1
    if offsetG4x:
        gcodeList.append(';m62 p3 (inactive due to g41)')
        codeWarn = True
        warnCompTorch.append(lineNum)
        errorLines.append(lineNumOrg)
    else:
        gcodeList.append('m62 p3 (disable torch)')
        torchEnable = False
    #clockwise arc
    if line.startswith('g2'):
        endX = centerX + radius * ((cosB * cosA) + (sinB * sinA))
        endY = centerY + radius * ((sinB * cosA) - (cosB * sinA))
        dir = '2'
    #counterclockwise arc
    else:
        endX = centerX + radius * ((cosB * cosA) - (sinB * sinA))
        endY = centerY + radius * ((sinB * cosA) + (cosB * sinA))
        dir = '3'
    lineNum += 1
    # restore I & J back to absolute from incremental conversion in check_if_hole
    if arcDistMode == 90.1:
        I += lastX
        J += lastY
    if distMode == 91: # output incremental X & Y
        gcodeList.append('g{0} x{1:0.{5}f} y{2:0.{5}f} i{3:0.{5}f} j{4:0.{5}f} (overburn)'.format(dir, endX - lastX, endY - lastY, I, J, precision))
    else: # output absolute X & Y
        gcodeList.append('g{0} x{1:0.{5}f} y{2:0.{5}f} i{3:0.{5}f} j{4:0.{5}f} (overburn)'.format(dir, endX, endY, I, J, precision))
    oBurnX = endX - lastX
    oBurnY = endY - lastY

# fix incremental coordinates after overburn
def fix_overburn_incremental_coordinates(line):
    newLine = line[:2]
    if 'x' in line and 'y' in line:
        x = get_axis_value('x')
        if x is not None:
            newLine += 'x{:0.4f}'.format(x - oBurnX)
        y = get_axis_value('y')
        if y is not None:
            newLine += 'y{:0.4f}'.format(y - oBurnY)
        return newLine
    elif 'x in line':
        x = get_axis_value('x')
        if x is not None:
            newLine += 'x{:0.4f}y{:0.4f}'.format(x - oBurnX, oBurnY)
        return newLine
    elif 'y' in line:
        y = get_axis_value('y')
        if y is not None:
            newLine += 'x{:0.4f}y{:0.4f}'.format(oBurnX, y - oBurnY)
        return newLine
    else:
        return line

# get axis value
def get_axis_value(axis):
    tmp1 = line.split(axis)[1].replace(' ','')
    if not tmp1[0].isdigit() and not tmp1[0] == '.' and not tmp1[0] == '-':
        return None
    n = 0
    tmp2 = ''
    while 1:
        if tmp1[n].isdigit() or tmp1[n] == '.' or tmp1[n] == '-':
            tmp2 += tmp1[n]
            n += 1
        else:
            break
        if n >= len(tmp1):
            break
    return float(tmp2)

# set the last X and Y coordinates
def set_last_coordinates(Xpos, Ypos):
    if line[0] in ['g','x','y']:
        if 'x' in line:
            if get_axis_value('x') is not None:
                if distMode == 91: # get absolute X from incremental position
                    Xpos += get_axis_value('x')
                else: # get absolute X
                    Xpos = get_axis_value('x')
        if 'y' in line:
            if get_axis_value('y') is not None:
                if distMode == 91: # get absolute X from incremental position
                    Ypos += get_axis_value('y')
                else: # get absolute X
                    Ypos = get_axis_value('y')
    return Xpos, Ypos

# comment out all Z commands
def comment_out_z_commands():
    global lineNum, holeActive
    newline = ''
    newz = ''
    removing = 0
    comment = 0
    maths = 0
    for bit in line:
        if comment:
            if bit == ')':
                comment = 0
            newline += bit
        elif removing:
            if bit == '[':
                newz += bit
                maths += 1
            elif bit == ']':
                newz += bit
                maths -= 1
            elif maths:
                newz += bit
            elif bit in '0123456789.- ':
                newz += bit
            else:
                removing = 0
                if newz:
                    newz = newz.rstrip()
                newline += bit
        elif bit == '(':
            comment = 1
            newline += bit
        elif bit == 'z':
            removing = 1
            newz += '(' + bit
        else:
            newline += bit
    if holeActive:
        lineNum += 1
        gcodeList.append('m67 e3 q0 (arc complete, velocity 100%)')
        holeActive = False
    return '{} {})'.format(newline, newz)

# check if math used or explicit values
def check_math(axis):
    global lineNum, lineNumOrg, errorLines, codeError, errorMath
    tmp1 = line.split(axis)[1]
    if tmp1.startswith('[') or tmp1.startswith('#'):
        codeError = True
        if lineNum not in errorMath:
            errorMath.append(lineNum)
            errorLines.append(lineNumOrg)
        return True
    return False
# do material change
def do_material_change():
    global lineNum, lineNumOrg, errorLines, firstMaterial, codeError, errorMissMat, errorNoMat, errorBadMat
    if '(' in line:
        a = line.split('(', 1)[0]
    elif ';' in line:
        a = line.split(';', 1)[0]
    else:
        a = line
    b = a.replace('m190', '').strip()
    # check for missing p or material
    if not len(b) or b[0] != 'p' or b == 'p':
        codeError = True
        errorNoMat.append(lineNum)
        errorLines.append(lineNumOrg)
        gcodeList.append(';{}'.format(line))
        return True
    c = b.split('p', 1)[1]
    m = ''
    # get the material number
    for mNum in c.strip():
        if mNum in '0123456789':
            m += mNum
        else:
            codeError = True
            errorBadMat.append(lineNum)
            errorLines.append(lineNumOrg)
            gcodeList.append(';{}'.format(line))
            return True
    material[0] = int(m)
    material[1] = True
    # check if material exists
    if material[0] not in materialDict and material[0] < 1000000:
        codeError = True
        errorMissMat.append(lineNum)
        errorLines.append(lineNumOrg)
    RUN(['halcmd', 'setp', '{}'.format(matNumPin), '{}'.format(material[0])])
    if not firstMaterial:
        firstMaterial = material[0]
    gcodeList.append(line)

# check if material edit required
def check_material_edit():
    global lineNum, lineNumOrg, errorLines, tmpMatNum, tmpMatNam, codeError, errorNewMat, errorEditMat
    tmpMaterial = False
    newMaterial = []
    th = 0
    kw = jh = jd = pe = gp = 0.0
    cm = 1
    ca = 15
    cv = 100
    try:
        if 'ph=' in line and 'pd=' in line and 'ch=' in line and 'fr=' in line:
            if '(o=0' in line:
                tmpMaterial = True
                nu = tmpMatNum
                na = 'Temporary {}'.format(tmpMatNum)
                tmpMatNam = na
                newMaterial.append(0)
            elif '(o=1' in line and 'nu=' in line and 'na=' in line:
                newMaterial.append(1)
            elif '(o=2' in line and 'nu=' in line and 'na=' in line:
                newMaterial.append(2)
            if newMaterial[0] in [0, 1, 2]:
                for item in line.split('(')[1].split(')')[0].split(','):
                    # mandatory items
                    if 'nu=' in item and not tmpMaterial:
                        nu = int(item.split('=')[1])
                    elif 'na=' in item:
                        na = item.split('=')[1].strip()
                        if tmpMaterial:
                            tmpMatNam = na
                    elif 'ph=' in item:
                        ph = float(item.split('=')[1])
                        if unitMultiplier != 1:
                            ph = ph / unitMultiplier
                    elif 'pd=' in item:
                        pd = float(item.split('=')[1])
                    elif 'ch=' in item:
                        ch = float(item.split('=')[1])
                        if unitMultiplier != 1:
                            ch = ch / unitMultiplier
                    elif 'fr=' in item:
                        fr = float(item.split('=')[1])
                        if unitMultiplier != 1:
                            fr = fr / unitMultiplier
                    # optional items
                    elif 'kw=' in item:
                        kw = float(item.split('=')[1])
                        if unitMultiplier != 1:
                            kw = kw / unitMultiplier
                    elif 'th=' in item:
                        th = int(item.split('=')[1])
                    elif 'jh=' in item:
                        jh = float(item.split('=')[1])
                        if unitMultiplier != 1:
                            jh = ph / unitMultiplier
                    elif 'jd=' in item:
                        jd = float(item.split('=')[1])
                    elif 'ca=' in item:
                        ca = float(item.split('=')[1])
                    elif 'cv=' in item:
                        cv = float(item.split('=')[1])
                    elif 'pe=' in item:
                        pe = float(item.split('=')[1])
                    elif 'gp=' in item:
                        gp = float(item.split('=')[1])
                    elif 'cm=' in item:
                        cm = float(item.split('=')[1])
                for i in [nu,na,kw,th,ph,pd,jh,jd,ch,fr,ca,cv,pe,gp,cm]:
                    newMaterial.append(i)
                if newMaterial[0] == 0:
                    write_temporary_material(newMaterial)
                elif nu in materialDict and newMaterial[0] == 1:
                    codeError = True
                    errorNewMat.append(lineNum)
                    errorLines.append(lineNumOrg)
                else:
                    rewrite_material_file(newMaterial)
            else:
                codeError = True
                errorEditMat.append(lineNum)
                errorLines.append(lineNumOrg)
    except:
        codeError = True
        errorLines.append(lineNumOrg)

# write temporary materials file
def write_temporary_material(data):
    global lineNum, lineNumOrg, errorLines, errorTempMat, warnMatLoad, material, codeError, codeWarn
    try:
        with open(tmpMaterialFile, 'w') as fWrite:
            if gui == 'plasmac':
                fWrite.write('[MATERIAL_NUMBER_{}]\n'.format(tmpMatNum))
            else:
                fWrite.write('#plasmac temporary material file\n')
                fWrite.write('\nnumber={}\n'.format(tmpMatNum))
            fWrite.write('NAME={}\n'.format(tmpMatNam))
            fWrite.write('KERF_WIDTH={}\n'.format(data[3]))
            fWrite.write('THC_ENABLE={}\n'.format(data[4]))
            fWrite.write('PIERCE_HEIGHT={}\n'.format(data[5]))
            fWrite.write('PIERCE_DELAY={}\n'.format(data[6]))
            fWrite.write('PUDDLE_JUMP_HEIGHT={}\n'.format(data[7]))
            fWrite.write('PUDDLE_JUMP_DELAY={}\n'.format(data[8]))
            fWrite.write('CUT_HEIGHT={}\n'.format(data[9]))
            fWrite.write('CUT_SPEED={}\n'.format(data[10]))
            fWrite.write('CUT_AMPS={}\n'.format(data[11]))
            fWrite.write('CUT_VOLTS={}\n'.format(data[12]))
            fWrite.write('PAUSE_AT_END={}\n'.format(data[13]))
            fWrite.write('GAS_PRESSURE={}\n'.format(data[14]))
            fWrite.write('CUT_MODE={}\n'.format(data[15]))
            fWrite.write('\n')
    except:
        codeError = True
        errorTempMat.append(lineNum)
        errorLines.append(lineNumOrg)
    materialDict[tmpMatNum] = [data[10], data[3]]
    RUN(['halcmd', 'setp', '{}'.format(matTmpPin), '{}'.format(tmpMatNum)])
    material[0] = tmpMatNum
    matDelay = time.time()
    while 1:
        if time.time() > matDelay + 3:
            codeWarn = True
            warnMatLoad.append(lineNum)
            errorLines.append(lineNumOrg)
            break
        response = RUN(['halcmd', 'getp', '{}'.format(matTmpPin)], capture_output = True)
        if not int(response.stdout.decode()):
            break

# rewrite the material file
def rewrite_material_file(newMaterial):
    global lineNum, warnMatLoad, errorLines, codeWarn
    copyFile = '{}.bkp'.format(materialFile)
    shutil.copy(materialFile, copyFile)
    inFile = open(copyFile, 'r')
    outFile = open(materialFile, 'w')
    while 1:
        line = inFile.readline()
        if not line:
            break
        if not line.strip().startswith('[MATERIAL_NUMBER_'):
            outFile.write(line)
        else:
            break
    while 1:
        if not line:
            add_edit_material(newMaterial, outFile)
            break
        if line.strip().startswith('[MATERIAL_NUMBER_'):
            mNum = int(line.split('NUMBER_')[1].replace(']',''))
            if mNum == newMaterial[1]:
                add_edit_material(newMaterial, outFile)
        if mNum != newMaterial[1]:
            outFile.write(line)
        line = inFile.readline()
        if not line:
            break
    if newMaterial[1] not in materialDict:
        add_edit_material(newMaterial, outFile)
    inFile.close()
    outFile.close()
    RUN(['halcmd', 'setp', '{}'.format(matReloadPin), '1'])
    get_materials()
    matDelay = time.time()
    while 1:
        if time.time() > matDelay + 3:
            codeWarn = True
            warnMatLoad.append(lineNum)
            errorLines.append(lineNumOrg)
            break
        response = RUN(['halcmd', 'getp', '{}'.format(matReloadPin)], capture_output = True)
        if not int(response.stdout.decode()):
            break

# add a new material or or edit an existing material
def add_edit_material(material, outFile):
    global lineNum, lineNumOrg, errorLines, codeError, ErrorWriteMat
    try:
        outFile.write('[MATERIAL_NUMBER_{}]\n'.format(material[1]))
        outFile.write('NAME               = {}\n'.format(material[2]))
        outFile.write('KERF_WIDTH         = {}\n'.format(material[3]))
        outFile.write('THC                = {}\n'.format(material[4]))
        outFile.write('PIERCE_HEIGHT      = {}\n'.format(material[5]))
        outFile.write('PIERCE_DELAY       = {}\n'.format(material[6]))
        outFile.write('PUDDLE_JUMP_HEIGHT = {}\n'.format(material[7]))
        outFile.write('PUDDLE_JUMP_DELAY  = {}\n'.format(material[8]))
        outFile.write('CUT_HEIGHT         = {}\n'.format(material[9]))
        outFile.write('CUT_SPEED          = {}\n'.format(material[10]))
        outFile.write('CUT_AMPS           = {}\n'.format(material[11]))
        outFile.write('CUT_VOLTS          = {}\n'.format(material[12]))
        outFile.write('PAUSE_AT_END       = {}\n'.format(material[13]))
        outFile.write('GAS_PRESSURE       = {}\n'.format(material[14]))
        outFile.write('CUT_MODE           = {}\n'.format(material[15]))
        outFile.write('\n')
    except:
        codeError = True
        errorWriteMat.append(lineNum)
        errorLines.append(lineNumOrg)

# create a dict of material numbers and kerf widths
def get_materials():
    global lineNum, lineNumOrg, errorLines, materialDict, codeError, errorReadMat
    try:
        with open(prefsFile, 'r') as rFile:
            fRate = kWidth = 0.0
            for line in rFile:
                if line.startswith('Cut feed rate'):
                    fRate = float(line.split('=')[1].strip())
                if line.startswith('Kerf width'):
                    kWidth = float(line.split('=')[1].strip())
        mNumber = 0
        with open(materialFile, 'r') as mFile:
            materialDict = {mNumber: [fRate, kWidth]}
            while 1:
                line = mFile.readline()
                if not line:
                    break
                elif line.startswith('[MATERIAL_NUMBER_') and line.strip().endswith(']'):
                    mNumber = int(line.rsplit('_', 1)[1].strip().strip(']'))
                    break
            while 1:
                line = mFile.readline()
                if not line:
                    materialDict[mNumber] = [fRate, kWidth]
                    break
                elif line.startswith('[MATERIAL_NUMBER_') and line.strip().endswith(']'):
                    materialDict[mNumber] = [fRate, kWidth]
                    mNumber = int(line.rsplit('_', 1)[1].strip().strip(']'))
                elif line.startswith('CUT_SPEED'):
                    fRate = float(line.split('=')[1].strip())
                elif line.startswith('KERF_WIDTH'):
                    kWidth = float(line.split('=')[1].strip())
    except:
        codeError = True
        errorReadMat.append(lineNum)
        errorLines.append(lineNumOrg)

def check_f_word(line):
    global lineNum, lineNumOrg, errorLines, materialDict, codeWarn, warnFeed
    begin, inFeed = line.split('f', 1)
    # if feed rate from material file
    if inFeed.startswith('#<_hal[plasmac.cut-feed-rate]>'):
        # change feed rate if gcode file not in same units as machine units
        if unitMultiplier != 1:
            line = begin + '{}f[#<_hal[plasmac.cut-feed-rate]> * {}]\n'.format(begin, unitMultiplier)
        return line
    # if explicit feed rate
    rawFeed = ''
    codeFeed = 0.0
    # get feed rate if it is digits
    while len(inFeed) and (inFeed[0].isdigit() or inFeed[0] == '.'):
        rawFeed = rawFeed + inFeed[0]
        inFeed = inFeed[1:].lstrip()
    if not rawFeed:
        return line
    codeFeed = float(rawFeed)
    matFeed = float(materialDict[material[0]][0]) * unitMultiplier

    # this may need scaling ...
    diff = 1

    if codeFeed < matFeed - diff or codeFeed > matFeed + diff:
        codeWarn = True
        warnFeed.append([lineNum, rawFeed, material[0], materialDict[material[0]][0]])
        errorLines.append(lineNumOrg)
    return line

# *** we need a lot more work done here ***
def illegal_character(line):
    line = line.replace(' ', '')
    # single character line with invalid character
    if len(line) == 1  and line not in '/;%': return 1
    # comment is missing a parenthesis
    elif ('(' in line and line[-1] != ')') or ((line[-1] == ')' and not '(' in line)): return 1
    # process starting alpha
    elif line[0].isalpha():
        # line starts with two alpha characters (this could be refined)
        if line[1].isalpha(): return 1
    # process starting non-alpha
    elif not line[0].isalpha():
        # invalid first character
        if line[0] not in '/;(#@^%': return 1
    # process numbered and named parameters
    if line[0] == '#' or line[:2] == '#<':
        line = line.lstrip('#')
        # remove trailing comment for further processing
        if '(' in line:
            line = line.split('(')[0].strip()
        # parameter is missing equals sign
        if not '=' in line: return 1
        # left = parameter, right = value (we don't process right side yet)
        left, right = line.split('=')
        # named parameter is missing a chevron
        if left[0] == '<' and not '>' in left: return 1
        # numbered parameter is not a number
        elif left[0] != '<' and not left.isdigit(): return 1
    return 0

def message_set(msgType, msg):
    if len(msgType) > 1:
        msg += 'Lines: '
    else:
        msg += 'Line: '
    count = 0
    for line in msgType:
        if codeError:
            line += 1
        if count > 0:
            msg += ', {}'.format(line)
        else:
            msg += '{}'.format(line)
        count += 1
    msg += '\n\n'
    return msg

# get a dict of materials
get_materials()

# start processing the gcode file
with open(inPath, 'r') as inCode:
    if ';qtplasmac filtered G-code file' in inCode.read():
        filtered = True
    inCode.seek(0)
    for line in inCode:
        lineNum += 1
        lineNumOrg += 1
        # if original is already filtered there is no need to process the line
        if filtered:
            if not ';qtplasmac filtered G-code file' in line:
                gcodeList.append(line.rstrip())
            continue
        # check if original is a conversational block
        if line.startswith(';conversational block'):
            convBlock = True
            RUN(['halcmd', 'setp', '{}'.format(convBlockPin), '1'])
        # remove whitespace and trailing periods
        line = line.strip().rstrip('.')
        # remove line numbers
        if line.lower().startswith('n'):
            line = line[1:]
            while line[0].isdigit() or line[0] == '.':
                line = line[1:].lstrip()
                if not line:
                    break
        # if empty line then no need to process
        if not line:
            gcodeList.append(line)
            continue
        # if line is a comment then gcodeList.append it and get next line
        if line.startswith(';') or line.startswith('(') and not line.startswith('(o='):
            gcodeList.append(line)
            continue
        # if illegal characters comment the line
        if illegal_character(line):
            codeWarn = True
            warnChar.append(lineNum)
            errorLines.append(lineNumOrg)
            gcodeList.append(';{}'.format(line))
            continue
        # check for a material edit
        if line.startswith('(o='):
            check_material_edit()
            # add comment for temporary material
            if line.startswith('(o=0'):
                lineNum += 1
                gcodeList.append(';temporary material #{}'.format(tmpMatNum))
            gcodeList.append(line)
            # add material change for temporary material
            if line.startswith('(o=0'):
                lineNum += 1
                gcodeList.append('m190 p{}'.format(tmpMatNum))
                lineNum += 1
                gcodeList.append('m66 p3 l3 q1')
                tmpMatNum += 1
            continue
        # if a ; comment at end of line, convert line to lower case and remove spaces, preserve comment as is
        elif ';' in line:
            a,b = line.split(';', 1)
            line = '{} ({})'.format(a.strip().lower().replace(' ',''),b.replace(';','').replace('(','').replace(')',''))
        # if a () comment at end of line, convert line to lower case and remove spaces, preserve comment as is
        elif '(' in line:
            a,b = line.split('(', 1)
            line = '{} ({})'.format(a.strip().lower().replace(' ',''),b.replace('(','').replace(')',''))
        # if any other line, convert line to lower case and remove spaces
        else:
            line = line.lower().replace(' ','')
        # remove leading 0's from G & M codes
        if (line.lower().startswith('g') or \
           line.lower().startswith('m')) and \
           len(line) > 2:
            while line[1] == '0' and len(line) > 2:
                if line[2].isdigit():
                    line = line[:1] + line[2:]
                else:
                    break
        # if incremental distance mode fix overburn coordinates
        if line[:2] in ['g0', 'g1'] and distMode == 91 and (oBurnX or oBurnY):
            line = fix_overburn_incremental_coordinates(line)
        # if z motion is to be kept
        if line.startswith('#<keep-z-motion>'):
            if '(' in line:
                keepZ = int(line.split('=')[1].split('(')[0])
            else:
                keepZ = int(line.split('=')[1])
            if keepZ == 1:
                zBypass = True
            else:
                zBypass = False
            gcodeList.append(line)
            continue
        # remove any additional z max moves
        if '[#<_ini[axis_z]max_limit>' in line and zSetup:
            continue
        # set path blending
        if 'g64' in line:
            pathBlend = True
        if not pathBlend and ('g0' in line or 'g1' in line or 'm3' in line):
            blend = blendTolerance * unitMultiplier
            gcodeList.append('g64p{}'.format(blend))
            pathBlend = True
        # set initial Z height
        if not zSetup and not zBypass and ('g0' in line or 'g1' in line or 'm3' in line):
            offsetTopZ = zMaxOffset * unitsPerMm * unitMultiplier
            moveTopZ = 'g53g0z[#<_ini[axis_z]max_limit>*{}-{:.3f}] (Z just below max height)'.format(unitMultiplier, offsetTopZ)
            if not '[#<_ini[axis_z]max_limit>' in line:
                lineNum += 1
                gcodeList.append(moveTopZ)
            else:
                line = moveTopZ
            zSetup = True
        # set default units
        if 'g21' in line:
            if units == 'in':
                unitMultiplier = 25.4
                if not customDia:
                    minDiameter = 32
                if not customLen:
                    ocLength = 4
        elif 'g20' in line:
            if units == 'mm':
                unitMultiplier = 0.03937
                if not customDia:
                    minDiameter = 1.26
                if not customLen:
                    ocLength = 0.157
        # check for g41 or g42 offsets
        if 'g41' in line or 'g42' in line:
            offsetG4x = True
            if 'kerf_width-f]>' in line and unitMultiplier != 1:
                line = line.replace('#<_hal[qtplasmac.kerf_width-f]>', \
                                   '[#<_hal[qtplasmac.kerf_width-f]> * {}]'.format(unitMultiplier))
        # check for g4x offset cleared
        elif 'g40' in line:
            offsetG4x = False
        # set first movement flag
        if line[:3] in ['g0x', 'g0y', 'g1x', 'g1y'] or line[:6] in ['g53g0x', 'g53g0y', 'g53g1x', 'g53g1y']:
            firstMove = True
        # is there an m3 before we've made a movement
        if 'm3' in line and not 'm30' in line and not firstMove:
            codeError = True
            errorFirstMove.append(lineNum)
            errorLines.append(lineNumOrg)
        # are we scribing
        if line.startswith('m3$1s'):
            if pierceOnly:
                codeWarn = True
                warnPierceScribe.append(lineNum)
                errorLines.append(lineNumOrg)
                scribing = False
            else:
                scribing = True
                gcodeList.append(line)
                continue
        # if pierce only mode
        if pierceOnly:
            # Don't pierce spotting operations
            if line.startswith('m3$2'):
                spotting = True
                gcodeList.append('(Ignoring spotting operation as pierce-only is active)')
                continue
            # Ignore spotting blocks when pierceOnly
            if spotting:
                if line.startswith('m5$2'):
                    firstMove = False
                    spotting = False
                continue
            if line.startswith('g0'):
                rapidLine = line
                continue
            if line.startswith('m3') and not line.startswith('m3$1'):
                pierces += 1
                gcodeList.append('\n(Pierce #{})'.format(pierces))
                gcodeList.append(rapidLine)
                gcodeList.append('M3 $0 S1')
                gcodeList.append('G91')
                gcodeList.append('G1 X.000001')
                gcodeList.append('G90\nM5 $0')
                rapidLine = ''
                continue
            if not pierces or line.startswith('o') or line.startswith('#'):
                gcodeList.append(line)
            continue
        # test for pierce only mode
        if (line.startswith('#<pierce-only>') and line.split('=')[1][0] == '1') or (not pierceOnly and cutType == 1):
            if scribing:
                codeWarn = True
                warnPierceScribe.append(lineNum)
                errorLines.append(lineNumOrg)
            else:
                pierceOnly = True
                pierces = 0
                rapidLine = ''
                gcodeList.append(line)
            if not cutType == 1:
                continue
        if line.startswith('#<oclength>'):
            if '(' in line:
                ocLength = float(line.split('=')[1].split('(')[0])
            else:
                ocLength = float(line.split('=')[1])
            customLen = True
            gcodeList.append(line)
            continue
        # if hole sensing code
        if line.startswith('#<holes>'):
            if '(' in line:
                set_hole_type(int(line.split('=')[1].split('(')[0]))
            else:
                set_hole_type(int(line.split('=')[1]))
            gcodeList.append(line)
            continue
        # if hole diameter command
        if line.startswith('#<h_diameter>') or line.startswith('#<m_diameter>') or line.startswith('#<i_diameter>'):
            if '(' in line:
                minDiameter = float(line.split('=')[1].split('(')[0])
                customDia = True
            else:
                minDiameter = float(line.split('=')[1])
                customDia = True
            gcodeList.append(line)
            if '#<m_d' in line or '#<i_d' in line:
                codeWarn = True
                warnUnitsDep.append(lineNum)
                errorLines.append(lineNumOrg)
            continue
        # if hole velocity command
        if line.startswith('#<h_velocity>'):
            if '(' in line:
                holeVelocity = float(line.split('=')[1].split('(')[0])
            else:
                holeVelocity = float(line.split('=')[1])
            gcodeList.append(line)
            continue
        # if material change
        if line.startswith('m190'):
            do_material_change()
            if not 'm66' in line:
                continue
        # wait for material change
        if 'm66' in line:
            if offsetG4x:
                codeError = True
                errorCompMat.append(lineNum)
                errorLines.append(lineNumOrg)
            gcodeList.append(line)
            continue
        # set arc modes
        if 'g90' in line and not 'g90.' in line:
            distMode = 90 # absolute distance mode
        if 'g91' in line and not 'g91.' in line:
            distMode = 91 # incremental distance mode
        if 'g90.1' in line:
            arcDistMode = 90.1 # absolute arc distance mode
        if 'g91.1' in line:
            arcDistMode = 91.1 # incremental arc distance mode
        if not zBypass:
            # if z axis in line
            if 'z' in line and line.split('z')[1][0] in '0123456789.- [' and not '[axis_z]max_limit' in line:
                # if no other axes comment it
                if 1 not in [c in line for c in 'xybcuvw']:
                    if '(' in line:
                        gcodeList.append('({} {}'.format(line.split('(')[0], line.split('(')[1]))
                    elif ';' in line:
                        gcodeList.append('({} {}'.format(line.split(';')[0], line.split(';')[1]))
                    else:
                        gcodeList.append('({})'.format(line))
                    continue
                # other axes in line, comment out the Z axis
                if not '(z' in line:
                    if holeEnable and ('x' in line or 'y' in line):
                        lastX, lastY = set_last_coordinates(lastX, lastY)
                    result = comment_out_z_commands()
                    gcodeList.append(result)
                    continue
        # if an arc command
        if (line.startswith('g2') or line.startswith('g3')) and line[2].isalpha():
            if holeEnable and not convBlock:
                stop = False
                # check if we can read the values correctly
                if 'x' in line: stop = check_math('x')
                if 'y' in line and not stop: stop = check_math('y')
                if 'i' in line and not stop: stop = check_math('i')
                if 'j' in line and not stop: stop = check_math('j')
                if not stop:
                    check_if_hole()
            else:
                gcodeList.append(line)
            continue
        # if torch off, flag it then gcodeList.append it
        if line.startswith('m62p3') or line.startswith('m64p3'):
            torchEnable = False
            gcodeList.append(line)
            continue
        # if torch on, flag it then gcodeList.append it
        if line.startswith('m63p3') or line.startswith('m65p3'):
            torchEnable = True
            gcodeList.append(line)
            continue
        # if spindle off
        if line.startswith('m5'):
            if len(line) == 2 or (len(line) > 2 and not line[2].isdigit()):
                firstMove = False
                gcodeList.append(line)
                # restore velocity if required
                if holeActive:
                    lineNum += 1
                    gcodeList.append('m68 e3 q0 (arc complete, velocity 100%)')
                    holeActive = False
                # if torch off, allow torch on
                if not torchEnable:
                    lineNum += 1
                    gcodeList.append('m65 p3 (enable torch)')
                    torchEnable = True
            else:
                gcodeList.append(line)
            continue
        # if program end
        if line.startswith('m2') or line.startswith('m30') or line.startswith('%'):
            # restore velocity if required
            if holeActive:
                lineNum += 1
                gcodeList.append('m68 e3 q0 (arc complete, velocity 100%)')
                holeActive = False
            # if torch off, allow torch on
            if not torchEnable:
                lineNum += 1
                gcodeList.append('m65 p3 (enable torch)')
                torchEnable = True
            # restore hole sensing to default
            if holeEnable:
                lineNum += 1
                gcodeList.append('#<holes>=0 (disable hole sensing)')
                holeEnable = False
            if firstMaterial:
                RUN(['halcmd', 'setp', '{}'.format(matNumPin), '{}'.format(firstMaterial)])
            gcodeList.append(line)
            continue
        # check feed rate
        if 'f' in line:
            line = check_f_word(line)
        # restore velocity if required
        if holeActive:
            lineNum += 1
            gcodeList.append('m67 e3 q0 (arc complete, velocity 100%)')
            holeActive = False
        # set last X/Y position
        if holeEnable and len(line) and ('x' in line or 'y' in line):
            lastX, lastY = set_last_coordinates(lastX, lastY)
        gcodeList.append(line)

# for pierce only mode
if pierceOnly:
    gcodeList.append('')
    if rapidLine:
        gcodeList.append('{}'.format(rapidLine))
    gcodeList.append('M2 (END)')

# error and warning notifications
if codeError or codeWarn:
    errorText = ''
    warnText = ''
    with open(errorFile, 'w') as errFile:
        for line in errorLines:
                errFile.write('{}\n'.format(line))
    if codeError:
        print('M2 (end program)')
        if errorMath:
            msg  = 'G2 and G3 moves require explicit values if hole sensing is enabled.\n'
            errorText += message_set(errorMath, msg)
        if errorMissMat:
            msg  = 'The Material selected is missing from the material file.\n'
            errorText += message_set(errorMissMat, msg)
        if errorNoMat:
            msg  = 'A Material was not specified after M190.\n'
            errorText += message_set(errorNoMat, msg)
        if errorBadMat:
            msg  = 'An invalid Material was specified after M190 P.\n'
            errorText += message_set(errorBadMat, msg)
        if errorTempMat:
            msg  = 'Error attempting to add a temporary material.\n'
            errorText += message_set(errorTempMat, msg)
        if errorNewMat:
            msg  = 'Cannot add new material, number is in use.\n'
            errorText += message_set(errorNewMat, msg)
        if errorEditMat:
            msg  = 'Cannot add or edit material from G-Code file with invalid parameter or value.\n'
            errorText += message_set(errorEditMat, msg)
        if errorWriteMat:
            msg  = 'Error attempting to write to the material file.\n'
            errorText += message_set(errorWriteMat, msg)
        if errorReadMat:
            msg  = 'Error attempting to read from the material file.\n'
            errorText += message_set(errorReadMat, msg)
        if errorCompMat:
            msg  = 'Cannot validate a material change with cutter compensation active.\n'
            errorText += message_set(errorCompMat, msg)
        if errorFirstMove:
            msg  = 'M3 command detected before movement.\n'
            errorText += message_set(errorFirstMove, msg)
    if codeWarn:
        if warnUnitsDep:
            msg  = '<m_diameter> and #<i_diameter> are deprecated in favour of #<h_diameter>.\n'
            msg += 'The diameter will be set in the current units of the G-Code file.\n'
            warnText += message_set(warnUnitsDep, msg)
        if warnPierceScribe:
            msg  = 'Pierce only mode is invalid while scribing.\n'
            warnText += message_set(warnPierceScribe, msg)
        if warnMatLoad:
            msg  = 'Materials were not reloaded in a timely manner.\n'
            msg  = 'Try reloading the G-Code file.\n'
            warnText += message_set(warnMatLoad, msg)
        if warnHoleDir:
            msg  = 'This cut appears to be a hole, did you mean to cut it clockwise?\n'
            warnText += message_set(warnHoleDir, msg)
        if warnCompTorch:
            msg  = 'Cannot enable/disable torch with G41/G42 compensation active.\n'
            warnText += message_set(warnCompTorch, msg)
        if warnCompVel:
            msg  = 'Cannot reduce velocity with G41/G42 compensation active.\n'
            warnText += message_set(warnCompVel, msg)
        if warnFeed:
            for n in range(0, len(warnFeed)):
                msg0 = 'Line'
                msg1 = 'does not match Material'
                msg2 = 'feed rate of '
                warnText += '{} {:0.0f}: F{} {}_{}\'s {} {:0.0f}\n'.format(msg0, warnFeed[n][0], warnFeed[n][1], msg1, warnFeed[n][2], msg2, warnFeed[n][3])
        if warnChar:
            msg  = 'Invalid characters, line has been commented out.\n'
            warnText += message_set(warnChar, msg)
    dialog_box('G-Code Errors & Warnings', QStyle.SP_MessageBoxCritical, errorText, warnText)
# create empty error file if no errors
else:
    with open(errorFile, 'w') as errFile:
        pass

# output the finalised G-code
with open(filteredBkp, 'w') as outFile:
    for line in gcodeList:
        print(line)
        outFile.write('{}\n'.format(line))
    print(';qtplasmac filtered G-code file')
    outFile.write(';qtplasmac filtered G-code file')
bues.ch cgit interface