File: run_from_line.py

package info (click to toggle)
linuxcnc 1%3A2.9.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 285,604 kB
  • sloc: python: 202,568; ansic: 109,036; cpp: 99,239; tcl: 16,054; xml: 10,631; sh: 10,303; makefile: 1,255; javascript: 138; sql: 72; asm: 15
file content (348 lines) | stat: -rw-r--r-- 14,557 bytes parent folder | download | duplicates (2)
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
'''
run_from_line.py

Copyright (C) 2019-2024  Phillip A Carter
Copyright (C) 2020-2024  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 math

def run_from_line_get(file, startLine):
    preData,postData,newData,params,material = [],[],[],[],[]
    codes = {'g2_':'','g4_':'','g6_':'','g9_':'','g9arc':'','d3':'','d2':'','a3':'','x1':'','y1':'','x2':'','y2':''}
    codes['last'] = {'feed':'', 'code':''}
    codes['move'] = {'isSet':False, 'isG00':False}
    codes['spindle'] = {'code':False, 'line':None}
    oSub = []
    cutComp = False
    count = 0
    with open(file, 'r') as inFile:
        for line in inFile:
            # code before selected line
            if count < startLine:
                preData.append(line)
            # remaining code
            else:
                if count == startLine:
                    if 'G21' in line:
                        newData.append('G21')
                    elif 'G20' in line:
                        newData.append('G20')
                # find the type of first move
                    ''' IT IS POSSIBLE THERE MAY BE SPACES IN THE INCOMING LINE '''
                if not codes['move']['isSet'] and not 'G53G0' in line.replace(' ','') and not 'G20' in line and not 'G21' in line:
                    if 'G00' in line:
                        codes['move']['isSet'] = True
                        codes['move']['isG00'] = True
                        codes['x2'] = get_rfl_pos(line.strip(), codes['x2'], 'X')
                        codes['y2'] = get_rfl_pos(line.strip(), codes['y2'], 'Y')
                    if 'G01' in line or 'G02' in line or 'G03' in line:
                        codes['move']['isSet'] = True
                        codes['move']['isG00'] = False
                        codes['x2'] = get_rfl_pos(line.strip(), codes['x2'], 'X')
                        codes['y2'] = get_rfl_pos(line.strip(), codes['y2'], 'Y')
                    if 'm03' in line:
                        if not codes['spindle']['line']:
                            codes['spindle']['line'] = line.strip()
                            continue
                        codes['spindle']['line'] = line.strip()
                postData.append(line)
            count += 1
    # read all lines before selected line to get last used codes
    for line in preData:
        if line.startswith('('):
            if line.startswith('(o='):
                material = [line.strip()]
            continue
        elif line.startswith('M190'):
            material.append(line.strip())
            continue
        elif line.replace(' ','').startswith('M66P3'):
            material.append(line.strip())
            continue
        elif line.startswith('#'):
            params.append(line.strip())
            continue
        for t1 in ['G20','G21','G40','G41.1','G42.1','G61','G61.1','G64','G90','G90.1','G91','G91.1']:
            if t1 in line:
                if t1[1] == '2':
                    codes['g2_'] = t1
                elif t1[1] == '4':
                    codes['g4_'] = t1
                    if t1 != 'G40':
                        cutComp = True
                    else:
                        cutComp = False
                elif t1[1] == '6':
                    codes['g6_'] = t1 + line.split(t1)[1]
                elif t1 == 'G90' and not 'G90.1' in line:
                    codes['g9_'] = 'G90'
                elif t1 == 'G91' and not 'G91.1' in line:
                    codes['g9_'] = 'G91'
                elif t1 == 'G90.1' in line:
                    codes['g9arc'] = 'G90.1'
                elif t1 == 'G91.1' in line:
                    codes['g9arc'] = 'G91.1'
        if 'G00' in line and not 'G53g00' in line:
            codes['last']['code'] = 'G0'
        if 'G01' in line:
            tmp = line.split('G01')[1]
            if tmp[0] not in '0123456789':
                codes['last']['code'] = 'G01'
        if 'G02' in line:
            tmp = line.split('G02')[1]
            if tmp[0] not in '0123456789':
                codes['last']['code'] = 'G02'
        if 'G03' in line:
            tmp = line.split('G03')[1]
            if tmp[0] not in '0123456789':
                codes['last']['code'] = 'G03'
        if 'X' in line:
            codes['x1'] = get_rfl_pos(line.strip(), codes['x1'], 'X')
        if 'Y' in line:
            codes['y1'] = get_rfl_pos(line.strip(), codes['y1'], 'Y')
        if 'M03$' in line.replace(' ','') and not codes['spindle']['line']:
            codes['spindle']['line'] = line.strip()
        if 'M62P3' in line.replace(' ',''):
            codes['d3'] = 'M62 P3 (Disable Torch)'
        elif 'M63P3' in line.replace(' ',''):
            codes['d3'] = 'M63 P3 (Enable Torch)'
        elif 'M64P3' in line.replace(' ',''):
            codes['d3'] = 'M64 P3 (Disable Torch)'
        elif 'M65P3' in line.replace(' ',''):
            codes['d3'] = 'M65 P3 (Enable Torch)'
        if 'M62P2' in line.replace(' ',''):
            codes['d2'] = 'M62 P2 (Disable THC)'
        elif 'M63P2' in line.replace(' ',''):
            codes['d2'] = 'M63 P2 (Enable THC)'
        elif 'M64P2' in line.replace(' ',''):
            codes['d2'] = 'M64 P2 (Disable THC)'
        elif 'M65P2' in line.replace(' ',''):
            codes['d2'] = 'M65 P2 (Enable THC)'
        if 'M67E3Q' in line.replace(' ',''):
            codes['a3'] = 'M67 E3 Q'
            tmp = line.replace(' ','').split('M67E3Q')[1]
            while 1:
                if tmp[0] in '-.0123456789':
                    codes['a3'] += tmp[0]
                    tmp = tmp[1:]
                else:
                    break
            pc = float(codes['a3'].split('M67 E3 Q')[1])
            pc = pc if pc > 0 else 100
            codes['a3'] += ' (Velocity {}%)'.format(pc)
        if 'M68E3Q' in line.replace(' ',''):
            codes['a3'] = 'M68 E3 Q'
            tmp = line.replace(' ','').split('M68E3Q')[1]
            while 1:
                if tmp[0] in '-.0123456789':
                    codes['a3'] += tmp[0]
                    tmp = tmp[1:]
                else:
                    break
            pc = float(codes['a3'].split('M68 E3 Q')[1])
            pc = pc if pc > 0 else 100
            codes['a3'] += ' (Velocity {}%)'.format(pc)
        # test if inside a subroutine
        if line.startswith('O'):
            if 'END' in line:
                oSub = False
            else:
                if line[1] == '<':
                    os = 'O<'
                    tmp = line.replace(' ','').split('O<')[1]
                    while 1:
                        if tmp[0] != '>':
                            os += tmp[0]
                            tmp = tmp[1:]
                        else:
                            break
                    oSub.append('{}>'.format(os))
                else:
                    os = 'O'
                    tmp = line.replace(' ','').split('O')[1]
                    while 1:
                        if tmp[0] in '0123456789':
                            os += tmp[0]
                            tmp = tmp[1:]
                        else:
                            break
                    oSub.append(os)
        if '#<_hal[plasmac.cut-feed-rate]>' in line:
            codes['last']['feed'] = line.strip()
    # return an error line within a subroutine or if using cutter compensation
    if cutComp or oSub:
        return {'error':True, 'compError':cutComp, 'subError':oSub}
    # else return all data
    return {'error':False, 'codes':codes, 'params':params, 'material':material, 'postData':postData, 'newData':newData}

def run_from_line_set(rflFile, data, leadin, unitsPerMm):
    error = False
    # add all the codes retrieved from before the start line
    for param in data['params']:
        if param:
            data['newData'].append(param)
    scale = 1
    zMax = ''
    if unitsPerMm == 1:
        if data['codes']['g2_'] == 'G20':
            scale = 0.03937
            zMax = 'G53 G00 Z[[#<_ini[axis_z]max_limit> - 5] * 0.03937]'
        else:
            zMax = 'G53 G00 Z[#<_ini[axis_z]max_limit> - 5]'
    else:
        if data['codes']['g2_'] == 'G21':
            scale = 25.4
            zMax = 'G53 G00 Z[[#<_ini[axis_z]max_limit> * 25.4] - 5]'
        else:
            zMax = 'G53 G00 Z[#<_ini[axis_z]max_limit> - 0.02]'
    if data['codes']['g2_']:
        data['newData'].append(data['codes']['g2_'])
    if data['codes']['g4_']:
        data['newData'].append(data['codes']['g4_'])
    if data['codes']['g6_']:
        data['newData'].append(data['codes']['g6_'])
    if data['codes']['g9_']:
        data['newData'].append(data['codes']['g9_'])
    if data['codes']['g9arc']:
        data['newData'].append(data['codes']['g9arc'])
    data['newData'].append('M52 P1')
    if data['codes']['d3']:
        data['newData'].append(data['codes']['d3'])
    if data['codes']['d2']:
        data['newData'].append(data['codes']['d2'])
    if data['codes']['a3']:
        data['newData'].append(data['codes']['a3'])
    if zMax:
        data['newData'].append(zMax)
    if data['material']:
        for line in data['material']:
            data['newData'].append(line)
    if data['codes']['last']['feed']:
        data['newData'].append(data['codes']['last']['feed'])
    # if g00 is not the first motion command after selected line set x and y coordinates
    if not data['codes']['move']['isG00']:
        if leadin['do']:
            error, xL, yL = set_leadin_coordinates(data['codes']['x1'], data['codes']['y1'], scale, leadin['length'], leadin['angle'])
        else:
            xL = data['codes']['x1']
            yL = data['codes']['y1']
        data['codes']['spindle']['code'] = set_spindle_start(xL, yL, data['codes']['x1'], data['codes']['y1'], data['codes']['spindle']['line'], data['newData'], False)
    # if no spindle command yet then find the next one for the correct tool
    if not data['codes']['spindle']['line']:
        for line in data['postData']:
            if 'M3$' in line.replace(' ',''):
                data['codes']['spindle']['line'] = line.strip()
                break
    # add all the code from the selected line to the end
    for line in data['postData']:
        # if we have the first spindle code we don't need it again
        if 'M03$' in line.replace(' ','') and data['codes']['spindle']['code']:
            data['codes']['spindle']['code'] = False
            continue
        # if G00 is the first motion command after the selected line
        if data['codes']['move']['isG00']:
            # if G0 is the current motion command
            if 'G00' in line:
                # no need to process a G53G0 command]
                if 'G53G00' in line or 'G20' in line or 'G21' in line:
                    data['newData'].append(line.strip())
                    continue
                if leadin['do']:
                    error, xL, yL = set_leadin_coordinates(data['codes']['x2'], data['codes']['y2'], scale, leadin['length'], leadin['angle'])
                else:
                    xL = data['codes']['x2']
                    yL = data['codes']['y2']
                data['codes']['spindle']['code'] = set_spindle_start(xL, yL, data['codes']['x2'], data['codes']['y2'], data['codes']['spindle']['line'], data['newData'], True)
                # no need to process any more G00 commands
                data['codes']['move']['isG00'] = False
                continue
        data['newData'].append(line.strip())
    # create the rfl file
    with open(rflFile, 'w') as outFile:
        for line in data['newData']:
            outFile.write('{}\n'.format(line))
    return {'error':error}

def set_leadin_coordinates(x, y, scale, length, angle):
    xL = x
    yL = y
    try:
        if x[-1] == ']':
            xL = '{}[[{}]+{:0.6f}]'.format(x[:1], x[1:], (length * scale) * math.cos(math.radians(angle)))
            yL = '{}[[{}]+{:0.6f}]'.format(y[:1], y[1:], (length * scale) * math.sin(math.radians(angle)))
        else:
            xL = float(x) + ((length * scale) * math.cos(math.radians(angle)))
            yL = float(y) + ((length * scale) * math.sin(math.radians(angle)))
    except:
        return(True, x, y)
    return(False, xL, yL)

def set_spindle_start(xL, yL, x, y, line, newData, reply):
    leadIn = {}
    if xL != x and yL != y:
        newData.append('G00 X{} Y{}'.format(xL, yL))
        leadIn['x'] = x
        leadIn['y'] = y
    else:
        if x and y:
            newData.append('G00 X{} Y{}'.format(x, y))
        elif x:
            newData.append('G00 X{}'.format(x))
        elif y:
            newData.append('G00 Y{}'.format(y))
    if line:
        newData.append(line)
    if leadIn:
        newData.append('G01 X{} Y{} (leadin)'.format(leadIn['x'], leadIn['y']))
    return reply

def get_rfl_pos(line, axisPos, axisLetter):
    maths = 0
    pos = ''
    done = False
    if line.startswith('(') or line.startswith(';'):
        return pos if pos else axisPos
    while len(line):
        if line[0] == ('('):
            break
        if not line[0] == axisLetter:
            line = line[1:]
        else:
            while 1:
                line = line[1:]
                if line[0] in '-.0123456789#':
                    pos += line[0]
                elif line[0] == '[' or line[0] == '<':
                    pos += line[0]
                    maths += 1
                elif (line[0] == ']' or line[0] == '>') and maths > 0:
                    pos += line[0]
                    maths -= 1
                elif maths:
                    pos += line[0]
                elif (pos and not maths) or line[0] == '(':
                    done = True
                    break
                else:
                    if len(line) == 1: break
                    break
                if len(line) == 1:
                    break
        if done:
            break
    return pos if pos else axisPos