Debian Codegraph @

examples/python/pycam_bench.py

raw
1 from opencamlib import ocl, camvtk
2 import time
3 import toolpath_examples.ngc_writer as ngc_writer
4
5 def filter_path(path,tol):
6 f = ocl.LineCLFilter()
7 f.setTolerance(tol)
8 for p in path:
9 p2 = ocl.CLPoint(p.x,p.y,p.z)
10 f.addCLPoint(p2)
11 f.run()
12 return f.getCLPoints()
13
14
15 def adaptive_path_drop_cutter(s, cutter, path):
16 apdc = ocl.AdaptivePathDropCutter()
17 apdc.setSTL(s)
18 apdc.setCutter(cutter)
19 # set the minimum Z-coordinate, or "floor" for drop-cutter
20 #apdc.minimumZ = -1
21 apdc.setSampling(0.04)
22 apdc.setMinSampling(0.0008)
23 apdc.setPath( path )
24 apdc.run()
25 return apdc.getCLPoints()
26
27 def drawPaths(paths):
28 ngc_writer.preamble()
29
30 for path in cl_filtered_paths:
31 ngc_writer.pen_up()
32 first_pt = path[0]
33 ngc_writer.xy_rapid_to( first_pt.x, first_pt.y )
34 ngc_writer.pen_down( first_pt.z )
35 for p in path[1:]:
36 ngc_writer.line_to(p.x,p.y,p.z)
37
38 ngc_writer.postamble()
39
40 if __name__ == "__main__":
41 print(ocl.version())
42
43 myscreen = camvtk.VTKScreen()
44 #stl = camvtk.STLSurf("../../stl/demo.stl")
45 stl = camvtk.STLSurf("../../stl/pycam-textbox.stl")
46 print("STL surface read")
47 myscreen.addActor(stl)
48 stl.SetWireframe()
49 polydata = stl.src.GetOutput()
50 s= ocl.STLSurf()
51 camvtk.vtkPolyData2OCLSTL(polydata, s)
52 print("STLSurf with ", s.size(), " triangles")
53 print(s.getBounds())
54 # define a cutter
55 cutter = ocl.CylCutter(10, 50) # diameter, length
56 #cutter = ocl.BullCutter(0.6, 0.01, 5)
57 print(cutter)
58 #pdc = ocl.PathDropCutter() # create a pdc
59 apdc = ocl.AdaptivePathDropCutter()
60 #pdc.setSTL(s)
61 apdc.setSTL(s)
62 #pdc.setCutter(cutter) # set the cutter
63 apdc.setCutter(cutter)
64 #print "set minimumZ"
65 #pdc.minimumZ = -1 # set the minimum Z-coordinate, or "floor" for drop-cutter
66 #apdc.minimumZ = -1
67 #print "set the sampling interval"
68 #pdc.setSampling(0.4)
69 apdc.setSampling(0.4)
70 apdc.setMinSampling(0.0008)
71 print(" apdc sampling = ", apdc.getSampling())
72
73
74
75 ymin=0
76 ymax=50
77 Ny=40 # number of lines in the y-direction
78 dy = float(ymax-ymin)/(Ny-1) # the y step-over
79
80 # create a simple "Zig" pattern where we cut only in one direction.
81 paths = []
82 # create a list of paths
83 for n in range(0,Ny):
84 path = ocl.Path()
85 y = ymin+n*dy # current y-coordinate
86 p1 = ocl.Point(0,y,0) # start-point of line
87 p2 = ocl.Point(130,y,0) # end-point of line
88 l = ocl.Line(p1,p2) # line-object
89 path.append( l ) # add the line to the path
90 paths.append(path)
91
92 cl_paths=[]
93
94 # we now have a list of paths to run through apdc
95 t_before = time.time()
96 n_aclp=0
97 for p in paths:
98 aclp = adaptive_path_drop_cutter(s,cutter,p) # the output is a list of Cutter-Locations
99 n_aclp = n_aclp + len(aclp)
100 cl_paths.append(aclp)
101
102 t_after = time.time()
103 print("( OpenCAMLib::AdaptivePathDropCutter run took %.2f s )" % ( t_after-t_before ))
104 print("( got %d raw CL-points )" % ( n_aclp ))
105 # to reduce the G-code size we filter here. (this is not strictly required and could be omitted)
106 # we could potentially detect G2/G3 arcs here, if there was a filter for that.
107
108 tol = 0.001
109 print("( filtering to tolerance %.4f )" % ( tol ) )
110 cl_filtered_paths = []
111 t_before = time.time()
112 n_filtered=0
113 for cl_path in cl_paths:
114 cl_filtered = filter_path(cl_path,tol)
115 n_filtered = n_filtered + len(cl_filtered)
116 cl_filtered_paths.append(cl_filtered)
117 t_after = time.time()
118 calctime = t_after-t_before
119 print("( got %d filtered CL-points. Filter done in %.3f s )" % ( n_filtered , calctime ))
120
121 drawPaths(cl_filtered_paths)
122 """
123 # some parameters for this "zigzig" pattern
124 ymin=0
125 ymax=50
126 Ny=10 # number of lines in the y-direction
127 dy = float(ymax-ymin)/Ny # the y step-over
128 print("step-over ",dy)
129 #path = ocl.Path() # create an empty path object
130 path2 = ocl.Path()
131 # add Line objects to the path in this loop
132 for n in range(0,Ny):
133 y = ymin+n*dy
134 p1 = ocl.Point(0,y,-100) # start-point of line
135 p2 = ocl.Point(130,y,-100) # end-point of line
136 #sl = ocl.Line(p1,p2) # line-object
137 l2 = ocl.Line(p1,p2)
138 #path.append( l ) # add the line to the path
139 path2.append( l2 )
140
141 print(" set the path for pdf ")
142 #pdc.setPath( path )
143 apdc.setPath( path2 )
144
145 #print " run the calculation "
146 #t_before = time.time()
147 #pdc.run() # run drop-cutter on the path
148 #t_after = time.time()
149 #print " pdc run took ", t_after-t_before," s"
150
151 print(" run the calculation ")
152 t_before = time.time()
153 apdc.run() # run drop-cutter on the path
154 t_after = time.time()
155 print(" apdc run took ", t_after-t_before," s")
156
157
158 print("get the results ")
159 #clp = pdc.getCLPoints() # get the cl-points from pdf
160 aclp = apdc.getCLPoints()
161 print("got ", len(aclp) ," adaptive points")
162
163 #aclp_lifted=[]
164 #for p in aclp:
165 # p2 = ocl.Point(p.x,p.y,p.z) + ocl.Point(0,0,1)
166 # aclp_lifted.append(p2)
167
168 # filter the adaptively sampled toolpaths
169
170 print("filtering. before filter we have", len(aclp),"cl-points")
171 t_before = time.time()
172 f = ocl.LineCLFilter()
173 f.setTolerance(0.001)
174 for p in aclp:
175 p2 = ocl.CLPoint(p.x,p.y,p.z)
176 f.addCLPoint(p2)
177
178 f.run()
179 t_after = time.time()
180 calctime = t_after-t_before
181 print(" done in ", calctime," s")
182
183 cl_filtered = f.getCLPoints()
184 #aclp_lifted2=[]
185 #for p in cl_filtered:
186 # p2 = ocl.Point(p.x,p.y,p.z) + ocl.Point(0,0,1)
187 # aclp_lifted2.append(p2)
188
189
190 print(" render the CL-points")
191 #camvtk.drawCLPointCloud(myscreen, clp)
192
193 camvtk.drawCLPointCloud(myscreen, cl_filtered)
194
195 for p in cl_filtered:
196 myscreen.
197 """
198
199 #camvtk.drawCLPointCloud(myscreen, aclp_lifted2)
200 #myscreen.addActor( camvtk.PointCloud(pointlist=clp, collist=ccp) )
201 myscreen.camera.SetPosition(3, 23, 15)
202 myscreen.camera.SetFocalPoint(5, 5, 0)
203 myscreen.render()
204 print(" All done.")
205 myscreen.iren.Start()