forked from ushakov/autocut
/
levels.py
executable file
·334 lines (285 loc) · 9.7 KB
/
levels.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
#!/usr/bin/python
import sys
import ocl
import camvtk
import time
import vtk
import math
import area
import ngc_writer as nw
import config_pb2
from google.protobuf import text_format
config = None
def ReadConfig(fn):
basename = fn
if fn.endswith(".conf"):
basename = fn[:len(fn)-len(".conf")]
with open(fn, "r") as f:
content = f.read()
global config
config = config_pb2.Config()
config.in_filename = basename + ".stl"
config.out_filename = basename + ".ngc"
text_format.Merge(content, config)
class TriangleProcessor(object):
def __init__(self, tr):
self.p = tr.getPoints()
self.low = min(p.z for p in self.p)
self.high = max(p.z for p in self.p)
self.n = tr.n
def Horizontal(self):
if self.n.z < 0:
return False
return self.high - self.low >= 0 and self.high - self.low < 0.05
def Vertical(self):
d = self.n.dot(ocl.Point(0,0,1))
return abs(d) < 0.01
def Level(self):
return self.low
def seq(pl):
ret = []
for p in pl:
ret.append([p.x, p.y, p.z])
return ret
def drawLoops(myscreen, loops):
points = []
for loop in loops:
for p in loop:
points.append(p)
myscreen.addActor(camvtk.PointCloud(points))
def IsNegative(prev, vertex):
p1 = prev - vertex.c
p2 = vertex.p - vertex.c
smaller_angle_ccw = (p1 ^ p2 > 0)
if vertex.type == 1: # CCW arc, positive iff smaller angle is CCW
return smaller_angle_ccw
else: # CW arc, positive if smaller angle is CW
return not smaller_angle_ccw
def drawCurve(myscreen, curve, z):
vertices = curve.getVertices()
current = vertices[0].p
#print "start at (%.2f, %.2f) z=%.2f" % (current.x, current.y, z)
for v in vertices[1:]:
if v.type == 0:
#print "line to (%.2f,%.2f) z=%.2f" % (v.p.x, v.p.y, z)
myscreen.addActor(camvtk.Line(p1=(current.x, current.y, z), p2 = (v.p.x, v.p.y, z)))
else:
r = math.hypot(v.p.x-v.c.x, v.p.y-v.c.y)
#print "arc to (%.2f,%.2f) center=(%.2f,%.2f) r=%.2f z=%.2f" % (v.p.x, v.p.y, v.c.x, v.c.y, r, z)
src = vtk.vtkArcSource()
src.SetCenter(v.c.x, v.c.y, z)
src.SetPoint1(current.x, current.y, z)
src.SetPoint2(v.p.x, v.p.y, z)
src.SetResolution(20)
src.SetNegative(not IsNegative(current, v))
mapper = vtk.vtkPolyDataMapper()
mapper.SetInput(src.GetOutput())
actor = camvtk.CamvtkActor()
actor.SetMapper(mapper)
myscreen.addActor(actor)
current = v.p
def GetEssentialLevels(s):
levels = []
good = 0
total = 0
for t in s.getTriangles():
tr = TriangleProcessor(t)
total += 1
if tr.Horizontal():
levels.append(tr.Level())
good += 1
elif tr.Vertical():
good += 1
else:
p = seq(t.getPoints())
print "Triangles: good=", good, " total=", total, "percentage=", (good*100./total)
levels.sort()
essential_levels = []
i = 0
while i < len(levels):
l0 = levels[i]
j = i+1
while j < len(levels):
l1 = levels[j]
if l1 > l0 + config.vertical_tolerance:
break
j += 1
essential_levels.append(l0)
i = j
essential_levels.sort(reverse=True)
essential_levels = [l for l in essential_levels if l >= config.bottom]
if len(essential_levels) == 0 or essential_levels[len(essential_levels) - 1] != config.bottom:
essential_levels.append(config.bottom)
return essential_levels
def GetWaterlines(s, essential_levels):
cutter = ocl.CylCutter(config.tool_diameter, 20)
level_loops = []
for l in essential_levels:
print "################## Making waterline at", l, "##############################"
wl = ocl.Waterline()
wl.setSTL(s)
wl.setCutter(cutter)
wl.setSampling(0.02)
wl.setZ(l + config.vertical_tolerance)
wl.run2()
level_loops.append(wl.getLoops())
return level_loops
def MakeArea(loops):
ar = area.Area()
for loop in loops:
curve = area.Curve()
for p in loop:
curve.append(area.Point(p.x, p.y))
ar.append(curve)
ar.FitArcs()
ar.Reorder()
return ar
def ConvertLoopsToAreas(level_loops):
ret = []
for i, loops in enumerate(level_loops):
print "Processing", i, "th level:"
ar = MakeArea(loops)
print "Area made"
ret.append(ar)
print "Done processing"
return ret
def MakePocketAreas(levels, areas):
outer_bound = area.Area(areas[len(areas) - 1])
outer_bound.Offset(-config.tool_diameter)
cut_levels = [ ]
cut_areas = [ ]
for i, ar in enumerate(areas):
for curve in outer_bound.getCurves():
ar.append(curve)
ar.Reorder()
cut_areas.append(ar)
cut_levels.append(levels[i])
return cut_levels, cut_areas
def MakePocketToolpaths(levels, areas):
tps = []
for i, ar in enumerate(areas):
print "Making", i, "th toolpath at", levels[i]
tp = ar.MakePocketToolpath(3.175, -3.175, config.step_over, True, False, 0)
tps.append(tp)
print " -- Got", len(tp), "curves"
print "Out:", len(tps), "levels"
return levels, tps
def MakeWaterlineToolpaths(areas):
return [ ar.getCurves() for ar in areas ]
# Last level produced will be levels[len(levels)-1]
def StepDownLevels(levels):
cur_lev = config.top
cur_idx = 0
if config.machine_top or cur_lev < config.top - config.vertical_tolerance:
yield cur_lev, cur_idx
# Inv.: last level marked was cur_lev, and last index was cur_idx
while True:
if abs(cur_lev - levels[cur_idx]) < config.vertical_tolerance:
cur_idx += 1
if cur_idx >= len(levels):
break
cur_lev -= config.step_down
if cur_lev < levels[cur_idx] + config.vertical_tolerance:
cur_lev = levels[cur_idx]
if config.machine_top or cur_lev < config.top - config.vertical_tolerance:
yield cur_lev, cur_idx
def FillInStepDowns(tp_levels, tp_paths):
levs = [ ]
tps = [ ]
for lev, i in StepDownLevels(tp_levels):
levs.append(lev)
tps.append(tp_paths[i])
return levs, tps
class FileWriter(object):
def __init__(self, fn):
self.f = open(fn, "w")
def Print(self, *x):
s = [ str(t) for t in x ]
self.f.write(" ".join(s))
self.f.write('\n')
def Close(self):
self.f.close()
def OutputGCode(lev, paths, fn):
nw.clearance_height = config.top + config.clearance_above_top
nw.feed_height = config.top + config.engage_above_top
nw.feed = config.feed
nw.plunge_feed = config.plunge_feed
nw.writer = FileWriter(fn)
nw.comment("============ START G-CODE ===============")
nw.preamble()
nw.pen_up()
pairs = zip(lev, paths)
for lev, path in sorted(pairs, key = lambda(p): -p[0]):
nw.comment("level=%s" % lev)
for curve in path:
vertices = curve.getVertices()
current = vertices[0].p
nw.xy_rapid_to(current.x, current.y)
nw.pen_down(lev)
for v in vertices[1:]:
if v.type == 0:
nw.line_to(v.p.x, v.p.y, lev)
else:
r = math.hypot(v.p.x - v.c.x, v.p.y - v.c.y)
nw.xy_arc_to(v.p.x, v.p.y, r, v.c.x, v.c.y, v.type != 1)
nw.pen_up()
nw.postamble()
nw.comment("============ END G-CODE ===============")
nw.writer.Close()
if __name__ == "__main__":
print ocl.version()
if len(sys.argv) == 1:
print "Usage: autocut [config filename]"
sys.exit(1)
ReadConfig(sys.argv[1])
stl = camvtk.STLSurf(config.in_filename, color=camvtk.green)
stl.SetOpacity(0.2)
stl.RotateX(config.rotate_x)
stl.RotateY(config.rotate_y)
stl.RotateZ(config.rotate_z)
print "STL surface read"
polydata = stl.src.GetOutput()
s= ocl.STLSurf()
camvtk.vtkPolyData2OCLSTL(polydata, s)
s.rotate(config.rotate_x * math.pi / 180,
config.rotate_y * math.pi / 180,
config.rotate_z * math.pi / 180)
bb = s.getBounds()
sx = max(abs(bb[0]), abs(bb[1]))
sy = max(abs(bb[1]), abs(bb[2]))
ms = max(sx, sy)
print "STLSurf with ", s.size(), " triangles"
levels = GetEssentialLevels(s)
if config.oblique_approximation:
all_levels = []
for lev, idx in StepDownLevels(levels):
all_levels.append(lev)
levels = all_levels
print "Levels:", ", ".join([str(l) for l in levels])
loops = GetWaterlines(s, levels)
for i, lev in enumerate(levels):
lengths = [str(len(loop)) for loop in loops[i]]
print "L%02d@%smm: %s" % (i, lev, ",".join(lengths))
areas = ConvertLoopsToAreas(loops)
if config.waterline_only:
paths = MakeWaterlineToolpaths(areas)
else:
levels, areas = MakePocketAreas(levels, areas)
levels, paths = MakePocketToolpaths(levels, areas)
# If config.oblique_approximation is set, step-downs are already
# produced.
if not config.oblique_approximation:
levels, paths = FillInStepDowns(levels, paths)
OutputGCode(levels, paths, config.out_filename)
myscreen = camvtk.VTKScreen()
myscreen.addActor(stl)
for i, lev in enumerate(levels):
tp = paths[i]
print "Lev", i, "@", lev, ":", len(tp), "curves"
for c in tp:
drawCurve(myscreen, c, lev)
myscreen.camera.SetPosition(3, 23, 15)
myscreen.camera.SetFocalPoint(5, 5, 0)
myscreen.render()
print " All done."
myscreen.iren.Start()