gcodeparser.py 9.69 KB
Newer Older
1
2
3
4
5
6
7
8
9
"""GCODE Parser

Parses a GCODE file and creates a model reconstruction split into layers and lines.
Currently only supports GCODE with linear moves (G0/G1) in absolute mode.

The parse_gcode() function takes a GCODE file and turns it into a Model object.
"""

import re
10
import numpy as np
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

try:
	import matplotlib.pyplot as plt
except:
	print("Matplotlib not installed")

class Model():
    """Model class for storing layer and max/min data"""
    
    def __init__(self, layers, max_x, max_y, max_z, min_x, min_y, min_z):
        """
        Parameters
        ----------
        layers: [Layer]
            List of Layer objects
        max_x: float
            Maximum x value of model
        max_y: float
            Maximum y value of model
        max_z: float
            Maximum z value of model
        min_x: float
            Minimum x value of model
        min_y: float
            Minimum y value of model
        min_z: float
            Minimum z value of model
        """
        
        self.layers = layers
        self.max_y = max_y
        self.max_x = max_x
        self.max_z = max_z
        self.min_y = min_y
        self.min_x = min_x
        self.min_z = min_z
Nicholas Gar Hei Chan's avatar
Nicholas Gar Hei Chan committed
47
        self.layer_heights = [layer.z_height for layer in layers]
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
    def to_svgs(self, dir_name):
        """Saves layers as SVG images in directory

        Parameters
        ----------
        dir_name: str
            Directory name to save images into 
        """
        
        for layer_index, layer in enumerate(self.layers):
            layer.to_svg(self.max_y, self.max_x, dir_name + "/{}.svg".format(layer_index))

class Line():
    """Line class defines a continuous extrusion in a layer"""
    
    def __init__(self):
        self.x = []
        self.y = []
        
    def append_coords(self, x, y):
        self.x.append(x)
        self.y.append(y)

    def len(self): 
        return len(self.x) - 1
    
    def is_empty(self):
        if self.len() > 0:
            return False
        else:
            return True
        
class Layer():
    """Layer class contains all lines in a layer, along with z-height info and
       functions for updating a layer and converting a layer to an SVG image."""
    
    def __init__(self, z_height):
        """
        Parameters
Nicholas Gar Hei Chan's avatar
Nicholas Gar Hei Chan committed
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
        z_height: float
            The height of the layer
        """
        
        self.lines  = []
        self.new_line()
        self.prev_e = 0
        self.prev_g = -1
        self.z_height = z_height
        
    def append_coords(self, x, y, e, g):
        """
        Appends coordinates to a line as appropriate. Checks if a command is an extrusion,
        if it is, adds the coordinates to a line. Also checks if the line is continuous,
        if it is not, starts a new line.
        
        Parameters
        ----------
        x: float
            X-coordinate of command
        y: float
            Y-coordinate of command
        e: float
            Extursion value
        g: int
            G-CODE command type (not used)
        """
        
        if e > self.prev_e:                                 # If extrusion is taking place
            self.lines[-1].append_coords(x,y)               # Append coordinate to line
            self.prev_e = e
            self.prev_g = g
        else:                                               # If no extrusion is taking place
            if not self.lines[-1].is_empty():               # And the line object is not empty
                self.new_line()                             # Start a new line
            self.lines[-1].x = [x]
            self.lines[-1].y = [y]
                
    def new_line(self):
        """Makes a new line"""
        
        self.lines.append(Line())
            
    def to_svg(self, max_height, max_width, fn):
        """
        Saves a layer as an SVG image file

        Parameters
        ----------
        max_height: float
            Maximum height of model in mm
        max_width: float
            Maximum width of model in mm
        fn: str
            Filename to save image to
        """
        
        with open(fn, "w") as f:
            f.write(('<svg xmlns="http://www.w3.org/2000/svg"'
                     ' xmlns:xlink="http://www.w3.org/1999/xlink"'
                     ' viewBox="0 0 250 40" height="{}mm" width="{}mm">\n').format(max_height, max_width))
            for line in self.lines:
                points = '\t<polyline points="'
                coords = [f"{x},{y} " for x,y in zip(line.x, line.y)]
                points = points + "".join(coords) + ('"\n\tstyle="fill:none;stroke:black;stroke-width:0.4;'
                                                     'stroke-linejoin:round;stroke-linecap:round" />\n')
                f.write(points)

            f.write('</svg>')
            
    def to_svg_inline(self, max_height, viewbox_width, viewbox_height):
        out = ('<svg xmlns="http://www.w3.org/2000/svg"'
                ' xmlns:xlink="http://www.w3.org/1999/xlink"'
                ' viewBox="0 0 {} {}" height="{}" width="100%">\n').format(viewbox_width, viewbox_height, max_height)
        for line in self.lines:
            points = '\t<polyline points="'
            coords = [f"{x},{y} " for x,y in zip(line.x, line.y)]
            points = points + "".join(coords) + ('"\n\tstyle="fill:none;stroke:black;stroke-width:0.4;'
                                                 'stroke-linejoin:round;stroke-linecap:round" />\n')
            out += points

        out += '</svg>'
        return out
    
    def plot_layer(self):
        for line in self.lines:
            plt.plot(line.x, line.y, 'xk')

    def to_csv(self, fn):
        with open(fn, "w") as f:
            for line in self.lines:
                [f.write("{},{}\n".format(x, y)) for (x,y) in zip(line.x, line.y)]
             
    def len(self):
        return len([line for line in self.lines if line.len() > 0])

    def get_points(self):
        x_pts = []
        y_pts = []

        [x_pts.extend(line.x) for line in self.lines]
        [y_pts.extend(line.y) for line in self.lines]

        return dict([('x', x_pts), ('y', y_pts), ('num_pts', len(x_pts))])
192
193
194
195
196
197
198
199

    def get_sample_points(self, method, num_samples):
        points = self.get_points()
        if method == "random":
            sample_pts = list(np.random.randint(0, points['num_pts'], num_samples))
            x_pts = [points['x'][i] for i in sample_pts]
            y_pts = [points['y'][i] for i in sample_pts]
            return dict([('x', x_pts), ('y', y_pts)])
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
def parse_gcode(filename):
    layers  = []
    num_layers = 0
    layer_heights = []
    lines  = []
    Gs     = []
    xs     = []
    ys     = []
    zs     = []
    es     = []
    prev_x = 0
    prev_y = 0
    prev_z = 0
    prev_e = 0

    with open(filename) as f:                                           # Open GCODE file
        file = f.readlines()
                    
    for line in file:                                                   # Iterate through lines
        line = line.strip()                                             # Strip newline character
        if not line or line[0] == ";":                                  # Skip empty lines and comments
            continue
        line = line.split(";")[0].strip()                               # Remove any trailing comments

        #TODO: Add handling for G2/G3 and incremental mode
        
        G_match = re.match("^(?:G0|G1)(\.\d+)?\s", line)                # Match for any linear movements
        if G_match:
            lines.append(line)

            G = re.match("G([0123])", line)                             # Match for G command
            Gs.append(int(G.group(1)))
                      
            x_loc = re.match(".*X(-?\d*\.?\d*)", line)                  # Match for X coordinates. If none, use prior
            if x_loc:
                x_loc = float(x_loc.group(1))
                prev_x = x_loc
                xs.append(x_loc)
            else:
                xs.append(prev_x)
            y_loc = re.match(".*Y(-?\d*\.?\d*)", line)                  # Match for Y coordinates. If none, use prior
            if y_loc:
                y_loc = float(y_loc.group(1))
                prev_y = y_loc
                ys.append(y_loc)
            else:
                ys.append(prev_y)

            z_loc = re.match(".*Z(-?\d*\.?\d*)", line)                  # Match for Z coordinates. If none, use prior
            if z_loc:
                z_loc = float(z_loc.group(1))
                prev_z = z_loc
                zs.append(z_loc)
            else:
                zs.append(prev_z)

            e_loc = re.match(".*E(-?\d*\.?\d*)", line)                  # Match for Z coordinates. If none, use prior
            if e_loc:
                e_loc = float(e_loc.group(1))
                prev_e = e_loc
                es.append(e_loc)
            else:
                es.append(prev_e)
                
    [layer_heights.append(height) for height in set(zs)]                # Find the unique Z heights in the GCODE file
    layer_heights.sort()                                                # Make it a sorted list

    num_layers = len(layer_heights)                                     # Number of layers
    index = dict(zip(layer_heights, range(num_layers)))                 # Map layer height to an index

    layers = [Layer(layer_heights[i]) for i in range(num_layers)]       # Initialize model list with number of layers

    for i in range(len(lines)):                                         # Iterate through each line 
        layers[index[zs[i]]].append_coords(xs[i],ys[i],es[i],Gs[i])     # Append each command into the model list

    layers = [layer for layer in layers if layer.len() > 0]
    model = Model(layers, max(xs), max(ys), max(zs), min(xs), min(ys), min(zs))
    return model