Sat, 07 Nov 2015 20:50:55 +0100
finished refactoring
0 | 1 | #!/usr/bin/env python |
2 | # | |
3 | # cad.py | |
4 | # | |
5 | # Neil Gershenfeld | |
6 | # | |
7 | # (c) Massachusetts Institute of Technology 2007 | |
8 | # Permission granted for experimental and personal use; | |
9 | # license for commercial sale available from MIT. | |
10 | # | |
11 | #Altered by Capo to output gcode with the '.gcode' extension as opposed to '.g' | |
12 | #and to work with a solenoid/Laser cutter. | |
13 | #For more information see http://capolight.wordpress.com/2012/04/29/converting_images_to_gcode/printers | |
14 | DATE = "29/04/2012" | |
15 | ||
16 | from numpy import * | |
17 | import scipy.signal.signaltools | |
18 | from string import * | |
19 | from Tkinter import * | |
20 | from tkFileDialog import * | |
21 | import Image, ImageTk, ImageDraw, ImageFont, ImageOps | |
22 | import os, struct | |
23 | #import time | |
24 | ||
25 | class point: | |
26 | # | |
27 | # an xyz point | |
28 | # | |
29 | def __init__(self,x,y,z=0): | |
30 | self.x = x | |
31 | self.y = y | |
32 | self.z = z | |
33 | ||
34 | class cad_variables: | |
35 | # | |
36 | # cad variables | |
37 | # | |
38 | def __init__(self): | |
39 | self.xmin = 0 # minimum x value to render | |
40 | self.xmax = 0 # maximum x value to render | |
41 | self.ymin = 0 # minimum y value to render | |
42 | self.ymax = 0 # maximum y value to render | |
43 | self.zmin = 0 # minimum z value to render | |
44 | self.zmax = 0 # maximum z value to render | |
45 | self.zlist = [] # z values to render | |
46 | self.nx = 0 # number of x points to render | |
47 | self.ny = 0 # number of y points to render | |
48 | self.nz = 1 # number of z points to render | |
49 | self.rz = 0 # perspective view z rotation (degrees) | |
50 | self.rx = 0 # perspective view x rotation (degrees) | |
51 | self.units = 'in' # file units | |
52 | self.function = '0' # cad function | |
53 | self.toolpaths = [] # toolpaths | |
54 | self.x = [] # x triangulation | |
55 | self.y = [] # y triangulation | |
56 | self.z = [] # z triangulation | |
57 | self.labels = [] # display labels | |
58 | self.image_r = array(0) # red array | |
59 | self.image_g = array(0) # green array | |
60 | self.image_b = array(0) # blue array | |
61 | self.image_min = 0 # image min value | |
62 | self.image_max = 0 # image max value | |
63 | self.stop = 0 # stop rendering | |
64 | self.nplot = 200 # plot window size | |
65 | self.inches_per_unit = 1 # file units | |
66 | self.views = 'xyzr' | |
67 | self.cam = '' # CAM export type | |
68 | self.editor_width = 30 # editor width | |
69 | self.editor_height = 10 # editor height | |
70 | def view(self,arg): | |
71 | global canvas_xy,canvas_yz,canvas_xz,canvas_xyz | |
72 | if (arg == 'xy'): | |
73 | view_frame2.grid_forget() | |
74 | view_frame3.grid_forget() | |
75 | canvas_xy.grid_forget() | |
76 | self.views = 'xy' | |
77 | self.nplot = 2*int(string_window_size.get()) # plot window size | |
78 | canvas_xy = Canvas(view_frame2, width=self.nplot, height=self.nplot) | |
79 | imxy = Image.new("RGBX",(self.nplot,self.nplot),'black') | |
80 | image_xy = ImageTk.PhotoImage(imxy) | |
81 | canvas_xy.create_image(self.nplot/2,self.nplot/2,image=image_xy) | |
82 | canvas_xy.bind('<Motion>',msg_xy) | |
83 | canvas_xy.grid(row=0,column=0) | |
84 | view_frame2.grid(row=2,column=0) | |
85 | elif (arg == 'xyzr'): | |
86 | view_frame2.grid_forget() | |
87 | view_frame3.grid_forget() | |
88 | canvas_xy.grid_forget() | |
89 | canvas_yz.grid_forget() | |
90 | canvas_xz.grid_forget() | |
91 | canvas_xyz.grid_forget() | |
92 | self.views = 'xyzr' | |
93 | self.nplot = int(string_window_size.get()) # plot window size | |
94 | canvas_xy = Canvas(view_frame3, width=self.nplot, height=self.nplot) | |
95 | canvas_yz = Canvas(view_frame3, width=self.nplot, height=self.nplot) | |
96 | canvas_xz = Canvas(view_frame3, width=self.nplot, height=self.nplot) | |
97 | canvas_xyz = Canvas(view_frame3, width=self.nplot, height=cad.nplot) | |
98 | imxy = Image.new("RGBX",(self.nplot,self.nplot),'black') | |
99 | image_xy = ImageTk.PhotoImage(imxy) | |
100 | canvas_xy.create_image(self.nplot/2,self.nplot/2,image=image_xy) | |
101 | canvas_xy.bind('<Motion>',msg_xy) | |
102 | canvas_xy.grid(row=0,column=0) | |
103 | imyz = Image.new("RGBX",(self.nplot,self.nplot),'black') | |
104 | image_yz = ImageTk.PhotoImage(imyz) | |
105 | canvas_yz.create_image(self.nplot/2,self.nplot/2,image=image_yz) | |
106 | canvas_yz.bind('<Motion>',msg_yz) | |
107 | canvas_yz.grid(row=0,column=1) | |
108 | imxz = Image.new("RGBX",(self.nplot,self.nplot),'black') | |
109 | image_xz = ImageTk.PhotoImage(imxz) | |
110 | canvas_xz.create_image(self.nplot/2,self.nplot/2,image=image_xz) | |
111 | canvas_xz.bind('<Motion>',msg_xz) | |
112 | canvas_xz.grid(row=1,column=0) | |
113 | imxyz = Image.new("RGBX",(self.nplot,self.nplot),'black') | |
114 | image_xyz = ImageTk.PhotoImage(imxyz) | |
115 | canvas_xyz.create_image(self.nplot/2,self.nplot/2,image=image_xyz) | |
116 | canvas_xyz.bind('<Motion>',msg_nomsg) | |
117 | canvas_xyz.grid(row=1,column=1) | |
118 | view_frame3.grid(row=2,column=0) | |
119 | else: | |
120 | print "view not supported" | |
121 | def nxplot(self): | |
122 | xwidth = self.xmax - self.xmin | |
123 | ywidth = self.ymax - self.ymin | |
124 | zwidth = self.zmax - self.zmin | |
125 | if ((xwidth >= ywidth) & (xwidth >= zwidth)): | |
126 | n = int(self.nplot*xwidth/float(xwidth)) | |
127 | elif ((ywidth >= xwidth) & (ywidth >= zwidth)): | |
128 | n = int(self.nplot*xwidth/float(ywidth)) | |
129 | else: | |
130 | n = int(self.nplot*xwidth/float(zwidth)) | |
131 | return n | |
132 | def nyplot(self): | |
133 | xwidth = self.xmax - self.xmin | |
134 | ywidth = self.ymax - self.ymin | |
135 | zwidth = self.zmax - self.zmin | |
136 | if ((xwidth >= ywidth) & (xwidth >= zwidth)): | |
137 | n = int(self.nplot*ywidth/float(xwidth)) | |
138 | elif ((ywidth >= xwidth) & (ywidth >= zwidth)): | |
139 | n = int(self.nplot*ywidth/float(ywidth)) | |
140 | else: | |
141 | n = int(self.nplot*ywidth/float(zwidth)) | |
142 | return n | |
143 | def nzplot(self): | |
144 | xwidth = self.xmax - self.xmin | |
145 | ywidth = self.ymax - self.ymin | |
146 | zwidth = self.zmax - self.zmin | |
147 | if ((xwidth >= ywidth) & (xwidth >= zwidth)): | |
148 | n = int(self.nplot*zwidth/float(xwidth)) | |
149 | elif ((ywidth >= xwidth) & (ywidth >= zwidth)): | |
150 | n = int(self.nplot*zwidth/float(ywidth)) | |
151 | else: | |
152 | n = int(self.nplot*zwidth/float(zwidth)) | |
153 | return n | |
154 | ||
155 | cad = cad_variables() | |
156 | ||
157 | class cad_text: | |
158 | def __init__(self,x,y,z=0,text='',size=10,color='#ff0000',anchor=CENTER): | |
159 | self.x = x | |
160 | self.y = y | |
161 | self.z = z | |
162 | self.text = text | |
163 | self.size = size | |
164 | self.color = color | |
165 | self.anchor = anchor | |
166 | ||
167 | class im_class: | |
168 | # | |
169 | # for PIL images | |
170 | # | |
171 | def __init__(self): | |
172 | self.xy = 0 | |
173 | self.xz = 0 | |
174 | self.yz = 0 | |
175 | self.xyz = 0 | |
176 | self.intensity_xy = 0 | |
177 | self.intensity_xz = 0 | |
178 | self.intensity_yz = 0 | |
179 | self.intensity_xyz = 0 | |
180 | ||
181 | im = im_class() | |
182 | ||
183 | class images_class: | |
184 | # | |
185 | # for PhotoImages | |
186 | # | |
187 | def __init__(self): | |
188 | self.xy = 0 | |
189 | self.xz = 0 | |
190 | self.yz = 0 | |
191 | self.xyz = 0 | |
192 | ||
193 | images = images_class() | |
194 | ||
195 | class CA_states: | |
196 | # | |
197 | # CA state definition class | |
198 | # | |
199 | def __init__(self): | |
200 | self.empty = 0 | |
201 | self.interior = 1 | |
202 | self.edge = (1 << 1) # 2 | |
203 | self.north = (1 << 2) # 4 | |
204 | self.west = (2 << 2) # 8 | |
205 | self.east = (3 << 2) # 12 | |
206 | self.south = (4 << 2) # 16 | |
207 | self.stop = (5 << 2) # 20 | |
208 | self.corner = (6 << 2) # 24 | |
209 | ||
210 | class rule_table: | |
211 | # | |
212 | # CA rule table class | |
213 | # | |
214 | # 0 = empty | |
215 | # 1 = interior | |
216 | # 2 = edge | |
217 | # edge+direction = start | |
218 | # | |
219 | def __init__(self): | |
220 | self.table = zeros(2**(9*2),uint32) | |
221 | self.s = CA_states() | |
222 | # | |
223 | # 1 0: | |
224 | # | |
225 | # 011 | |
226 | # 111 | |
227 | # 111 | |
228 | self.add_rule(0,1,1,1,1,1,1,1,1,self.s.north) | |
229 | # 101 | |
230 | # 111 | |
231 | # 111 | |
232 | self.add_rule(1,0,1,1,1,1,1,1,1,self.s.east) | |
233 | # | |
234 | # 2 0's: | |
235 | # | |
236 | # 001 | |
237 | # 111 | |
238 | # 111 | |
239 | self.add_rule(0,0,1,1,1,1,1,1,1,self.s.east) | |
240 | # 100 | |
241 | # 111 | |
242 | # 111 | |
243 | self.add_rule(1,0,0,1,1,1,1,1,1,self.s.east) | |
244 | # 010 | |
245 | # 111 | |
246 | # 111 | |
247 | self.add_rule(0,1,0,1,1,1,1,1,1,self.s.east) | |
248 | # 011 | |
249 | # 110 | |
250 | # 111 | |
251 | self.add_rule(0,1,1,1,1,0,1,1,1,self.s.south) | |
252 | # 110 | |
253 | # 011 | |
254 | # 111 | |
255 | self.add_rule(1,1,0,0,1,1,1,1,1,self.s.east) | |
256 | # 101 | |
257 | # 011 | |
258 | # 111 | |
259 | self.add_rule(1,0,1,0,1,1,1,1,1,self.s.east) | |
260 | # 101 | |
261 | # 110 | |
262 | # 111 | |
263 | self.add_rule(1,0,1,1,1,0,1,1,1,self.s.south) | |
264 | # 011 | |
265 | # 111 | |
266 | # 110 | |
267 | self.add_rule(0,1,1,1,1,1,1,1,0,self.s.corner) | |
268 | # 011 | |
269 | # 111 | |
270 | # 101 | |
271 | self.add_rule(0,1,1,1,1,1,1,0,1,self.s.north) | |
272 | # 110 | |
273 | # 111 | |
274 | # 101 | |
275 | self.add_rule(1,1,0,1,1,1,1,0,1,self.s.west) | |
276 | # 101 | |
277 | # 111 | |
278 | # 110 | |
279 | self.add_rule(1,0,1,1,1,1,1,1,0,self.s.south) | |
280 | # 101 | |
281 | # 111 | |
282 | # 011 | |
283 | self.add_rule(1,0,1,1,1,1,0,1,1,self.s.east) | |
284 | # | |
285 | # 3 0's: | |
286 | # | |
287 | # 001 | |
288 | # 011 | |
289 | # 111 | |
290 | self.add_rule(0,0,1,0,1,1,1,1,1,self.s.east) | |
291 | # 010 | |
292 | # 011 | |
293 | # 111 | |
294 | self.add_rule(0,1,0,0,1,1,1,1,1,self.s.east) | |
295 | # 010 | |
296 | # 110 | |
297 | # 111 | |
298 | self.add_rule(0,1,0,1,1,0,1,1,1,self.s.south) | |
299 | # 010 | |
300 | # 111 | |
301 | # 011 | |
302 | self.add_rule(0,1,0,1,1,1,0,1,1,self.s.east) | |
303 | # 010 | |
304 | # 111 | |
305 | # 110 | |
306 | self.add_rule(0,1,0,1,1,1,1,1,0,self.s.south) | |
307 | # 110 | |
308 | # 011 | |
309 | # 011 | |
310 | self.add_rule(1,1,0,0,1,1,0,1,1,self.s.east) | |
311 | # 011 | |
312 | # 110 | |
313 | # 110 | |
314 | self.add_rule(0,1,1,1,1,0,1,1,0,self.s.south) | |
315 | # 101 | |
316 | # 011 | |
317 | # 011 | |
318 | self.add_rule(1,0,1,0,1,1,0,1,1,self.s.east) | |
319 | # 101 | |
320 | # 110 | |
321 | # 110 | |
322 | self.add_rule(1,0,1,1,1,0,1,1,0,self.s.south) | |
323 | # 011 | |
324 | # 011 | |
325 | # 011 | |
326 | self.add_rule(0,1,1,0,1,1,0,1,1,self.s.north) | |
327 | # | |
328 | # 4 0's: | |
329 | # | |
330 | # 001 | |
331 | # 011 | |
332 | # 011 | |
333 | self.add_rule(0,0,1,0,1,1,0,1,1,self.s.east) | |
334 | # 100 | |
335 | # 110 | |
336 | # 110 | |
337 | self.add_rule(1,0,0,1,1,0,1,1,0,self.s.south) | |
338 | # 010 | |
339 | # 011 | |
340 | # 011 | |
341 | self.add_rule(0,1,0,0,1,1,0,1,1,self.s.east) | |
342 | # 010 | |
343 | # 110 | |
344 | # 110 | |
345 | self.add_rule(0,1,0,1,1,0,1,1,0,self.s.south) | |
346 | # 001 | |
347 | # 110 | |
348 | # 110 | |
349 | self.add_rule(0,0,1,1,1,0,1,1,0,self.s.south) | |
350 | # 100 | |
351 | # 011 | |
352 | # 011 | |
353 | self.add_rule(1,0,0,0,1,1,0,1,1,self.s.east) | |
354 | # | |
355 | # 5 0's: | |
356 | # | |
357 | # 000 | |
358 | # 011 | |
359 | # 011 | |
360 | self.add_rule(0,0,0,0,1,1,0,1,1,self.s.east) | |
361 | # | |
362 | # edge states | |
363 | # | |
364 | # 200 | |
365 | # 211 | |
366 | # 211 | |
367 | self.add_rule(2,0,0,2,1,1,2,1,1,self.s.east+self.s.edge) | |
368 | # 201 | |
369 | # 211 | |
370 | # 211 | |
371 | self.add_rule(2,0,1,2,1,1,2,1,1,self.s.east+self.s.edge) | |
372 | # 210 | |
373 | # 211 | |
374 | # 211 | |
375 | self.add_rule(2,1,0,2,1,1,2,1,1,self.s.east+self.s.edge) | |
376 | # 002 | |
377 | # 112 | |
378 | # 112 | |
379 | self.add_rule(0,0,2,1,1,2,1,1,2,self.s.stop) | |
380 | # 102 | |
381 | # 112 | |
382 | # 112 | |
383 | self.add_rule(1,0,2,1,1,2,1,1,2,self.s.stop) | |
384 | # 002 | |
385 | # 112 | |
386 | # 102 | |
387 | self.add_rule(0,0,2,1,1,2,1,0,2,self.s.stop) | |
388 | # 012 | |
389 | # 112 | |
390 | # 112 | |
391 | self.add_rule(0,1,2,1,1,2,1,1,2,self.s.stop) | |
392 | # 012 | |
393 | # 112 | |
394 | # 102 | |
395 | self.add_rule(0,1,2,1,1,2,1,0,2,self.s.stop) | |
396 | ||
397 | def add_rule(self,nw,nn,ne,ww,cc,ee,sw,ss,se,rule): | |
398 | # | |
399 | # add a CA rule, with rotations | |
400 | # | |
401 | s = CA_states() | |
402 | # | |
403 | # add the rule | |
404 | # | |
405 | state = \ | |
406 | (nw << 0) + (nn << 2) + (ne << 4) + \ | |
407 | (ww << 6) + (cc << 8) + (ee << 10) + \ | |
408 | (sw << 12) + (ss << 14) + (se << 16) | |
409 | self.table[state] = rule | |
410 | # | |
411 | # rotate 90 degrees | |
412 | # | |
413 | state = \ | |
414 | (sw << 0) + (ww << 2) + (nw << 4) + \ | |
415 | (ss << 6) + (cc << 8) + (nn << 10) + \ | |
416 | (se << 12) + (ee << 14) + (ne << 16) | |
417 | if (rule == s.east): | |
418 | self.table[state] = s.south | |
419 | elif (rule == s.south): | |
420 | self.table[state] = s.west | |
421 | elif (rule == s.west): | |
422 | self.table[state] = s.north | |
423 | elif (rule == s.north): | |
424 | self.table[state] = s.east | |
425 | elif (rule == (s.east+s.edge)): | |
426 | self.table[state] = s.south+s.edge | |
427 | elif (rule == (s.south+s.edge)): | |
428 | self.table[state] = s.west+s.edge | |
429 | elif (rule == (s.west+s.edge)): | |
430 | self.table[state] = s.north+s.edge | |
431 | elif (rule == (s.north+s.edge)): | |
432 | self.table[state] = s.east+s.edge | |
433 | elif (rule == s.corner): | |
434 | self.table[state] = s.corner | |
435 | elif (rule == s.stop): | |
436 | self.table[state] = s.stop | |
437 | # | |
438 | # rotate 180 degrees | |
439 | # | |
440 | state = \ | |
441 | (se << 0) + (ss << 2) + (sw << 4) + \ | |
442 | (ee << 6) + (cc << 8) + (ww << 10) + \ | |
443 | (ne << 12) + (nn << 14) + (nw << 16) | |
444 | if (rule == s.east): | |
445 | self.table[state] = s.west | |
446 | elif (rule == s.south): | |
447 | self.table[state] = s.north | |
448 | elif (rule == s.west): | |
449 | self.table[state] = s.east | |
450 | elif (rule == s.north): | |
451 | self.table[state] = s.south | |
452 | elif (rule == (s.east+s.edge)): | |
453 | self.table[state] = s.west+s.edge | |
454 | elif (rule == (s.south+s.edge)): | |
455 | self.table[state] = s.north+s.edge | |
456 | elif (rule == (s.west+s.edge)): | |
457 | self.table[state] = s.east+s.edge | |
458 | elif (rule == (s.north+s.edge)): | |
459 | self.table[state] = s.south+s.edge | |
460 | elif (rule == s.corner): | |
461 | self.table[state] = s.corner | |
462 | elif (rule == s.stop): | |
463 | self.table[state] = s.stop | |
464 | # | |
465 | # rotate 270 degrees | |
466 | # | |
467 | state = \ | |
468 | (ne << 0) + (ee << 2) + (se << 4) + \ | |
469 | (nn << 6) + (cc << 8) + (ss << 10) + \ | |
470 | (nw << 12) + (ww << 14) + (sw << 16) | |
471 | if (rule == s.east): | |
472 | self.table[state] = s.north | |
473 | elif (rule == s.south): | |
474 | self.table[state] = s.east | |
475 | elif (rule == s.west): | |
476 | self.table[state] = s.south | |
477 | elif (rule == s.north): | |
478 | self.table[state] = s.west | |
479 | elif (rule == (s.east+s.edge)): | |
480 | self.table[state] = s.north+s.edge | |
481 | elif (rule == (s.south+s.edge)): | |
482 | self.table[state] = s.east+s.edge | |
483 | elif (rule == (s.west+s.edge)): | |
484 | self.table[state] = s.south+s.edge | |
485 | elif (rule == (s.north+s.edge)): | |
486 | self.table[state] = s.west+s.edge | |
487 | elif (rule == s.corner): | |
488 | self.table[state] = s.corner | |
489 | elif (rule == s.stop): | |
490 | self.table[state] = s.stop | |
491 | ||
492 | def evaluate_state(arr): | |
493 | # | |
494 | # assemble the state bit strings | |
495 | # | |
496 | (ny, nx) = shape(arr) | |
497 | s = CA_states() | |
498 | nn = concatenate(([s.edge+zeros(nx,uint32)],arr[:(ny-1)])) | |
499 | ss = concatenate((arr[1:],[s.edge+zeros(nx,uint32)])) | |
500 | ww = concatenate((reshape(s.edge+zeros(ny,uint32),(ny,1)),arr[:,:(nx-1)]),1) | |
501 | ee = concatenate((arr[:,1:],reshape(s.edge+zeros(ny,uint32),(ny,1))),1) | |
502 | cc = arr | |
503 | nw = concatenate(([s.edge+zeros(nx,uint32)],ww[:(ny-1)])) | |
504 | ne = concatenate(([s.edge+zeros(nx,uint32)],ee[:(ny-1)])) | |
505 | sw = concatenate((ww[1:],[s.edge+zeros(nx,uint32)])) | |
506 | se = concatenate((ee[1:],[s.edge+zeros(nx,uint32)])) | |
507 | state = (nw << 0) + (nn << 2) + (ne << 4) + \ | |
508 | (ww << 6) + (cc << 8) + (ee << 10) + \ | |
509 | (sw << 12) + (ss << 14) + (se << 16) | |
510 | return state | |
511 | ||
512 | def vectorize_toolpaths(arr): | |
513 | # | |
514 | # convert lattice toolpath directions to vectors | |
515 | # | |
516 | s = CA_states() | |
517 | toolpaths = [] | |
518 | max_dist = float(string_vector_error.get()) | |
519 | start_sites = (arr == (s.north+s.edge)) | (arr == (s.south+s.edge)) | \ | |
520 | (arr == (s.east+s.edge)) | (arr == (s.west+s.edge)) | |
521 | num_start_sites = sum(sum(1.0*start_sites)) | |
522 | path_sites = (arr == s.north) | (arr == s.south) | (arr == s.east) | \ | |
523 | (arr == s.west) | |
524 | num_path_sites = sum(sum(1.0*path_sites)) | |
525 | remaining_sites = num_start_sites + num_path_sites | |
526 | while (remaining_sites != 0): | |
527 | #print remaining_sites | |
528 | if (num_start_sites > 0): | |
529 | # | |
530 | # begin segment on a start state | |
531 | # | |
532 | if (argmax(start_sites[0,:],axis=0) != 0): | |
533 | x = argmax(start_sites[0,:],axis=0) | |
534 | y = 0 | |
535 | elif (argmax(start_sites[:,0],axis=0) != 0): | |
536 | x = 0 | |
537 | y = argmax(start_sites[:,0],axis=0) | |
538 | elif (argmax(start_sites[-1,:],axis=0) != 0): | |
539 | x = argmax(start_sites[-1,:],axis=0) | |
540 | y = cad.ny-1 | |
541 | elif (argmax(start_sites[:,-1],axis=0) != 0): | |
542 | x = cad.nx-1 | |
543 | y = argmax(start_sites[:,-1],axis=0) | |
544 | else: | |
545 | print "error: internal start" | |
546 | sys.exit() | |
547 | #print "start from ",x,y | |
548 | else: | |
549 | # | |
550 | # no start states; begin segment on upper-left boundary point | |
551 | # | |
552 | maxcols = argmax(path_sites,axis=1) | |
553 | y = argmax(argmax(path_sites,axis=1)) | |
554 | x = maxcols[y] | |
555 | arr[y][x] += s.edge | |
556 | #print "segment from ",x,y | |
557 | segment = [point(x,y)] | |
558 | vector = [point(x,y)] | |
559 | while 1: | |
560 | # | |
561 | # follow path | |
562 | # | |
563 | y = vector[-1].y | |
564 | x = vector[-1].x | |
565 | state = arr[y][x] | |
566 | # | |
567 | # if start state, set stop | |
568 | # | |
569 | if (state == (s.north + s.edge)): | |
570 | state = s.north | |
571 | arr[y][x] = s.stop | |
572 | elif (state == (s.south + s.edge)): | |
573 | state = s.south | |
574 | arr[y][x] = s.stop | |
575 | elif (state == (s.east + s.edge)): | |
576 | state = s.east | |
577 | arr[y][x] = s.stop | |
578 | elif (state == (s.west + s.edge)): | |
579 | state = s.west | |
580 | arr[y][x] = s.stop | |
581 | #print "x,y,state,arr: ",x,y,state,arr[y][x] | |
582 | # | |
583 | # move if a valid direction | |
584 | # | |
585 | if (state == s.north): | |
586 | direction = "north" | |
587 | #print "north" | |
588 | ynew = y - 1 | |
589 | xnew = x | |
590 | elif (state == s.south): | |
591 | direction = "south" | |
592 | #print "south" | |
593 | ynew = y + 1 | |
594 | xnew = x | |
595 | elif (state == s.east): | |
596 | direction = "east" | |
597 | #print "east" | |
598 | ynew = y | |
599 | xnew = x + 1 | |
600 | elif (state == s.west): | |
601 | direction = "west" | |
602 | #print "west" | |
603 | ynew = y | |
604 | xnew = x - 1 | |
605 | elif (state == s.corner): | |
606 | #print "corner" | |
607 | if (direction == "east"): | |
608 | #print "south" | |
609 | xnew = x | |
610 | ynew = y + 1 | |
611 | elif (direction == "west"): | |
612 | #print "north" | |
613 | xnew = x | |
614 | ynew = y - 1 | |
615 | elif (direction == "north"): | |
616 | #print "east" | |
617 | ynew = y | |
618 | xnew = x + 1 | |
619 | elif (direction == "south"): | |
620 | #print "west" | |
621 | ynew = y | |
622 | xnew = x - 1 | |
623 | else: | |
624 | # | |
625 | # not a valid direction, terminate segment on previous point | |
626 | # | |
627 | print "unexpected path termination at",x,y | |
628 | #sys.exit() | |
629 | segment.append(point(x,y)) | |
630 | toolpaths.append(segment) | |
631 | arr[y][x] = s.interior | |
632 | break | |
633 | #print "xnew,ynew,snew",xnew,ynew,arr[ynew][xnew] | |
634 | # | |
635 | # check if stop reached | |
636 | # | |
637 | if (arr[ynew][xnew] == s.stop): | |
638 | #print "stop at ",xnew,ynew | |
639 | segment.append(point(xnew,ynew)) | |
640 | toolpaths.extend([segment]) | |
641 | if (state != s.corner): | |
642 | arr[y][x] = s.interior | |
643 | arr[ynew][xnew] = s.interior | |
644 | break | |
645 | # | |
646 | # find max transverse distance from vector to new point | |
647 | # | |
648 | dmax = 0 | |
649 | dx = xnew - vector[0].x | |
650 | dy = ynew - vector[0].y | |
651 | norm = sqrt(dx**2 + dy**2) | |
652 | nx = dy / norm | |
653 | ny = -dx / norm | |
654 | for i in range(len(vector)): | |
655 | dx = vector[i].x - vector[0].x | |
656 | dy = vector[i].y - vector[0].y | |
657 | d = abs(nx*dx + ny*dy) | |
658 | if (d > dmax): | |
659 | dmax = d | |
660 | # | |
661 | # start new vector if transverse distance > max_dist | |
662 | # | |
663 | if (dmax >= max_dist): | |
664 | #print "max at ",x,y | |
665 | segment.append(point(x,y)) | |
666 | vector = [point(x,y)] | |
667 | # | |
668 | # otherwise add point to vector | |
669 | # | |
670 | else: | |
671 | #print "add ",xnew,ynew | |
672 | vector.append(point(xnew,ynew)) | |
673 | if ((arr[y][x] != s.corner) & (arr[y][x] != s.stop)): | |
674 | arr[y][x] = s.interior | |
675 | start_sites = (arr == (s.north+s.edge)) | (arr == (s.south+s.edge)) | \ | |
676 | (arr == (s.east+s.edge)) | (arr == (s.west+s.edge)) | |
677 | num_start_sites = sum(sum(1.0*start_sites)) | |
678 | path_sites = (arr == s.north) | (arr == s.south) | (arr == s.east) | \ | |
679 | (arr == s.west) | |
680 | num_path_sites = sum(sum(1.0*path_sites)) | |
681 | remaining_sites = num_start_sites + num_path_sites | |
682 | # | |
683 | # reverse segment order, to start from inside to out | |
684 | # | |
685 | newpaths = [] | |
686 | for segment in range(len(toolpaths)): | |
687 | newpaths.append(toolpaths[-1-segment]) | |
688 | root.update() | |
689 | return newpaths | |
690 | ||
691 | def evaluate(): | |
692 | # | |
693 | # evaluate .cad program/image | |
694 | # | |
695 | if (len(widget_cad_text.get("1.0",END)) > 1): | |
696 | # | |
697 | # .cad | |
698 | # | |
699 | cad.zlist = [] | |
700 | cad_text_string = widget_cad_text.get("1.0",END) | |
701 | exec cad_text_string in globals() | |
702 | widget_function_text.config(state=NORMAL) | |
703 | widget_function_text.delete("1.0",END) | |
704 | widget_function_text.insert("1.0",cad.function) | |
705 | widget_function_text.config(state=DISABLED) | |
706 | if (cad.image_r.size > 1): | |
707 | # | |
708 | # image | |
709 | # | |
710 | cad.xmin = float(string_image_xmin.get()) | |
711 | xwidth = float(string_image_xwidth.get()) | |
712 | cad.xmax = cad.xmin + xwidth | |
713 | cad.ymin = float(string_image_ymin.get()) | |
714 | yheight = float(string_image_yheight.get()) | |
715 | cad.ymax = cad.ymin + yheight | |
716 | cad.image_min = float(string_image_min.get()) | |
717 | cad.image_max = float(string_image_max.get()) | |
718 | cad.zmin = float(string_image_zmin.get()) | |
719 | cad.zmax = float(string_image_zmax.get()) | |
720 | cad.nz = int(string_image_nz.get()) | |
721 | cad.inches_per_unit = float(string_image_units.get()) | |
722 | ||
723 | def render(view='xyzr'): | |
724 | render_stop_flag = 0 | |
725 | cad.stop = 0 | |
726 | # | |
727 | # if .cad doesn't call render, delete windows and add stop button | |
728 | # | |
729 | if (find(widget_cad_text.get("1.0",END),"render(") == -1): | |
730 | string_msg.set("render ...") | |
731 | widget_stop.pack() | |
732 | delete_windows() | |
733 | # | |
734 | # initialize variables | |
735 | # | |
736 | cad.toolpaths = [] | |
737 | rx = pi*cad.rx/180. | |
738 | rz = pi*cad.rz/180. | |
739 | r = rule_table() | |
740 | s = CA_states() | |
741 | # | |
742 | # evaluate coordinate arrays | |
743 | # | |
744 | Xarray = outer(ones((cad.ny,1)),cad.xmin+(cad.xmax-cad.xmin)*arange(cad.nx)/(cad.nx-1.0)) | |
745 | Yarray = outer(cad.ymin+(cad.ymax-cad.ymin)*arange(cad.ny-1,-1,-1)/(cad.ny-1.0),ones((1,cad.nx))) | |
746 | if (cad.zlist == []): | |
747 | if ((cad.nz == 1) & (cad.image_r.size != 1)): | |
748 | cad.zlist = [cad.zmax] | |
749 | cad.view('xy') | |
750 | elif (cad.nz == 1): | |
751 | cad.zlist = [cad.zmin] | |
752 | cad.view('xy') | |
753 | else: | |
754 | cad.zlist = cad.zmin + (cad.zmax-cad.zmin)*arange(cad.nz)/(cad.nz-1.0) | |
755 | cad.view('xyzr') | |
756 | else: | |
757 | cad.nz = len(cad.zlist) | |
758 | cad.zmin = cad.zlist[0] | |
759 | cad.zmax = cad.zlist[-1] | |
760 | # | |
761 | # draw orthogonal views | |
762 | # | |
763 | X = Xarray | |
764 | Y = Yarray | |
765 | accum_r = zeros((cad.ny,cad.nx),uint32) | |
766 | accum_g = zeros((cad.ny,cad.nx),uint32) | |
767 | accum_b = zeros((cad.ny,cad.nx),uint32) | |
768 | im.intensity_yz = zeros((cad.ny,cad.nz),uint32) | |
769 | im.intensity_xz = zeros((cad.nz,cad.nx),uint32) | |
770 | im.intensity_xyz = zeros((cad.nz,cad.nx),uint32) | |
771 | for layer in range(cad.nz): | |
772 | # | |
773 | # check render stop button | |
774 | # | |
775 | if (cad.stop == 1): | |
776 | break | |
777 | # | |
778 | # xy view | |
779 | # | |
780 | Z = cad.zlist[layer] | |
781 | string_msg.set("render z = %.3f"%Z) | |
782 | # root.update() | |
783 | if (cad.image_r.size == 1): | |
784 | # | |
785 | # .cad | |
786 | # | |
787 | array_r = eval(cad.function) | |
788 | array_g = array_r | |
789 | array_b = array_r | |
790 | if ((cad.zmax == cad.zmin) | (cad.nz == 1)): | |
791 | zi = array([255],uint32) | |
792 | else: | |
793 | zi = array([55.0 + 200.0*layer/(cad.nz-1.0)],uint32) | |
794 | accum_r = where(((zi*array_r) > accum_r),(zi*array_r),accum_r) | |
795 | accum_g = where(((zi*array_g) > accum_g),(zi*array_g),accum_g) | |
796 | accum_b = where(((zi*array_b) > accum_b),(zi*array_b),accum_b) | |
797 | im.intensity_xy = (1 << 16)*accum_b + (1 << 8)*accum_g + (1 << 0)*accum_r | |
798 | else: | |
799 | # | |
800 | # bitmap | |
801 | # | |
802 | array_r = (cad.image_r[0,] >= (cad.image_min + (cad.image_max-cad.image_min)*(Z-cad.zmin)/float(cad.zmax-cad.zmin))) | |
803 | array_g = (cad.image_g[0,] >= (cad.image_min + (cad.image_max-cad.image_min)*(Z-cad.zmin)/float(cad.zmax-cad.zmin))) | |
804 | array_b = (cad.image_b[0,] >= (cad.image_min + (cad.image_max-cad.image_min)*(Z-cad.zmin)/float(cad.zmax-cad.zmin))) | |
805 | image_z = int(cad.image_min + (cad.image_max-cad.image_min)*(Z-cad.zmin)/float(cad.zmax-cad.zmin)) | |
806 | intensity_r = where((cad.image_r[0,] <= image_z),cad.image_r[0,],image_z) | |
807 | intensity_g = where((cad.image_g[0,] <= image_z),cad.image_g[0,],image_z) | |
808 | intensity_b = where((cad.image_b[0,] <= image_z),cad.image_b[0,],image_z) | |
809 | im.intensity_xy = (1 << 16)*intensity_b + (1 << 8)*intensity_g + (1 << 0)*intensity_r | |
810 | im.xy = Image.fromarray(im.intensity_xy,mode="RGBX") | |
811 | im.xy_draw = ImageDraw.Draw(im.xy) | |
812 | im.xy = im.xy.resize((cad.nxplot(),cad.nyplot())) | |
813 | images.xy = ImageTk.PhotoImage(im.xy) | |
814 | canvas_xy.create_image(cad.nplot/2,cad.nplot/2,image=images.xy) | |
815 | # root.update() | |
816 | # | |
817 | # find toolpaths if needed | |
818 | # | |
819 | ncontours = int(string_num_contours.get()) | |
820 | if (ncontours == -1): | |
821 | ncontours = 2**20 # a big number | |
822 | cad.toolpaths.append([]) | |
823 | """ | |
824 | if (ncontours != 0): | |
825 | # | |
826 | # grassfire convolve (to come) | |
827 | # | |
828 | interior = (array_r | array_g | array_b) | |
829 | print shape(X[interior]) | |
830 | conv_array = interior | |
831 | """ | |
832 | for contour in range(ncontours): | |
833 | # | |
834 | # check render stop button | |
835 | # | |
836 | if (cad.stop == 1): | |
837 | break | |
838 | # | |
839 | # convolve tool for contour | |
840 | # | |
841 | string_msg.set(" convolve tool ... ") | |
842 | # | |
843 | # FFT convolve | |
844 | # | |
845 | # root.update() | |
846 | tool_rad = float(string_tool_dia.get())/2.0 | |
847 | tool_dia = float(string_tool_dia.get()) | |
848 | tool_overlap = float(string_tool_overlap.get()) | |
849 | kernel_rad = tool_rad + contour*tool_overlap*tool_dia | |
850 | ikernel_rad = 1 + int(cad.nx*kernel_rad/(cad.xmax-cad.xmin)) | |
851 | if (ikernel_rad > (((cad.nx/2),(cad.ny/2))[(cad.ny/2) > (cad.nx/2)])): | |
852 | break | |
853 | kx = 1+outer(ones((2*ikernel_rad,1)),arange(2*ikernel_rad)) | |
854 | ky = 1+outer(arange(2*ikernel_rad),ones((1,2*ikernel_rad))) | |
855 | k = (((kx-ikernel_rad)**2 + (ky-ikernel_rad)**2) < ikernel_rad**2).astype('uint32') | |
856 | interior = (array_r == s.interior).astype('uint32') | |
857 | #tstart = time.time() | |
858 | conv = scipy.signal.signaltools.fftconvolve(interior,k,mode='same') | |
859 | conv = where(conv > 0.01,s.interior,0) | |
860 | conv_array = conv + (conv != s.interior)*array_r | |
861 | #tend = time.time() | |
862 | #print 'convolve:',tend-tstart | |
863 | # | |
864 | # use CA rule table to find edge directions | |
865 | # | |
866 | string_msg.set(" follow edges ... ") | |
867 | # root.update() | |
868 | state = evaluate_state(conv_array) | |
869 | toolpath = r.table[state] | |
870 | tool_array = toolpath + (toolpath == s.empty)*conv_array | |
871 | tool_intensity = \ | |
872 | ((0 << 16) + (0 << 8) + (0 << 0))*(tool_array == s.empty).astype('uint32') +\ | |
873 | ((255 << 16) + (255 << 8) + (255 << 0))*(tool_array == s.interior).astype('uint32') +\ | |
874 | (( 0 << 16) + ( 0 << 8) + (255 << 0))*(tool_array == s.north).astype('uint32') +\ | |
875 | (( 0 << 16) + (255 << 8) + ( 0 << 0))*(tool_array == s.south).astype('uint32') +\ | |
876 | ((255 << 16) + ( 0 << 8) + ( 0 << 0))*(tool_array == s.east).astype('uint32') +\ | |
877 | (( 0 << 16) + (255 << 8) + (255 << 0))*(tool_array == s.west ).astype('uint32') +\ | |
878 | ((128 << 16) + ( 0 << 8) + (128 << 0))*(tool_array == s.stop).astype('uint32') | |
879 | ||
880 | # | |
881 | # show CA | |
882 | # | |
883 | """ | |
884 | im.xy = Image.fromarray(tool_intensity,mode="RGBX") | |
885 | im.xy = im.xy.resize((cad.nplot,cad.nplot)) | |
886 | images.xy = ImageTk.PhotoImage(im.xy) | |
887 | canvas_xy.create_image(cad.nplot/2,cad.nplot/2,image=images.xy) | |
888 | """ | |
889 | # | |
890 | # vectorize contour | |
891 | # | |
892 | #tstart = time.time() | |
893 | string_msg.set(" vectorize ... ") | |
894 | # root.update() | |
895 | new_paths = vectorize_toolpaths(tool_array) | |
896 | if (len(new_paths) == 0): | |
897 | break | |
898 | cad.toolpaths[layer].extend(new_paths) | |
899 | #tend = time.time() | |
900 | #print 'vector:',tend-tstart | |
901 | # | |
902 | # draw toolpath | |
903 | # | |
904 | im.xy_draw = ImageDraw.Draw(im.xy) | |
905 | for segment in range(len(cad.toolpaths[layer])): | |
906 | x = cad.nxplot()*(cad.toolpaths[layer][segment][0].x+0.5)/float(cad.nx) | |
907 | y = cad.nyplot()*(cad.toolpaths[layer][segment][0].y+0.5)/float(cad.ny) | |
908 | for vertex in range(1,len(cad.toolpaths[layer][segment])): | |
909 | xnew = cad.nxplot()*(cad.toolpaths[layer][segment][vertex].x+0.5)/float(cad.nx) | |
910 | ynew = cad.nyplot()*(cad.toolpaths[layer][segment][vertex].y+0.5)/float(cad.ny) | |
911 | im.xy_draw.line([x,y,xnew,ynew],fill="#ffa0a0",width=1) | |
912 | x = xnew | |
913 | y = ynew | |
914 | # | |
915 | # show xy toolpath view | |
916 | # | |
917 | images.xy = ImageTk.PhotoImage(im.xy) | |
918 | canvas_xy.create_image(cad.nplot/2,cad.nplot/2,image=images.xy) | |
919 | # | |
920 | # add send_to button | |
921 | # | |
922 | string_send_to_time.set("") | |
923 | send_to_frame.pack() | |
924 | # root.update() | |
925 | # | |
926 | # draw labels | |
927 | # | |
928 | for label in range(len(cad.labels)): | |
929 | x = cad.nplot/2. + cad.nxplot()*(cad.labels[label].x-(cad.xmax+cad.xmin)/2.0)/(cad.xmax-cad.xmin) | |
930 | y = cad.nplot/2. - cad.nyplot()*(cad.labels[label].y-(cad.ymax+cad.ymin)/2.0)/(cad.ymax-cad.ymin) | |
931 | string = cad.labels[label].text | |
932 | size = cad.labels[label].size | |
933 | color = cad.labels[label].color | |
934 | anch = cad.labels[label].anchor | |
935 | canvas_xy.create_text(x,y,text=string,font=('arial',size,'bold'),fill=color,anchor=anch,justify=CENTER) | |
936 | # | |
937 | # draw origin | |
938 | # | |
939 | x0 = cad.nplot/2. + cad.nxplot()*(0-(cad.xmax+cad.xmin)/2.)/(cad.xmax-cad.xmin) | |
940 | y0 = cad.nplot/2. - cad.nyplot()*(0-(cad.ymax+cad.ymin)/2.)/(cad.ymax-cad.ymin) | |
941 | dxy = .025*cad.nplot | |
942 | canvas_xy.create_line([x0-dxy,y0,x0+dxy,y0],fill="green") | |
943 | canvas_xy.create_line([x0,y0-dxy,x0,y0+dxy],fill="green") | |
944 | # | |
945 | # yz view | |
946 | # | |
947 | if (cad.views == 'xyzr'): | |
948 | accum_yz_r = zeros(cad.ny,uint32) | |
949 | accum_yz_g = zeros(cad.ny,uint32) | |
950 | accum_yz_b = zeros(cad.ny,uint32) | |
951 | for vertex in range(cad.nx): | |
952 | xi = array([55.0 + 200.0*vertex/(cad.nx-1.0)],uint32) | |
953 | slice_r = array_r[:,vertex] | |
954 | slice_g = array_g[:,vertex] | |
955 | slice_b = array_b[:,vertex] | |
956 | accum_yz_r = where(((xi*slice_r) >= accum_yz_r),(xi*slice_r),accum_yz_r) | |
957 | accum_yz_g = where(((xi*slice_g) >= accum_yz_g),(xi*slice_g),accum_yz_g) | |
958 | accum_yz_b = where(((xi*slice_b) >= accum_yz_b),(xi*slice_b),accum_yz_b) | |
959 | im.intensity_yz[:,layer] = (1 << 16)*accum_yz_b + (1 << 8)*accum_yz_g + (1 << 0)*accum_yz_r | |
960 | im.yz = Image.fromarray(im.intensity_yz,mode="RGBX") | |
961 | im.yz = im.yz.transpose(Image.FLIP_LEFT_RIGHT) | |
962 | im.yz = im.yz.resize((cad.nzplot(),cad.nyplot())) | |
963 | images.yz = ImageTk.PhotoImage(im.yz) | |
964 | canvas_yz.create_image(cad.nplot/2,cad.nplot/2,image=images.yz) | |
965 | # | |
966 | # draw origin | |
967 | # | |
968 | z0 = cad.nplot/2. - cad.nzplot()*(0-(cad.zmax+cad.zmin)/2.)/(cad.zmax-cad.zmin) | |
969 | y0 = cad.nplot/2. - cad.nyplot()*(0-(cad.ymax+cad.ymin)/2.)/(cad.ymax-cad.ymin) | |
970 | canvas_yz.create_line([z0-dxy,y0,z0+dxy,y0],fill="green") | |
971 | canvas_yz.create_line([z0,y0-dxy,z0,y0+dxy],fill="green") | |
972 | # | |
973 | # xz view | |
974 | # | |
975 | if (cad.views == 'xyzr'): | |
976 | accum_xz_r = zeros(cad.nx,uint32) | |
977 | accum_xz_g = zeros(cad.nx,uint32) | |
978 | accum_xz_b = zeros(cad.nx,uint32) | |
979 | for vertex in range(cad.ny): | |
980 | yi = array([55.0+200.0*vertex/(cad.ny-1.0)],uint32) | |
981 | slice_r = array_r[vertex,:] | |
982 | slice_g = array_g[vertex,:] | |
983 | slice_b = array_b[vertex,:] | |
984 | accum_xz_r = where(((yi*slice_r) >= accum_xz_r),(yi*slice_r),accum_xz_r) | |
985 | accum_xz_g = where(((yi*slice_g) >= accum_xz_g),(yi*slice_g),accum_xz_g) | |
986 | accum_xz_b = where(((yi*slice_b) >= accum_xz_b),(yi*slice_b),accum_xz_b) | |
987 | im.intensity_xz[(cad.nz-1-layer),:] = (1 << 16)*accum_xz_b + (1 << 8)*accum_xz_g + (1 << 0)*accum_xz_r | |
988 | im.xz = Image.fromarray(im.intensity_xz,mode="RGBX") | |
989 | im.xz = im.xz.resize((cad.nxplot(),cad.nzplot())) | |
990 | images.xz = ImageTk.PhotoImage(im.xz) | |
991 | canvas_xz.create_image(cad.nplot/2,cad.nplot/2,image=images.xz) | |
992 | # | |
993 | # draw origin | |
994 | # | |
995 | x0 = cad.nplot/2. + cad.nxplot()*(0-(cad.xmax+cad.xmin)/2.)/(cad.xmax-cad.xmin) | |
996 | z0 = cad.nplot/2. - cad.nzplot()*(0-(cad.zmax+cad.zmin)/2.)/(cad.zmax-cad.zmin) | |
997 | canvas_xz.create_line([x0-dxy,z0,x0+dxy,z0],fill="green") | |
998 | canvas_xz.create_line([x0,z0-dxy,x0,z0+dxy],fill="green") | |
999 | # | |
1000 | # draw it | |
1001 | # | |
1002 | root.update() | |
1003 | # | |
1004 | # rotated view | |
1005 | # | |
1006 | if ((cad.views == 'xyzr') & (cad.image_r.size == 1)): | |
1007 | accum = zeros((cad.ny,cad.nx),uint32) | |
1008 | for z in cad.zlist: | |
1009 | # | |
1010 | # check render stop button | |
1011 | # | |
1012 | if (cad.stop == 1): | |
1013 | break | |
1014 | string_msg.set("render z = %.3f"%z) | |
1015 | dY = cos(rx)*(Yarray-(cad.ymax+cad.ymin)/2.0) - sin(rx)*(z-(cad.zmax+cad.zmin)/2.0) | |
1016 | Z = (cad.zmax+cad.zmin)/2.0 + sin(rx)*(Yarray-(cad.ymax+cad.ymin)/2.0) + cos(rx)*(z-(cad.zmax+cad.zmin)/2.0) | |
1017 | X = (cad.xmax+cad.xmin)/2.0 + cos(rz)*(Xarray-(cad.xmax+cad.xmin)/2.0) - sin(rz)*dY | |
1018 | Y = (cad.ymax+cad.ymin)/2.0 + sin(rz)*(Xarray-(cad.xmax+cad.xmin)/2.0) + cos(rz)*dY | |
1019 | arr = eval(cad.function) | |
1020 | if (cad.zmax == cad.zmin): | |
1021 | zi = array([255],uint32) | |
1022 | else: | |
1023 | zi = array([55.0 + 200.0*(z-cad.zmin)/(cad.zmax-cad.zmin)],uint32) | |
1024 | accum = where(((zi*arr) > accum),(zi*arr),accum) | |
1025 | im.intensity_xyz = ((1 << 16) + (1 << 8) + (1 << 0)) * accum | |
1026 | im.xyz = Image.fromarray(im.intensity_xyz,mode="RGBX") | |
1027 | im.xyz = im.xyz.resize((cad.nxplot(),cad.nyplot())) | |
1028 | images.xyz = ImageTk.PhotoImage(im.xyz) | |
1029 | canvas_xyz.create_image(cad.nplot/2,cad.nplot/2,image=images.xyz) | |
1030 | root.update() | |
1031 | # | |
1032 | # return | |
1033 | # | |
1034 | cad.zwrite = cad.zlist | |
1035 | cad.zlist = [] | |
1036 | widget_stop.pack_forget() | |
1037 | string_msg.set("done") | |
1038 | root.update() | |
1039 | return | |
1040 | ||
1041 | def draw_toolpath(): | |
1042 | im.xy = Image.new("RGBX",(cad.nxplot(),cad.nyplot()),'white') | |
1043 | im.xy_draw = ImageDraw.Draw(im.xy) | |
1044 | for layer in range(len(cad.toolpaths)): | |
1045 | for segment in range(len(cad.toolpaths[layer])): | |
1046 | x = cad.nxplot()*(cad.toolpaths[layer][segment][0].x+0.5)/float(cad.nx) | |
1047 | y = cad.nyplot()*(cad.toolpaths[layer][segment][0].y+0.5)/float(cad.ny) | |
1048 | for vertex in range(1,len(cad.toolpaths[layer][segment])): | |
1049 | xnew = cad.nxplot()*(cad.toolpaths[layer][segment][vertex].x+0.5)/float(cad.nx) | |
1050 | ynew = cad.nyplot()*(cad.toolpaths[layer][segment][vertex].y+0.5)/float(cad.ny) | |
1051 | im.xy_draw.line([x,y,xnew,ynew],fill="black") | |
1052 | x = xnew | |
1053 | y = ynew | |
1054 | images.xy = ImageTk.PhotoImage(im.xy) | |
1055 | canvas_xy.create_image(cad.nplot/2,cad.nplot/2,image=images.xy) | |
1056 | ||
1057 | def delete_windows(): | |
1058 | im.xy = Image.new("RGBX",(cad.nplot,cad.nplot),'black') | |
1059 | images.xy = ImageTk.PhotoImage(im.xy) | |
1060 | canvas_xy.create_image(cad.nplot/2,cad.nplot/2,image=images.xy) | |
1061 | im.yz = Image.new("RGBX",(cad.nplot,cad.nplot),'black') | |
1062 | images.yz = ImageTk.PhotoImage(im.yz) | |
1063 | canvas_yz.create_image(cad.nplot/2,cad.nplot/2,image=images.yz) | |
1064 | im.xz = Image.new("RGBX",(cad.nplot,cad.nplot),'black') | |
1065 | images.xz = ImageTk.PhotoImage(im.xz) | |
1066 | canvas_xz.create_image(cad.nplot/2,cad.nplot/2,image=images.xz) | |
1067 | im.xyz = Image.new("RGBX",(cad.nplot,cad.nplot),'black') | |
1068 | images.xyz = ImageTk.PhotoImage(im.xyz) | |
1069 | canvas_xyz.create_image(cad.nplot/2,cad.nplot/2,image=images.xyz) | |
1070 | root.update() | |
1071 | ||
1072 | def select_cad(): | |
1073 | image_x_frame.pack_forget() | |
1074 | image_y_frame.pack_forget() | |
1075 | image_z_frame.pack_forget() | |
1076 | image_intensity_frame.pack_forget() | |
1077 | image_units_frame.pack_forget() | |
1078 | image_invert_frame.pack_forget() | |
1079 | cad_input_frame.pack_forget() | |
1080 | widget_cad_text.delete("1.0",END) | |
1081 | widget_cad_text.insert("1.0",cad_template) | |
1082 | editor_frame.pack() | |
1083 | cad.image = array(0) | |
1084 | cad_input_frame.pack() | |
1085 | cad.toolpaths = [] | |
1086 | string_num_contours.set('0') | |
1087 | widget_cad_save.pack(side='left') | |
1088 | delete_windows() | |
1089 | ||
1090 | def select_image(): | |
1091 | editor_frame.pack_forget() | |
1092 | cad_input_frame.pack_forget() | |
1093 | image_x_frame.pack() | |
1094 | image_y_frame.pack() | |
1095 | image_z_frame.pack() | |
1096 | image_intensity_frame.pack() | |
1097 | image_units_frame.pack() | |
1098 | image_invert_frame.pack() | |
1099 | cad_input_frame.pack() | |
1100 | cad.toolpaths = [] | |
1101 | string_num_contours.set('0') | |
1102 | widget_cad_save.pack_forget() | |
1103 | delete_windows() | |
1104 | ||
1105 | def input_open(): | |
1106 | filename = askopenfilename() | |
1107 | string_input_file.set(filename) | |
1108 | if (find(filename,'.cad') != -1): | |
1109 | cad_load(0) | |
1110 | elif ((find(filename,'.jpg') != -1) | (find(filename,'.JPG') != -1) | | |
1111 | (find(filename,'.png') != -1) | (find(filename,'.PNG') != -1) | | |
1112 | (find(filename,'.gif') != -1) | (find(filename,'.GIF') != -1)): | |
1113 | widget_cad_text.delete("1.0",END) | |
1114 | image_load(0) | |
1115 | else: | |
1116 | string_msg.set("unsupported input file format") | |
1117 | root.update() | |
1118 | ||
1119 | def cad_load(event): | |
1120 | global cad | |
1121 | cad = cad_variables() | |
1122 | cam_pack_forget() | |
1123 | select_cad() | |
1124 | input_file_name = string_input_file.get() | |
1125 | input_file = open(input_file_name,'rb') | |
1126 | cad_text_string = input_file.read() | |
1127 | widget_cad_text.delete("1.0",END) | |
1128 | widget_cad_text.insert("1.0",cad_text_string) | |
1129 | input_file.close() | |
1130 | cad.toolpaths = [] | |
1131 | cad.image = array(0) | |
1132 | cad.nz = 1 | |
1133 | string_num_contours.set('0') | |
1134 | evaluate() | |
1135 | if (find(widget_cad_text.get("1.0",END),"render(") == -1): | |
1136 | render() | |
1137 | ||
1138 | def image_load(event): | |
1139 | global cad | |
1140 | cad = cad_variables() | |
1141 | cam_pack_forget() | |
1142 | select_image() | |
1143 | function_string_frame.pack_forget() | |
1144 | input_file_name = string_input_file.get() | |
1145 | input_file = open(input_file_name,'rb') | |
1146 | input_file.close() | |
1147 | cad.toolpaths = [] | |
1148 | string_num_contours.set('0') | |
1149 | image = Image.open(input_file_name) | |
1150 | num_layers = 1 | |
1151 | while 1: # check number of layers | |
1152 | try: | |
1153 | image.seek(image.tell()+1) | |
1154 | num_layers += 1 | |
1155 | except: | |
1156 | break | |
1157 | image = Image.open(input_file_name) | |
1158 | if image.mode != "RGBX": | |
1159 | image = image.convert("RGBX") | |
1160 | (cad.nx,cad.ny) = image.size | |
1161 | info = image.info | |
1162 | if ('dpi' in info): | |
1163 | (xdpi,ydpi) = info['dpi'] | |
1164 | else: | |
1165 | xdpi = cad.nx | |
1166 | ydpi = xdpi | |
1167 | string_image_nx.set(" nx = "+str(cad.nx)) | |
1168 | string_image_ny.set(" ny = "+str(cad.ny)) | |
1169 | cad.nz = 1 | |
1170 | string_image_nz.set(str(cad.nz)) | |
1171 | cad.xmin = 0 | |
1172 | string_image_xmin.set('0') | |
1173 | cad.xmax = cad.nx/float(xdpi) | |
1174 | string_image_xwidth.set(str(cad.xmax-cad.xmin)) | |
1175 | cad.ymin = 0 | |
1176 | string_image_ymin.set('0') | |
1177 | cad.ymax = cad.ny/float(ydpi) | |
1178 | string_image_yheight.set(str(cad.ymax-cad.ymin)) | |
1179 | cad.zmin = -.005 | |
1180 | string_image_zmin.set('-0.05') | |
1181 | cad.zmax = 0.05 | |
1182 | string_image_zmax.set('0.05') | |
1183 | cad.inches_per_unit = 1.0 | |
1184 | string_image_units.set('25.4') | |
1185 | data = zeros((num_layers,cad.nx*cad.ny,3),uint32) | |
1186 | data[0,] = array(image.convert("RGB").getdata(),uint32) | |
1187 | for layer in range(1,num_layers): | |
1188 | image.seek(image.tell()+1) | |
1189 | data[layer,] = array(image.convert("RGB").getdata(),uint32) | |
1190 | cad.image_r = array(data[:,:,0],uint32) | |
1191 | cad.image_r = cad.image_r.reshape((num_layers,cad.ny,cad.nx)) | |
1192 | cad.image_g = array(data[:,:,1],uint32) | |
1193 | cad.image_g = cad.image_g.reshape((num_layers,cad.ny,cad.nx)) | |
1194 | cad.image_b = array(data[:,:,2],uint32) | |
1195 | cad.image_b = cad.image_b.reshape((num_layers,cad.ny,cad.nx)) | |
1196 | cad.image_min = 1 | |
1197 | string_image_min.set(str(cad.image_min)) | |
1198 | cad.image_max = 255 | |
1199 | string_image_max.set(str(cad.image_max)) | |
1200 | evaluate() | |
1201 | render() | |
1202 | ||
1203 | def invert_image(event): | |
1204 | cad.image_r = 255 - cad.image_r | |
1205 | cad.image_g = 255 - cad.image_g | |
1206 | cad.image_b = 255 - cad.image_b | |
1207 | evaluate() | |
1208 | render() | |
1209 | ||
1210 | def cad_save(event): | |
1211 | input_file_name = string_input_file.get() | |
1212 | input_file = open(input_file_name,'wb') | |
1213 | cad_text_string = widget_cad_text.get("1.0",END) | |
1214 | input_file.write(cad_text_string) | |
1215 | input_file.close() | |
1216 | string_msg.set(input_file_name+" saved") | |
1217 | root.update() | |
1218 | ||
1219 | def render_button(event): | |
1220 | cam_pack_forget() | |
1221 | cad.cam = '' | |
1222 | if (cad.image_r.size == 1): | |
1223 | function_string_frame.pack() | |
1224 | cad.toolpaths = [] | |
1225 | string_num_contours.set('0') | |
1226 | evaluate() | |
1227 | if (find(widget_cad_text.get("1.0",END),"render(") == -1): | |
1228 | render() | |
1229 | ||
1230 | def render_stop(event): | |
1231 | cad.stop = 1 | |
1232 | widget_stop.pack_forget() | |
1233 | ||
1234 | def cam(event): | |
1235 | function_string_frame.pack_forget() | |
1236 | cam_file_frame.pack() | |
1237 | string_num_contours.set('1') | |
1238 | root.update() | |
1239 | ||
1240 | def contour(event): | |
1241 | evaluate() | |
1242 | if (find(widget_cad_text.get("1.0",END),"render(") == -1): | |
1243 | render() | |
1244 | ||
1245 | def triangulate(event): | |
1246 | # | |
1247 | # triangulate for STL | |
1248 | # | |
1249 | # evaluate .cad | |
1250 | # | |
1251 | evaluate() | |
1252 | # | |
1253 | # initialize variables | |
1254 | # | |
1255 | render_stop_flag = 0 | |
1256 | cad.stop = 0 | |
1257 | widget_stop.pack() | |
1258 | delete_windows() | |
1259 | cad.toolpaths = [] | |
1260 | cad.zwrite = [] | |
1261 | cad.x = zeros(0) | |
1262 | cad.y = zeros(0) | |
1263 | cad.z = zeros(0) | |
1264 | ixlr = array([]) | |
1265 | iylrs = array([]) | |
1266 | iylre = array([]) | |
1267 | izlr = array([]) | |
1268 | ixfbs = array([]) | |
1269 | ixfbe = array([]) | |
1270 | iyfb = array([]) | |
1271 | izfb = array([]) | |
1272 | ixtbs = array([]) | |
1273 | ixtbe = array([]) | |
1274 | iytb = array([]) | |
1275 | iztb = array([]) | |
1276 | # | |
1277 | # evaluate coordinate arrays | |
1278 | # | |
1279 | (IY,IX) = indices((cad.ny,cad.nx)) | |
1280 | IY = IY[::-1,:] | |
1281 | X = cad.xmin+(cad.xmax-cad.xmin)*IX/(cad.nx-1.0) | |
1282 | Y = cad.ymin+(cad.ymax-cad.ymin)*IY/(cad.ny-1.0) | |
1283 | cad.zwrite = cad.zmin + (cad.zmax-cad.zmin)*arange(cad.nz)/(cad.nz-1.0) | |
1284 | # | |
1285 | # set up drawing images | |
1286 | # | |
1287 | im.xy = Image.new("RGBX",(cad.nxplot(),cad.nyplot()),'white') | |
1288 | im.xy_draw = ImageDraw.Draw(im.xy) | |
1289 | im.xz = Image.new("RGBX",(cad.nxplot(),cad.nzplot()),'white') | |
1290 | im.xz_draw = ImageDraw.Draw(im.xz) | |
1291 | im.yz = Image.new("RGBX",(cad.nzplot(),cad.nyplot()),'white') | |
1292 | im.yz_draw = ImageDraw.Draw(im.yz) | |
1293 | # | |
1294 | # loop over layers | |
1295 | # | |
1296 | Z = cad.zwrite[0] | |
1297 | array0 = eval(cad.function) | |
1298 | Z = cad.zwrite[1] | |
1299 | array1 = eval(cad.function) | |
1300 | for layer in range(2,len(cad.zwrite)): | |
1301 | # | |
1302 | # check render stop button | |
1303 | # | |
1304 | if (cad.stop == 1): | |
1305 | break | |
1306 | # | |
1307 | # evaluate new layer | |
1308 | # | |
1309 | Z = cad.zwrite[layer] | |
1310 | string_msg.set("triangulate z = %.3f"%Z) | |
1311 | root.update() | |
1312 | array2 = eval(cad.function) | |
1313 | # | |
1314 | # find left faces and merge y | |
1315 | # | |
1316 | elements = hstack((reshape((array1[:,0] == True),(cad.ny,1)),((array1[:,1:] == True) & (array1[:,:-1] == False)))) | |
1317 | starts = vstack((((elements[:-1,:] == True) & (elements[1:,:] == False)),reshape((elements[-1,:] == True),(1,cad.nx)))) | |
1318 | ends = vstack((reshape((elements[0,:] == True),(1,cad.nx)),((elements[1:,:] == True) & (elements[:-1,:] == False)))) | |
1319 | IY_t = transpose(IY) # for starts and ends to be read in same row | |
1320 | IX_t = transpose(IX) | |
1321 | starts_t = transpose(starts) | |
1322 | ends_t = transpose(ends) | |
1323 | ixlr = append(ixlr,IX_t[starts_t]) | |
1324 | iylrs = append(iylrs,IY_t[starts_t]) | |
1325 | iylre = append(iylre,1+IY_t[ends_t]) | |
1326 | izlr = append(izlr,(layer-1)*ones(len(IX_t[starts_t]))) | |
1327 | # | |
1328 | # find right faces and merge y | |
1329 | # | |
1330 | elements = hstack((((array1[:,1:] == False) & (array1[:,:-1] == True)),reshape((array1[:,1] == True),(cad.ny,1)))) | |
1331 | starts = vstack((((elements[:-1,:] == True) & (elements[1:,:] == False)),reshape((elements[-1,:] == True),(1,cad.nx)))) | |
1332 | ends = vstack((reshape((elements[0,:] == True),(1,cad.nx)),((elements[1:,:] == True) & (elements[:-1,:] == False)))) | |
1333 | IY_t = transpose(IY) # for starts and ends to be read in same row | |
1334 | IX_t = transpose(IX) | |
1335 | starts_t = transpose(starts) | |
1336 | ends_t = transpose(ends) | |
1337 | ixlr = append(ixlr,1+IX_t[starts_t]) | |
1338 | iylre = append(iylre,IY_t[starts_t]) | |
1339 | iylrs = append(iylrs,1+IY_t[ends_t]) | |
1340 | izlr = append(izlr,(layer-1)*ones(len(IX_t[starts_t]))) | |
1341 | # | |
1342 | # find front faces and merge x | |
1343 | # | |
1344 | elements = vstack((((array1[:-1,:] == True) & (array1[1:,:] == False)),reshape((array1[0,:] == True),(1,cad.nx)))) | |
1345 | starts = hstack((reshape((elements[:,0] == True),(cad.ny,1)),((elements[:,1:] == True) & (elements[:,:-1] == False)))) | |
1346 | ends = hstack((((elements[:,:-1] == True) & (elements[:,1:] == False)),reshape((elements[:,-1] == True),(cad.ny,1)))) | |
1347 | ixfbs = append(ixfbs,IX[starts]) | |
1348 | ixfbe = append(ixfbe,1+IX[ends]) | |
1349 | iyfb = append(iyfb,IY[starts]) | |
1350 | izfb = append(izfb,(layer-1)*ones(len(IX[starts]))) | |
1351 | # | |
1352 | # find back faces and merge x | |
1353 | # | |
1354 | elements = vstack((reshape((array1[-1,:] == True),(1,cad.nx)),((array1[1:,:] == True) & (array1[:-1,:] == False)))) | |
1355 | starts = hstack((reshape((elements[:,0] == True),(cad.ny,1)),((elements[:,1:] == True) & (elements[:,:-1] == False)))) | |
1356 | ends = hstack((((elements[:,:-1] == True) & (elements[:,1:] == False)),reshape((elements[:,-1] == True),(cad.ny,1)))) | |
1357 | ixfbe = append(ixfbe,IX[starts]) | |
1358 | ixfbs = append(ixfbs,1+IX[ends]) | |
1359 | iyfb = append(iyfb,1+IY[starts]) | |
1360 | izfb = append(izfb,(layer-1)*ones(len(IX[starts]))) | |
1361 | # | |
1362 | # find top faces and merge x | |
1363 | # | |
1364 | elements = ((array2 == False) & (array1 == True)) | |
1365 | starts = hstack((reshape((elements[:,0] == True),(cad.ny,1)),((elements[:,1:] == True) & (elements[:,:-1] == False)))) | |
1366 | ends = hstack((((elements[:,:-1] == True) & (elements[:,1:] == False)),reshape((elements[:,-1] == True),(cad.ny,1)))) | |
1367 | ixtbs = append(ixtbs,IX[starts]) | |
1368 | ixtbe = append(ixtbe,1+IX[ends]) | |
1369 | iytb = append(iytb,IY[starts]) | |
1370 | iztb = append(iztb,layer*ones(len(IX[starts]))) | |
1371 | # | |
1372 | # find bottom faces and merge x | |
1373 | # | |
1374 | elements = ((array0 == False) & (array1 == True)) | |
1375 | starts = hstack((reshape((elements[:,0] == True),(cad.ny,1)),((elements[:,1:] == True) & (elements[:,:-1] == False)))) | |
1376 | ends = hstack((((elements[:,:-1] == True) & (elements[:,1:] == False)),reshape((elements[:,-1] == True),(cad.ny,1)))) | |
1377 | ixtbe = append(ixtbe,IX[starts]) | |
1378 | ixtbs = append(ixtbs,1+IX[ends]) | |
1379 | iytb = append(iytb,IY[starts]) | |
1380 | iztb = append(iztb,(layer-1)*ones(len(IX[starts]))) | |
1381 | # | |
1382 | # push array stack | |
1383 | # | |
1384 | array0 = array1 | |
1385 | array1 = array2 | |
1386 | # | |
1387 | # z merge front/back faces | |
1388 | # | |
1389 | index = lexsort(keys=(izfb,ixfbe,ixfbs,iyfb)) | |
1390 | merge = (iyfb[index[1:]] == iyfb[index[:-1]]) & \ | |
1391 | (ixfbe[index[1:]] == ixfbe[index[:-1]]) & \ | |
1392 | (ixfbs[index[1:]] == ixfbs[index[:-1]]) & \ | |
1393 | ((izfb[index[1:]] - izfb[index[:-1]]) == 1) | |
1394 | merge = append(False,merge).astype(bool_) | |
1395 | starts = ((merge[1:] == True) & (merge[:-1] == False)) | |
1396 | starts = append(starts,False).astype(bool_) | |
1397 | ends = ((merge[1:] == False) & (merge[:-1] == True)) | |
1398 | if (merge[-1] == True): | |
1399 | ends = append(ends,True) | |
1400 | else: | |
1401 | ends = append(ends,False) | |
1402 | ends = ends.astype(bool_) | |
1403 | xs = ixfbs[index][starts | ~merge] | |
1404 | xe = ixfbe[index][starts | ~merge] | |
1405 | y = iyfb[index][starts | ~merge] | |
1406 | zs = izfb[index][starts | ~merge] | |
1407 | ze = izfb[index][ends | ~(merge | starts)]+1 | |
1408 | cad.x = ravel(transpose(vstack((xs,xe,xs,xs,xe,xe)))) | |
1409 | cad.y = ravel(transpose(vstack((y,y,y,y,y,y)))) | |
1410 | cad.z = ravel(transpose(vstack((zs,ze,ze,zs,zs,ze)))) | |
1411 | # | |
1412 | # z merge left/right faces | |
1413 | # | |
1414 | index = lexsort(keys=(izlr,iylre,iylrs,ixlr)) | |
1415 | merge = (ixlr[index[1:]] == ixlr[index[:-1]]) & \ | |
1416 | (iylre[index[1:]] == iylre[index[:-1]]) & \ | |
1417 | (iylrs[index[1:]] == iylrs[index[:-1]]) & \ | |
1418 | ((izlr[index[1:]] - izlr[index[:-1]]) == 1) | |
1419 | merge = append(False,merge).astype(bool_) | |
1420 | starts = ((merge[1:] == True) & (merge[:-1] == False)) | |
1421 | starts = append(starts,False).astype(bool_) | |
1422 | ends = ((merge[1:] == False) & (merge[:-1] == True)) | |
1423 | if (merge[-1] == True): | |
1424 | ends = append(ends,True) | |
1425 | else: | |
1426 | ends = append(ends,False) | |
1427 | ends = ends.astype(bool_) | |
1428 | x = ixlr[index][starts | ~merge] | |
1429 | ys = iylrs[index][starts | ~merge] | |
1430 | ye = iylre[index][starts | ~merge] | |
1431 | zs = izlr[index][starts | ~merge] | |
1432 | ze = izlr[index][ends | ~(merge | starts)]+1 | |
1433 | cad.x = append(cad.x,ravel(transpose(vstack((x,x,x,x,x,x))))) | |
1434 | cad.y = append(cad.y,ravel(transpose(vstack((ys,ye,ys,ys,ye,ye))))) | |
1435 | cad.z = append(cad.z,ravel(transpose(vstack((zs,ze,ze,zs,zs,ze))))) | |
1436 | # | |
1437 | # y merge top/bottom faces | |
1438 | # | |
1439 | index = lexsort(keys=(iytb,ixtbe,ixtbs,iztb)) | |
1440 | merge = (iztb[index[1:]] == iztb[index[:-1]]) & \ | |
1441 | (ixtbe[index[1:]] == ixtbe[index[:-1]]) & \ | |
1442 | (ixtbs[index[1:]] == ixtbs[index[:-1]]) & \ | |
1443 | ((iytb[index[1:]] - iytb[index[:-1]]) == 1) | |
1444 | merge = append(False,merge).astype(bool_) | |
1445 | starts = ((merge[1:] == True) & (merge[:-1] == False)) | |
1446 | starts = append(starts,False).astype(bool_) | |
1447 | ends = ((merge[1:] == False) & (merge[:-1] == True)) | |
1448 | if (merge[-1] == True): | |
1449 | ends = append(ends,True) | |
1450 | else: | |
1451 | ends = append(ends,False) | |
1452 | ends = ends.astype(bool_) | |
1453 | xs = ixtbs[index][starts | ~merge] | |
1454 | xe = ixtbe[index][starts | ~merge] | |
1455 | ys = iytb[index][starts | ~merge] | |
1456 | ye = iytb[index][ends | ~(merge | starts)]+1 | |
1457 | z = iztb[index][starts | ~merge] | |
1458 | cad.x = append(cad.x,ravel(transpose(vstack((xs,xe,xs,xs,xe,xe))))) | |
1459 | cad.y = append(cad.y,ravel(transpose(vstack((ys,ye,ye,ys,ys,ye))))) | |
1460 | cad.z = append(cad.z,ravel(transpose(vstack((z,z,z,z,z,z))))) | |
1461 | # | |
1462 | # draw triangulation | |
1463 | # | |
1464 | widget_stop.pack_forget() | |
1465 | string_msg.set("draw ...") | |
1466 | root.update() | |
1467 | N = len(cad.x) | |
1468 | for i in range(0,N,3): | |
1469 | string_msg.set("draw triangle %d/%d"%(i/3,N/3)) | |
1470 | root.update() | |
1471 | x0 = cad.nxplot()*(cad.x[i]+0.5)/float(cad.nx) | |
1472 | y0 = cad.nyplot()*(cad.ny-cad.y[i]+0.5)/float(cad.ny) | |
1473 | z0 = cad.nzplot()*(cad.nz-cad.z[i]+0.5)/float(cad.nz) | |
1474 | x1 = cad.nxplot()*(cad.x[i+1]+0.5)/float(cad.nx) | |
1475 | y1 = cad.nyplot()*(cad.ny-cad.y[i+1]+0.5)/float(cad.ny) | |
1476 | z1 = cad.nzplot()*(cad.nz-cad.z[i+1]+0.5)/float(cad.nz) | |
1477 | x2 = cad.nxplot()*(cad.x[i+2]+0.5)/float(cad.nx) | |
1478 | y2 = cad.nyplot()*(cad.ny-cad.y[i+2]+0.5)/float(cad.ny) | |
1479 | z2 = cad.nzplot()*(cad.nz-cad.z[i+2]+0.5)/float(cad.nz) | |
1480 | im.xy_draw.line([x0,y0,x1,y1,x2,y2,x0,y0],fill="black") | |
1481 | im.xz_draw.line([x0,z0,x1,z1,x2,z2,x0,z0],fill="black") | |
1482 | im.yz_draw.line([z0,y0,z1,y1,z2,y2,z0,y0],fill="black") | |
1483 | images.xy = ImageTk.PhotoImage(im.xy) | |
1484 | images.xz = ImageTk.PhotoImage(im.xz) | |
1485 | images.yz = ImageTk.PhotoImage(im.yz) | |
1486 | canvas_xy.create_image(cad.nplot/2,cad.nplot/2,image=images.xy) | |
1487 | canvas_xz.create_image(cad.nplot/2,cad.nplot/2,image=images.xz) | |
1488 | canvas_yz.create_image(cad.nplot/2,cad.nplot/2,image=images.yz) | |
1489 | im.xyz = Image.new("RGBX",(cad.nplot,cad.nplot),'white') | |
1490 | images.xyz = ImageTk.PhotoImage(im.xyz) | |
1491 | canvas_xyz.create_image(cad.nplot/2,cad.nplot/2,image=images.xyz) | |
1492 | string_msg.set("done") | |
1493 | root.update() | |
1494 | ||
1495 | def flash(event): | |
1496 | # | |
1497 | # convert to Gerber flashes | |
1498 | # | |
1499 | # evaluate .cad | |
1500 | # | |
1501 | evaluate() | |
1502 | # | |
1503 | # initialize variables | |
1504 | # | |
1505 | render_stop_flag = 0 | |
1506 | cad.stop = 0 | |
1507 | widget_stop.pack() | |
1508 | delete_windows() | |
1509 | cad.toolpaths = [] | |
1510 | cad.zwrite = [] | |
1511 | cad.x = zeros(0) | |
1512 | cad.y = zeros(0) | |
1513 | cad.z = zeros(0) | |
1514 | ixs = array([]) | |
1515 | ixe = array([]) | |
1516 | iy = array([]) | |
1517 | iz = array([]) | |
1518 | # | |
1519 | # evaluate coordinate arrays | |
1520 | # | |
1521 | (IY,IX) = indices((cad.ny,cad.nx)) | |
1522 | IY = IY[::-1,:] | |
1523 | IZ = arange(cad.nz) | |
1524 | X = cad.xmin+(cad.xmax-cad.xmin)*IX/(cad.nx-1.0) | |
1525 | Y = cad.ymin+(cad.ymax-cad.ymin)*IY/(cad.ny-1.0) | |
1526 | if (cad.zwrite == []): | |
1527 | if (cad.nz > 1): | |
1528 | cad.zwrite = cad.zmin + (cad.zmax-cad.zmin)*arange(cad.nz)/(cad.nz-1.0) | |
1529 | else: | |
1530 | cad.zwrite = [cad.zmin] | |
1531 | # | |
1532 | # set up drawing image | |
1533 | # | |
1534 | im.xy = Image.new("RGBX",(cad.nxplot(),cad.nyplot()),'white') | |
1535 | im.xy_draw = ImageDraw.Draw(im.xy) | |
1536 | # | |
1537 | # loop over layers | |
1538 | # | |
1539 | for layer in range(len(cad.zwrite)): | |
1540 | # | |
1541 | # check render stop button | |
1542 | # | |
1543 | if (cad.stop == 1): | |
1544 | break | |
1545 | # | |
1546 | # evaluate layer | |
1547 | # | |
1548 | Z = cad.zwrite[layer] | |
1549 | string_msg.set("convert z = %.3f"%Z) | |
1550 | root.update() | |
1551 | elements = eval(cad.function) | |
1552 | # | |
1553 | # merge x | |
1554 | # | |
1555 | starts = hstack((reshape((elements[:,0] == TRUE),(cad.ny,1)),((elements[:,1:] == TRUE) & (elements[:,:-1] == FALSE)))) | |
1556 | ends = hstack((((elements[:,:-1] == TRUE) & (elements[:,1:] == FALSE)),reshape((elements[:,-1] == TRUE),(cad.ny,1)))) | |
1557 | ixs = append(ixs,IX[starts]) | |
1558 | ixe = append(ixe,1+IX[ends]) | |
1559 | iy = append(iy,IY[starts]) | |
1560 | iz = append(iz,IZ[layer-1]*ones(len(IX[starts]))) | |
1561 | # | |
1562 | # merge y | |
1563 | # | |
1564 | index = lexsort(keys=(iy,ixe,ixs,iz)) | |
1565 | merge = (iz[index[1:]] == iz[index[:-1]]) & \ | |
1566 | (ixe[index[1:]] == ixe[index[:-1]]) & \ | |
1567 | (ixs[index[1:]] == ixs[index[:-1]]) & \ | |
1568 | ((iy[index[1:]] - iy[index[:-1]]) == 1) | |
1569 | merge = append(FALSE,merge).astype(bool_) | |
1570 | starts = ((merge[1:] == TRUE) & (merge[:-1] == FALSE)) | |
1571 | starts = append(starts,FALSE).astype(bool_) | |
1572 | ends = ((merge[1:] == FALSE) & (merge[:-1] == TRUE)) | |
1573 | if (merge[-1] == TRUE): | |
1574 | ends = append(ends,TRUE) | |
1575 | else: | |
1576 | ends = append(ends,FALSE) | |
1577 | ends = ends.astype(bool_) | |
1578 | xs = ixs[index][starts | ~merge] | |
1579 | xe = ixe[index][starts | ~merge] | |
1580 | ys = iy[index][starts | ~merge] | |
1581 | ye = iy[index][ends | ~(merge | starts)]+1 | |
1582 | cad.x = ravel(transpose(vstack((xs,xe)))) | |
1583 | cad.y = ravel(transpose(vstack((ys,ye)))) | |
1584 | # | |
1585 | # draw flashes | |
1586 | # | |
1587 | widget_stop.pack_forget() | |
1588 | cad.view('xy') | |
1589 | string_msg.set("draw ...") | |
1590 | root.update() | |
1591 | N = len(cad.x) | |
1592 | for i in range(0,N,2): | |
1593 | string_msg.set("draw flash %d/%d"%(i/4,N/4)) | |
1594 | root.update() | |
1595 | x0 = cad.nxplot()*(cad.x[i]+0.5)/float(cad.nx) | |
1596 | y0 = cad.nyplot()*(cad.ny-cad.y[i]+0.5)/float(cad.ny) | |
1597 | x1 = cad.nxplot()*(cad.x[i]+0.5)/float(cad.nx) | |
1598 | y1 = cad.nyplot()*(cad.ny-cad.y[i+1]+0.5)/float(cad.ny) | |
1599 | x2 = cad.nxplot()*(cad.x[i+1]+0.5)/float(cad.nx) | |
1600 | y2 = cad.nyplot()*(cad.ny-cad.y[i+1]+0.5)/float(cad.ny) | |
1601 | x3 = cad.nxplot()*(cad.x[i+1]+0.5)/float(cad.nx) | |
1602 | y3 = cad.nyplot()*(cad.ny-cad.y[i]+0.5)/float(cad.ny) | |
1603 | im.xy_draw.line([x0,y0,x1,y1,x2,y2,x3,y3,x0,y0],fill="black") | |
1604 | images.xy = ImageTk.PhotoImage(im.xy) | |
1605 | canvas_xy.create_image(cad.nplot/2,cad.nplot/2,image=images.xy) | |
1606 | string_msg.set("done") | |
1607 | root.update() | |
1608 | ||
1609 | def select_epi(): | |
1610 | input_file_name = string_input_file.get() | |
1611 | string_cam_file.set(input_file_name[0:-4]+'.epi') | |
1612 | cad.cam = 'epi' | |
1613 | cam_pack_forget() | |
1614 | cam_file_frame.pack() | |
1615 | cam_vector_frame.pack() | |
1616 | cam_dia_frame.pack() | |
1617 | cam_contour_frame.pack() | |
1618 | laser_frame1.pack() | |
1619 | if ((cad.nz > 1) | (cad.image_r.size > 1)): | |
1620 | laser_frame2.pack() | |
1621 | laser_frame3.pack() | |
1622 | string_laser_rate.set("2500") | |
1623 | string_laser_power.set("90") | |
1624 | string_laser_speed.set("50") | |
1625 | string_laser_min_power.set("10") | |
1626 | string_laser_max_power.set("100") | |
1627 | string_tool_dia.set("0.01") | |
1628 | root.update() | |
1629 | ||
1630 | def select_camm(): | |
1631 | input_file_name = string_input_file.get() | |
1632 | string_cam_file.set(input_file_name[0:-4]+'.camm') | |
1633 | cad.cam = 'camm' | |
1634 | cam_pack_forget() | |
1635 | cam_file_frame.pack() | |
1636 | cam_vector_frame.pack() | |
1637 | cam_dia_frame.pack() | |
1638 | cam_contour_frame.pack() | |
1639 | cut_frame.pack() | |
1640 | string_cut_force.set("45") | |
1641 | string_cut_velocity.set("2") | |
1642 | string_tool_dia.set("0.01") | |
1643 | root.update() | |
1644 | ||
1645 | def select_ps(): | |
1646 | input_file_name = string_input_file.get() | |
1647 | string_cam_file.set(input_file_name[0:-4]+'.ps') | |
1648 | cad.cam = 'ps' | |
1649 | cam_pack_forget() | |
1650 | cam_file_frame.pack() | |
1651 | cam_vector_frame.pack() | |
1652 | cam_dia_frame.pack() | |
1653 | cam_contour_frame.pack() | |
1654 | fill_frame.pack() | |
1655 | string_tool_dia.set("0.0") | |
1656 | root.update() | |
1657 | ||
1658 | def select_ord(): | |
1659 | input_file_name = string_input_file.get() | |
1660 | string_cam_file.set(input_file_name[0:-4]+'.ord') | |
1661 | cad.cam = 'ord' | |
1662 | cam_pack_forget() | |
1663 | cam_file_frame.pack() | |
1664 | cam_vector_frame.pack() | |
1665 | cam_dia_frame.pack() | |
1666 | cam_contour_frame.pack() | |
1667 | string_tool_dia.set("0.01") | |
1668 | waterjet_frame.pack() | |
1669 | string_lead_in.set("0.05") | |
1670 | string_quality.set("-3") | |
1671 | root.update() | |
1672 | ||
1673 | def select_g(): | |
1674 | input_file_name = string_input_file.get() | |
1675 | string_cam_file.set(input_file_name[0:-4]+'.gcode') | |
1676 | cad.cam = 'g' | |
1677 | cam_pack_forget() | |
1678 | cam_file_frame.pack() | |
1679 | cam_vector_frame.pack() | |
1680 | string_tool_dia.set("0.03") | |
1681 | cam_dia_frame.pack() | |
1682 | cam_contour_frame.pack() | |
1683 | string_g_feed_rate.set("20") | |
1684 | string_g_spindle_speed.set("5000") | |
1685 | string_g_tool.set("1") | |
1686 | integer_g_cool.set("0") | |
1687 | g_frame.pack() | |
1688 | root.update() | |
1689 | ||
1690 | def select_rml(): | |
1691 | input_file_name = string_input_file.get() | |
1692 | string_cam_file.set(input_file_name[0:-4]+'.rml') | |
1693 | cad.cam = 'rml' | |
1694 | cam_pack_forget() | |
1695 | cam_file_frame.pack() | |
1696 | cam_vector_frame.pack() | |
1697 | cam_dia_frame.pack() | |
1698 | cam_contour_frame.pack() | |
1699 | speed_frame.pack() | |
1700 | rml_move_frame.pack() | |
1701 | string_tool_dia.set("0.0156") | |
1702 | string_xy_speed.set("4") | |
1703 | string_z_speed.set("4") | |
1704 | string_rml_x_move.set("1") | |
1705 | string_rml_y_move.set("1") | |
1706 | root.update() | |
1707 | ||
1708 | def select_sbp(): | |
1709 | input_file_name = string_input_file.get() | |
1710 | string_cam_file.set(input_file_name[0:-4]+'.sbp') | |
1711 | cad.cam = 'sbp' | |
1712 | cam_pack_forget() | |
1713 | cam_file_frame.pack() | |
1714 | cam_vector_frame.pack() | |
1715 | cam_dia_frame.pack() | |
1716 | cam_contour_frame.pack() | |
1717 | jog_frame.pack() | |
1718 | speed_frame.pack() | |
1719 | string_tool_dia.set("0.125") | |
1720 | string_xy_speed.set("1.1") | |
1721 | string_z_speed.set("1.1") | |
1722 | string_jog_xy_speed.set("7") | |
1723 | string_jog_z_speed.set("7") | |
1724 | string_jog_z.set(".25") | |
1725 | root.update() | |
1726 | ||
1727 | def select_oms(): | |
1728 | input_file_name = string_input_file.get() | |
1729 | string_cam_file.set(input_file_name[0:-4]+'.oms') | |
1730 | cad.cam = 'oms' | |
1731 | cam_pack_forget() | |
1732 | cam_file_frame.pack() | |
1733 | cam_vector_frame.pack() | |
1734 | cam_dia_frame.pack() | |
1735 | cam_contour_frame.pack() | |
1736 | excimer_frame.pack() | |
1737 | string_pulse_period.set("10000") | |
1738 | string_tool_dia.set("0.001") | |
1739 | string_cut_vel.set("0.1") | |
1740 | string_cut_accel.set("5.0") | |
1741 | root.update() | |
1742 | ||
1743 | def select_dxf(): | |
1744 | input_file_name = string_input_file.get() | |
1745 | string_cam_file.set(input_file_name[0:-4]+'.dxf') | |
1746 | cad.cam = 'dxf' | |
1747 | cam_pack_forget() | |
1748 | cam_file_frame.pack() | |
1749 | cam_vector_frame.pack() | |
1750 | cam_dia_frame.pack() | |
1751 | cam_contour_frame.pack() | |
1752 | string_tool_dia.set("0.0") | |
1753 | root.update() | |
1754 | ||
1755 | def select_uni(): | |
1756 | input_file_name = string_input_file.get() | |
1757 | string_cam_file.set(input_file_name[0:-4]+'.uni') | |
1758 | cad.cam = 'uni' | |
1759 | cam_pack_forget() | |
1760 | cam_file_frame.pack() | |
1761 | cam_vector_frame.pack() | |
1762 | cam_dia_frame.pack() | |
1763 | cam_contour_frame.pack() | |
1764 | laser_frame1.pack() | |
1765 | if ((cad.nz > 1) | (cad.image_r.size > 1)): | |
1766 | laser_frame2.pack() | |
1767 | string_laser_rate.set("500") | |
1768 | string_laser_power.set("60") | |
1769 | string_laser_speed.set("15") | |
1770 | string_tool_dia.set("0.01") | |
1771 | string_laser_min_power.set("10") | |
1772 | string_laser_max_power.set("100") | |
1773 | string_vector_error.set('1.1') | |
1774 | root.update() | |
1775 | ||
1776 | def select_jpg(): | |
1777 | input_file_name = string_input_file.get() | |
1778 | string_cam_file.set(input_file_name[0:-4]+'.jpg') | |
1779 | cad.cam = 'jpg' | |
1780 | cam_pack_forget() | |
1781 | cam_file_frame.pack() | |
1782 | root.update() | |
1783 | ||
1784 | def select_png(): | |
1785 | input_file_name = string_input_file.get() | |
1786 | string_cam_file.set(input_file_name[0:-4]+'.png') | |
1787 | cad.cam = 'png' | |
1788 | cam_pack_forget() | |
1789 | cam_file_frame.pack() | |
1790 | root.update() | |
1791 | ||
1792 | def select_stl(): | |
1793 | input_file_name = string_input_file.get() | |
1794 | string_cam_file.set(input_file_name[0:-4]+'.stl') | |
1795 | cad.cam = 'stl' | |
1796 | cam_pack_forget() | |
1797 | cam_file_frame.pack() | |
1798 | STL_frame.pack() | |
1799 | root.update() | |
1800 | ||
1801 | def select_gerber(): | |
1802 | input_file_name = string_input_file.get() | |
1803 | string_cam_file.set(input_file_name[0:-4]+'.grb') | |
1804 | cad.cam = 'grb' | |
1805 | cam_pack_forget() | |
1806 | cam_file_frame.pack() | |
1807 | Gerber_frame.pack() | |
1808 | root.update() | |
1809 | ||
1810 | def select_excellon(): | |
1811 | input_file_name = string_input_file.get() | |
1812 | string_cam_file.set(input_file_name[0:-4]+'.drl') | |
1813 | cad.cam = 'drl' | |
1814 | cam_pack_forget() | |
1815 | cam_file_frame.pack() | |
1816 | Excellon_frame.pack() | |
1817 | root.update() | |
1818 | ||
1819 | def select_ca(): | |
1820 | input_file_name = string_input_file.get() | |
1821 | string_cam_file.set(input_file_name[0:-4]+'.ca') | |
1822 | cad.cam = 'ca' | |
1823 | cam_pack_forget() | |
1824 | cam_file_frame.pack() | |
1825 | root.update() | |
1826 | ||
1827 | def cam_pack_forget(): | |
1828 | cam_file_frame.pack_forget() | |
1829 | cam_vector_frame.pack_forget() | |
1830 | cam_dia_frame.pack_forget() | |
1831 | cam_contour_frame.pack_forget() | |
1832 | laser_frame1.pack_forget() | |
1833 | laser_frame2.pack_forget() | |
1834 | laser_frame3.pack_forget() | |
1835 | cut_frame.pack_forget() | |
1836 | speed_frame.pack_forget() | |
1837 | jog_frame.pack_forget() | |
1838 | rml_move_frame.pack_forget() | |
1839 | waterjet_frame.pack_forget() | |
1840 | excimer_frame.pack_forget() | |
1841 | STL_frame.pack_forget() | |
1842 | Gerber_frame.pack_forget() | |
1843 | Excellon_frame.pack_forget() | |
1844 | fill_frame.pack_forget() | |
1845 | g_frame.pack_forget() | |
1846 | send_to_frame.pack_forget() | |
1847 | ||
1848 | def save_cam(event): | |
1849 | # | |
1850 | # write toolpath | |
1851 | # | |
1852 | if (cad.cam == "epi"): | |
1853 | write_epi() | |
1854 | elif (cad.cam == "camm"): | |
1855 | write_camm() | |
1856 | elif (cad.cam == "ps"): | |
1857 | write_ps() | |
1858 | elif (cad.cam == "ord"): | |
1859 | write_ord() | |
1860 | elif (cad.cam == "g"): | |
1861 | write_G() | |
1862 | elif (cad.cam == "rml"): | |
1863 | write_rml() | |
1864 | elif (cad.cam == "sbp"): | |
1865 | write_sbp() | |
1866 | elif (cad.cam == "oms"): | |
1867 | write_oms() | |
1868 | elif (cad.cam == "dxf"): | |
1869 | write_dxf() | |
1870 | elif (cad.cam == "uni"): | |
1871 | write_uni() | |
1872 | elif (cad.cam == "jpg"): | |
1873 | write_jpg() | |
1874 | elif (cad.cam == "png"): | |
1875 | write_png() | |
1876 | elif (cad.cam == "stl"): | |
1877 | write_stl() | |
1878 | elif (cad.cam == "grb"): | |
1879 | write_gerber() | |
1880 | elif (cad.cam == "drl"): | |
1881 | write_excellon() | |
1882 | elif (cad.cam == "ca"): | |
1883 | write_ca() | |
1884 | else: | |
1885 | string_msg.set("unsupported output file format") | |
1886 | root.update() | |
1887 | ||
1888 | def write_epi(): | |
1889 | # | |
1890 | # Epilog lasercutter output | |
1891 | # todo: try 1200 DPI | |
1892 | # | |
1893 | units = 600*cad.inches_per_unit | |
1894 | filename = string_cam_file.get() | |
1895 | file = open(filename, 'wb') | |
1896 | if (integer_laser_autofocus.get() == 0): | |
1897 | # | |
1898 | # init with autofocus off | |
1899 | # | |
1900 | file.write("%-12345X@PJL JOB NAME="+string_cam_file.get()+"\r\nE@PJL ENTER LANGUAGE=PCL\r\n&y0A&l0U&l0Z&u600D*p0X*p0Y*t600R*r0F&y50P&z50S*r6600T*r5100S*r1A*rC%1BIN;XR"+string_laser_rate.get()+";YP"+string_laser_power.get()+";ZS"+string_laser_speed.get()+";") | |
1901 | else: | |
1902 | # | |
1903 | # init with autofocus on | |
1904 | # | |
1905 | file.write("%-12345X@PJL JOB NAME="+string_cam_file.get()+"\r\nE@PJL ENTER LANGUAGE=PCL\r\n&y1A&l0U&l0Z&u600D*p0X*p0Y*t600R*r0F&y50P&z50S*r6600T*r5100S*r1A*rC%1BIN;XR"+string_laser_rate.get()+";YP"+string_laser_power.get()+";ZS"+string_laser_speed.get()+";") | |
1906 | power = float(string_laser_power.get()) | |
1907 | min_power = float(string_laser_min_power.get()) | |
1908 | max_power = float(string_laser_max_power.get()) | |
1909 | for layer in range(len(cad.toolpaths)): | |
1910 | if ((len(cad.zwrite) > 1) & (len(cad.toolpaths[layer]) > 0)): | |
1911 | fraction = (cad.zwrite[layer]-cad.zwrite[0])/(cad.zwrite[-1]-cad.zwrite[0]) | |
1912 | layer_power = min_power + fraction*(max_power-min_power) | |
1913 | file.write("YP%f;"%layer_power) | |
1914 | for segment in range(len(cad.toolpaths[layer])): | |
1915 | x = int(units*(cad.xmin + (cad.xmax-cad.xmin)*(cad.toolpaths[layer][segment][0].x+0.5)/float(cad.nx))) | |
1916 | y = int(units*(-cad.ymin - ((cad.ymax-cad.ymin)*((cad.ny-cad.toolpaths[layer][segment][0].y)+0.5)/float(cad.ny)))) | |
1917 | file.write("PU"+str(x)+","+str(y)+";") | |
1918 | for vertex in range(1,len(cad.toolpaths[layer][segment])): | |
1919 | x = int(units*(cad.xmin + (cad.xmax-cad.xmin)*(cad.toolpaths[layer][segment][vertex].x+0.5)/float(cad.nx))) | |
1920 | y = int(units*(-cad.ymin - ((cad.ymax-cad.ymin)*((cad.ny-cad.toolpaths[layer][segment][vertex].y)+0.5)/float(cad.ny)))) | |
1921 | file.write("PD"+str(x)+","+str(y)+";") | |
1922 | file.write("%0B%1BPUE%-12345X@PJL EOJ \r\n") | |
1923 | file.close() | |
1924 | draw_toolpath() | |
1925 | string_msg.set("wrote %s"%filename) | |
1926 | root.update() | |
1927 | ||
1928 | def write_camm(): | |
1929 | filename = string_cam_file.get() | |
1930 | file = open(filename, 'wb') | |
1931 | units = 1016*cad.inches_per_unit | |
1932 | file.write("PA;PA;!ST1;!FS"+string_cut_force.get()+";VS"+string_cut_velocity.get()+";") | |
1933 | for layer in range(len(cad.toolpaths)): | |
1934 | for segment in range(len(cad.toolpaths[layer])): | |
1935 | x = int(units*(cad.xmin + (cad.xmax-cad.xmin)*(cad.toolpaths[layer][segment][0].x+0.5)/float(cad.nx))) | |
1936 | y = int(units*(cad.ymin + (cad.ymax-cad.ymin)*((cad.ny-cad.toolpaths[layer][segment][0].y)+0.5)/float(cad.ny))) | |
1937 | file.write("PU"+str(x)+","+str(y)+";") | |
1938 | for vertex in range(1,len(cad.toolpaths[layer][segment])): | |
1939 | x = int(units*(cad.xmin + (cad.xmax-cad.xmin)*(cad.toolpaths[layer][segment][vertex].x+0.5)/float(cad.nx))) | |
1940 | y = int(units*(cad.ymin + (cad.ymax-cad.ymin)*((cad.ny-cad.toolpaths[layer][segment][vertex].y)+0.5)/float(cad.ny))) | |
1941 | file.write("PD"+str(x)+","+str(y)+";") | |
1942 | file.write("PU0,0;") | |
1943 | file.close() | |
1944 | draw_toolpath() | |
1945 | string_msg.set("wrote %s"%filename) | |
1946 | root.update() | |
1947 | ||
1948 | def write_ps(): | |
1949 | # | |
1950 | # Postscript output | |
1951 | # | |
1952 | units = cad.inches_per_unit | |
1953 | filename = string_cam_file.get() | |
1954 | file = open(filename, 'wb') | |
1955 | file.write("%! cad.py output\n") | |
1956 | file.write("%%%%BoundingBox: 0 0 %.3f %.3f\n"% | |
1957 | (72.0*(cad.xmax-cad.xmin),72.0*(cad.ymax-cad.ymin))) | |
1958 | file.write("/m {moveto} def\n") | |
1959 | file.write("/l {lineto} def\n") | |
1960 | file.write("72 72 scale\n") | |
1961 | file.write(".005 setlinewidth\n") | |
1962 | file.write("%f %f translate\n"%(0.5,0.5)) | |
1963 | for layer in range(len(cad.toolpaths)): | |
1964 | for segment in range(len(cad.toolpaths[layer])): | |
1965 | x = units*(cad.xmin + (cad.xmax-cad.xmin)*(cad.toolpaths[layer][segment][0].x+0.5)/float(cad.nx)) | |
1966 | y = units*(cad.ymin + (cad.ymax-cad.ymin)*((cad.ny-cad.toolpaths[layer][segment][0].y)+0.5)/float(cad.ny)) | |
1967 | file.write("%f %f m\n"%(x,y)) | |
1968 | for vertex in range(1,len(cad.toolpaths[layer][segment])): | |
1969 | x = units*(cad.xmin + (cad.xmax-cad.xmin)*(cad.toolpaths[layer][segment][vertex].x+0.5)/float(cad.nx)) | |
1970 | y = units*(cad.ymin + (cad.ymax-cad.ymin)*((cad.ny-cad.toolpaths[layer][segment][vertex].y)+0.5)/float(cad.ny)) | |
1971 | file.write("%f %f l\n"%(x,y)) | |
1972 | if (integer_fill.get() == 0): | |
1973 | file.write("stroke\n") | |
1974 | else: | |
1975 | file.write("fill\n") | |
1976 | file.write("showpage\n") | |
1977 | file.close() | |
1978 | draw_toolpath() | |
1979 | string_msg.set("wrote %s"%filename) | |
1980 | root.update() | |
1981 | ||
1982 | def write_ord(): | |
1983 | # | |
1984 | # OMAX waterjet output | |
1985 | # | |
1986 | units = cad.inches_per_unit | |
1987 | lead_in = float(string_lead_in.get()) | |
1988 | quality = int(string_quality.get()) | |
1989 | filename = string_cam_file.get() | |
1990 | file = open(filename, 'wb') | |
1991 | xlead = [] | |
1992 | ylead = [] | |
1993 | for layer in range(len(cad.toolpaths)): | |
1994 | for segment in range(len(cad.toolpaths[layer])): | |
1995 | # | |
1996 | # calculate and write lead-in | |
1997 | # | |
1998 | x0 = units*(cad.xmin + (cad.xmax-cad.xmin)*(cad.toolpaths[layer][segment][0].x+0.5)/float(cad.nx)) | |
1999 | y0 = units*(cad.ymin + (cad.ymax-cad.ymin)*((cad.ny-cad.toolpaths[layer][segment][0].y)+0.5)/float(cad.ny)) | |
2000 | x1 = units*(cad.xmin + (cad.xmax-cad.xmin)*(cad.toolpaths[layer][segment][1].x+0.5)/float(cad.nx)) | |
2001 | y1 = units*(cad.ymin + (cad.ymax-cad.ymin)*((cad.ny-cad.toolpaths[layer][segment][1].y)+0.5)/float(cad.ny)) | |
2002 | dx = x1 - x0 | |
2003 | dy = y1 - y0 | |
2004 | norm_x = -dy | |
2005 | norm_y = dx | |
2006 | norm = sqrt(norm_x**2 + norm_y**2) | |
2007 | norm_x = norm_x/norm | |
2008 | norm_y = norm_y/norm | |
2009 | xlead.append(x0 + norm_x*lead_in) | |
2010 | ylead.append(y0 + norm_y*lead_in) | |
2011 | file.write("%f, %f, 0, %d\n"%(xlead[segment],ylead[segment],quality)) | |
2012 | # | |
2013 | # loop over segment | |
2014 | # | |
2015 | for vertex in range(len(cad.toolpaths[layer][segment])): | |
2016 | x = units*(cad.xmin + (cad.xmax-cad.xmin)*(cad.toolpaths[layer][segment][vertex].x+0.5)/float(cad.nx)) | |
2017 | y = units*(cad.ymin + (cad.ymax-cad.ymin)*((cad.ny-cad.toolpaths[layer][segment][vertex].y)+0.5)/float(cad.ny)) | |
2018 | file.write("%f, %f, 0, %d\n"%(x,y,quality)) | |
2019 | # | |
2020 | # write lead-out | |
2021 | # | |
2022 | file.write("%f, %f, 0, 0\n"%(x0,y0)) | |
2023 | file.write("%f, %f, 0, 0\n"%(xlead[segment],ylead[segment])) | |
2024 | file.close() | |
2025 | # | |
2026 | # draw toolpath with lead-in/out | |
2027 | # | |
2028 | im.xy = Image.new("RGBX",(cad.nxplot(),cad.nyplot()),'white') | |
2029 | im.xy_draw = ImageDraw.Draw(im.xy) | |
2030 | for layer in range(len(cad.toolpaths)): | |
2031 | for segment in range(len(cad.toolpaths[layer])): | |
2032 | x = cad.nxplot()*(cad.toolpaths[layer][segment][0].x+0.5)/float(cad.nx) | |
2033 | y = cad.nyplot()*(cad.toolpaths[layer][segment][0].y+0.5)/float(cad.ny) | |
2034 | xl = cad.nxplot()*(xlead[segment]-cad.xmin)/(cad.xmax-cad.xmin) | |
2035 | yl = cad.nyplot()-cad.nyplot()*(ylead[segment]-cad.ymin)/(cad.ymax-cad.ymin) | |
2036 | im.xy_draw.line([xl,yl,x,y],fill="black") | |
2037 | for vertex in range(1,len(cad.toolpaths[layer][segment])): | |
2038 | xnew = cad.nxplot()*(cad.toolpaths[layer][segment][vertex].x+0.5)/float(cad.nx) | |
2039 | ynew = cad.nyplot()*(cad.toolpaths[layer][segment][vertex].y+0.5)/float(cad.ny) | |
2040 | im.xy_draw.line([x,y,xnew,ynew],fill="black") | |
2041 | x = xnew | |
2042 | y = ynew | |
2043 | images.xy = ImageTk.PhotoImage(im.xy) | |
2044 | canvas_xy.create_image(cad.nplot/2,cad.nplot/2,image=images.xy) | |
2045 | string_msg.set("wrote %s"%filename) | |
2046 | root.update() | |
2047 | ||
2048 | def distance(x1, y1, x2, y2): | |
2049 | return sqrt((x1-x2)**2+(y1-y2)**2) | |
2050 | ||
2051 | def write_G(): | |
2052 | # | |
2053 | # G code output | |
2054 | # | |
2055 | units = cad.inches_per_unit | |
2056 | zup = units*cad.zmax | |
2057 | feed_rate = float(string_g_feed_rate.get()) | |
2058 | spindle_speed = float(string_g_spindle_speed.get()) | |
2059 | coolant = integer_g_cool.get() | |
2060 | tool = int(string_g_tool.get()) | |
2061 | if (cad.nz == 1): | |
2062 | cad.zwrite = [cad.zmin] | |
2063 | filename = string_cam_file.get() | |
2064 | file = open(filename, 'wb') | |
2065 | file.write("""(---------------------------------------------------------------) | |
2066 | (---------------------------------------------------------------) | |
2067 | (Start of sheet header) | |
2068 | G21 (metric) | |
2069 | G92 X0 Y0 Z0 (zero all axes) | |
2070 | (End of sheet header)\n""") | |
2071 | dxy = 0 | |
2072 | dz = 0 | |
2073 | xold = 0 | |
2074 | yold = 0 | |
2075 | for layer in range(len(cad.zwrite)-1,-1,-1): | |
2076 | zdown = units*cad.zwrite[layer] | |
2077 | # | |
2078 | # follow toolpaths CCW, for CW tool motion | |
2079 | # | |
2080 | unsorted_segments = cad.toolpaths[layer] | |
2081 | sorted_segments = [] | |
2082 | if len(unsorted_segments) > 0: | |
2083 | sorted_segments.append(unsorted_segments.pop(0)) #starts with the first path in the list | |
2084 | else: | |
2085 | print "empty path --- strange" | |
2086 | ||
2087 | while len(unsorted_segments) > 0: | |
2088 | #find closest start to the the last sorted segment start | |
2089 | min_dist = 99999 | |
2090 | min_dist_index = None | |
2091 | for i in range(len(unsorted_segments)): | |
2092 | dist = distance(sorted_segments[-1][0].x, sorted_segments[-1][0].y, | |
2093 | unsorted_segments[i][0].x, unsorted_segments[i][0].y) | |
2094 | if dist < min_dist: | |
2095 | min_dist = dist | |
2096 | min_dist_index = i | |
2097 | ||
2098 | #print "min_dist: %d index: %d" % (min_dist, min_dist_index) | |
2099 | sorted_segments.append(unsorted_segments.pop(min_dist_index)) | |
2100 | ||
2101 | for segment in range(len(sorted_segments)): | |
2102 | ||
2103 | x = units*(cad.xmin + (cad.xmax-cad.xmin)*(sorted_segments[segment][0].x+0.5)/float(cad.nx)) | |
2104 | y = units*(cad.ymin + (cad.ymax-cad.ymin)*((cad.ny-sorted_segments[segment][0].y)+0.5)/float(cad.ny)) | |
2105 | file.write("M106 S255 (Pen Up)\n") | |
2106 | file.write("G4 P120\n") | |
2107 | file.write("G1 X%0.4f "%x+"Y%0.4f "%y+" F2000.00\n") # rapid motion | |
2108 | file.write("M107 (Pen Down)\n") # linear motion | |
2109 | file.write("G4 P120\n") | |
2110 | dxy += sqrt((xold-x)**2+(yold-y)**2) | |
2111 | xold = x | |
2112 | yold = y | |
2113 | dz += zup-zdown | |
2114 | for vertex in range(1,len(sorted_segments[segment])): | |
2115 | x = units*(cad.xmin + (cad.xmax-cad.xmin)*(sorted_segments[segment][vertex].x+0.5)/float(cad.nx)) | |
2116 | y = units*(cad.ymin + (cad.ymax-cad.ymin)*((cad.ny-sorted_segments[segment][vertex].y)+0.5)/float(cad.ny)) | |
2117 | file.write("G1 X%0.4f "%x+"Y%0.4f"%y+" F2000.00\n") | |
2118 | dxy += sqrt((xold-x)**2+(yold-y)**2) | |
2119 | xold = x | |
2120 | yold = y | |
2121 | file.write("""(Start of sheet footer.) | |
2122 | M106 (Pen Up) | |
2123 | G4 P120 (wait 120ms) | |
2124 | G0 X0 Y0 Z15 F3500.00 (go to position for retrieving platform -- increase Z to Z25 or similar if you have trouble avoiding tool) | |
2125 | G4 P300 (wait 300ms) | |
2126 | G0 Z0 F3500.00 (return to start position of current sheet) | |
2127 | ||
2128 | G4 P300 (wait 300ms) | |
2129 | M18 (disengage drives) | |
2130 | (End of sheet footer) | |
2131 | ||
2132 | M01 (Printing on the next sheet?) | |
2133 | (yes, if dropping the default .1 mm to next sheet; no, if you will print again on same sheet) | |
2134 | G0 Z-0.10 F3500.00 (drop 0.1mm to next sheet) | |
2135 | M107 (Pen Down so as not to overheat solenoid) | |
2136 | ||
2137 | (Paste in further sheets below) | |
2138 | (---------------------------------------------------------------) | |
2139 | (---------------------------------------------------------------) | |
2140 | """) | |
2141 | file.close() | |
2142 | print "Path length: %f" % dxy | |
2143 | time = (dxy/feed_rate + dz/feed_rate) | |
2144 | string_send_to_time.set(" estimated time: %.1f minutes"%time) | |
2145 | draw_toolpath() | |
2146 | string_msg.set("wrote %s"%filename) | |
2147 | root.update() | |
2148 | ||
2149 | def write_rml(): | |
2150 | # | |
2151 | # Roland Modela output | |
2152 | # | |
2153 | units = 1016*cad.inches_per_unit # 40/mm | |
2154 | filename = string_cam_file.get() | |
2155 | file = open(filename, 'wb') | |
2156 | file.write("PA;PA;VS"+string_xy_speed.get()+";!VZ"+string_z_speed.get()+";!MC1;") | |
2157 | zup = cad.zmax | |
2158 | izup = int(units*zup) | |
2159 | if (cad.nz == 1): | |
2160 | cad.zwrite = [cad.zmin] | |
2161 | xy_speed = float(string_xy_speed.get()) # mm/s | |
2162 | z_speed = float(string_z_speed.get()) # mm/s | |
2163 | dxy = 0 | |
2164 | dz = 0 | |
2165 | xold = 0 | |
2166 | yold = 0 | |
2167 | for layer in range(len(cad.zwrite)-1,-1,-1): | |
2168 | zdown = cad.zwrite[layer] | |
2169 | izdown = int(units*zdown) | |
2170 | file.write("!PZ"+str(izdown)+","+str(izup)+";") | |
2171 | # | |
2172 | # follow toolpaths CCW, for CW tool motion | |
2173 | # | |
2174 | for segment in range(len(cad.toolpaths[layer])): | |
2175 | x = int(units*(cad.xmin + (cad.xmax-cad.xmin)*(cad.toolpaths[layer][segment][0].x+0.5)/float(cad.nx))) | |
2176 | y = int(units*(cad.ymin + (cad.ymax-cad.ymin)*((cad.ny-cad.toolpaths[layer][segment][0].y)+0.5)/float(cad.ny))) | |
2177 | file.write("PU"+str(x)+","+str(y)+";") | |
2178 | dxy += sqrt((xold-x)**2+(yold-y)**2) | |
2179 | xold = x | |
2180 | yold = y | |
2181 | dz += izup-izdown | |
2182 | for vertex in range(1,len(cad.toolpaths[layer][segment])): | |
2183 | x = int(units*(cad.xmin + (cad.xmax-cad.xmin)*(cad.toolpaths[layer][segment][vertex].x+0.5)/float(cad.nx))) | |
2184 | y = int(units*(cad.ymin + (cad.ymax-cad.ymin)*((cad.ny-cad.toolpaths[layer][segment][vertex].y)+0.5)/float(cad.ny))) | |
2185 | file.write("PD"+str(x)+","+str(y)+";") | |
2186 | dxy += sqrt((xold-x)**2+(yold-y)**2) | |
2187 | xold = x | |
2188 | yold = y | |
2189 | file.write("PU"+str(x)+","+str(y)+";!MC0;") | |
2190 | # | |
2191 | # file padding hack for end-of-file buffering problems | |
2192 | # | |
2193 | for i in range(1000): | |
2194 | file.write("!MC0;") | |
2195 | file.close() | |
2196 | time = ((dxy/40.0)/xy_speed + (dz/40.0)/z_speed)/60.0 | |
2197 | string_send_to_time.set(" estimated time: %.1f minutes"%time) | |
2198 | draw_toolpath() | |
2199 | string_msg.set("wrote %s"%filename) | |
2200 | root.update() | |
2201 | ||
2202 | def rml_move(event): | |
2203 | # | |
2204 | # move Roland Modela | |
2205 | # | |
2206 | units = 1016*cad.inches_per_unit # 40/mm | |
2207 | x = float(string_rml_x_move.get()) | |
2208 | y = float(string_rml_y_move.get()) | |
2209 | ix = int(units*x) | |
2210 | iy = int(units*y) | |
2211 | filename = "move.rml" | |
2212 | file = open(filename, 'wb') | |
2213 | file.write("PA;PA;!PZ0,400;VS10;!VZ10;!MC0;PU%d,%d;!MC0;"%(ix,iy)) | |
2214 | file.close() | |
2215 | send_to_file("move.rml") | |
2216 | os.remove("move.rml") | |
2217 | ||
2218 | def write_sbp(): | |
2219 | # | |
2220 | # ShopBot output | |
2221 | # | |
2222 | units = cad.inches_per_unit | |
2223 | filename = string_cam_file.get() | |
2224 | file = open(filename, 'wb') | |
2225 | file.write("SA\r\n") # set to absolute distances | |
2226 | file.write("SO,1,1\r\n") # set output number 1 to on | |
2227 | file.write("pause 2\r\n") # let spindle come up to speed | |
2228 | xy_speed = units*float(string_xy_speed.get()) | |
2229 | z_speed = units*float(string_z_speed.get()) | |
2230 | file.write("MS %f,%f\r\n"%(xy_speed,z_speed)) # set xy,z speed | |
2231 | jog_xy_speed = units*float(string_jog_xy_speed.get()) | |
2232 | jog_z_speed = units*float(string_jog_z_speed.get()) | |
2233 | file.write("JS %f,%f\r\n"%(jog_xy_speed,jog_z_speed)) # set jog xy,z speed | |
2234 | zup = units*float(string_jog_z.get()) | |
2235 | dxy = 0 | |
2236 | dz = 0 | |
2237 | xold = 0 | |
2238 | yold = 0 | |
2239 | for layer in range(len(cad.zwrite)-1,-1,-1): | |
2240 | zdown = cad.zwrite[layer] | |
2241 | # | |
2242 | # follow toolpaths CCW, for CW tool motion | |
2243 | # | |
2244 | for segment in range(len(cad.toolpaths[layer])): | |
2245 | x = units*(cad.xmin + (cad.xmax-cad.xmin)*(cad.toolpaths[layer][segment][0].x+0.5)/float(cad.nx)) | |
2246 | y = units*(cad.ymin + (cad.ymax-cad.ymin)*((cad.ny-cad.toolpaths[layer][segment][0].y)+0.5)/float(cad.ny)) | |
2247 | file.write("JZ %f\r\n"%zup) | |
2248 | file.write("J2 %f,%f\r\n"%(x,y)) | |
2249 | file.write("MZ %f\r\n"%zdown) | |
2250 | dxy += sqrt((xold-x)**2+(yold-y)**2) | |
2251 | xold = x | |
2252 | yold = y | |
2253 | dz += zup-zdown | |
2254 | for vertex in range(1,len(cad.toolpaths[layer][segment])): | |
2255 | x = units*(cad.xmin + (cad.xmax-cad.xmin)*(cad.toolpaths[layer][segment][vertex].x+0.5)/float(cad.nx)) | |
2256 | y = units*(cad.ymin + (cad.ymax-cad.ymin)*((cad.ny-cad.toolpaths[layer][segment][vertex].y)+0.5)/float(cad.ny)) | |
2257 | file.write("M2 %f,%f\r\n"%(x,y)) | |
2258 | dxy += sqrt((xold-x)**2+(yold-y)**2) | |
2259 | xold = x | |
2260 | yold = y | |
2261 | file.write("JZ %f\r\n"%zup) | |
2262 | file.close() | |
2263 | time = (dxy/xy_speed + dz/z_speed)/60.0 | |
2264 | string_send_to_time.set(" estimated time: %.1f minutes"%time) | |
2265 | draw_toolpath() | |
2266 | string_msg.set("wrote %s"%filename) | |
2267 | root.update() | |
2268 | ||
2269 | def write_oms(): | |
2270 | # | |
2271 | # Resonetics excimer micromachining center output | |
2272 | # | |
2273 | units = 25.4*cad.inches_per_unit | |
2274 | pulseperiod = float(string_pulse_period.get()) | |
2275 | cutvel = float(string_cut_vel.get()) | |
2276 | cutaccel = float(string_cut_accel.get()) | |
2277 | slewvel = 1 | |
2278 | slewaccel = 5 | |
2279 | settle = 100 | |
2280 | filename = string_cam_file.get() | |
2281 | file = open(filename, 'wb') | |
2282 | file.write("AA LP0,0,0,0,0\n") # set origin | |
2283 | file.write("PP%d\n"%pulseperiod) # set pulse period | |
2284 | for layer in range(len(cad.toolpaths)): | |
2285 | for segment in range(len(cad.toolpaths[layer])): | |
2286 | x = units*(cad.xmin + (cad.xmax-cad.xmin)*(cad.toolpaths[layer][segment][0].x+0.5)/float(cad.nx)) | |
2287 | y = units*(cad.ymin + (cad.ymax-cad.ymin)*((cad.ny-cad.toolpaths[layer][segment][0].y)+0.5)/float(cad.ny)) | |
2288 | file.write("VL%.1f,%.1f\n"%(slewvel,slewvel)) | |
2289 | file.write("AC%.1f,%.1f\n"%(slewaccel,slewaccel)) | |
2290 | file.write("MA%f,%f\n"%(x,y)) | |
2291 | file.write("VL%.1f,%.1f\n"%(cutvel,cutvel)) | |
2292 | file.write("AC%.1f,%.1f\n"%(cutaccel,cutaccel)) | |
2293 | file.write("WT%d\n"%settle) # wait to settle | |
2294 | for vertex in range(1,len(cad.toolpaths[layer][segment])): | |
2295 | x = units*(cad.xmin + (cad.xmax-cad.xmin)*(cad.toolpaths[layer][segment][vertex].x+0.5)/float(cad.nx)) | |
2296 | y = units*(cad.ymin + (cad.ymax-cad.ymin)*((cad.ny-cad.toolpaths[layer][segment][vertex].y)+0.5)/float(cad.ny)) | |
2297 | file.write("CutAbs %f,%f\n"%(x,y)) | |
2298 | file.write("END\n") | |
2299 | file.close() | |
2300 | draw_toolpath() | |
2301 | string_msg.set("wrote %s"%filename) | |
2302 | root.update() | |
2303 | ||
2304 | def write_dxf(): | |
2305 | # | |
2306 | # DXF output | |
2307 | # | |
2308 | units = cad.inches_per_unit | |
2309 | filename = string_cam_file.get() | |
2310 | file = open(filename, 'wb') | |
2311 | file.write("999\nDXF written by cad.py\n") | |
2312 | file.write("0\nSECTION\n") | |
2313 | file.write("2\nHEADER\n") | |
2314 | file.write("9\n$EXTMIN\n") | |
2315 | file.write("10\n%f\n"%cad.xmin) | |
2316 | file.write("20\n%f\n"%cad.ymin) | |
2317 | file.write("9\n$EXTMAX\n") | |
2318 | file.write("10\n%f\n"%cad.xmax) | |
2319 | file.write("20\n%f\n"%cad.ymax) | |
2320 | file.write("0\nENDSEC\n") | |
2321 | file.write("0\nSECTION\n") | |
2322 | file.write("2\nTABLES\n") | |
2323 | file.write("0\nTABLE\n") | |
2324 | file.write("2\nLTYPE\n70\n1\n") | |
2325 | file.write("0\nLTYPE\n") | |
2326 | file.write("2\nCONTINUOUS\n") | |
2327 | file.write("70\n64\n3\n") | |
2328 | file.write("Solid line\n") | |
2329 | file.write("72\n65\n73\n0\n40\n0.000000\n") | |
2330 | file.write("0\nENDTAB\n") | |
2331 | file.write("0\nTABLE\n2\nLAYER\n70\n1\n") | |
2332 | file.write("0\nLAYER\n2\ndefault\n70\n64\n62\n7\n6\n") | |
2333 | file.write("CONTINUOUS\n0\nENDTAB\n") | |
2334 | file.write("0\nENDSEC\n") | |
2335 | file.write("0\nSECTION\n") | |
2336 | file.write("2\nBLOCKS\n") | |
2337 | file.write("0\nENDSEC\n") | |
2338 | file.write("0\nSECTION\n") | |
2339 | file.write("2\nENTITIES\n") | |
2340 | for layer in range(len(cad.toolpaths)): | |
2341 | for segment in range(len(cad.toolpaths[layer])): | |
2342 | for vertex in range(1,len(cad.toolpaths[layer][segment])): | |
2343 | x0 = units*(cad.xmin + (cad.xmax-cad.xmin)*(cad.toolpaths[layer][segment][vertex-1].x+0.5)/float(cad.nx)) | |
2344 | y0 = units*(cad.ymin + (cad.ymax-cad.ymin)*((cad.ny-cad.toolpaths[layer][segment][vertex-1].y)+0.5)/float(cad.ny)) | |
2345 | x1 = units*(cad.xmin + (cad.xmax-cad.xmin)*(cad.toolpaths[layer][segment][vertex].x+0.5)/float(cad.nx)) | |
2346 | y1 = units*(cad.ymin + (cad.ymax-cad.ymin)*((cad.ny-cad.toolpaths[layer][segment][vertex].y)+0.5)/float(cad.ny)) | |
2347 | file.write("0\nLINE\n") | |
2348 | file.write("10\n%f\n"%x0) | |
2349 | file.write("20\n%f\n"%y0) | |
2350 | file.write("11\n%f\n"%x1) | |
2351 | file.write("21\n%f\n"%y1) | |
2352 | file.write("0\nENDSEC\n") | |
2353 | file.write("0\nEOF\n") | |
2354 | file.close() | |
2355 | draw_toolpath() | |
2356 | string_msg.set("wrote %s"%filename) | |
2357 | root.update() | |
2358 | ||
2359 | def write_uni(): | |
2360 | # | |
2361 | # Universal lasercutter output | |
2362 | # | |
2363 | units = 1000*cad.inches_per_unit | |
2364 | filename = string_cam_file.get() | |
2365 | file = open(filename, 'wb') | |
2366 | file.write("Z") # initialize | |
2367 | file.write("t%s~;"%filename) # title | |
2368 | file.write("IN;DF;PS0;DT~") # initialize | |
2369 | ppibyte = int(float(string_laser_rate.get())/10) | |
2370 | file.write("s%c"%ppibyte) # PPI | |
2371 | speed_hibyte = int(648*float(string_laser_speed.get()))/256 | |
2372 | speed_lobyte = int(648*float(string_laser_speed.get()))%256 | |
2373 | file.write("v%c%c"%(speed_hibyte,speed_lobyte)) # speed | |
2374 | power = float(string_laser_power.get()) | |
2375 | min_power = float(string_laser_min_power.get()) | |
2376 | max_power = float(string_laser_max_power.get()) | |
2377 | power_hibyte = (320*int(power))/256 | |
2378 | power_lobyte = (320*int(power))%256 | |
2379 | file.write("p%c%c"%(power_hibyte,power_lobyte)) # power | |
2380 | file.write("a%c"%2) # air assist on high | |
2381 | for layer in range(len(cad.toolpaths)): | |
2382 | if ((len(cad.zwrite) > 1) & (len(cad.toolpaths[layer]) > 0)): | |
2383 | fraction = (cad.zwrite[layer]-cad.zwrite[0])/(cad.zwrite[-1]-cad.zwrite[0]) | |
2384 | layer_power = min_power + fraction*(max_power-min_power) | |
2385 | power_hibyte = (320*int(layer_power))/256 | |
2386 | power_lobyte = (320*int(layer_power))%256 | |
2387 | file.write("p%c%c"%(power_hibyte,power_lobyte)) # power | |
2388 | for segment in range(len(cad.toolpaths[layer])): | |
2389 | x = int(units*(cad.xmin + (cad.xmax-cad.xmin)*(cad.toolpaths[layer][segment][0].x+0.5)/float(cad.nx))) | |
2390 | y = int(units*(cad.ymin + ((cad.ymax-cad.ymin)*((cad.ny-cad.toolpaths[layer][segment][0].y)+0.5)/float(cad.ny)))) | |
2391 | file.write("PU;PA"+str(x)+","+str(y)+";PD;") | |
2392 | for vertex in range(1,len(cad.toolpaths[layer][segment])): | |
2393 | x = int(units*(cad.xmin + (cad.xmax-cad.xmin)*(cad.toolpaths[layer][segment][vertex].x+0.5)/float(cad.nx))) | |
2394 | y = int(units*(cad.ymin + ((cad.ymax-cad.ymin)*((cad.ny-cad.toolpaths[layer][segment][vertex].y)+0.5)/float(cad.ny)))) | |
2395 | file.write("PA"+str(x)+","+str(y)+";") | |
2396 | file.write("e") # end of file | |
2397 | file.close() | |
2398 | draw_toolpath() | |
2399 | string_msg.set("wrote %s"%filename) | |
2400 | root.update() | |
2401 | ||
2402 | def write_jpg(): | |
2403 | # | |
2404 | # JPG image output | |
2405 | # | |
2406 | if (cad.views == "xy"): | |
2407 | filename = string_cam_file.get() | |
2408 | im.xy = Image.fromarray(im.intensity_xy,mode="RGBX") | |
2409 | im_rgb_xy = im.xy.convert("RGB") | |
2410 | dpi = int(cad.nx/float(cad.xmax-cad.xmin)) | |
2411 | im_rgb_xy.save(filename,dpi=(dpi,dpi)) | |
2412 | string_msg.set("wrote %s"%filename) | |
2413 | elif (cad.views == "xyzr"): | |
2414 | border = 5 | |
2415 | filename = string_cam_file.get() | |
2416 | im.xy = Image.fromarray(im.intensity_xy,mode="RGBX") | |
2417 | im.xz = Image.fromarray(im.intensity_xz,mode="RGBX") | |
2418 | im.yz = Image.fromarray(im.intensity_yz,mode="RGBX") | |
2419 | im.yz = im.yz.transpose(Image.FLIP_LEFT_RIGHT) | |
2420 | im.xyz = Image.fromarray(im.intensity_xyz,mode="RGBX") | |
2421 | (nx,ny) = im.xy.size | |
2422 | ny = (nx*cad.nyplot())/cad.nxplot() | |
2423 | nz = (nx*cad.nzplot())/cad.nxplot() | |
2424 | im.xy = im.xy.resize((nx,ny)) | |
2425 | im.yz = im.yz.resize((nz,ny)) | |
2426 | im.xz = im.xz.resize((nx,nz)) | |
2427 | im.xyz = im.xyz.resize((nx,ny)) | |
2428 | im_rgb_xy = im.xy.convert("RGB") | |
2429 | im_rgb_xz = im.xz.convert("RGB") | |
2430 | im_rgb_yz = im.yz.convert("RGB") | |
2431 | im_rgb_xyz = im.xyz.convert("RGB") | |
2432 | img = Image.new("RGB",(nx+border+nx,ny+border+ny),"white") | |
2433 | img.paste(im_rgb_xy,(0,0)) | |
2434 | img.paste(im_rgb_xz,(0,border+ny)) | |
2435 | img.paste(im_rgb_yz,(border+nx,0)) | |
2436 | img.paste(im_rgb_xyz,(border+nx,border+ny)) | |
2437 | img.save(filename) | |
2438 | string_msg.set("wrote %s"%filename) | |
2439 | else: | |
2440 | string_msg.set("unknown view") | |
2441 | ||
2442 | def write_png(): | |
2443 | # | |
2444 | # PNG image output | |
2445 | # | |
2446 | if (cad.views == "xy"): | |
2447 | filename = string_cam_file.get() | |
2448 | im.xy = Image.fromarray(im.intensity_xy,mode="RGBX") | |
2449 | im_rgb_xy = im.xy.convert("RGB") | |
2450 | dpi = int(cad.nx/float(cad.xmax-cad.xmin)) | |
2451 | im_rgb_xy.save(filename,dpi=(dpi,dpi)) | |
2452 | string_msg.set("wrote %s"%filename) | |
2453 | elif (cad.views == "xyzr"): | |
2454 | border = 5 | |
2455 | filename = string_cam_file.get() | |
2456 | im.xy = Image.fromarray(im.intensity_xy,mode="RGBX") | |
2457 | im.xz = Image.fromarray(im.intensity_xz,mode="RGBX") | |
2458 | im.yz = Image.fromarray(im.intensity_yz,mode="RGBX") | |
2459 | im.yz = im.yz.transpose(Image.FLIP_LEFT_RIGHT) | |
2460 | im.xyz = Image.fromarray(im.intensity_xyz,mode="RGBX") | |
2461 | (nx,ny) = im.xy.size | |
2462 | ny = (nx*cad.nyplot())/cad.nxplot() | |
2463 | nz = (nx*cad.nzplot())/cad.nxplot() | |
2464 | im.xy = im.xy.resize((nx,ny)) | |
2465 | im.yz = im.yz.resize((nz,ny)) | |
2466 | im.xz = im.xz.resize((nx,nz)) | |
2467 | im.xyz = im.xyz.resize((nx,ny)) | |
2468 | im_rgb_xy = im.xy.convert("RGB") | |
2469 | im_rgb_xz = im.xz.convert("RGB") | |
2470 | im_rgb_yz = im.yz.convert("RGB") | |
2471 | im_rgb_xyz = im.xyz.convert("RGB") | |
2472 | img = Image.new("RGB",(nx+border+nx,ny+border+ny),"white") | |
2473 | img.paste(im_rgb_xy,(0,0)) | |
2474 | img.paste(im_rgb_xz,(0,border+ny)) | |
2475 | img.paste(im_rgb_yz,(border+nx,0)) | |
2476 | img.paste(im_rgb_xyz,(border+nx,border+ny)) | |
2477 | img.save(filename) | |
2478 | string_msg.set("wrote %s"%filename) | |
2479 | else: | |
2480 | string_msg.set("unknown view") | |
2481 | ||
2482 | def write_stl(): | |
2483 | # | |
2484 | # STL output | |
2485 | # | |
2486 | filename = string_cam_file.get() | |
2487 | file = open(filename, 'wb') | |
2488 | units = cad.inches_per_unit | |
2489 | x = cad.xmin+(cad.xmax-cad.xmin)*(cad.x+0.5)/float(cad.nx) | |
2490 | y = cad.ymin+(cad.ymax-cad.ymin)*(cad.y+0.5)/float(cad.ny) | |
2491 | z = cad.zmin+(cad.zmax-cad.zmin)*(cad.z+0.5)/float(cad.nz) | |
2492 | # | |
2493 | # header | |
2494 | # | |
2495 | file.write('cad.py') | |
2496 | file.write('a'*74) | |
2497 | # | |
2498 | # length | |
2499 | # | |
2500 | N = len(cad.x) | |
2501 | file.write(struct.pack('L',N/3)) | |
2502 | # | |
2503 | # triangles | |
2504 | # | |
2505 | for i in range(0,N,3): | |
2506 | string_msg.set("write triangle %d/%d"%(i/3,N/3)) | |
2507 | root.update() | |
2508 | # | |
2509 | # normals | |
2510 | # | |
2511 | file.write(struct.pack('f',0)) | |
2512 | file.write(struct.pack('f',0)) | |
2513 | file.write(struct.pack('f',0)) | |
2514 | # | |
2515 | # vertices | |
2516 | # | |
2517 | file.write(struct.pack('f',x[i]*units)) | |
2518 | file.write(struct.pack('f',y[i]*units)) | |
2519 | file.write(struct.pack('f',z[i]*units)) | |
2520 | file.write(struct.pack('f',x[i+1]*units)) | |
2521 | file.write(struct.pack('f',y[i+1]*units)) | |
2522 | file.write(struct.pack('f',z[i+1]*units)) | |
2523 | file.write(struct.pack('f',x[i+2]*units)) | |
2524 | file.write(struct.pack('f',y[i+2]*units)) | |
2525 | file.write(struct.pack('f',z[i+2]*units)) | |
2526 | # | |
2527 | # padding | |
2528 | # | |
2529 | file.write(struct.pack('xx')) | |
2530 | file.close() | |
2531 | string_msg.set("wrote %s"%filename) | |
2532 | root.update() | |
2533 | ||
2534 | def write_gerber(): | |
2535 | # | |
2536 | # Gerber (RS-274X) output | |
2537 | # | |
2538 | filename = string_cam_file.get() | |
2539 | file = open(filename, 'wb') | |
2540 | units = cad.inches_per_unit | |
2541 | # | |
2542 | # write parameters | |
2543 | # | |
2544 | file.write("%FSLAX24Y24*%\n") # leading zeros omitted, absolute coordinates, 2.4 | |
2545 | file.write("%MOIN*%\n") # inches units | |
2546 | file.write("%OFA0B0*%\n") # no offset | |
2547 | # | |
2548 | # find and write apertures | |
2549 | # | |
2550 | ixs = cad.x[::2] | |
2551 | xs = cad.xmin+(cad.xmax-cad.xmin)*(ixs+0.5)/float(cad.nx) | |
2552 | ixe = cad.x[1::2] | |
2553 | xe = cad.xmin+(cad.xmax-cad.xmin)*(ixe+0.5)/float(cad.nx) | |
2554 | idx = ixe - ixs | |
2555 | dx = xe - xs | |
2556 | iys = cad.y[::2] | |
2557 | ys = cad.ymin+(cad.ymax-cad.ymin)*(iys+0.5)/float(cad.ny) | |
2558 | iye = cad.y[1::2] | |
2559 | ye = cad.ymin+(cad.ymax-cad.ymin)*(iye+0.5)/float(cad.ny) | |
2560 | idy = iye - iys | |
2561 | dy = ye - ys | |
2562 | mins = where((idx < idy),idx,idy) | |
2563 | uniques = unique(mins) | |
2564 | apertures = (cad.xmax-cad.xmin)*uniques/float(cad.nx) | |
2565 | index = searchsorted(uniques,mins) | |
2566 | for i in range(len(uniques)): | |
2567 | file.write("%%ADD%dR,%.4fX%.4f*%%\n"%(i+10,apertures[i],apertures[i])) | |
2568 | # | |
2569 | # write flashes | |
2570 | # | |
2571 | coords = arange(len(mins)) | |
2572 | for i in range(len(uniques)): | |
2573 | file.write("D%d*\n"%(i+10)) | |
2574 | coord = coords[index == i] | |
2575 | delta = apertures[i]/2. | |
2576 | ixs = (10000*(xs+delta)).astype(int32) | |
2577 | ixe = (10000*(xe-delta)).astype(int32) | |
2578 | iys = (10000*(ys+delta)).astype(int32) | |
2579 | iye = (10000*(ye-delta)).astype(int32) | |
2580 | for j in range(len(coord)): | |
2581 | n = coord[j] | |
2582 | if (idx[n] == idy[n]): | |
2583 | # | |
2584 | # flash | |
2585 | # | |
2586 | file.write('X%dY%dD03*\n'%(ixs[n],iys[n])) | |
2587 | elif (idx[n] > idy[n]): | |
2588 | # | |
2589 | # stroke horizontal | |
2590 | # | |
2591 | file.write('X%dY%dD02*\n'%(ixs[n],iys[n])) | |
2592 | file.write('X%dY%dD01*\n'%(ixe[n],iys[n])) | |
2593 | else: | |
2594 | # | |
2595 | # stroke vertical | |
2596 | # | |
2597 | file.write('X%dY%dD02*\n'%(ixs[n],iys[n])) | |
2598 | file.write('X%dY%dD01*\n'%(ixs[n],iye[n])) | |
2599 | file.write("M02*\n") # end of file | |
2600 | file.close() | |
2601 | string_msg.set("wrote %s (RS-274X)"%filename) | |
2602 | root.update() | |
2603 | ||
2604 | def write_excellon(): | |
2605 | # | |
2606 | # Excellon (RS-) output | |
2607 | # | |
2608 | """ | |
2609 | % Rewind and Stop | |
2610 | X#Y# Move and Drill | |
2611 | T# Tool Selection | |
2612 | M30 End of Program | |
2613 | M00 End of Program | |
2614 | R#X#Y# Repeat Hole | |
2615 | G05, G81 Select Drill Mode | |
2616 | G90 Absolute Mode | |
2617 | G91 Incremental Mode | |
2618 | G92 X#Y# Set Zero | |
2619 | G93 X#Y# Set Zero | |
2620 | M48 Program Header to first "%" | |
2621 | M72 English-Imperial Mode | |
2622 | ||
2623 | """ | |
2624 | filename = string_cam_file.get() | |
2625 | file = open(filename, 'wb') | |
2626 | units = cad.inches_per_unit | |
2627 | # | |
2628 | # write parameters | |
2629 | # | |
2630 | file.write("%FSLAX24Y24*%\n") # leading zeros omitted, absolute coordinates, 2.4 | |
2631 | file.write("%MOIN*%\n") # inches units | |
2632 | file.write("%OFA0B0*%\n") # no offset | |
2633 | # | |
2634 | # find and write apertures | |
2635 | # | |
2636 | ixs = cad.x[::2] | |
2637 | xs = cad.xmin+(cad.xmax-cad.xmin)*(ixs+0.5)/float(cad.nx) | |
2638 | ixe = cad.x[1::2] | |
2639 | xe = cad.xmin+(cad.xmax-cad.xmin)*(ixe+0.5)/float(cad.nx) | |
2640 | idx = ixe - ixs | |
2641 | dx = xe - xs | |
2642 | iys = cad.y[::2] | |
2643 | ys = cad.ymin+(cad.ymax-cad.ymin)*(iys+0.5)/float(cad.ny) | |
2644 | iye = cad.y[1::2] | |
2645 | ye = cad.ymin+(cad.ymax-cad.ymin)*(iye+0.5)/float(cad.ny) | |
2646 | idy = iye - iys | |
2647 | dy = ye - ys | |
2648 | mins = where((idx < idy),idx,idy) | |
2649 | uniques = unique(mins) | |
2650 | apertures = (cad.xmax-cad.xmin)*uniques/float(cad.nx) | |
2651 | index = searchsorted(uniques,mins) | |
2652 | for i in range(len(uniques)): | |
2653 | file.write("%%ADD%dR,%.4fX%.4f*%%\n"%(i+10,apertures[i],apertures[i])) | |
2654 | # | |
2655 | # write flashes | |
2656 | # | |
2657 | coords = arange(len(mins)) | |
2658 | for i in range(len(uniques)): | |
2659 | file.write("D%d*\n"%(i+10)) | |
2660 | coord = coords[index == i] | |
2661 | delta = apertures[i]/2. | |
2662 | ixs = (10000*(xs+delta)).astype(int32) | |
2663 | ixe = (10000*(xe-delta)).astype(int32) | |
2664 | iys = (10000*(ys+delta)).astype(int32) | |
2665 | iye = (10000*(ye-delta)).astype(int32) | |
2666 | for j in range(len(coord)): | |
2667 | n = coord[j] | |
2668 | if (idx[n] == idy[n]): | |
2669 | # | |
2670 | # flash | |
2671 | # | |
2672 | file.write('X%dY%dD03*\n'%(ixs[n],iys[n])) | |
2673 | elif (idx[n] > idy[n]): | |
2674 | # | |
2675 | # stroke horizontal | |
2676 | # | |
2677 | file.write('X%dY%dD02*\n'%(ixs[n],iys[n])) | |
2678 | file.write('X%dY%dD01*\n'%(ixe[n],iys[n])) | |
2679 | else: | |
2680 | # | |
2681 | # stroke vertical | |
2682 | # | |
2683 | file.write('X%dY%dD02*\n'%(ixs[n],iys[n])) | |
2684 | file.write('X%dY%dD01*\n'%(ixs[n],iye[n])) | |
2685 | file.write("M02*\n") # end of file | |
2686 | file.close() | |
2687 | string_msg.set("wrote %s (RS-274X)"%filename) | |
2688 | root.update() | |
2689 | ||
2690 | def write_ca(): | |
2691 | # | |
2692 | # CA output | |
2693 | # | |
2694 | filename = string_cam_file.get() | |
2695 | file = open(filename, 'wb') | |
2696 | file.write(chr(0xB9)) # magic number 0xB9 | |
2697 | file.write(chr(ca.nx/256)) # x size | |
2698 | file.write(chr(ca.nx%256)) # | |
2699 | file.write(chr(ca.ny/256)) # y size | |
2700 | file.write(chr(ca.ny%256)) # | |
2701 | file.write(chr(4)) # LED sub-array x | |
2702 | file.write(chr(2)) # LED sub-array y | |
2703 | for y in range(ca.nx): | |
2704 | for x in range(ca.nx): | |
2705 | if (ca.in1[y,x] == ca.E): | |
2706 | config = 0 | |
2707 | elif (ca.in1[y,x] == ca.NE): | |
2708 | config = 1 | |
2709 | elif (ca.in1[y,x] == ca.N): | |
2710 | config = 2 | |
2711 | elif (ca.in1[y,x] == ca.NW): | |
2712 | config = 3 | |
2713 | elif (ca.in1[y,x] == ca.W): | |
2714 | config = 4 | |
2715 | elif (ca.in1[y,x] == ca.SW): | |
2716 | config = 5 | |
2717 | elif (ca.in1[y,x] == ca.S): | |
2718 | config = 6 | |
2719 | elif (ca.in1[y,x] == ca.SE): | |
2720 | config = 7 | |
2721 | elif (ca.in1[y,x] == ca.empty): # XOR W W for empty | |
2722 | config = 4 | |
2723 | if (ca.in2[y,x] == ca.E): | |
2724 | config += 0 | |
2725 | elif (ca.in2[y,x] == ca.NE): | |
2726 | config += (1 << 3) | |
2727 | elif (ca.in2[y,x] == ca.N): | |
2728 | config += (2 << 3) | |
2729 | elif (ca.in2[y,x] == ca.NW): | |
2730 | config += (3 << 3) | |
2731 | elif (ca.in2[y,x] == ca.W): | |
2732 | config += (4 << 3) | |
2733 | elif (ca.in2[y,x] == ca.SW): | |
2734 | config += (5 << 3) | |
2735 | elif (ca.in2[y,x] == ca.S): | |
2736 | config += (6 << 3) | |
2737 | elif (ca.in2[y,x] == ca.SE): | |
2738 | config += (7 << 3) | |
2739 | elif (ca.in2[y,x] == ca.empty): # XOR W W for empty | |
2740 | config += (4 << 3) | |
2741 | if (ca.gates[y,x] == ca.AND): | |
2742 | config += 0 | |
2743 | elif (ca.gates[y,x] == ca.OR): | |
2744 | config += (1 << 6) | |
2745 | elif (ca.gates[y,x] == ca.XOR): | |
2746 | config += (2 << 6) | |
2747 | elif (ca.gates[y,x] == ca.NAND): | |
2748 | config += (3 << 6) | |
2749 | elif (ca.gates[y,x] == ca.empty): # XOR W W for empty | |
2750 | config += (2 << 6) | |
2751 | file.write(chr(config)) | |
2752 | for y in range(ca.ny): | |
2753 | for x in range((ca.nx/8)): | |
2754 | state = \ | |
2755 | (ca.states[y,8*x+0] << 7) \ | |
2756 | + (ca.states[y,8*x+1] << 6) \ | |
2757 | + (ca.states[y,8*x+2] << 5) \ | |
2758 | + (ca.states[y,8*x+3] << 4) \ | |
2759 | + (ca.states[y,8*x+4] << 3) \ | |
2760 | + (ca.states[y,8*x+5] << 2) \ | |
2761 | + (ca.states[y,8*x+6] << 1) \ | |
2762 | + (ca.states[y,8*x+7] << 0) | |
2763 | file.write(chr(state)) | |
2764 | if ((ca.nx%8) != 0): | |
2765 | x = cad.nx/8 | |
2766 | state = 0 | |
2767 | for i in range((ca.nx%8)): | |
2768 | state += (ca.states[y,8*x+i] << (7-i)) | |
2769 | file.write(chr(state)) | |
2770 | file.close() | |
2771 | string_msg.set("wrote %s"%filename) | |
2772 | root.update() | |
2773 | ||
2774 | def msg_xy(event): | |
2775 | x = (cad.xmin+cad.xmax)/2. + (cad.xmax-cad.xmin)*(1+event.x-cad.nplot/2.)/float(cad.nxplot()) | |
2776 | y = (cad.ymin+cad.ymax)/2. + (cad.ymin-cad.ymax)*(1+event.y-cad.nplot/2.)/float(cad.nyplot()) | |
2777 | string_msg.set("x = %.2f y = %.2f"%(x,y)) | |
2778 | ||
2779 | def msg_yz(event): | |
2780 | if (cad.nz > 1): | |
2781 | y = (cad.ymin+cad.ymax)/2. + (cad.ymin-cad.ymax)*(1+event.y-cad.nplot/2.)/float(cad.nyplot()) | |
2782 | z = (cad.zmin+cad.zmax)/2. + (cad.zmin-cad.zmax)*(1+event.x-cad.nplot/2.)/float(cad.nzplot()) | |
2783 | string_msg.set("y = %.2f z = %.2f"%(y,z)) | |
2784 | else: | |
2785 | string_msg.set("") | |
2786 | ||
2787 | def msg_xz(event): | |
2788 | if (cad.nz > 1): | |
2789 | x = (cad.xmin+cad.xmax)/2. + (cad.xmax-cad.xmin)*(1+event.x-cad.nplot/2.)/float(cad.nxplot()) | |
2790 | z = (cad.zmin+cad.zmax)/2. + (cad.zmin-cad.zmax)*(1+event.y-cad.nplot/2.)/float(cad.nzplot()) | |
2791 | string_msg.set("x = %.2f z = %.2f"%(x,z)) | |
2792 | else: | |
2793 | string_msg.set("") | |
2794 | ||
2795 | def msg_nomsg(event): | |
2796 | string_msg.set("") | |
2797 | ||
2798 | def image_min_x(event): | |
2799 | cad.xmin = float(string_image_xmin.get()) | |
2800 | xwidth = float(string_image_xwidth.get()) | |
2801 | cad.xmax = cad.xmin + xwidth | |
2802 | root.update() | |
2803 | ||
2804 | def image_min_y(event): | |
2805 | cad.ymin = float(string_image_ymin.get()) | |
2806 | yheight = float(string_image_yheight.get()) | |
2807 | cad.ymax = cad.ymin + yheight | |
2808 | root.update() | |
2809 | ||
2810 | def image_scale_x(event): | |
2811 | yheight = float(string_image_yheight.get()) | |
2812 | xwidth = yheight*cad.nx/float(cad.ny) | |
2813 | cad.xmax = cad.xmin + xwidth | |
2814 | string_image_xwidth.set(str(xwidth)) | |
2815 | root.update() | |
2816 | ||
2817 | def image_scale_y(event): | |
2818 | xwidth = float(string_image_xwidth.get()) | |
2819 | yheight = xwidth*cad.ny/float(cad.nx) | |
2820 | cad.ymax = cad.ymin + yheight | |
2821 | string_image_yheight.set(str(yheight)) | |
2822 | root.update() | |
2823 | ||
2824 | def send_to(event): | |
2825 | save_cam(0) | |
2826 | cam_file_name = string_cam_file.get() | |
2827 | send_to_file(cam_file_name) | |
2828 | ||
2829 | def send_to_file(cam_file_name): | |
2830 | cad_path = os.path.dirname(sys.argv[0]) | |
2831 | if (sys.argv[0] == "cad.py"): | |
2832 | cfg_path = "cad.cfg" | |
2833 | else: | |
2834 | cfg_path = os.path.dirname(sys.argv[0])+"/cad.cfg" | |
2835 | try: | |
2836 | config_file = open(cfg_path, 'r') | |
2837 | except: | |
2838 | string_msg.set(cfg_path+" not found") | |
2839 | root.update() | |
2840 | return() | |
2841 | dot = find(cam_file_name,".") | |
2842 | while 1: | |
2843 | new_dot = find(cam_file_name,".",dot+1) | |
2844 | if (new_dot == -1): | |
2845 | break | |
2846 | else: | |
2847 | dot = new_dot | |
2848 | suffix = cam_file_name[dot+1:] | |
2849 | while 1: | |
2850 | line = config_file.readline() | |
2851 | if (find(line,suffix) == 0): | |
2852 | string_msg.set("sending "+cam_file_name+" ...") | |
2853 | root.update() | |
2854 | quote1 = find(line,"'") | |
2855 | quote2 = find(line,"'",quote1+1) | |
2856 | cmd = line[(quote1+1):quote2] | |
2857 | if (os.name == 'nt'): | |
2858 | cam_file_name = replace(cam_file_name,'/','\\') | |
2859 | cmd = replace(cmd,'file','"'+cam_file_name+'"') | |
2860 | os.system(cmd) | |
2861 | string_msg.set(cam_file_name+" sent") | |
2862 | root.update() | |
2863 | config_file.close() | |
2864 | root.update() | |
2865 | return() | |
2866 | elif (line == ""): | |
2867 | string_msg.set(suffix+" driver not defined in "+cfg_path) | |
2868 | config_file.close() | |
2869 | root.update() | |
2870 | return() | |
2871 | ||
2872 | def resize_window(event): | |
2873 | # | |
2874 | # resize drawing windows | |
2875 | # | |
2876 | cad.nplot = int(string_window_size.get()) | |
2877 | cad.view(cad.views) | |
2878 | render() | |
2879 | ||
2880 | def resize_editor(event): | |
2881 | # | |
2882 | # resize editing windows | |
2883 | # | |
2884 | cad.editor_height = int(string_editor_height.get()) | |
2885 | widget_cad_text.config(height=cad.editor_height) | |
2886 | cad.editor_width = int(string_editor_width.get()) | |
2887 | widget_cad_text.config(width=cad.editor_width) | |
2888 | widget_function_text.config(width=cad.editor_width) | |
2889 | root.update() | |
2890 | ||
2891 | def reload(): | |
2892 | # | |
2893 | # reload input file | |
2894 | # | |
2895 | filename = string_input_file.get() | |
2896 | if (find(filename,'.cad') != -1): | |
2897 | cad_load(0) | |
2898 | elif ((find(filename,'.jpg') != -1) | (find(filename,'.JPG') != -1) | | |
2899 | (find(filename,'.png') != -1) | (find(filename,'.PNG') != -1) | | |
2900 | (find(filename,'.gif') != -1) | (find(filename,'.GIF') != -1)): | |
2901 | widget_cad_text.delete("1.0",END) | |
2902 | image_load(0) | |
2903 | else: | |
2904 | string_msg.set("unsupported input file format") | |
2905 | root.update() | |
2906 | ||
2907 | # | |
2908 | # set up GUI | |
2909 | # | |
2910 | root = Tk() | |
2911 | root.title('cad.py') | |
2912 | # | |
2913 | # message frame | |
2914 | # | |
2915 | msg_frame = Frame(root) | |
2916 | string_msg = StringVar() | |
2917 | widget_msg = Label(msg_frame, textvariable = string_msg) | |
2918 | widget_msg.pack(side='right') | |
2919 | Label(msg_frame, text=" ").pack(side='right') | |
2920 | widget_stop = Button(msg_frame, text='stop', borderwidth=2) | |
2921 | widget_stop.bind('<Button-1>',render_stop) | |
2922 | msg_frame.grid(row=0,column=0) | |
2923 | # | |
2924 | # size frame | |
2925 | # | |
2926 | size_frame = Frame(root) | |
2927 | Label(size_frame, text="window size: ").pack(side='left') | |
2928 | string_window_size = StringVar() | |
2929 | string_window_size.set(str(cad.nplot)) | |
2930 | widget_window_size = Entry(size_frame, width=4, bg='white', textvariable=string_window_size) | |
2931 | widget_window_size.bind('<Return>',resize_window) | |
2932 | widget_window_size.pack(side='left') | |
2933 | Label(size_frame, text=" editor width: ").pack(side='left') | |
2934 | string_editor_width = StringVar() | |
2935 | string_editor_width.set(str(cad.editor_width)) | |
2936 | widget_editor_width = Entry(size_frame, width=3, bg='white', textvariable=string_editor_width) | |
2937 | widget_editor_width.bind('<Return>',resize_editor) | |
2938 | widget_editor_width.pack(side='left') | |
2939 | Label(size_frame, text=" height: ").pack(side='left') | |
2940 | string_editor_height = StringVar() | |
2941 | string_editor_height.set(str(cad.editor_height)) | |
2942 | widget_editor_height = Entry(size_frame, width=3, bg='white', textvariable=string_editor_height) | |
2943 | widget_editor_height.bind('<Return>',resize_editor) | |
2944 | widget_editor_height.pack(side='left') | |
2945 | size_frame.grid(row=0,column=1) | |
2946 | # | |
2947 | # view frame | |
2948 | # | |
2949 | view_frame2 = Frame(root) | |
2950 | view_frame3 = Frame(root) | |
2951 | canvas_xy = Canvas(view_frame3) | |
2952 | canvas_xz = Canvas(view_frame3) | |
2953 | canvas_yz = Canvas(view_frame3) | |
2954 | canvas_xyz = Canvas(view_frame3) | |
2955 | cad.view('xyzr') | |
2956 | # | |
2957 | # I/O frame | |
2958 | # | |
2959 | io_frame = Frame(root) | |
2960 | io_frame.grid(row=2,column=1,sticky=N) | |
2961 | #cad_frame.bind('<Motion>',msg_nomsg) | |
2962 | # | |
2963 | # input frame | |
2964 | # | |
2965 | input_frame = Frame(io_frame) | |
2966 | input_frame.pack() | |
2967 | # | |
2968 | # .cad editor | |
2969 | # | |
2970 | editor_frame = Frame(input_frame) | |
2971 | widget_text_yscrollbar = Scrollbar(editor_frame) | |
2972 | widget_cad_text = Text(editor_frame, bg='white', bd=5, width=cad.editor_width, height=cad.editor_height, yscrollcommand=widget_text_yscrollbar.set) | |
2973 | widget_cad_text.grid(row=1,column=1) | |
2974 | widget_text_yscrollbar.grid(row=1,column=2,sticky=N+S) | |
2975 | widget_text_yscrollbar.config(command=widget_cad_text.yview) | |
2976 | widget_cad_text.bind('<Motion>',msg_nomsg) | |
2977 | editor_frame.pack() | |
2978 | # | |
2979 | # input file | |
2980 | # | |
2981 | cad_input_frame = Frame(input_frame) | |
2982 | widget_input_file = Button(cad_input_frame, text="input:",command=input_open) | |
2983 | widget_input_file.pack(side='left') | |
2984 | string_input_file = StringVar() | |
2985 | string_input_file.set('out.cad') | |
2986 | widget_cad = Entry(cad_input_frame, width=17, bg='white', textvariable=string_input_file) | |
2987 | widget_cad.pack(side='left') | |
2988 | Label(cad_input_frame, text=" ").pack(side='left') | |
2989 | widget_cad_save = Button(cad_input_frame, text="save") | |
2990 | widget_cad_save.bind('<Button-1>',cad_save) | |
2991 | widget_cad_save.pack(side='left') | |
2992 | Label(cad_input_frame, text=" ").pack(side='left') | |
2993 | widget_reload = Button(cad_input_frame, text="reload",command=reload) | |
2994 | widget_reload.pack(side='left') | |
2995 | cad_input_frame.pack() | |
2996 | # | |
2997 | # image x | |
2998 | # | |
2999 | image_x_frame = Frame(input_frame) | |
3000 | Label(image_x_frame, text="x min: ").pack(side='left') | |
3001 | string_image_xmin = StringVar() | |
3002 | widget_image_xmin = Entry(image_x_frame, width=6, bg='white', textvariable=string_image_xmin) | |
3003 | widget_image_xmin.bind('<Return>',image_min_x) | |
3004 | widget_image_xmin.pack(side='left') | |
3005 | Label(image_x_frame, text=" x width: ").pack(side='left') | |
3006 | string_image_xwidth = StringVar() | |
3007 | widget_image_xwidth = Entry(image_x_frame, width=6, bg='white', textvariable=string_image_xwidth) | |
3008 | widget_image_xwidth.bind('<Return>',image_scale_y) | |
3009 | widget_image_xwidth.pack(side='left') | |
3010 | string_image_nx = StringVar() | |
3011 | Label(image_x_frame, textvariable = string_image_nx).pack(side='left') | |
3012 | # | |
3013 | # image y | |
3014 | # | |
3015 | image_y_frame = Frame(input_frame) | |
3016 | Label(image_y_frame, text="y min: ").pack(side='left') | |
3017 | string_image_ymin = StringVar() | |
3018 | widget_image_ymin = Entry(image_y_frame, width=6, bg='white', textvariable=string_image_ymin) | |
3019 | widget_image_ymin.bind('<Return>',image_min_y) | |
3020 | widget_image_ymin.pack(side='left') | |
3021 | Label(image_y_frame, text=" y height: ").pack(side='left') | |
3022 | string_image_yheight = StringVar() | |
3023 | widget_image_yheight = Entry(image_y_frame, width=6, bg='white', textvariable=string_image_yheight) | |
3024 | widget_image_yheight.bind('<Return>',image_scale_x) | |
3025 | widget_image_yheight.pack(side='left') | |
3026 | string_image_ny = StringVar() | |
3027 | Label(image_y_frame, textvariable = string_image_ny).pack(side='left') | |
3028 | # | |
3029 | # image z | |
3030 | # | |
3031 | image_z_frame = Frame(input_frame) | |
3032 | Label(image_z_frame, text="z min: ").pack(side='left') | |
3033 | string_image_zmin = StringVar() | |
3034 | widget_image_zmin = Entry(image_z_frame, width=6, bg='white', textvariable=string_image_zmin) | |
3035 | widget_image_zmin.pack(side='left') | |
3036 | Label(image_z_frame, text=" z max: ").pack(side='left') | |
3037 | string_image_zmax = StringVar() | |
3038 | widget_image_zmax = Entry(image_z_frame, width=6, bg='white', textvariable=string_image_zmax) | |
3039 | widget_image_zmax.pack(side='left') | |
3040 | Label(image_z_frame, text=" nz: ").pack(side='left') | |
3041 | string_image_nz = StringVar() | |
3042 | widget_image_nz = Entry(image_z_frame, width=6, bg='white', textvariable=string_image_nz) | |
3043 | widget_image_nz.pack(side='left') | |
3044 | # | |
3045 | # image intensity | |
3046 | # | |
3047 | image_intensity_frame = Frame(input_frame) | |
3048 | Label(image_intensity_frame, text="intensity min: ").pack(side='left') | |
3049 | string_image_min = StringVar() | |
3050 | widget_image_min = Entry(image_intensity_frame, width=6, bg='white', textvariable=string_image_min) | |
3051 | widget_image_min.pack(side='left') | |
3052 | Label(image_intensity_frame, text=" intensity max: ").pack(side='left') | |
3053 | string_image_max = StringVar() | |
3054 | widget_image_max = Entry(image_intensity_frame, width=6, bg='white', textvariable=string_image_max) | |
3055 | widget_image_max.pack(side='left') | |
3056 | # | |
3057 | # image units | |
3058 | # | |
3059 | image_units_frame = Frame(input_frame) | |
3060 | Label(image_units_frame, text="inches per unit: ").pack(side='left') | |
3061 | string_image_units = StringVar() | |
3062 | widget_image_units = Entry(image_units_frame, width=6, bg='white', textvariable=string_image_units) | |
3063 | widget_image_units.pack(side='left') | |
3064 | # | |
3065 | # image invert | |
3066 | # | |
3067 | image_invert_frame = Frame(input_frame) | |
3068 | Label(image_invert_frame, text=" ").pack(side='left') | |
3069 | widget_image_invert = Button(image_invert_frame, text="invert image") | |
3070 | widget_image_invert.pack(side='left') | |
3071 | widget_image_invert.bind('<Button-1>',invert_image) | |
3072 | # | |
3073 | # output frame | |
3074 | # | |
3075 | output_frame = Frame(io_frame) | |
3076 | output_frame.pack() | |
3077 | # | |
3078 | # controls | |
3079 | # | |
3080 | control_frame = Frame(output_frame) | |
3081 | widget_render = Button(control_frame, text="render") | |
3082 | widget_render.bind('<Button-1>',render_button) | |
3083 | widget_render.pack(side='left') | |
3084 | Label(control_frame, text=" ").pack(side='left') | |
3085 | canvas_logo = Canvas(control_frame, width=26, height=26, background="white") | |
3086 | canvas_logo.create_oval(2,2,8,8,fill="red",outline="") | |
3087 | canvas_logo.create_rectangle(11,2,17,8,fill="blue",outline="") | |
3088 | canvas_logo.create_rectangle(20,2,26,8,fill="blue",outline="") | |
3089 | canvas_logo.create_rectangle(2,11,8,17,fill="blue",outline="") | |
3090 | canvas_logo.create_oval(10,10,16,16,fill="red",outline="") | |
3091 | canvas_logo.create_rectangle(20,11,26,17,fill="blue",outline="") | |
3092 | canvas_logo.create_rectangle(2,20,8,26,fill="blue",outline="") | |
3093 | canvas_logo.create_rectangle(11,20,17,26,fill="blue",outline="") | |
3094 | canvas_logo.create_rectangle(20,20,26,26,fill="blue",outline="") | |
3095 | canvas_logo.pack(side='left') | |
3096 | control_text = " cad.py (%s) "%DATE | |
3097 | Label(control_frame, text=control_text).pack(side='left') | |
3098 | widget_cam = Button(control_frame, text="cam") | |
3099 | widget_cam.bind('<Button-1>',cam) | |
3100 | widget_cam.pack(side='left') | |
3101 | Label(control_frame, text=" ").pack(side='left') | |
3102 | widget_quit = Button(control_frame, text="quit", command='exit') | |
3103 | widget_quit.pack(side='left') | |
3104 | control_frame.pack() | |
3105 | # | |
3106 | # function string | |
3107 | # | |
3108 | function_string_frame = Frame(output_frame) | |
3109 | Label(function_string_frame, text="function:").grid(row=1,column=1) | |
3110 | widget_function_yscrollbar = Scrollbar(function_string_frame) | |
3111 | widget_function_text = Text(function_string_frame, bg='white', bd=5, width=cad.editor_width, height=12, yscrollcommand=widget_function_yscrollbar.set, state=DISABLED) | |
3112 | widget_function_text.grid(row=2,column=1) | |
3113 | widget_function_yscrollbar.grid(row=2,column=2,sticky=N+S) | |
3114 | widget_function_yscrollbar.config(command=widget_function_text.yview) | |
3115 | function_string_frame.pack() | |
3116 | # | |
3117 | # CAM file | |
3118 | # | |
3119 | cam_file_frame = Frame(output_frame) | |
3120 | widget_cam_menu_button = Menubutton(cam_file_frame,text="output format", relief=RAISED) | |
3121 | widget_cam_menu_button.pack(side='left') | |
3122 | widget_cam_menu = Menu(widget_cam_menu_button) | |
3123 | widget_cam_menu.add_command(label='.epi (Epilog)',command=select_epi) | |
3124 | widget_cam_menu.add_command(label='.camm (CAMM)',command=select_camm) | |
3125 | widget_cam_menu.add_command(label='.rml (Modela)',command=select_rml) | |
3126 | widget_cam_menu.add_command(label='.sbp (ShopBot)',command=select_sbp) | |
3127 | widget_cam_menu.add_command(label='.gcode (Gcode)',command=select_g) | |
3128 | widget_cam_menu.add_command(label='.ps (Postscript)',command=select_ps) | |
3129 | widget_cam_menu.add_command(label='.ord (OMAX)',command=select_ord) | |
3130 | widget_cam_menu.add_command(label='.oms (Resonetics)',command=select_oms) | |
3131 | widget_cam_menu.add_command(label='.grb (Gerber)',command=select_gerber) | |
3132 | widget_cam_menu.add_command(label='.drl (Excellon)',command=select_excellon) | |
3133 | widget_cam_menu.add_command(label='.stl (STL)',command=select_stl) | |
3134 | widget_cam_menu.add_command(label='.dxf (DXF)',command=select_dxf) | |
3135 | widget_cam_menu.add_command(label='.jpg (JPG)',command=select_jpg) | |
3136 | widget_cam_menu.add_command(label='.png (PNG)',command=select_png) | |
3137 | widget_cam_menu.add_command(label='.ca (CA)',command=select_ca) | |
3138 | widget_cam_menu.add_command(label='.uni (Universal)',command=select_uni) | |
3139 | widget_cam_menu.add_command(label='.epb (Epilog bitmap)',state=DISABLED) | |
3140 | widget_cam_menu_button['menu'] = widget_cam_menu | |
3141 | Label(cam_file_frame, text=" output file: ").pack(side='left') | |
3142 | string_cam_file = StringVar() | |
3143 | widget_cam_file = Entry(cam_file_frame, width=12, bg='white', textvariable=string_cam_file) | |
3144 | widget_cam_file.pack(side='left') | |
3145 | Label(cam_file_frame, text=" ").pack(side='left') | |
3146 | widget_cam_save = Button(cam_file_frame, text="save") | |
3147 | widget_cam_save.bind('<Button-1>',save_cam) | |
3148 | widget_cam_save.pack(side='left') | |
3149 | # | |
3150 | # vectorization | |
3151 | # | |
3152 | cam_vector_frame = Frame(output_frame) | |
3153 | Label(cam_vector_frame, text="maximum vector fit error (lattice units): ").pack(side='left') | |
3154 | string_vector_error = StringVar() | |
3155 | string_vector_error.set('.75') | |
3156 | widget_vector_error = Entry(cam_vector_frame, width=6, bg='white', textvariable=string_vector_error) | |
3157 | widget_vector_error.pack(side='left') | |
3158 | # | |
3159 | # tool | |
3160 | # | |
3161 | cam_dia_frame = Frame(output_frame) | |
3162 | Label(cam_dia_frame, text="tool diameter: ").pack(side='left') | |
3163 | string_tool_dia = StringVar() | |
3164 | string_tool_dia.set('0') | |
3165 | widget_tool_dia = Entry(cam_dia_frame, width=6, bg='white', textvariable=string_tool_dia) | |
3166 | widget_tool_dia.pack(side='left') | |
3167 | Label(cam_dia_frame, text=" tool overlap: ").pack(side='left') | |
3168 | string_tool_overlap = StringVar() | |
3169 | string_tool_overlap.set('0.5') | |
3170 | widget_tool_overlap = Entry(cam_dia_frame, width=6, bg='white', textvariable=string_tool_overlap) | |
3171 | widget_tool_overlap.pack(side='left') | |
3172 | # | |
3173 | # contour | |
3174 | # | |
3175 | cam_contour_frame = Frame(output_frame) | |
3176 | Label(cam_contour_frame, text=" # contours (-1 for max): ").pack(side='left') | |
3177 | string_num_contours = StringVar() | |
3178 | string_num_contours.set('0') | |
3179 | widget_num_contours = Entry(cam_contour_frame, width=6, bg='white', textvariable=string_num_contours) | |
3180 | widget_num_contours.pack(side='left') | |
3181 | Label(cam_contour_frame, text=" ").pack(side='left') | |
3182 | widget_cam_contour = Button(cam_contour_frame, text="contour") | |
3183 | widget_cam_contour.pack(side='left') | |
3184 | widget_cam_contour.bind('<Button-1>',contour) | |
3185 | # | |
3186 | # laser power | |
3187 | # | |
3188 | laser_frame1 = Frame(output_frame) | |
3189 | Label(laser_frame1, text=" power:").pack(side='left') | |
3190 | string_laser_power = StringVar() | |
3191 | Entry(laser_frame1, width=6, bg='white', textvariable=string_laser_power).pack(side='left') | |
3192 | Label(laser_frame1, text=" speed:").pack(side='left') | |
3193 | string_laser_speed = StringVar() | |
3194 | Entry(laser_frame1, width=6, bg='white', textvariable=string_laser_speed).pack(side='left') | |
3195 | Label(laser_frame1, text=" rate: ").pack(side='left') | |
3196 | string_laser_rate = StringVar() | |
3197 | Entry(laser_frame1, width=6, bg='white', textvariable=string_laser_rate).pack(side='left') | |
3198 | # | |
3199 | # power range | |
3200 | # | |
3201 | laser_frame2 = Frame(output_frame) | |
3202 | Label(laser_frame2, text=" min power:").pack(side='left') | |
3203 | string_laser_min_power = StringVar() | |
3204 | Entry(laser_frame2, width=6, bg='white', textvariable=string_laser_min_power).pack(side='left') | |
3205 | Label(laser_frame2, text="% max power:").pack(side='left') | |
3206 | string_laser_max_power = StringVar() | |
3207 | Entry(laser_frame2, width=6, bg='white', textvariable=string_laser_max_power).pack(side='left') | |
3208 | Label(laser_frame2, text="%").pack(side='left') | |
3209 | # | |
3210 | # autofocus | |
3211 | # | |
3212 | laser_frame3 = Frame(output_frame) | |
3213 | integer_laser_autofocus = IntVar() | |
3214 | widget_autofocus = Checkbutton(laser_frame3, text="Auto Focus", variable=integer_laser_autofocus).pack(side='left') | |
3215 | # | |
3216 | # cutting | |
3217 | # | |
3218 | cut_frame = Frame(output_frame) | |
3219 | Label(cut_frame, text="force: ").pack(side='left') | |
3220 | string_cut_force = StringVar() | |
3221 | Entry(cut_frame, width=6, bg='white', textvariable=string_cut_force).pack(side='left') | |
3222 | Label(cut_frame, text=" velocity:").pack(side='left') | |
3223 | string_cut_velocity = StringVar() | |
3224 | Entry(cut_frame, width=6, bg='white', textvariable=string_cut_velocity).pack(side='left') | |
3225 | # | |
3226 | # speed | |
3227 | # | |
3228 | speed_frame = Frame(output_frame) | |
3229 | Label(speed_frame, text="xy speed:").pack(side='left') | |
3230 | string_xy_speed = StringVar() | |
3231 | Entry(speed_frame, width=4, bg='white', textvariable=string_xy_speed).pack(side='left') | |
3232 | Label(speed_frame, text=" z speed:").pack(side='left') | |
3233 | string_z_speed = StringVar() | |
3234 | Entry(speed_frame, width=4, bg='white', textvariable=string_z_speed).pack(side='left') | |
3235 | # | |
3236 | # jog | |
3237 | # | |
3238 | jog_frame = Frame(output_frame) | |
3239 | Label(jog_frame, text="jog xy speed:").pack(side='left') | |
3240 | string_jog_xy_speed = StringVar() | |
3241 | Entry(jog_frame, width=4, bg='white', textvariable=string_jog_xy_speed).pack(side='left') | |
3242 | Label(jog_frame, text=" z speed:").pack(side='left') | |
3243 | string_jog_z_speed = StringVar() | |
3244 | Entry(jog_frame, width=4, bg='white', textvariable=string_jog_z_speed).pack(side='left') | |
3245 | Label(jog_frame, text=" z:").pack(side='left') | |
3246 | string_jog_z = StringVar() | |
3247 | Entry(jog_frame, width=4, bg='white', textvariable=string_jog_z).pack(side='left') | |
3248 | # | |
3249 | # RML move | |
3250 | # | |
3251 | rml_move_frame = Frame(output_frame) | |
3252 | Label(rml_move_frame, text="x: ").pack(side='left') | |
3253 | string_rml_x_move = StringVar() | |
3254 | Entry(rml_move_frame, width=6, bg='white', textvariable=string_rml_x_move).pack(side='left') | |
3255 | Label(rml_move_frame, text=" y: ").pack(side='left') | |
3256 | string_rml_y_move = StringVar() | |
3257 | Entry(rml_move_frame, width=6, bg='white', textvariable=string_rml_y_move).pack(side='left') | |
3258 | Label(rml_move_frame, text=" ").pack(side='left') | |
3259 | widget_rml_move = Button(rml_move_frame, text="move") | |
3260 | widget_rml_move.pack(side='left') | |
3261 | widget_rml_move.bind('<Button-1>',rml_move) | |
3262 | # | |
3263 | # G codes | |
3264 | # | |
3265 | g_frame = Frame(output_frame) | |
3266 | Label(g_frame, text=" feed rate:").pack(side="left") | |
3267 | string_g_feed_rate = StringVar() | |
3268 | Entry(g_frame, width=6, textvariable=string_g_feed_rate).pack(side="left") | |
3269 | Label(g_frame, text=" spindle speed:").pack(side="left") | |
3270 | string_g_spindle_speed = StringVar() | |
3271 | Entry(g_frame, width=6, textvariable=string_g_spindle_speed).pack(side="left") | |
3272 | Label(g_frame, text=" tool:").pack(side="left") | |
3273 | string_g_tool = StringVar() | |
3274 | Entry(g_frame, width=3, textvariable=string_g_tool).pack(side="left") | |
3275 | integer_g_cool = IntVar() | |
3276 | widget_g_cool = Checkbutton(g_frame, text="coolant", variable=integer_g_cool) | |
3277 | widget_g_cool.pack(side="left") | |
3278 | # | |
3279 | # waterjet | |
3280 | # | |
3281 | waterjet_frame = Frame(output_frame) | |
3282 | Label(waterjet_frame,text="lead-in/out: ").pack(side='left') | |
3283 | string_lead_in = StringVar() | |
3284 | widget_lead_in = Entry(waterjet_frame, width=4, bg='white', textvariable=string_lead_in) | |
3285 | widget_lead_in.pack(side='left') | |
3286 | Label(waterjet_frame,text="quality: ").pack(side='left') | |
3287 | string_quality = StringVar() | |
3288 | widget_quality = Entry(waterjet_frame, width=4, bg='white', textvariable=string_quality) | |
3289 | widget_quality.pack(side='left') | |
3290 | # | |
3291 | # excimer | |
3292 | # | |
3293 | excimer_frame = Frame(output_frame) | |
3294 | Label(excimer_frame,text="period (usec): ").pack(side='left') | |
3295 | string_pulse_period = StringVar() | |
3296 | widget_pulse_period = Entry(excimer_frame, width=5, bg='white', textvariable=string_pulse_period) | |
3297 | widget_pulse_period.pack(side='left') | |
3298 | Label(excimer_frame,text="velocity: ").pack(side='left') | |
3299 | string_cut_vel = StringVar() | |
3300 | widget_cut_vel = Entry(excimer_frame, width=4, bg='white', textvariable=string_cut_vel) | |
3301 | widget_cut_vel.pack(side='left') | |
3302 | Label(excimer_frame,text="acceleration: ").pack(side='left') | |
3303 | string_cut_accel = StringVar() | |
3304 | widget_cut_accel = Entry(excimer_frame, width=4, bg='white', textvariable=string_cut_accel) | |
3305 | widget_cut_accel.pack(side='left') | |
3306 | # | |
3307 | # STL | |
3308 | # | |
3309 | STL_frame = Frame(output_frame) | |
3310 | widget_STL_triangulate = Button(STL_frame, text="triangulate") | |
3311 | widget_STL_triangulate.pack(side='left') | |
3312 | widget_STL_triangulate.bind('<Button-1>',triangulate) | |
3313 | # | |
3314 | # Gerber | |
3315 | # | |
3316 | Gerber_frame = Frame(output_frame) | |
3317 | widget_Gerber_convert = Button(Gerber_frame, text="convert") | |
3318 | widget_Gerber_convert.pack(side='left') | |
3319 | widget_Gerber_convert.bind('<Button-1>',flash) | |
3320 | # | |
3321 | # Excellon | |
3322 | # | |
3323 | Excellon_frame = Frame(output_frame) | |
3324 | widget_Excellon_convert = Button(Excellon_frame, text="convert") | |
3325 | widget_Excellon_convert.pack(side='left') | |
3326 | widget_Excellon_convert.bind('<Button-1>',flash) | |
3327 | # | |
3328 | # filling | |
3329 | # | |
3330 | fill_frame = Frame(output_frame) | |
3331 | integer_fill = IntVar() | |
3332 | widget_fill = Checkbutton(fill_frame, text="fill polygons", variable=integer_fill).pack(side='left') | |
3333 | # | |
3334 | # send to | |
3335 | # | |
3336 | send_to_frame = Frame(output_frame) | |
3337 | widget_send_to = Button(send_to_frame, text="send to machine") | |
3338 | widget_send_to.bind('<Button-1>',send_to) | |
3339 | widget_send_to.pack(side='left') | |
3340 | string_send_to_time = StringVar() | |
3341 | string_send_to_time.set("") | |
3342 | Label(send_to_frame,textvariable=string_send_to_time).pack(side='left') | |
3343 | ||
3344 | # | |
3345 | # define .cad template | |
3346 | # | |
3347 | cad_template = """# | |
3348 | # .cad template | |
3349 | # | |
3350 | ||
3351 | # | |
3352 | # define shapes and transformation | |
3353 | # | |
3354 | # circle(x0, y0, r) | |
3355 | # cylinder(x0, y0, z0, z1, r) | |
3356 | # cone(x0, y0, z0, z1, r0) | |
3357 | # sphere(x0, y0, z0, r) | |
3358 | # torus(x0, y0, z0, r0, r1) | |
3359 | # rectangle(x0, x1, y0, y1) | |
3360 | # cube(x0, x1, y0, y1, z0, z1) | |
3361 | # right_triangle(x0, y0, h) | |
3362 | # triangle(x0, y0, x1, y1, x2, y2) (points in clockwise order) | |
3363 | # pyramid(x0, x1, y0, y1, z0, z1) | |
3364 | # function(Z_of_XY) | |
3365 | # functions(upper_Z_of_XY,lower_Z_of_XY) | |
3366 | # add(part1, part2) | |
3367 | # subtract(part1, part2) | |
3368 | # intersect(part1, part2) | |
3369 | # move(part,dx,dy) | |
3370 | # translate(part,dx,dy,dz) | |
3371 | # rotate(part, angle) | |
3372 | # rotate_x(part, angle) | |
3373 | # rotate_y(part, angle) | |
3374 | # rotate_z(part, angle) | |
3375 | # rotate_90(part) | |
3376 | # rotate_180(part) | |
3377 | # rotate_270(part) | |
3378 | # reflect_x(part) | |
3379 | # reflect_y(part) | |
3380 | # reflect_z(part) | |
3381 | # reflect_xy(part) | |
3382 | # reflect_xz(part) | |
3383 | # reflect_yz(part) | |
3384 | # scale_x(part, x0, sx) | |
3385 | # scale_y(part, y0, sy) | |
3386 | # scale_z(part, z0, sz) | |
3387 | # scale_xy(part, x0, y0, sxy) | |
3388 | # scale_xyz(part, x0, y0, z0, sxyz) | |
3389 | # coscale_x_y(part, x0, y0, y1, angle0, angle1, amplitude, offset) | |
3390 | # coscale_x_z(part, x0, z0, z1, angle0, angle1, amplitude, offset) | |
3391 | # coscale_xy_z(part, x0, y0, z0, z1, angle0, angle1, amplitude, offset) | |
3392 | # taper_x_y(part, x0, y0, y1, s0, s1) | |
3393 | # taper_x_z(part, x0, z0, z1, s0, s1) | |
3394 | # taper_xy_z(part, x0, y0, z0, z1, s0, s1) | |
3395 | # shear_x_y(part, y0, y1, dx0, dx1) | |
3396 | # shear_x_z(part, z0, z1, dx0, dx1) | |
3397 | # (more to come) | |
3398 | ||
3399 | def circle(x0, y0, r): | |
3400 | part = "(((X-x0)**2 + (Y-y0)**2) <= r**2)" | |
3401 | part = replace(part,'x0',str(x0)) | |
3402 | part = replace(part,'y0',str(y0)) | |
3403 | part = replace(part,'r',str(r)) | |
3404 | return part | |
3405 | ||
3406 | def cylinder(x0, y0, z0, z1, r): | |
3407 | part = "(((X-x0)**2 + (Y-y0)**2 <= r**2) & (Z >= z0) & (Z <= z1))" | |
3408 | part = replace(part,'x0',str(x0)) | |
3409 | part = replace(part,'y0',str(y0)) | |
3410 | part = replace(part,'z0',str(z0)) | |
3411 | part = replace(part,'z1',str(z1)) | |
3412 | part = replace(part,'r',str(r)) | |
3413 | return part | |
3414 | ||
3415 | def cone(x0, y0, z0, z1, r0): | |
3416 | part = cylinder(x0, y0, z0, z1, r0) | |
3417 | part = taper_xy_z(part, x0, y0, z0, z1, 1.0, 0.0) | |
3418 | return part | |
3419 | ||
3420 | def sphere(x0, y0, z0, r): | |
3421 | part = "(((X-x0)**2 + (Y-y0)**2 + (Z-z0)**2) <= r**2)" | |
3422 | part = replace(part,'x0',str(x0)) | |
3423 | part = replace(part,'y0',str(y0)) | |
3424 | part = replace(part,'z0',str(z0)) | |
3425 | part = replace(part,'r',str(r)) | |
3426 | return part | |
3427 | ||
3428 | def torus(x0, y0, z0, r0, r1): | |
3429 | part = "(((r0 - sqrt((X-x0)**2 + (Y-y0)**2))**2 + (Z-z0)**2) <= r1**2)" | |
3430 | part = replace(part,'x0',str(x0)) | |
3431 | part = replace(part,'y0',str(y0)) | |
3432 | part = replace(part,'z0',str(z0)) | |
3433 | part = replace(part,'r0',str(r0)) | |
3434 | part = replace(part,'r1',str(r1)) | |
3435 | return part | |
3436 | ||
3437 | def rectangle(x0, x1, y0, y1): | |
3438 | part = "((X >= x0) & (X <= x1) & (Y >= y0) & (Y <= y1))" | |
3439 | part = replace(part,'x0',str(x0)) | |
3440 | part = replace(part,'x1',str(x1)) | |
3441 | part = replace(part,'y0',str(y0)) | |
3442 | part = replace(part,'y1',str(y1)) | |
3443 | return part | |
3444 | ||
3445 | def cube(x0, x1, y0, y1, z0, z1): | |
3446 | part = "((X >= x0) & (X <= x1) & (Y >= y0) & (Y <= y1) & (Z >= z0) & (Z <= z1))" | |
3447 | part = replace(part,'x0',str(x0)) | |
3448 | part = replace(part,'x1',str(x1)) | |
3449 | part = replace(part,'y0',str(y0)) | |
3450 | part = replace(part,'y1',str(y1)) | |
3451 | part = replace(part,'z0',str(z0)) | |
3452 | part = replace(part,'z1',str(z1)) | |
3453 | return part | |
3454 | ||
3455 | def right_triangle(x0, y0, h): | |
3456 | part = "((X > x0) & (X < x0 + h - (Y-y0)) & (Y > y0))" | |
3457 | part = replace(part,'x0',str(x0)) | |
3458 | part = replace(part,'y0',str(y0)) | |
3459 | part = replace(part,'h',str(h)) | |
3460 | return part | |
3461 | ||
3462 | def triangle(x0, y0, x1, y1, x2, y2): # points in clockwise order | |
3463 | part = "((((y1-y0)*(X-x0)-(x1-x0)*(Y-y0)) >= 0) & (((y2-y1)*(X-x1)-(x2-x1)*(Y-y1)) >= 0) & (((y0-y2)*(X-x2)-(x0-x2)*(Y-y2)) >= 0))" | |
3464 | part = replace(part,'x0',str(x0)) | |
3465 | part = replace(part,'y0',str(y0)) | |
3466 | part = replace(part,'x1',str(x1)) | |
3467 | part = replace(part,'y1',str(y1)) | |
3468 | part = replace(part,'x2',str(x2)) | |
3469 | part = replace(part,'y2',str(y2)) | |
3470 | return part | |
3471 | ||
3472 | def pyramid(x0, x1, y0, y1, z0, z1): | |
3473 | part = cube(x0, x1, y0, y1, z0, z1) | |
3474 | part = taper_xy_z(part, (x0+x1)/2., (y0+y1)/2., z0, z1, 1.0, 0.0) | |
3475 | return part | |
3476 | ||
3477 | def function(Z_of_XY): | |
3478 | part = '(Z <= '+Z_of_XY+')' | |
3479 | return part | |
3480 | ||
3481 | def functions(upper_Z_of_XY,lower_Z_of_XY): | |
3482 | part = '(Z <= '+upper_Z_of_XY+') & (Z >= '+lower_Z_of_XY+')' | |
3483 | return part | |
3484 | ||
3485 | def add(part1, part2): | |
3486 | part = "part1 | part2" | |
3487 | part = replace(part,'part1',part1) | |
3488 | part = replace(part,'part2',part2) | |
3489 | return part | |
3490 | ||
3491 | def subtract(part1, part2): | |
3492 | part = "(part1) & ~(part2)" | |
3493 | part = replace(part,'part1',part1) | |
3494 | part = replace(part,'part2',part2) | |
3495 | return part | |
3496 | ||
3497 | def intersect(part1, part2): | |
3498 | part = "(part1) & (part2)" | |
3499 | part = replace(part,'part1',part1) | |
3500 | part = replace(part,'part2',part2) | |
3501 | return part | |
3502 | ||
3503 | def move(part,dx,dy): | |
3504 | part = replace(part,'X','(X-'+str(dx)+')') | |
3505 | part = replace(part,'Y','(Y-'+str(dy)+')') | |
3506 | return part | |
3507 | ||
3508 | def translate(part,dx,dy,dz): | |
3509 | part = replace(part,'X','(X-'+str(dx)+')') | |
3510 | part = replace(part,'Y','(Y-'+str(dy)+')') | |
3511 | part = replace(part,'Z','(Z-'+str(dz)+')') | |
3512 | return part | |
3513 | ||
3514 | def rotate(part, angle): | |
3515 | angle = angle*pi/180 | |
3516 | part = replace(part,'X','(cos(angle)*X+sin(angle)*y)') | |
3517 | part = replace(part,'Y','(-sin(angle)*X+cos(angle)*y)') | |
3518 | part = replace(part,'y','Y') | |
3519 | part = replace(part,'angle',str(angle)) | |
3520 | return part | |
3521 | ||
3522 | def rotate_x(part, angle): | |
3523 | angle = angle*pi/180 | |
3524 | part = replace(part,'Y','(cos(angle)*Y+sin(angle)*z)') | |
3525 | part = replace(part,'Z','(-sin(angle)*Y+cos(angle)*z)') | |
3526 | part = replace(part,'z','Z') | |
3527 | part = replace(part,'angle',str(angle)) | |
3528 | return part | |
3529 | ||
3530 | def rotate_y(part, angle): | |
3531 | angle = angle*pi/180 | |
3532 | part = replace(part,'X','(cos(angle)*X+sin(angle)*z)') | |
3533 | part = replace(part,'Z','(-sin(angle)*X+cos(angle)*z)') | |
3534 | part = replace(part,'z','Z') | |
3535 | part = replace(part,'angle',str(angle)) | |
3536 | return part | |
3537 | ||
3538 | def rotate_z(part, angle): | |
3539 | angle = angle*pi/180 | |
3540 | part = replace(part,'X','(cos(angle)*X+sin(angle)*y)') | |
3541 | part = replace(part,'Y','(-sin(angle)*X+cos(angle)*y)') | |
3542 | part = replace(part,'y','Y') | |
3543 | part = replace(part,'angle',str(angle)) | |
3544 | return part | |
3545 | ||
3546 | def rotate_90(part): | |
3547 | part = reflect_xy(part) | |
3548 | part = reflect_y(part) | |
3549 | return part | |
3550 | ||
3551 | def rotate_180(part): | |
3552 | part = reflect_xy(part) | |
3553 | part = reflect_y(part) | |
3554 | part = reflect_xy(part) | |
3555 | part = reflect_y(part) | |
3556 | return part | |
3557 | ||
3558 | def rotate_270(part): | |
3559 | part = reflect_xy(part) | |
3560 | part = reflect_y(part) | |
3561 | part = reflect_xy(part) | |
3562 | part = reflect_y(part) | |
3563 | part = reflect_xy(part) | |
3564 | part = reflect_y(part) | |
3565 | return part | |
3566 | ||
3567 | def reflect_x(part): | |
3568 | part = replace(part,'X','(-X)') | |
3569 | return part | |
3570 | ||
3571 | def reflect_y(part): | |
3572 | part = replace(part,'Y','(-Y)') | |
3573 | return part | |
3574 | ||
3575 | def reflect_z(part): | |
3576 | part = replace(part,'Z','(-Z)') | |
3577 | return part | |
3578 | ||
3579 | def reflect_xy(part): | |
3580 | part = replace(part,'X','temp') | |
3581 | part = replace(part,'Y','X') | |
3582 | part = replace(part,'temp','Y') | |
3583 | return part | |
3584 | ||
3585 | def reflect_xz(part): | |
3586 | part = replace(part,'X','temp') | |
3587 | part = replace(part,'Z','X') | |
3588 | part = replace(part,'temp','Z') | |
3589 | return part | |
3590 | ||
3591 | def reflect_yz(part): | |
3592 | part = replace(part,'Y','temp') | |
3593 | part = replace(part,'Z','Y') | |
3594 | part = replace(part,'temp','Z') | |
3595 | return part | |
3596 | ||
3597 | def scale_x(part, x0, sx): | |
3598 | part = replace(part,'X','(x0 + (X-x0)/sx)') | |
3599 | part = replace(part,'x0',str(x0)) | |
3600 | part = replace(part,'sx',str(sx)) | |
3601 | return part | |
3602 | ||
3603 | def scale_y(part, y0, sy): | |
3604 | part = replace(part,'Y','(y0 + (Y-y0)/sy)') | |
3605 | part = replace(part,'y0',str(y0)) | |
3606 | part = replace(part,'sy',str(sy)) | |
3607 | return part | |
3608 | ||
3609 | def scale_z(part, z0, sz): | |
3610 | part = replace(part,'Z','(z0 + (Z-z0)/sz)') | |
3611 | part = replace(part,'z0',str(z0)) | |
3612 | part = replace(part,'sz',str(sz)) | |
3613 | return part | |
3614 | ||
3615 | def scale_xy(part, x0, y0, sxy): | |
3616 | part = replace(part,'X','(x0 + (X-x0)/sxy)') | |
3617 | part = replace(part,'Y','(y0 + (Y-y0)/sxy)') | |
3618 | part = replace(part,'x0',str(x0)) | |
3619 | part = replace(part,'y0',str(y0)) | |
3620 | part = replace(part,'sxy',str(sxy)) | |
3621 | return part | |
3622 | ||
3623 | def scale_xyz(part, x0, y0, z0, sxyz): | |
3624 | part = replace(part,'X','(x0 + (X-x0)/sxyz)') | |
3625 | part = replace(part,'Y','(y0 + (Y-y0)/sxyz)') | |
3626 | part = replace(part,'Z','(z0 + (Z-z0)/sxyz)') | |
3627 | part = replace(part,'x0',str(x0)) | |
3628 | part = replace(part,'y0',str(y0)) | |
3629 | part = replace(part,'z0',str(z0)) | |
3630 | part = replace(part,'sxyz',str(sxyz)) | |
3631 | return part | |
3632 | ||
3633 | def coscale_x_y(part, x0, y0, y1, angle0, angle1, amplitude, offset): | |
3634 | phase0 = pi*angle0/180. | |
3635 | phase1 = pi*angle1/180. | |
3636 | part = replace(part,'X','(x0 + (X-x0)/(offset + amplitude*cos(phase0 + (phase1-phase0)*(Y-y0)/(y1-y0))))') | |
3637 | part = replace(part,'x0',str(x0)) | |
3638 | part = replace(part,'y0',str(y0)) | |
3639 | part = replace(part,'y1',str(y1)) | |
3640 | part = replace(part,'phase0',str(phase0)) | |
3641 | part = replace(part,'phase1',str(phase1)) | |
3642 | part = replace(part,'amplitude',str(amplitude)) | |
3643 | part = replace(part,'offset',str(offset)) | |
3644 | return part | |
3645 | ||
3646 | def coscale_x_z(part, x0, z0, z1, angle0, angle1, amplitude, offset): | |
3647 | phase0 = pi*angle0/180. | |
3648 | phase1 = pi*angle1/180. | |
3649 | part = replace(part,'X','(x0 + (X-x0)/(offset + amplitude*cos(phase0 + (phase1-phase0)*(Z-z0)/(z1-z0))))') | |
3650 | part = replace(part,'x0',str(x0)) | |
3651 | part = replace(part,'z0',str(z0)) | |
3652 | part = replace(part,'z1',str(z1)) | |
3653 | part = replace(part,'phase0',str(phase0)) | |
3654 | part = replace(part,'phase1',str(phase1)) | |
3655 | part = replace(part,'amplitude',str(amplitude)) | |
3656 | part = replace(part,'offset',str(offset)) | |
3657 | return part | |
3658 | ||
3659 | def coscale_xy_z(part, x0, y0, z0, z1, angle0, angle1, amplitude, offset): | |
3660 | phase0 = pi*angle0/180. | |
3661 | phase1 = pi*angle1/180. | |
3662 | part = replace(part,'X','(x0 + (X-x0)/(offset + amplitude*cos(phase0 + (phase1-phase0)*(Z-z0)/(z1-z0))))') | |
3663 | part = replace(part,'Y','(y0 + (Y-y0)/(offset + amplitude*cos(phase0 + (phase1-phase0)*(Z-z0)/(z1-z0))))') | |
3664 | part = replace(part,'x0',str(x0)) | |
3665 | part = replace(part,'y0',str(y0)) | |
3666 | part = replace(part,'z0',str(z0)) | |
3667 | part = replace(part,'z1',str(z1)) | |
3668 | part = replace(part,'phase0',str(phase0)) | |
3669 | part = replace(part,'phase1',str(phase1)) | |
3670 | part = replace(part,'amplitude',str(amplitude)) | |
3671 | part = replace(part,'offset',str(offset)) | |
3672 | return part | |
3673 | ||
3674 | def taper_x_y(part, x0, y0, y1, s0, s1): | |
3675 | part = replace(part,'X','(x0 + (X-x0)*(y1-y0)/(s1*(Y-y0) + s0*(y1-Y)))') | |
3676 | part = replace(part,'x0',str(x0)) | |
3677 | part = replace(part,'y0',str(y0)) | |
3678 | part = replace(part,'y1',str(y1)) | |
3679 | part = replace(part,'s0',str(s0)) | |
3680 | part = replace(part,'s1',str(s1)) | |
3681 | return part | |
3682 | ||
3683 | def taper_x_z(part, x0, z0, z1, s0, s1): | |
3684 | part = replace(part,'X','(x0 + (X-x0)*(z1-z0)/(s1*(Z-z0) + s0*(z1-Z)))') | |
3685 | part = replace(part,'x0',str(x0)) | |
3686 | part = replace(part,'z0',str(z0)) | |
3687 | part = replace(part,'z1',str(z1)) | |
3688 | part = replace(part,'s0',str(s0)) | |
3689 | part = replace(part,'s1',str(s1)) | |
3690 | return part | |
3691 | ||
3692 | def taper_xy_z(part, x0, y0, z0, z1, s0, s1): | |
3693 | part = replace(part,'X','(x0 + (X-x0)*(z1-z0)/(s1*(Z-z0) + s0*(z1-Z)))') | |
3694 | part = replace(part,'Y','(y0 + (Y-y0)*(z1-z0)/(s1*(Z-z0) + s0*(z1-Z)))') | |
3695 | part = replace(part,'x0',str(x0)) | |
3696 | part = replace(part,'y0',str(y0)) | |
3697 | part = replace(part,'z0',str(z0)) | |
3698 | part = replace(part,'z1',str(z1)) | |
3699 | part = replace(part,'s0',str(s0)) | |
3700 | part = replace(part,'s1',str(s1)) | |
3701 | return part | |
3702 | ||
3703 | def shear_x_y(part, y0, y1, dx0, dx1): | |
3704 | part = replace(part,'X','(X - dx0 - (dx1-dx0)*(Y-y0)/(y1-y0))') | |
3705 | part = replace(part,'y0',str(y0)) | |
3706 | part = replace(part,'y1',str(y1)) | |
3707 | part = replace(part,'dx0',str(dx0)) | |
3708 | part = replace(part,'dx1',str(dx1)) | |
3709 | return part | |
3710 | ||
3711 | def shear_x_z(part, z0, z1, dx0, dx1): | |
3712 | part = replace(part,'X','(X - dx0 - (dx1-dx0)*(Z-z0)/(z1-z0))') | |
3713 | part = replace(part,'z0',str(z0)) | |
3714 | part = replace(part,'z1',str(z1)) | |
3715 | part = replace(part,'dx0',str(dx0)) | |
3716 | part = replace(part,'dx1',str(dx1)) | |
3717 | return part | |
3718 | ||
3719 | def coshear_x_z(part, z0, z1, angle0, angle1, amplitude, offset): | |
3720 | phase0 = pi*angle0/180. | |
3721 | phase1 = pi*angle1/180. | |
3722 | part = replace(part,'X','(X - offset - amplitude*cos(phase0 + (phase1-phase0)*(Z-z0)/(z1-z0)))') | |
3723 | part = replace(part,'z0',str(z0)) | |
3724 | part = replace(part,'z1',str(z1)) | |
3725 | part = replace(part,'phase0',str(phase0)) | |
3726 | part = replace(part,'phase1',str(phase1)) | |
3727 | part = replace(part,'amplitude',str(amplitude)) | |
3728 | part = replace(part,'offset',str(offset)) | |
3729 | return part | |
3730 | ||
3731 | # | |
3732 | # define part | |
3733 | # | |
3734 | ||
3735 | d = .5 | |
3736 | teapot = cylinder(0,0,-d,d,d) | |
3737 | teapot = coscale_xy_z(teapot,0,0,-d,d,-90,90,.5,.75) | |
3738 | ||
3739 | handle = torus(0,0,0,3.5*d/5.,d/10.) | |
3740 | handle = reflect_xz(handle) | |
3741 | handle = reflect_xy(handle) | |
3742 | handle = scale_x(handle,0,.75) | |
3743 | handle = scale_y(handle,0,3) | |
3744 | handle = translate(handle,-6*d/5.,0,0) | |
3745 | teapot = add(teapot,handle) | |
3746 | ||
3747 | spout = torus(2.1*d,-.2*d,0,1.1*d,.2*d) | |
3748 | spout = reflect_yz(spout) | |
3749 | spout = intersect(spout,cube(-3*d,1.8*d,-3*d,3*d,0,3*d)) | |
3750 | teapot = add(teapot,spout) | |
3751 | ||
3752 | interior = cylinder(0,0,.1-d,.1+d,d-.1) | |
3753 | interior = coscale_xy_z(interior,0,0,-d,d,-90,90,.5,.75) | |
3754 | teapot = subtract(teapot,interior) | |
3755 | ||
3756 | spout_interior = torus(2.1*d,-.2*d,0,1.1*d,.15*d) | |
3757 | spout_interior = reflect_yz(spout_interior) | |
3758 | spout_interior = intersect(spout_interior,cube(-3*d,1.8*d,-3*d,3*d,0,3*d)) | |
3759 | teapot = subtract(teapot,spout_interior) | |
3760 | ||
3761 | part = teapot | |
3762 | ||
3763 | part = subtract(part,cube(0,3*d,-3*d,0,-3*d,3*d)) | |
3764 | ||
3765 | # | |
3766 | # define limits and parameters | |
3767 | # | |
3768 | ||
3769 | width = 2.5 | |
3770 | x0 = 0 | |
3771 | y0 = 0 | |
3772 | z0 = 0 | |
3773 | cad.xmin = x0-width/2. # min x to render | |
3774 | cad.xmax = x0+width/2. # max x to render | |
3775 | cad.ymin = y0-width/2. # min y to render | |
3776 | cad.ymax = y0+width/2. # max y to render | |
3777 | #cad.zmin = z0-width/4. # min z to render | |
3778 | #cad.zmax = z0+width/4. # max x to render | |
3779 | cad.zmin = z0-width/4. # min z to render | |
3780 | cad.zmax = z0+width/4. # max x to render | |
3781 | cad.rx = 30 # x view rotation (degrees) | |
3782 | cad.rz = 20 # z view rotation (degrees) | |
3783 | dpi = 100 # rendering resolution | |
3784 | cad.nx = int(dpi*(cad.xmax-cad.xmin)) # x points to render | |
3785 | cad.ny = int(dpi*(cad.ymax-cad.ymin)) # y points to render | |
3786 | cad.nz = int(dpi*(cad.zmax-cad.zmin)) # z points to render | |
3787 | cad.inches_per_unit = 1.0 # use inch units | |
3788 | ||
3789 | # | |
3790 | # assign part to cad.function | |
3791 | # | |
3792 | ||
3793 | cad.function = part | |
3794 | ||
3795 | """ | |
3796 | ||
3797 | # | |
3798 | # check config file for window parameters | |
3799 | # | |
3800 | ||
3801 | cad_path = os.path.dirname(sys.argv[0]) | |
3802 | if (sys.argv[0] == "cad.py"): | |
3803 | cfg_path = "cad.cfg" | |
3804 | else: | |
3805 | cfg_path = os.path.dirname(sys.argv[0])+"/cad.cfg" | |
3806 | try: | |
3807 | config_file = open(cfg_path, 'r') | |
3808 | string_msg.set("found "+cfg_path) | |
3809 | while 1: | |
3810 | line = config_file.readline() | |
3811 | if (find(line,"window size:") == 0): | |
3812 | string_window_size.set(int(line[12:])) | |
3813 | elif (find(line,"editor width:") == 0): | |
3814 | string_editor_width.set(int(line[13:])) | |
3815 | elif (find(line,"editor height:") == 0): | |
3816 | string_editor_height.set(int(line[14:])) | |
3817 | elif (line == ""): | |
3818 | break | |
3819 | config_file.close() | |
3820 | resize_editor(0) | |
3821 | except: | |
3822 | string_msg.set(cfg_path+" not found") | |
3823 | ||
3824 | # | |
3825 | # read input file if on command line, otherwise use template | |
3826 | # | |
3827 | ||
3828 | if len(sys.argv) == 2: | |
3829 | filename = sys.argv[1] | |
3830 | string_input_file.set(filename) | |
3831 | if (find(filename,'.cad') != -1): | |
3832 | cad_load(0) | |
3833 | elif ((find(filename,'.jpg') != -1) | (find(filename,'.JPG') != -1) | | |
3834 | (find(filename,'.png') != -1) | (find(filename,'.PNG') != -1) | | |
3835 | (find(filename,'.gif') != -1) | (find(filename,'.GIF') != -1)): | |
3836 | widget_cad_text.delete("1.0",END) | |
3837 | image_load(0) | |
3838 | else: | |
3839 | string_msg.set("unsupported input file format") | |
3840 | root.update() | |
3841 | else: | |
3842 | widget_cad_text.insert("1.0",cad_template) | |
3843 | ||
3844 | # | |
3845 | # start GUI | |
3846 | # | |
3847 | ||
3848 | root.mainloop() |