-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgcode_contour_rectangles.py
408 lines (359 loc) · 16 KB
/
gcode_contour_rectangles.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
from gcode import GCode, UnseenFormatter
import numpy as np
class GCode_Contour_Rectangle(GCode):
""" create a sharp cornerd rectangle """
def __init__ (self, cfg, version="GCode_Contour_Rectange V0.1"):
super().__init__(cfg, version)
@staticmethod
def createRectangle(self, x, y, depth, w, h, f, indent=3, helical=True):
"""create a rectant from position x/y with width and height and a depth for z of f
Args:
x ([type]): [start postion]
y ([type]): [start position]
z ([array]): [start_depth, end_depth, depth_step]
w ([type]): [width ]
h ([type]): [height]
f ([type]): [movement speed]
indent (int, optional): [description]. Defaults to 3.
"""
x = round(float(x),4)
y = round(float(y),4)
z_offset = [0,0,0,0]
z = depth[0]
if depth[1] - depth[0] <= 0.0:
z_offset = [depth[1],depth[1],depth[1],depth[1]]
elif helical and depth[1] > 0.0:
o = (depth[1]-depth[0]) / 4.0
z_offset = np.arange (start=depth[0]+o,stop=depth[1], step=o)
z_offset = np.append(z_offset, depth[1])
self.addGCode(self._cfg.GCODES['lin_move_xyzf'], args={'x':f"{x:.4f}", 'y':f"{(y+h):.4f}", 'z':f"{-z_offset[0]:.4f}", 'feed': f}, indent=indent )
self.addGCode(self._cfg.GCODES['lin_move_xyzf'], args={'x':f"{x+w:.4f}",'y':f"{(y+h):.4f}", 'z':f"{-z_offset[1]:.4f}", 'feed': f}, indent=indent )
self.addGCode(self._cfg.GCODES['lin_move_xyzf'], args={'x':f"{x+w:.4f}",'y':f"{(y):.4f}", 'z':f"{-z_offset[2]:.4f}", 'feed': f}, indent=indent )
self.addGCode(self._cfg.GCODES['lin_move_xyzf'], args={'x':f"{x:.4f}", 'y':f"{(y):.4f}", 'z':f"{-z_offset[3]:.4f}", 'feed': f}, indent=indent )
@staticmethod
def helicalRecHole(self, xy, ab, wh, td, f, depth, contour='on', dir='CW'):
"""
cut a rectangle hole with a width(w) and height(h). XY is the lower left corner to start
Args:
xy (float tuple): (x,y)
ab (float tuple): (distance from 0,0 to cp 2, only used if cp = 0)
wh ([float tuple]): [(width, height)]
td ([float]): [tool diameter]
f ([int]): [feed/speed]
cp ([int]): [center point (0,1,2)]
depth (float tuple) : (depth_total, depth_step)
contour (str, optional): [description]. Defaults to 'on'.
dir (str, optional): [description]. Defaults to 'CW'.
"""
# Step 1: starting xy position is allways lower left corner
#
if self.cp == '0' :
# cutter is on machines 0/0 position
xy[0] += ab[0]
xy[1] += ab[1]
elif self.cp == '1':
# center of rectangle
xy[0] -= (wh[0] / 2)
xy[1] -= (wh[1] / 2)
dr = self.getDepthStepRangeArray(depth)
comment = f"-- helical rectangle start --"
last_z = 0
self.addComment(comment, leadingBlank=True, endingBlank=True)
if contour == 'outside':
xy[0] -= (td / 2.0)
xy[1] -= (td / 2.0)
wh[0] += td # if we cut outside, width of rectangle is tool_diameter wider
wh[1] += td # if we cut outside, height of rectangle is tool_diameter wider
pass
elif contour == 'inside':
xy[0] += (td / 2.0) # move
xy[1] += (td / 2.0)
wh[0] -= td # if we cut outside, width of rectangle is tool_diameter smaller
wh[1] -= td # if we cut outside, height of rectangle is tool_diameter smaller
pass
else:
# 'on'
pass
pass
#
# (loop)
self.addGCode(self._cfg.GCODES['spindle_cw'])
self.addGCode(self._cfg.GCODES['spindle_speed'],args={'speed':self.speed })
self.addGCode(self._cfg.GCODES['feed_change'],args={'feed':self.rapid_move_xy })
comment = f"Start milling"
self.addComment(comment, leadingBlank=True, endingBlank=True)
#
# Go to start position
self.addGCode(self._cfg.GCODES['rapid_move_zf'], args={'z':self.z_safety, 'feed':self.rapid_move_z})
self.addGCode(self._cfg.GCODES['rapid_move_xyf'],args={'x':xy[0], 'y':xy[1], 'feed':self.rapid_move_xy})
#
# Start milling
self.addGCode(self._cfg.GCODES['lin_move_zf'],args={'z':abs(self.z_start), 'feed':self.lin_move_z})
comment = f"-- loop --"
self.addComment(comment, leadingBlank=True, endingBlank=True)
last_z = 0
self.addGCode(self._cfg.GCODES['lin_move_xyzf'],args={'x':xy[0], 'y':xy[1], 'z':-0, 'feed': self.lin_move_xy}, indent=3 )
for z in dr:
comment = f"-- depth {z} --"
self.addComment(comment, leadingBlank=True, endingBlank=True)
if self.dir == 'CW':
self.createRectangle(self, x=xy[0], y=xy[1], depth=[last_z, z, self.depth_step], w=wh[0], h=wh[1], f=f, indent=3)
last_z = z
pass
comment = f"-- endloop --"
self.addComment(comment, leadingBlank=True, endingBlank=True)
#self.createRectangle(self, x=xy[0], y=xy[1], depth=[last_z,self.depth_total, self.depth_step], w=wh[0], h=wh[1], f=f, indent=3, helical=False)
def generateGcode(self, data):
"""[summary]
{'pre_gcode': 'G90 G64 G17 G40 G49', 'post_gcode': 'G00 Z10 F100 M2',
'center_point': '1',
'unit': 'mm', 'direction': 'CCW',
'cutter_compensation': 'on', 'depth_step': '0.5', 'depth_total': '33',
'feed_g00_xy': '600', 'feed_g00_z': '400', 'feed_g01_xy': '300', 'feed_g01_z': '15',
'z_start': '3.0', 'z_safety': '15.0',
'tool_dia':'3.0',
'tool_id' : '1',
'speed' : '20000',
---- specific from project -----
'width' : '20',
'height' : '10'
}
Args:
data ([type]): [description]
"""
width = float(data['width'])
height = float(data['height'])
xy = [0,0]
(xy, tool_comp, range) = self.addStandardGCodes(
data,
comments= {
"intro" : {
"text" : 'GCode_Contour_Rectangle. Version {0} - {1}',
"args" : [
"V0.1",
"12-2021"
]
},
"c1" : {
"text" : 'Rectangle with a width of {0} and a height of {2}{1}, milling contour {3}, cutting direction {3}',
"args" : [
width,
data['unit'] ,
height,
data['cutter_compensation'],
data['direction']
]
}
}
)
# call static method, note: it's important to send current object as well to method
self.helicalRecHole( self,
xy=xy,
ab=[self.center_offset_x, self.center_offset_y],
wh=[width, height],
td=self.tool_dia,
f=self.lin_move_xy,
depth=[self.depth_total, self.depth_step],
contour=self.contour,
dir=self.dir
)
pass
pass
class GCode_Contour_RoundedRectangle(GCode):
""" create a sharp cornerd rectangle """
def __init__ (self, cfg, version="GCode_Contour_Rectange V0.1"):
super().__init__(cfg, version)
@staticmethod
def createRoundedRectangle(self, x, y, depth, w, h, r, f, indent=3, helical=True):
"""create a rectant from position x/y with width and height and a depth for z of f
Args:
x ([type]): [start postion]
y ([type]): [start position]
z ([array]): [start_depth, end_depth, depth_step]
w ([type]): [width ]
h ([type]): [height]
f ([type]): [movement speed]
indent (int, optional): [description]. Defaults to 3.
"""
x = round(float(x),4)
y = round(float(y),4)
z_offset = [0,0,0,0,0,0,0,0]
z = depth[0]
if depth[1] - depth[0] <= 0.0:
z_offset = [depth[1],depth[1],depth[1],depth[1],depth[1],depth[1],depth[1],depth[1]]
elif helical and depth[1] > 0.0:
o = (depth[1]-depth[0]) / 8.0
z_offset = np.arange (start=depth[0]+o,stop=depth[1], step=o)
z_offset = np.append(z_offset, depth[1])
# start point
self.addGCode(self._cfg.GCODES['lin_move_xyzf'], args={'x':f"{x:.4f}", 'y':f"{y:.4f}",
'z':f"{-z_offset[0]:.4f}", 'feed': f}, indent=indent )
# 1. edge lower left; positiv I<value>
x = w / 2.0
y = h / 2.0 - r
self.addGCode(self._cfg.GCODES['arc_int_cw_xyjz'], args={'x':f"{-x:.4f}", 'y':f"{-y:.4f}",
'j':f"{abs(r):.4f}", 'z':f"{-z_offset[0]:.4f}"}, indent=indent )
# 2. G01
self.addGCode(self._cfg.GCODES['lin_move_xyz'], args={'x':f"{-x:.4f}", 'y':f"{abs(y):.4f}",
'z':f"{-z_offset[1]:.4f}"}, indent=indent )
# 3. G02 edge upper left
x = w / 2.0 - r
y = h / 2.0
self.addGCode(self._cfg.GCODES['arc_int_cw_xyiz'], args={'x':f"{-x:.4f}", 'y':f"{abs(y):.4f}",
'i':f"{abs(r):.4f}", 'z':f"{-z_offset[2]:.4f}"}, indent=indent )
# 4. G01
self.addGCode(self._cfg.GCODES['lin_move_xyz'], args={'x':f"{abs(x):.4f}", 'y':f"{abs(y):.4f}",
'z':f"{-z_offset[3]:.4f}"}, indent=indent )
# 5. G02 edge upper right
x = w / 2.0
y = h / 2.0 - r
self.addGCode(self._cfg.GCODES['arc_int_cw_xyjz'], args={'x':f"{x:.4f}", 'y':f"{y:.4f}",
'j':f"{-r:.4f}", 'z':f"{-z_offset[4]:.4f}"}, indent=indent )
# 6. G01
self.addGCode(self._cfg.GCODES['lin_move_xyz'], args={'x':f"{abs(x):.4f}", 'y':f"{-y:.4f}",
'z':f"{-z_offset[5]:.4f}"}, indent=indent )
# 7. G02 edge lower right
x = w / 2 - r
y = h / 2
self.addGCode(self._cfg.GCODES['arc_int_cw_xyiz'], args={'x':f"{x:.4f}", 'y':f"{-y:.4f}",
'i':f"{-r:.4f}", 'z':f"{-z_offset[6]:.4f}"}, indent=indent )
# 8. G01
self.addGCode(self._cfg.GCODES['lin_move_xyz'], args={'x':f"{-x:.4f}", 'y':f"{-y:.4f}",
'z':f"{-z_offset[7]:.4f}"}, indent=indent )
@staticmethod
def helicalRoundedRecHole(self, xy, ab, wh, td, r, f, depth, contour='on', dir='CW'):
"""
cut a rectangle hole with a width(w) and height(h). XY is the lower left corner to start
Args:
xy (float tuple): (x,y)
ab (float tuple): (distance from 0,0 to cp 2, only used if cp = 0)
wh ([float tuple]): [(width, height)]
td ([float]): [tool diameter]
r ([float]): [edge radius]
f ([int]): [feed/speed]
cp ([int]): [center point (0,1,2)]
depth (float tuple) : (depth_total, depth_step)
contour (str, optional): [description]. Defaults to 'on'.
dir (str, optional): [description]. Defaults to 'CW'.
"""
# Step 1: starting xy position is allways lower left corner
#
# Math
# a = width, b=height, r=radius
# Example a=10, b=6, r=2.8)
# ( x= b/2-r => x = 6 / 2 = 3 - 2.8 = 0.2 )
# ( G01 X0.2 Y....)
# ( y= a/2-r => y = 10 / 2 = 5 - 2.8 = 2.2 )
# G02 X0.2 Y2.2
if self.cp == '0' :
# cutter is on machines 0/0 position
xy[0] += ab[0]
xy[1] += ab[1]
elif self.cp == '1':
# center of rectangle
xy[0] -= (wh[0] / 2) - r
xy[1] -= (wh[1] / 2)
dr = self.getDepthStepRangeArray(depth)
comment = f"-- helical rounded rectangle start --"
last_z = 0
self.addComment(comment, leadingBlank=True, endingBlank=True)
if contour == 'outside':
xy[0] -= (td / 2.0)
xy[1] -= (td / 2.0)
wh[0] += td # if we cut outside, width of rectangle is tool_diameter wider
wh[1] += td # if we cut outside, height of rectangle is tool_diameter wider
pass
elif contour == 'inside':
xy[0] += (td / 2.0) # move
xy[1] += (td / 2.0)
wh[0] -= td # if we cut outside, width of rectangle is tool_diameter smaller
wh[1] -= td # if we cut outside, height of rectangle is tool_diameter smaller
pass
else:
# 'on'
pass
pass
#
# (loop)
self.addGCode(self._cfg.GCODES['spindle_cw'])
self.addGCode(self._cfg.GCODES['spindle_speed'],args={'speed':self.speed })
self.addGCode(self._cfg.GCODES['feed_change'],args={'feed':self.rapid_move_xy })
comment = f"Start milling"
self.addComment(comment, leadingBlank=True, endingBlank=True)
#
# Go to start position
self.addGCode(self._cfg.GCODES['rapid_move_zf'], args={'z':self.z_safety, 'feed':self.rapid_move_z})
self.addGCode(self._cfg.GCODES['rapid_move_xyf'],args={'x':xy[0], 'y':xy[1], 'feed':self.rapid_move_xy})
#
# Start milling
self.addGCode(self._cfg.GCODES['lin_move_zf'],args={'z':abs(self.z_start), 'feed':self.lin_move_z})
comment = f"-- loop --"
self.addComment(comment, leadingBlank=True, endingBlank=True)
last_z = 0
for z in dr:
comment = f"-- depth {z} --"
self.addComment(comment, leadingBlank=True, endingBlank=True)
if self.dir == 'CW':
self.createRoundedRectangle(self, x=xy[0], y=xy[1], depth=[last_z, z, self.depth_step], w=wh[0], h=wh[1], r=r, f=f, indent=3)
last_z = z
pass
comment = f"-- endloop --"
self.addComment(comment, leadingBlank=True, endingBlank=True)
#self.createRectangle(self, x=xy[0], y=xy[1], depth=[last_z,self.depth_total, self.depth_step], w=wh[0], h=wh[1], f=f, indent=3, helical=False)
def generateGcode(self, data):
"""[summary]
{'pre_gcode': 'G90 G64 G17 G40 G49', 'post_gcode': 'G00 Z10 F100 M2',
'center_point': '1',
'unit': 'mm', 'direction': 'CCW',
'cutter_compensation': 'on', 'depth_step': '0.5', 'depth_total': '33',
'feed_g00_xy': '600', 'feed_g00_z': '400', 'feed_g01_xy': '300', 'feed_g01_z': '15',
'z_start': '3.0', 'z_safety': '15.0',
'tool_dia':'3.0',
'tool_id' : '1',
'speed' : '20000',
---- specific from project -----
'width' : '20',
'height' : '10'
}
Args:
data ([type]): [description]
"""
width = float(data['width'])
height = float(data['height'])
radius = float(data['radius'])
xy = [0,0]
(xy, tool_comp, range) = self.addStandardGCodes(
data,
comments= {
"intro" : {
"text" : 'GCode_Contour_RoundedRectangle. Version {0} - {1}',
"args" : [
"V0.1",
"12-2021"
]
},
"c1" : {
"text" : 'Rounded rectangle with a width of {0} and a height of {2}{1} and edge radius {3}',
"args" : [
width,
data['unit'] ,
height,
radius
]
}
}
)
# call static method, note: it's important to send current object as well to method
self.helicalRoundedRecHole( self,
xy=xy,
ab=[self.center_offset_x, self.center_offset_y],
wh=[width, height],
td=self.tool_dia,
r=radius,
f=self.lin_move_xy,
depth=[self.depth_total, self.depth_step],
contour=self.contour,
dir=self.dir
)
pass
pass