-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrastercarve_ru.py
496 lines (461 loc) · 18.6 KB
/
rastercarve_ru.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
from tkinter import *
from tkinter import filedialog, ttk
from tkinter.messagebox import askquestion, showerror
from PIL.ImageTk import PhotoImage
from PIL import Image, ImageTk, ImageFilter
from math import cos, pi, tan, sqrt, sin
class App:
def __init__(self):
self.filename =""
self.lpx = 800.0
self.lpy = 500.0
self.lpz = 19.3
self.lx = 800.0
self.ly = 500.0
self.toolname = "UGOL90"
self.work_speed = 8000
self.v_bit_angle = 90
self.depth = 2.0
self.prgname = "prog"
self.maxlines = 100000
self.stepover = 100
self.strategy = 'angle45'
self.lin_resol = 0.5
self.root = Tk()
root=self.root
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Открыть", command=self.open_file)
filemenu.add_separator()
filemenu.add_command(label="Выход", command=self.quit)
menubar.add_cascade(label="Изображение", menu=filemenu)
progmenu = Menu(menubar, tearoff=0)
progmenu.add_command(label="Создать bpp файл", command=self.create_files)
menubar.add_cascade(label="Программа", menu=progmenu)
self.use_blur = BooleanVar()
self.param_panel=self.param_panel()
self.param_panel.pack(side=LEFT)
self.canvas = Canvas(root, width = 1200, height = 800)
self.canvas.pack(fill=BOTH)
root.config(menu=menubar)
root.mainloop()
def quit(self):
sys.exit()
self.root.quit()
def open_file(self):
self.filename = filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
if self.filename != "":
self.root.title(self.filename)
load = Image.open(self.filename)
img = ImageTk.PhotoImage(load)
self.canvas.create_image(2, 2, anchor=NW, image = img)
self.canvas.image = img
def param_panel(self):
filewin = Frame(self.root)
lab = Label(filewin,text="Данные заготовки :")
lab.pack()
rowx = Frame(filewin)
labx = Label(rowx,width=10, text="LPX: ")
entx= Entry(rowx)
entx.insert(0, str(self.lpx))
self.entx = entx
rowx.pack(side=TOP, fill=X)
labx.pack(side=LEFT)
entx.pack(side=RIGHT, expand=YES, fill=X)
rowy = Frame(filewin)
laby = Label(rowy,width=10, text="LPY: ")
enty= Entry(rowy)
enty.insert(0,str(self.lpy))
self.enty = enty
rowy.pack(side=TOP, fill=X)
laby.pack(side=LEFT)
enty.pack(side=RIGHT, expand=YES, fill=X)
rowz = Frame(filewin)
labz = Label(rowz,width=10, text="LPZ: ")
entz= Entry(rowz)
entz.insert(0,str(self.lpz))
self.entz = entz
rowz.pack(side=TOP, fill=X)
labz.pack(side=LEFT)
entz.pack(side=RIGHT, expand=YES, fill=X)
lab1 = Label(filewin,text = "Данные гравировки :")
lab1.pack()
rowlx = Frame(filewin)
lablx = Label(rowlx,width=10, text="длина: ")
entlx= Entry(rowlx)
entlx.insert(0, str(self.lx))
self.entlx = entlx
rowlx.pack(side=TOP, fill=X)
lablx.pack(side=LEFT)
entlx.pack(side=RIGHT, expand=YES, fill=X)
rowly = Frame(filewin)
lably = Label(rowly,width=10, text="высота: ")
ently= Entry(rowly)
ently.insert(0, str(self.ly))
self.ently = ently
rowly.pack(side=TOP, fill=X)
lably.pack(side=LEFT)
ently.pack(side=RIGHT, expand=YES, fill=X)
lab2 =Label(filewin,text = "Данные обработки :")
lab2.pack()
toolname_row = Frame(filewin)
toolname_lab = Label(toolname_row,width=10, text="код инструмента : ")
toolname_ent= Entry(toolname_row)
toolname_ent.insert(0, self.toolname)
self.toolname_ent = toolname_ent
toolname_row.pack(side=TOP, fill=X)
toolname_lab.pack(side=LEFT, expand=YES, fill=X)
toolname_ent.pack(side=RIGHT, expand=YES, fill=X)
work_speed_row = Frame(filewin)
work_speed_lab = Label(work_speed_row,width=10, text="подача : ")
work_speed_ent= Entry(work_speed_row)
work_speed_ent.insert(0, str(self.work_speed))
self.work_speed_ent = work_speed_ent
work_speed_row.pack(side=TOP, fill=X)
work_speed_lab.pack(side=LEFT, expand=YES, fill=X)
work_speed_ent.pack(side=RIGHT, expand=YES, fill=X)
v_bit_angle_row = Frame(filewin)
v_bit_angle_lab = Label(v_bit_angle_row,width=10, text="V-угол фрезы : ")
v_bit_angle_ent= Entry(v_bit_angle_row)
v_bit_angle_ent.insert(0, str(self.v_bit_angle))
self.v_bit_angle_ent = v_bit_angle_ent
v_bit_angle_row.pack(side=TOP, fill=X)
v_bit_angle_lab.pack(side=LEFT, expand=YES, fill=X)
v_bit_angle_ent.pack(side=RIGHT, expand=YES, fill=X)
lin_resol_row = Frame(filewin)
lin_resol_lab = Label(lin_resol_row,width=20, text="шаг вдоль линии : ")
lin_resol_ent= Entry(lin_resol_row)
lin_resol_ent.insert(0, str(self.lin_resol))
self.lin_resol_ent = lin_resol_ent
lin_resol_row.pack(side=TOP, fill=X)
lin_resol_lab.pack(side=LEFT, expand=YES, fill=X)
lin_resol_ent.pack(side=RIGHT, expand=YES, fill=X)
use_blur_row = Frame(filewin)
use_blur_cb = Checkbutton(use_blur_row, text = "использовать фильтр Гаусса", variable= self.use_blur)
use_blur_row.pack(side=TOP, fill=X)
use_blur_cb.pack(side=LEFT, expand=YES, fill=X)
rowd = Frame(filewin)
labd = Label(rowd,width=5, text="глубина : ")
entd= Entry(rowd)
entd.insert(0, str(self.depth))
self.entd = entd
rowd.pack(side=TOP, fill=X)
labd.pack(side=LEFT, expand=YES, fill=X)
entd.pack(side=RIGHT, expand=YES, fill=X)
rowst = Frame(filewin)
labst = Label(rowst,width=10, text=" увеличение шага,%: ")
entst= Entry(rowst)
entst.insert(0, str(self.stepover))
self.entst = entst
rowst.pack(side=TOP, fill=X)
labst.pack(side=LEFT,fill=X, expand=YES)
entst.pack(side=RIGHT, expand=YES, fill=X)
rowstrat = Frame(filewin)
labstrat = Label(rowstrat,width=10, text="тип линии : ")
combostrat = ttk.Combobox(rowstrat,values=['angle45','circle','spiral'])
combostrat.current(0)
self.combostrat = combostrat
rowstrat.pack(side=TOP, fill=X)
labstrat.pack(side=LEFT,fill=X, expand=YES)
combostrat.pack(side=RIGHT, expand=YES, fill=X)
labp = Label(filewin, text = "Данные программы :")
labp.pack()
rown = Frame(filewin)
labn = Label(rown,width=10, text="имя : ")
entn= Entry(rown)
entn.insert(0, self.prgname)
self.entn = entn
rown.pack(side=TOP, fill=X)
labn.pack(side=LEFT)
entn.pack(side=RIGHT, expand=YES, fill=X)
rowml = Frame(filewin)
labml = Label(rowml,width=12, text=" max число строк: ")
entml= Entry(rowml)
entml.insert(0, self.maxlines)
self.entml = entml
rowml.pack(side=TOP, fill=X)
labml.pack(side=LEFT, expand=YES, fill=X )
entml.pack(side=RIGHT, expand=YES, fill=X)
button = Button(filewin, text="Применить", command = self.set_param)
button.pack()
return filewin
def set_param(self):
self.lpx = float(self.entx.get())
self.lpy = float(self.enty.get())
self.lpz = float(self.entz.get())
self.lx = float(self.entlx.get())
self.ly = float(self.ently.get())
self.depth = float(self.entd.get())
self.prgname = self.entn.get()
self.maxlines = int(self.entml.get())
self.stepover = float(self.entst.get())
self.toolname = self.toolname_ent.get()
self.work_speed = float(self.work_speed_ent.get())
self.v_bit_angle = float(self.v_bit_angle_ent.get())
self.lin_resol = float(self.lin_resol_ent.get())
self.strategy = self.combostrat.get()
def header(self):
return "[HEADER]\nTYPE=BPP\nVER=150\n\n[DESCRIPTION]\n\n"
def variables(self):
strin = """[VARIABLES]
PAN=LPX|{0:3.3f}||4|
PAN=LPY|{1:3.3f}||4|
PAN=LPZ|{2:3.3f}||4|
PAN=ORLST|"5"||3|
PAN=SIMMETRY|1||1|
PAN=TLCHK|0||1|
PAN=TOOLING|""||3|
PAN=CUSTSTR|$B$KBsExportToNcRvA.XncExtraPanelData$V""||3|
PAN=FCN|1.000000||2|
PAN=XCUT|0||4|
PAN=YCUT|0||4|
PAN=JIGTH|0||4|
PAN=CKOP|0||1|
PAN=UNIQUE|0||1|
PAN=MATERIAL|"wood"||3|
PAN=PUTLST|""||3|
PAN=OPPWKRS|0||1|
PAN=UNICLAMP|0||1|
PAN=CHKCOLL|0||1|
PAN=WTPIANI|0||1|
PAN=COLLTOOL|0||1|
PAN=CALCEDTH|0||1|
PAN=ENABLELABEL|0||1|
PAN=LOCKWASTE|0||1|
PAN=LOADEDGEOPT|0||1|
PAN=ITLTYPE|0||1|
PAN=RUNPAV|0||1|
PAN=FLIPEND|0||1|
PAN=ENABLEMACHLINKS|0||1|
PAN=ENABLEPURSUITS|0||1|
PAN=ENABLEFASTVERTBORINGS|0||1|
PAN=FASTVERTBORINGSVALUE|0||4|
\n""".format(self.lpx, self.lpy, self.lpz)
return strin
def programstart2(self):
dx = (self.lpx - self.lx)/2
dy = (self.lpy - self.ly)/2
strin = """[PROGRAM]
@ SHIFT, "", "", 374974684, "", 0 : {0:3.3f}, {1:3.3f}
@ ROUT, "", "", 95893420, "", 0 : "P1001", 0, "1", 0, 0, "", 1, 1.6, -1, 0, 0, 32, 32, 50, 0, 45, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, -1, 0, 0, 0, 0, 8000, "UGOL90", 100, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", 0, 0, 0, 0, 0, 0, 0, 0, 0, "", 5, 0, 20, 80, 60, 0, "", "", "ROUT", 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.1, 0, 0, 0, 99, 0\n""".format(dx, dy)
return strin
def programstart(self):
dx = (self.lpx - self.lx)/2
dy = (self.lpy - self.ly)/2
strin = '[PROGRAM]\n @ SHIFT, "", "", 374974684, "", 0 : {0:3.3f}, {1:3.3f}\n'.format(dx, dy)
strin = strin + ' @ ROUT, "", "", 95893420, "", 0 : "P1001", 0, "1", 0, 0, "", 1, 1.6, -1, 0, 0, 32, 32, 50, 0, 45, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, -1, 0, 0, 0, 0,'
strin = strin + '{0:5.0f},"{1:s}", 100, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", 0, 0, 0, 0, 0, 0, 0, 0, 0, "", 5, 0, 20, 80, 60,'.format(self.work_speed,self.toolname)
strin = strin + ', 0, "", "", "ROUT", 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.1, 0, 0, 0, 99, 0\n'
return strin
def startpoint(self,x, y):
return " @ START_POINT, "", "", 95893036, "", 0 : {0:3.3f}, {1:3.3f}, 0\n".format(x,y)
def prog_end(self):
return " @ ENDPATH, "", "", 95894508, "", 0 :\n\n[VBSCRIPT]\n\n[MACRODATA]\n\n[TDCODES]\n\n[PCF]\n\n[TOOLING]\n\n[SUBPROGS]\n\n"
def prog_line(self,x,y,z):
return " @ LINE_EP, "", "", 95894956, "", 0 : {0:3.3f}, {1:3.3f}, 0, {2:3.3f}, 0, 0, 0, 0, 0\n".format(x,y,z)
def create_files(self):
if self.strategy=='angle45':
self.angle45_strategy()
if self.strategy=='circle':
self.circle_strategy()
if self.strategy=='spiral':
self.spiral_strategy()
def spiral_strategy(self):
if self.filename=="":
showerror('Error!',"Open a image")
return
prg = self.header()
prg = prg + self.variables()
prg = prg + self.programstart()
im=Image.open(self.filename)
if self.use_blur.get():
im=im.filter(ImageFilter.GaussianBlur(radius=2))
depth = self.depth
step = 2 * depth *tan(self.v_bit_angle * pi/360) * self.stepover / 100
rmax = sqrt(self.lx**2 +self.ly**2) / 2
rmax = rmax - step
ri = step/2
alfa = 0
zo =0.0
zi = 0.0
start_flag = True
ni = 0
fi = 0
out_f = self.prgname + ".bpp"
while ri <= rmax :
zo = zi
xi = self.lx/2 + ri * cos(alfa)
yi = self.ly/2 + ri * sin(alfa)
if ((xi < self.lx) & (xi >0) & (yi < self.ly) & (yi > 0)):
if start_flag:
start_flag = False
zo = 0.0
prg = prg + self.startpoint(xi, yi)
i = int (round(xi * im.size[0] / self.lx))
j = int (round(yi * im.size[1] / self.ly))
if i<0:
i=0
if i>im.size[0]-1:
i=im.size[0]-1
if j<0:
j=0
if j>im.size[1]-1:
j=im.size[1]-1
gray = 0.299 * im.getpixel((i,j))[0] + 0.587 * im.getpixel((i,j))[1] + 0.114 * im.getpixel((i,j))[2]
gray = 256 - gray
zi = depth * gray /255
dz = zi - zo
prg = prg + self.prog_line(xi, yi, dz)
ni = ni +1
else:
start_flag = True
alfa_step = pi * self.lin_resol / ri
alfa += alfa_step
ri = step/2 + step * alfa / (2 * pi)
if ni > self.maxlines :
ni = 0
prg = prg + self.prog_end()
fo = open(out_f, "w")
fo.write(prg)
fo.close()
fi = fi + 1
out_f = self.prgname + str(fi) +".bpp"
prg = self.header()
prg = prg + self.variables()
prg = prg + self.programstart()
start_flag = True
prg = prg + self.prog_end()
fo = open(out_f, "w")
fo.write(prg)
fo.close()
return prg
def circle_strategy(self):
if self.filename=="":
showerror('Error!',"Open a image")
return
prg = self.header()
prg = prg + self.variables()
prg = prg + self.programstart()
im=Image.open(self.filename)
if self.use_blur.get():
im=im.filter(ImageFilter.GaussianBlur(radius=2))
depth = self.depth
step = 2 * depth *tan(self.v_bit_angle * pi/360) * self.stepover / 100
n = int(sqrt(self.lx**2 +self.ly**2)/(2*step))
ni = 0
fi = 0
out_f = self.prgname + ".bpp"
for ki in range(0,n):
ri = step * (ki + 0.5)
alfa_step = pi * self.lin_resol / ri
alfa = 0
zi = 0.0
start_flag = True
while alfa <= (2 * pi + alfa_step) :
zo = zi
xi = self.lx/2 + ri * cos(alfa)
yi = self.ly/2 + ri * sin(alfa)
if ((xi < self.lx) & (xi >0) & (yi < self.ly) & (yi > 0)):
if start_flag:
start_flag = False
zo = 0.0
prg = prg + self.startpoint(xi, yi)
i = int (round(xi * im.size[0] / self.lx))
j = int (round(yi * im.size[1] / self.ly))
if i<0:
i=0
if i>im.size[0]-1:
i=im.size[0]-1
if j<0:
j=0
if j>im.size[1]-1:
j=im.size[1]-1
gray = 0.299 * im.getpixel((i,j))[0] + 0.587 * im.getpixel((i,j))[1] + 0.114 * im.getpixel((i,j))[2]
gray = 256 - gray
zi = depth * gray /255
dz = zi - zo
prg = prg + self.prog_line(xi, yi, dz)
ni = ni +1
else:
start_flag = True
alfa += alfa_step
if ni > self.maxlines :
ni = 0
prg = prg + self.prog_end()
fo = open(out_f, "w")
fo.write(prg)
fo.close()
fi = fi + 1
out_f = self.prgname + str(fi) +".bpp"
prg = self.header()
prg = prg + self.variables()
prg = prg + self.programstart()
start_flag = True
prg = prg + self.prog_end()
fo = open(out_f, "w")
fo.write(prg)
fo.close()
return prg
def angle45_strategy(self):
if self.filename=="":
showerror('Error!',"Open a image")
return
prg = self.header()
prg = prg + self.variables()
prg = prg + self.programstart()
im=Image.open(self.filename)
if self.use_blur.get():
im=im.filter(ImageFilter.GaussianBlur(radius=2))
depth = self.depth
step = 2 * depth *tan(self.v_bit_angle * pi/360) * self.stepover / (100 * cos(pi/4) )
step_xy = self.lin_resol * cos(pi/4)
n = int((self.lx + self.ly)/step)
ni = 0
fi = 0
out_f = self.prgname + ".bpp"
for ki in range(1,n):
xi = step * ki
yi = 0.0
zi = 0.0
if xi > self.lx :
yi = xi - self.lx
xi = self.lx
prg = prg + self.startpoint(xi, yi)
while ((xi > 0.0) & (yi < self.ly)):
zo = zi
i = int (round(xi * im.size[0] / self.lx))
j = int (round(yi * im.size[1] / self.ly))
if i<0:
i=0
if i>im.size[0]-1:
i=im.size[0]-1
if j<0:
j=0
if j>im.size[1]-1:
j=im.size[1]-1
gray = 0.299 * im.getpixel((i,j))[0] + 0.587 * im.getpixel((i,j))[1] + 0.114 * im.getpixel((i,j))[2]
gray = 256 - gray
zi = depth * gray /255
dz = zi - zo
prg = prg + self.prog_line(xi, yi, dz)
ni = ni +1
xi = xi - step_xy
yi = yi + step_xy
if ni > self.maxlines :
ni = 0
prg = prg + self.prog_end()
fo = open(out_f, "w")
fo.write(prg)
fo.close()
fi = fi + 1
out_f = self.prgname + str(fi) +".bpp"
prg = self.header()
prg = prg + self.variables()
prg = prg + self.programstart()
prg = prg + self.prog_end()
fo = open(out_f, "w")
fo.write(prg)
fo.close()
return prg
if __name__ == '__main__' :
app=App()