-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgcode.py
More file actions
234 lines (183 loc) · 6.64 KB
/
gcode.py
File metadata and controls
234 lines (183 loc) · 6.64 KB
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
import re, sys, warnings
class Line(object):
def __init__(self, line='', code=None, args={}, comment=None):
"""Parse a single line of gcode into its code and named
arguments."""
self.line = line
self.code = code
self.args = args
self.comment = comment
if args or code:
if not (args and code):
raise ValueError("Both code and args must be specified")
else:
#Check for comment-only lines
if re.match(r'\s*;', line):
self.comment = line[line.index(';')+1:]
else:
#Extract the comment if there is one
lc = self.line.split(';', 1)
if len(lc) > 1:
self.line, self.comment = lc
#Get the actual code and the arguments
args = self.line.split()
self.code = args[0]
self.args = {}
if self.code == 'M117':
self.args[None] = self.line.split(None, 1)[1]
else:
for arg in args[1:]:
if re.match('[A-Za-z]', arg[0]):
if arg[1:] is not None and arg[1:] != '':
try:
self.args[arg[0]] = float(arg[1:]) if '.' in arg[1:] else int(arg[1:])
except ValueError:
sys.stderr.write("Line: %s\n" % line)
raise
else:
self.args[arg[0]] = None
else:
self.args[None] = arg
def __repr__(self):
return self.construct()
return '%s: %s' % (self.code, repr(self.args))
def construct(self):
"""Construct and return a line of gcode based on self.code and
self.args."""
if not self.code:
return ';%s' % self.comment
return ' '.join([self.code] +
['%s%s' % (k if k is not None else '', v if v is not None else '')
for k,v in self.args.iteritems()]) +\
(' ;%s' % self.comment if self.comment else '')
class Layer(object):
def __init__(self, lines=[], layernum=None):
"""Parse a layer of gcode line-by-line, making Line objects."""
self.layernum = layernum
self.preamble = []
self.lines = [Line(l) for l in lines if l]
self.postamble = []
def __repr__(self):
return '<Layer %s at Z=%s; corners: (%d, %d), (%d, %d); %d lines>' % (
(self.layernum, self.z()) + self.extents() + (len(self.lines),))
def extents(self):
"""Return the extents of the layer: the min/max in x and y that
occur. Note this does not take arcs into account."""
min_x = min(self.lines, key=lambda l: l.args.get('X', float('inf'))).args['X']
min_y = min(self.lines, key=lambda l: l.args.get('Y', float('inf'))).args['Y']
max_x = max(self.lines, key=lambda l: l.args.get('X', float('-inf'))).args['X']
max_y = max(self.lines, key=lambda l: l.args.get('Y', float('-inf'))).args['Y']
return min_x, min_y, max_x, max_y
def extents_gcode(self):
"""Return two Lines of gcode that move to the extents."""
min_x, min_y, max_x, max_y = self.extents()
return Line(code='G0', args={'X': min_x, 'Y': min_y}),\
Line(code='G0', args={'X': max_x, 'Y': max_y})
def z(self):
"""Return the first Z height found for this layer. It should be
the only Z unless it's been messed with, so returning the first is
safe."""
for l in self.lines:
if 'Z' in l.args:
return l.args['Z']
def set_preamble(self, gcodestr):
"""Insert lines of gcode at the beginning of the layer."""
self.preamble = [Line(l) for l in gcodestr.split('\n')]
def set_postamble(self, gcodestr):
"""Add lines of gcode at the end of the layer."""
self.postamble = [Line(l) for l in gcodestr.split('\n')]
def find(self, code):
"""Return all lines in this layer matching the given G code."""
return [line for line in self.lines if line.code == code]
def shift(self, **kwargs):
"""Shift this layer by the given amount, applied to the given
args. Operates by going through every line of gcode for this layer
and adding amount to each given arg, if it exists, otherwise
ignoring."""
for line in self.lines:
for arg in kwargs:
if arg in line.args:
line.args[arg] += kwargs[arg]
def multiply(self, **kwargs):
"""Same as shift but with multiplication instead."""
for line in self.lines:
for arg in kwargs:
if arg in line.args:
line.args[arg] *= kwargs[arg]
def construct(self):
"""Construct and return a gcode string."""
return '\n'.join(l.construct() for l in self.preamble + self.lines
+ self.postamble)
class Gcode(object):
def __init__(self, filename=None, filestring=''):
"""Parse a file's worth of gcode passed as a string. Example:
g = Gcode(open('mycode.gcode').read())"""
self.preamble = None
self.layers = []
if filename:
if filestring:
warnings.warn("Ignoring passed filestring in favor of loading file.")
filestring = open(filename).read()
self.parse(filestring)
def __repr__(self):
return '<Gcode with %d layers>' % len(self.layers)
def construct(self, outfile=None):
"""Construct all and return of the gcode. If outfile is given,
write the gcode to the file instead of returning it."""
s = (self.preamble.construct() + '\n') if self.preamble else ''
for i,layer in enumerate(self.layers):
s += ';LAYER:%d\n' % i
s += layer.construct()
s += '\n'
if outfile:
with open(outfile, 'w') as f:
f.write(s)
else:
return s
def shift(self, layernum=0, **kwargs):
"""Shift given layer and all following. Provide arguments and
amount as kwargs. Example: shift(17, X=-5) shifts layer 17 and all
following by -5 in the X direction."""
for layer in self.layers[layernum:]:
layer.shift(**kwargs)
def multiply(self, layernum=0, **kwargs):
"""The same as shift() but multiply the given argument by a
factor."""
for layer in self.layers[layernum:]:
layer.multiply(**kwargs)
def parse(self, filestring):
"""Parse the gcode."""
if not filestring:
return
in_preamble = True
#Cura and Simplify3D nicely add a "LAYER" comment just before each layer
if re.search(';\s*layer', filestring, re.I): in filestring:
#Split into layers
splits = re.split(r'^;LAYER:\d+\n', filestring, flags=re.M)
self.preamble = Layer(splits[0].split('\n'), layernum=0)
self.layers = [Layer(l.split('\n'), layernum=i) for i,l in
enumerate(splits[1:])]
#Sliced with Slic3r, so no LAYER comments; we have to look for
# G0 or G1 commands with a Z in them
else:
layernum = 1
curr_layer = []
for l in filestring.split('\n'):
#Looks like a layer change because we have a Z
if re.match(r'G[01]\s.*Z-?\.?\d+', l):
if in_preamble:
self.preamble = Layer(curr_layer, layernum=0)
in_preamble = False
else:
self.layers.append(Layer(curr_layer, layernum=layernum))
layernum =+ 1
curr_layer = [l]
#Not a layer change so add it to the current layer
else:
curr_layer.append(l)
self.layers.append(Layer(curr_layer))
if __name__ == "__main__":
if sys.argv[1:]:
g = Gcode(sys.argv[1])
print g
print g.layers