-
Notifications
You must be signed in to change notification settings - Fork 0
/
pocket.py
549 lines (511 loc) · 19.2 KB
/
pocket.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
import sys
from math import acos, ceil, cos, degrees, pi, radians, sin
import pyclipper
from pyclipper import PyclipperOffset
from dbgprt import dprt, dprtSet
from geometry import (ARC, LINE, MAX_VALUE, Arc, Line, calcAngle, degAtan2,
fix, orientation, oStr, pathDir, xyDist)
SCALE = 1000000.0
NO_SYMMETRY = 0
UPPER = 1
LOWER = 2
RIGHT = 3
LEFT = 4
def floatScale(p):
return(float(p[0]) / SCALE, float(p[1]) / SCALE)
def intScale(p):
return(int(p[0] * SCALE), int(p[1] * SCALE))
class pocket():
def __init__(self, cfg):
self.cfg = cfg
print("test loaded")
self.stepOver = 0.85
self.arcs = False
self.dbg = False
self.symmetry = NO_SYMMETRY
self.symmetryValues = \
( \
('none', NO_SYMMETRY), \
('upper', UPPER), \
('lower', LOWER), \
('right', RIGHT), \
('left', LEFT), \
)
self.cmds = \
( \
('pocket', self.pocket, True), \
('stepover', self.setStepOver), \
('pocketarcs', self.pocketArcs), \
('pocketdbg', self.pocketDbg), \
('outside', self.outside), \
('symmetry', self.setSymmetry), \
# ('', self.), \
)
dprtSet(True)
def setStepOver(self, args):
self.stepOver = self.cfg.evalFloatArg(args[1]) / 100.0
def pocketArcs(self, args):
self.arcs = self.cfg.evalBoolArg(args[1])
def pocketDbg(self, args):
self.dbg = self.cfg.evalBoolArg(args[1])
def setSymmetry(self, args):
val = args[1].lower()
for (x, i) in self.symmetryValues:
if val == x:
self.symmetry = i
break
def pocket(self, args, dbg=None):
if dbg is None:
dbg = self.dbg
layer = args[1]
cfg = self.cfg
# dir = CCW
# if cfg.dir is not None and cfg.dir == 'CW':
# dir = CW
stepOver = cfg.endMillSize * self.stepOver
cfg.ncInit()
segments = cfg.dxfInput.getPath(layer)
pco = PyclipperOffset()
mp = cfg.getMillPath()
self.last = cfg.mill.last
for seg in segments:
self.pDir = pathDir(seg)
pco.Clear()
mainPath = []
for (i, l) in enumerate(seg):
l.draw()
if dbg:
l.prt()
if l.lType == LINE:
mainPath.append(intScale(l.p0))
elif l.lType == ARC:
self.arcLines(mainPath, l, dbg=dbg)
if dbg:
dprt()
for (i, p) in enumerate(mainPath):
(x, y) = floatScale(p)
dprt("%3d (%7.4f %7.4f)" % (i, x, y))
dprt()
pco.AddPath(mainPath, pyclipper.JT_ROUND, \
pyclipper.ET_CLOSEDPOLYGON)
offset = cfg.endMillSize / 2.0 + cfg.finishAllowance
step = 0
offsetPaths = []
while True:
result = pco.Execute(-int(offset * SCALE))
if dbg:
dprt("%2d offset %7.4f results %d" % \
(step, offset, len(result)))
if len(result) == 0:
break
rData = []
for (rNum, r) in enumerate(result):
# convert from list of points to lines
if self.arcs:
pLast = floatScale(r[-1])
index = 0
maxDist = -1
for (i, p) in enumerate(r):
p = floatScale(p)
dist = xyDist(p, pLast)
if dbg:
dprt("%3d (%7.4f %7.4f) dist %9.6f" % \
(i, p[0], p[1], dist))
if dist > maxDist:
maxDist = dist
index = i
r[i] = p
pLast = p
r = r[index:] + r[:index]
if dbg:
dprt("index %d maxDist %7.4f" % (index, maxDist))
pLast = r[-1]
dprt("pLast (%7.4f %7.4f)" % (pLast[0], pLast[1]))
for (i, p) in enumerate(r):
dprt("%3d (%7.4f %7.4f) dist %9.6f" % \
(i, p[0], p[1], xyDist(p, pLast)))
pLast = p
dprt()
path = self.makePath(r, step, rNum, dbg)
else:
pLast = floatScale(r[-1])
path = []
for (i, p) in enumerate(r):
p = floatScale(p)
if dbg:
dprt("%3d (%7.4f %7.4f)" % (i, p[0], p[1]))
l = Line(pLast, p, i)
txt = "s %d r %d i %d" % (step, rNum, i)
l.label(txt)
path.append(l)
pLast = p
result[rNum] = path
if dbg:
dprt()
for l in path:
l.prt()
dprt()
# find shortest distance to each path
oData = []
for (oNum, oPath) in enumerate(offsetPaths):
lEnd = oPath[-1]
pEnd = lEnd.p1
minDist = MAX_VALUE
index = None
for (i, l) in enumerate(path):
dist = xyDist(pEnd, l.p0)
if dist < minDist:
minDist = dist
index = i
oData.append((index, minDist))
rData.append(oData)
# connect to nearest path
if dbg and len(result) > 1:
dprt("multiple results")
oConnect = [False] * len(offsetPaths)
while True:
minDist = MAX_VALUE
index = None
rPath = None
for (rNum, oData) in enumerate(rData):
if oData == None:
continue
rPath = rNum
for (oNum, (i, dist)) in enumerate(oData):
if dbg:
dprt("step %d rNum %d Onum %d index %3d "\
"dist %9.6f" % \
(step, rNum, oNum, i, dist))
if not oConnect[oNum] and dist < minDist:
minDist = dist
index = i
rIndex = rNum
oIndex = oNum
if rPath is None:
break
if index is not None: # connect path
if dbg:
dprt("connect rIndex %d index %3d to "\
"oIndex %d dist %9.6f" % \
(rIndex, index, oIndex, minDist))
path = result[rIndex]
rData[rIndex] = None
oConnect[oIndex] = True
oPath = offsetPaths[oIndex]
oPath.append(Line(oPath[-1].p1, path[index].p0))
oPath += path[index:] + path[:index]
else: # add new path
if dbg:
dprt("add rPath %d oNum %d" % \
(rPath, len(offsetPaths)))
rData[rPath] = None
path = result[rPath]
path = self.closest(path)
offsetPaths.append(path)
offset += stepOver
step += 1
if step > 99:
break
for path in offsetPaths:
mp.millPath(path, closed=False, minDist=False)
def outside(self, args, dbg=True):
layer = args[1]
cfg = self.cfg
# dir = CCW
# if cfg.dir is not None and cfg.dir == 'CW':
# dir = CW
# stepOver = cfg.endMillSize * self.stepOver
cfg.ncInit()
segments = cfg.dxfInput.getPath(layer)
dxf = cfg.dxfInput
s = self.symmetry
if s == NO_SYMMETRY:
pass
elif s == UPPER:
yMin = (dxf.yMax + dxf.yMin) / 2
p0 = (dxf.xMin, dxf.yMax)
p1 = (dxf.xMax, dxf.yMax)
p2 = (dxf.xMax, yMin)
p3 = (dxf.xMin, yMin)
elif s == LOWER:
pass
elif s == RIGHT:
pass
elif s == LEFT:
pass
else:
pass
p0 = intScale(p0)
p1 = intScale(p1)
p2 = intScale(p2)
p3 = intScale(p3)
pco = pyclipper.PyclipperOffset()
pc = pyclipper.Pyclipper()
clip = (p0, p1, p2, p3)
# mp = cfg.getMillPath()
self.last = cfg.mill.last
for seg in segments:
self.pDir = pathDir(seg)
mainPath = []
for (i, l) in enumerate(seg):
l.draw()
if dbg:
l.prt()
if l.lType == LINE:
mainPath.append(intScale(l.p0))
elif l.lType == ARC:
self.arcLines(mainPath, l, dbg=dbg)
if dbg:
dprt("\nclip")
for (i, p) in enumerate(clip):
(x, y) = floatScale(p)
dprt("%3d (%7.4f %7.4f)" % (i, x, y))
dprt("\nmainPath")
for (i, p) in enumerate(mainPath):
(x, y) = floatScale(p)
dprt("%3d (%7.4f %7.4f)" % (i, x, y))
pco.Clear()
pco.AddPath(mainPath, pyclipper.JT_ROUND, False)
offset = int((cfg.endMillSize / 2.0) * SCALE)
offsetResult = pco.Execute(offset)
if dbg:
dprt("\noffsetResult len %d" % (len(offsetResult)))
for r in offsetResult:
dprt("r len %d" % (len(r)))
for (i, p) in enumerate(r):
(x, y) = floatScale(p)
dprt("%3d (%7.4f %7.4f)" % (i, x, y))
dprt()
pc.Clear()
pc.AddPath(clip, pyclipper.PT_CLIP, True)
pc.AddPath(mainPath, pyclipper.PT_SUBJECT, False)
result = pc.Execute(pyclipper.CT_INTERSECTION,
pyclipper.PFT_EVENODD,
pyclipper.PFT_EVENODD)
if dbg:
for r in result:
dprt("\nresult")
for (i, p) in enumerate(r):
(x, y) = floatScale(p)
dprt("%3d (%7.4f %7.4f)" % (i, x, y))
dprt()
def closest(self, path):
p0 = self.last
minD = MAX_VALUE
for (i, l) in enumerate(path):
dist = xyDist(p0, l.p0)
if dist < minD:
minD = dist
index = i
path = path[index:] + path[:index]
self.last = path[0].p0
return(path)
def arcLines(self, mainPath, l, err=0.001, dbg=False):
r = l.r
adjacent = r - err
angle = 2 * degrees(acos(adjacent / r))
a0 = degrees(calcAngle(l.c, l.p0))
a1 = degrees(calcAngle(l.c, l.p1))
if not l.swapped: # clockwise
if a1 < a0:
a1 += 360.0
if dbg:
dprt("a0 %5.1f a1 %5.1f total %5.1f cw" % \
(fix(a0), fix(a1), a1 - a0))
arcAngle = a1 - a0
segments = int(ceil((arcAngle) / angle))
aInc = arcAngle / segments
# aRad = radians(aInc)
else: # counter clockwise
if a0 < a1:
a0 += 360.0
if dbg:
dprt("a0 %5.1f a1 %5.1f total %5.1f ccw" % \
(fix(a0), fix(a1), a0 - a1))
arcAngle = a0 - a1
segments = int(ceil((arcAngle) / angle))
aInc = -arcAngle / segments
if dbg:
dprt("segments %d arcAngle %7.2f aInc %7.2f" % \
(segments, arcAngle, aInc))
mainPath.append(intScale(l.p0))
(x, y) = l.c
aRad = radians(aInc)
a = radians(a0) + aRad
for i in range(1, segments):
p = (r * cos(a) + x, r * sin(a) + y)
if dbg:
dprt("%2d a %7.2f (%7.2f %7.2f)" % \
(i, degrees(a), p[0], p[1]))
mainPath.append(intScale(p))
a += aRad
def pointsArc(self, p0, p1, p2, dbg=False):
if dbg:
dprt("p0 (%7.4f %7.4f) p1 (%7.4f %7.4f) "\
"p2 (%7.4f %7.4f) %7.4f %7.4f" % \
(p0[0], p0[1], p1[0], p1[1], p2[0], p2[1], \
xyDist(p0, p1), xyDist(p1, p2)))
(eqType0, m0, b0) = self.lineMid90(p0, p1, dbg)
(eqType1, m1, b1) = self.lineMid90(p1, p2, dbg)
if dbg:
dprt("eqType0 %5s m0 %7.4f b0 %7.4f "\
"eqType1 %5s m1 %7.4f b1 %7.4f" % \
(eqType0, m0, b0, eqType1, m1, b1))
if eqType0:
if eqType1:
# y = m0*x + b0
# y = m1*x + b1
# m0*x + b0 = m1*x + b1
# x*(m0 - m1) = b1 - b0
x = (b1 - b0) / (m0 - m1)
y = m0 * x + b0
else:
# y = m0*x + b0
# x = m1*y + b1
# y = m0 * (m1*y + b1) + b0
# y = m0*m1*y + m0*b1 + b0
# y - m0*m1*y = m0*b1 + b0
# y * (1 - m0*m1) = m0*b1 + b0
# y = (m0*b1 + b0) / (1 - m0*m1)
y = (m0*b1 + b0) / (1 - m0*m1)
x = m1 * y + b1
else:
if eqType1:
x = (m0*b1 + b0) / (1 - m0*m1)
y = m1 * x + b1
else:
y = (b1 - b0) / (m0 - m1)
x = m0 * y + b0
c = (x, y)
r0 = xyDist(c, p0)
if dbg:
r1 = xyDist(c, p1)
r2 = xyDist(c, p2)
dprt("c (%7.4f %7.4f) r0 %7.4f r1 %7.4f r2 %7.4f" % \
(c[0], c[1], r0, r1, r2))
return((c, r0))
def lineMid90(self, p0, p1, dbg=False):
(x0, y0) = p0
(x1, y1) = p1
xM = (x0 + x1) / 2
yM = (y0 + y1) / 2
dx = x1 - x0
dy = y1 - y0
eqType = abs(dx) > abs(dy)
if dbg:
dprt("eqType %5s dx %10.6f dy %10.6f" % (eqType, abs(dx), abs(dy)))
# eqType = False
if eqType: # if dx > dy line y = mx + b per x = -m*y + b
m = -dy / dx # negate slope
# y = mx + b
# b = y - mx
b = xM - m * yM # swap positions of x and y
else: # if dy > dx x = my + b
m = -dx / dy
b = yM - m * xM
return((not eqType, m, b))
def makePath(self, points, step, rNum, dbg=False):
if False:
r = 1
for i in range(4):
a0 = float(i) * pi / 2 + pi / 4
a = a0
p0 = (r * cos(a), r * sin(a))
for j in range(2):
tmp = (pi, -pi)[j]
a = a0 + tmp / 2
p1 = (r * cos(a), r * sin(a))
a = a0 + tmp / 1
p2 = (r * cos(a), r * sin(a))
self.pointsArc(p0, p1, p2)
dprt()
sys.exit()
numPoints = len(points)
pLast = points[-1]
i = 0
path = []
# points.append(points[0])
o0 = orientation(pLast, points[0], points[1])
if dbg:
dprt("path orientation %s" % oStr(o0))
while i < numPoints:
p0 = points[i]
if dbg:
dprt("i %3d (%7.4f %7.4f)" % (i, p0[0], p0[1]))
d0 = xyDist(p0, pLast)
j = i + 1
pa = p0
while j < numPoints - 1:
pj = points[j]
dist = xyDist(pa, pj)
if abs(dist - d0) > 0.001:
if dbg:
dprt("dist %9.6f d0 %9.6f" % (dist, d0))
break
j += 1
pa = pj
delta = j - i
if delta < 4:
l = Line(pLast, p0, i)
txt = "s %d r %d i %d" % (step, rNum, i)
l.label(txt)
i += 1
pLast = p0
else:
p1 = points[i + delta / 2]
(c, r) = self.pointsArc(pLast, p1, pa)
o = orientation(pLast, p1, pa)
if o != o0:
(pLast, pa) = (pa, pLast)
a0 = degAtan2(pLast[1] - c[1], pLast[0] - c[0])
a1 = degAtan2(pa[1] - c[1], pa[0] - c[0])
l = Arc(c, r, a0, a1, direction=o)
if dbg:
dprt("arc %s %2d i %d (%7.4f %7.4f) %8.3f "\
" %d (%7.4f %7.4f) %8.3f" % \
(oStr(o), delta, i, pLast[0], pLast[1], a0, \
j, pa[0], pa[1], a1))
i = j
pLast = pa
if dbg:
l.prt()
path.append(l)
return(path)
# while i < numPoints:
# p0 = points[i]
# dprt("i %3d (%7.4f %7.4f)" % (i, p0[0], p0[1]))
# if i < numPoints - 3:
# j = i + 1
# p1 = points[j]
# j += 1
# p2 = points[j]
# j += 1
# (c, r) = self.pointsArc(p0, p1, p2)
# arc = False
# while j < numPoints - 1:
# pj = points[j + 1]
# dist = abs(xyDist(c, pj) - r)
# dprt("%3d (%7.4f %7.4f) dist %10.6f" % \
# (j, pj[0], pj[1], dist))
# if dist > 0.001:
# break
# j += 1
# arc = True
# if arc:
# a0 = degAtan2(p0[1] - c[1], p0[0] - c[0])
# a1 = degAtan2(pj[1] - c[1], pj[0] - c[0])
# dprt("arc i %d (%7.4f %7.4f) %8.3f j %d (%7.4f %7.4f) %8.3f" % \
# (i, p0[0], p0[1], a0, j, pj[0], pj[1], a1))
# i = j
# else:
# dprt("%3d (%7.4f %7.4f)" % (i, p0[0], p0[1]))
# l = Line(pLast, p0, i)
# l.prt()
# txt = "s %d r %d i %d" % (step, rNum, i)
# l.label(txt)
# i += 1
# if l is not None:
# path.append(l)
# l = None
# pLast = p0
# return(path)