-
Notifications
You must be signed in to change notification settings - Fork 0
/
rectPocket.py
622 lines (586 loc) · 21.2 KB
/
rectPocket.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
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
ofrom math import ceil, sqrt
from dbgprt import dprt, ePrint
from geometry import ARC, CCW, CW, LINE, MAX_VALUE, MIN_DIST, \
Arc, Line, Point, newPoint, prtSeg, reverseSeg, xyDist
from math import asin, atan2, degrees, pi, sqrt
HORIZONTAL = 0
VERTICAL = 1
class RectPocket():
def __init__(self, cfg):
self.cfg = cfg
dprt("Rect loaded")
self.stepOver = 0.85
self.stepOverPercent = True
self.spiral = True
self.slotMin = 0.0
self.slotMax = MAX_VALUE
self.cmds = \
( \
('rectpocket', self.rectPocket, True), \
('rectstepover', self.setStepOver), \
('rectspiral', self.setSpiral), \
('roundedpocket', self.roundedPocket), \
('rectslot', self.rectSlot,), \
('slotRange', self.slotRange), \
('openSlot', self.openSlot,), \
# ('', self.), \
)
def setStepOver(self, args):
tmp = args[1].strip()
self.stepOverPercent = tmp.endswith('%')
if self.stepOverPercent:
tmp = tmp[:-1]
self.stepOver = self.cfg.evalFloatArg(tmp) / 100.0
else:
self.stepOver = self.cfg.evalFloatArg(tmp)
def getStepOver(self):
return self.stepOver * self.cfg.endMillSize if self.stepOverPercent \
else self.stepOver
def setSpiral(self, args):
self.spiral = self.cfg.evalBoolArg(args[1])
def slotRange(self, args):
if len(args) >= 2:
if args[1] == "*":
self.slotMin = self.drillSize
else:
self.slotMin = self.cfg.evalFloatArg(args[1])
if len(args) >= 3:
val = args[2]
if val.startswith("+-"):
val = self.cfg.evalFloatArg(val[2:])
self.slotMax = self.slotMin + val
self.slotMin -= val
else:
self.slotMax = self.cfg.evalFloatArg(args[2])
else:
self.slotMax = self.slotMin + MIN_DIST
self.slotMin -= MIN_DIST
else:
self.slotMin = 0.0
self.slotMax = MAX_VALUE
def rectPocket(self, args):
layer = args[1]
cfg = self.cfg
cfg.ncInit()
segments = cfg.dxfInput.getPath(layer)
for seg in segments:
if len(seg) != 4:
ePrint("rectPocket wrong number if sides")
continue
vert = []
horiz = []
for l in seg:
if l.lType == LINE:
if abs(l.p0[0] - l.p1[0]) < MIN_DIST:
vert.append(l)
elif abs(l.p0[1] - l.p1[1]) < MIN_DIST:
horiz.append(l)
else:
ePrint("rectPocket line not horizontal or vertical")
continue
else:
ePrint("rectPocket segment not a line")
continue
if len(vert) != 2 or len(horiz) != 2:
ePrint("rectPocket incorrect number of sides")
continue
hl0 = horiz[0]
vl0 = vert[0]
w = abs(hl0.p0[0] - hl0.p1[0]) # width
h = abs(vl0.p0[1] - vl0.p1[1]) # height
if h < w:
if (h < self.slotMin) or (h > self.slotMax):
continue
else:
if (w < self.slotMin) or (w > self.slotMax):
continue
x = min(hl0.p0[0], hl0.p1[0])
y = min(vl0.p0[1], vl0.p1[1])
self.drawRect(x, y, w, h)
stepOver = self.getStepOver()
self.path = []
self.index = 0
prev = None
offset = cfg.endMillSize / 2.0 + cfg.finishAllowance
if self.spiral:
dist = min(h, w) / 2.0 - offset
if dist < 0:
ePrint("rectPocket rectangle too small for end mill")
continue
passes = int(ceil(dist / stepOver))
stepOver = dist / passes
dprt("x %7.4f y %7.4f w %7.4f h %7.4f" % (x, y, w, h))
dprt("passes %2d stepOver %7.4f dist %7.4f" % \
(passes, stepOver, dist))
x0 = x + offset
y0 = y + offset
w -= 2.0 * offset
h -= 2.0 * offset
for i in range(passes + 1):
dprt("pass %2d x0 %7.4f y0 %7.4f w %7.4f h %7.4f" % \
(i, x0, y0, w, h))
prev = self.millRect(prev, x0, y0, w, h, cfg.dir)
x0 += stepOver
y0 += stepOver
w -= 2 * stepOver
h -= 2 * stepOver
self.millPath()
else:
if min(h, w) < cfg.endMillSize + cfg.finishAllowance * 2.0:
ePrint("rectPocket rectangle too small for end mill")
continue
dist = h - 4 * offset + (1 - self.getStepOver())
passes = int(ceil(dist / stepOver))
stepOver = dist / passes
r = cfg.endMillSize / 2.0
s = stepOver / 2.0
d = sqrt(r*r - s*s)
dprt("x %7.4f y %7.4f w %7.4f h %7.4f" % (x, y, w, h))
dprt("passes %2d offset %7.4f dist %7.4f stepOver %7.4f" \
"d %7.4f" % \
(passes, offset, dist, stepOver, d))
x0 = x + offset
y0 = y + offset
w -= 2.0 * offset
h -= 2.0 * offset
prev = self.millRect(None, x0, y0, w, h, cfg.dir)
self.drawRect(x0 + offset, y0 + offset, \
w - 2*offset, h - 2*offset)
x0 += offset + d
y0 += stepOver
w -= (offset + d) * 2
h -= offset * 2
if h > 0 and w > 0:
sign = 1
for i in range(passes):
dprt("pass %2d x0 %7.4f y0 %7.4f w %7.4f h %7.4f" % \
(i, x0, y0, w, h))
x1 = x0 + w
if sign > 0:
p0 = (x0, y0)
p1 = (x1, y0)
else:
p0 = (x1, y0)
p1 = (x0, y0)
sign = - sign
if prev is not None:
self.addLineSeg(prev, p0)
self.addLineSeg(p0, p1)
self.drawMill(p0)
self.drawMill(p1)
prev = p1
y0 += stepOver
self.millPath()
def millRect(self, prev, x0, y0, w, h, direction=CW) :
x1 = x0 + w
y1 = y0 + h
p0 = (x0, y0)
p1 = (x1, y0)
p2 = (x1, y1)
p3 = (x0, y1)
if prev is not None:
self.addLineSeg(prev, p0)
if w > 0 and h > 0:
if direction == CW:
self.addLineSeg(p0, p1)
self.addLineSeg(p1, p2)
self.addLineSeg(p2, p3)
self.addLineSeg(p3, p0)
else:
self.addLineSeg(p0, p3)
self.addLineSeg(p3, p2)
self.addLineSeg(p2, p1)
self.addLineSeg(p1, p0)
prev = p0
elif h <= 0:
self.addLineSeg(p0, p1)
prev = p1
elif w <= 0:
self.addLineSeg(p0, p3)
prev = p3
self.drawMill(p0)
self.drawMill(p1)
self.drawMill(p2)
self.drawMill(p3)
return(prev)
def addLineSeg(self, p0, p1):
self.path.append(Line(p0, p1, self.index))
self.index += 1
def addArcSeg(self, c, r, a0, a1, direction=CCW):
self.path.append(Arc(c, r, a0, a1, self.index, direction=CCW))
self.index += 1
def millPath(self):
for l in self.path:
l.prt()
dprt()
cfg = self.cfg
mp = cfg.getMillPath()
cfg.mill.last = self.path[0].p0 # start at beginning
mp.millPath(self.path, closed=False)
def drawRect(self, x, y, w, h):
draw = self.cfg.draw
if draw is not None:
x1 = x + w
y1 = y + h
p0 = (x, y)
p1 = (x1, y)
p2 = (x1, y1)
p3 = (x, y1)
draw.move(p0)
draw.line(p1)
draw.line(p2)
draw.line(p3)
draw.line(p0)
def drawMill(self, p):
draw = self.cfg.draw
if draw is not None:
draw.circle(p, self.cfg.endMillSize / 2.0)
def roundedPocket(self, args):
cfg = self.cfg
cfg.ncInit()
x = cfg.x
y = cfg.y
if len(args) >= 2:
w = float(cfg.evalFloatArg(args[1]))
if len(args) >= 3:
h = float(cfg.evalFloatArg(args[2]))
self.pocket(x, y, w, h)
def pocket(self, x, y, w, h):
cfg = self.cfg
draw = cfg.draw
self.path = []
self.index = 0
if h < w:
if (h < self.slotMin) or (h > self.slotMax):
return
r = h / 2.0
w0 = w / 2.0 - r
c0 = (x - w0, y)
c1 = (x + w0, y)
r0 = r - cfg.endMillSize / 2.0 + cfg.finishAllowance
# |<-----------w----------->|
# | p0 ----------- p3 |--
# | | | | ^
# | | +r0 | | |
# |<-r c0 -w0 x,y +w0 c1 r->| h
# | -r0 | | |
# | | | v
# p1 ----------- p2 |--
p0 = (x - w0, y + r0)
p1 = (x - w0, y - r0)
p2 = (x + w0, y - r0)
p3 = (x + w0, y + r0)
self.addArcSeg(c0, r0, 90, 270)
self.addLineSeg(p1, p2)
self.addArcSeg(c1, r0, 270, 90)
self.addLineSeg(p3, p0)
if draw is not None:
for l in self.path:
l.prt()
dprt()
draw.move(p0)
draw.arc(p1, c0)
draw.line(p2)
draw.arc(p3, c1)
draw.line(p0)
else:
if (w < self.slotMin) or (w > self.slotMax):
return
r = w / 2.0
h0 = h / 2.0 - r
c0 = (x, y - h0)
c1 = (x, y + h0)
r0 = r - cfg.endMillSize / 2.0 + cfg.finishAllowance
p0 = (x + r, y - h0)
p1 = (x + r, y + h0)
p2 = (x - r, y + h0)
p3 = (x - r, y - h0)
self.addArcSeg(c0, r, 180, 0)
self.addLineSeg(p0, p1)
self.addArcSeg(c1, r, 0, 180)
self.addLineSeg(p2, p3)
if draw is not None:
p0 = (x + r, y - h0)
p1 = (x + r, y + h0)
p2 = (x - r, y + h0)
p3 = (x - r, y - h0)
draw.move(p3)
draw.arc(p0, c0)
draw.line(p1)
draw.arc(p2, c1)
draw.line(p3)
self.millPath()
def rectSlot(self, args):
cfg = self.cfg
cfg.ncInit()
if len(args) >= 2:
layer = args[1]
segments = cfg.dxfInput.getPath(layer)
else:
allSeg = cfg.dxfInput.getOpenPath()
segments = []
for seg in allSeg:
if len(seg) <= 2:
continue
d = xyDist(seg[0].p0, seg[-1].p1)
if (d < MIN_DIST) and (len(seg) == 4):
segments.append(seg)
dprt("len %2d d %6.3f" % (len(seg), d))
for l in seg:
l.prt()
l.draw()
l.label()
dprt()
millSeg = []
for seg in segments:
if len(seg) != 4:
ePrint("rectPocket wrong number of sides")
continue
vert = []
horiz = []
arc = []
for l in seg:
l.prt()
if l.lType == LINE:
if abs(l.p0[0] - l.p1[0]) < MIN_DIST:
vert.append(l)
elif abs(l.p0[1] - l.p1[1]) < MIN_DIST:
horiz.append(l)
elif l.lType == ARC:
arc.append(l)
dprt()
if len(arc) == 2:
if abs(arc[0].r - cfg.endMillSize / 2.0) < MIN_DIST:
path = []
path.append(Line(arc[0].c, arc[1].c))
millSeg.append(path)
else:
a0 = arc[0]
a1 = arc[1]
if len(vert) == 2:
x = a0.c.x
if a0.c.y > a1.c.y:
(a0, a1) = (a1, a0)
y = a0.c.y + (a1.c.y - a0.c.y) / 2
w = abs(a0.p0.x - a0.p0.x)
h = abs(a0.c.y - a1.c.y)
self.pocket(x, y, w, h)
elif len(horiz) == 2:
if a0.c.x > a1.c.y:
(a0, a1) = (a1, a0)
x = a0.c.x + (a1.c.x - a0.c.x) / 2
y = a0.c.y
w = abs(a0.c.x - a1.c.x) + 2 * a0.r
h = abs(a0.p0.y - a0.p1.y)
self.pocket(x, y, w, h)
last = cfg.mill.last
mp = cfg.getMillPath()
while len(millSeg):
minDist = MAX_VALUE
index = None
for i, path in enumerate(millSeg):
dist0 = xyDist(last, path[0].p0)
dist1 = xyDist(last, path[-1].p1)
if dist0 < dist1:
dist = dist0
swap = False
else:
dist = dist1
swap = True
if dist < minDist:
minDist = dist
minSwap = swap
index = i
path = millSeg.pop(index)
if swap:
path = reverseSeg(path, False)
mp.millPath(path, closed=False)
def openSlot(self, args):
layer = args[1]
cfg = self.cfg
cfg.ncInit()
segments = cfg.dxfInput.getPath(layer)
for seg in segments:
vert = []
horiz = []
for l in seg:
if l.lType == LINE:
if abs(l.p0.x - l.p1.x) < MIN_DIST:
vert.append(l)
elif abs(l.p0.y - l.p1.y) < MIN_DIST:
horiz.append(l)
else:
pass
else:
pass
if (len(horiz) == 2) and (len(vert) == 2):
dxf = cfg.dxfInput
xMin = dxf.xMin
xMax = dxf.xMax
direction = None
match = 0
ySum = 0.0
for l in horiz:
p0 = l.p0
p1 = l.p1
ySum += p0.y
if p0.x > p1.x:
(p0, p1) = (p1, p0)
if abs(p0.x - xMin) < MIN_DIST and \
abs(p1.x - xMax) < MIN_DIST:
match += 1
if match == 2:
direction = HORIZONTAL
l = horiz[0]
slotLen = abs(l.p1.x - l.p0.x)
slotWidth = abs(horiz[1].p0.y - l.p0.y)
offset = (xMin, ySum / 2)
rotateAngle = 0
else:
yMin = dxf.yMin
yMax = dxf.yMax
xSum = 0.0
for l in vert:
p0 = l.p0
p1 = l.p1
xSum += p0.x
if p0.y > p1.y:
(p0, p1) = (p1.y, p0.y)
if abs(p0.y - yMin) < MIN_DIST and \
abs(p1.y - yMax) < MIN_DIST:
match += 1
if match == 2:
direction = VERTICAL
l = vert[0]
slotLen = abs(l.p1.x - l.p0.x)
slotWidth = abs(vert[1].p0.y - l.p0.y)
offset = (xSum / 2, yMin)
rotateAngle = pi / 2.0
else:
continue
else:
continue
r = cfg.endMillSize / 2.0
h = slotWidth / 2.0 - r - cfg.finishAllowance
stepOver = self.getStepOver()
millDist = slotLen + r
passes = int(ceil(millDist / stepOver))
stepOver = millDist / passes
path = self.path2(r, h, stepOver, passes, CCW)
for l in path:
if rotateAngle != 0:
l.rotate(Point(0, 0), rotateAngle)
l.offset(offset)
cfg.mill.last = path[0].p0 # start at beginning
mp = cfg.getMillPath()
mp.millPath(path, closed=False)
def path0(self, r, h, stepOver, passes, direction=None):
x = -r
y = h
path = []
for i in range(passes):
if (i & 1) == 0:
c = Point(x, y - stepOver)
l = Arc(c, stepOver, 0, 90, direction=CW)
path.append(l)
p1 = l.p1
x = p1.x
y = -h
l = Line(p1, Point(x, y))
path.append(l)
else:
c = Point(x, y + stepOver)
l = Arc(c, stepOver, 270, 0, direction=CCW)
path.append(l)
p1 = l.p1
x = p1.x
y = h
l = Line(p1, Point(x, y))
path.append(l)
return path
def path1(self, r, h, stepOver, passes, direction=CW):
r0 = stepOver # milling arc radius
r1 = r0 / 3 # return radius
a = degrees(pi / 2 + 2 * atan2(r0 - r1, r0))
x = -r
y = 0
c0 = Point(x, y)
path = []
if direction == CW:
arc0 = Arc(c0, r0, 270, 90, direction=CW)
for i in range(passes):
path.append(arc0)
c1 = Point(x, arc0.p1.y + r1)
arc1 = Arc(c1, r1, a, 270, direction=CW)
path.append(arc1)
x += r0
c0 = Point(x, y)
arc0 = Arc(c0, r0, 270, a, direction=CW)
l = Line(arc1.p1, arc0.p0)
path.append(l)
else:
a = 360 - a
arc0 = Arc(c0, r0, 270, 90, direction=CCW)
for i in range(passes):
path.append(arc0)
c1 = Point(x, arc0.p1.y - r1)
arc1 = Arc(c1, r1, 90, a, direction=CCW)
path.append(arc1)
x += r0
c0 = Point(x, y)
arc0 = Arc(c0, r0, a, 90, direction=CCW)
l = Line(arc1.p1, arc0.p0)
path.append(l)
return path
def path2(self, r, h, stepOver, passes, direction=CW):
r0 = stepOver # milling arc radius
r1 = r0 / 3 # return radius
centerVDist = 2 * h - (r0 + r1)
centerDist = sqrt(centerVDist * centerVDist + r0 * r0)
a0 = atan2(centerVDist, r0)
a1 = asin((r0 - r1) / centerDist)
a = degrees(pi / 2 + a0 + a1)
h0 = h - r0
path = []
x = -r
y = 0
if direction == CW:
c0 = Point(x, h0)
arc0 = Arc(c0, r0, 0, 90, direction=CW)
for i in range(passes):
path.append(arc0)
p0 = arc0.p1
l = Line(p0, Point(p0.x, -h0))
path.append(l)
c1 = Point(x, -h0)
l = Arc(c1, r0, 270, 0, direction=CW)
path.append(l)
c2 = Point(x, -h0 - r0 + r1)
arc1 = Arc(c2, r1, a, 270, direction=CW)
path.append(arc1)
x += r0
c0 = Point(x, h0)
arc0 = Arc(c0, r0, 0, a, direction=CW)
l = Line(arc1.p1, arc0.p0)
path.append(l)
else:
a = 360 - a
c0 = Point(x, -h0)
arc0 = Arc(c0, r0, 270, 0, direction=CCW)
for i in range(passes):
path.append(arc0)
p0 = arc0.p1
l = Line(p0, Point(p0.x, h0))
path.append(l)
c1 = Point(x, h0)
l = Arc(c1, r0, 0, 90, direction=CCW)
path.append(l)
c2 = Point(x, h0 + r0 - r1)
arc1 = Arc(c2, r1, 90, a, direction=CCW)
path.append(arc1)
x += r0
c0 = Point(x, -h0)
arc0 = Arc(c0, r0, a, 0, direction=CCW)
l = Line(arc1.p1, arc0.p0)
path.append(l)
return path