forked from pmonta/gerber2graphtec
-
Notifications
You must be signed in to change notification settings - Fork 5
/
g2g_gui.py
executable file
·432 lines (350 loc) · 13.6 KB
/
g2g_gui.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
#!/usr/bin/env python
import Tkinter
import tkMessageBox
import tkFileDialog
import sys
import os
import string
import subprocess
from Tkinter import *
from os import path, access, R_OK, W_OK
top = Tkinter.Tk()
top.title("Gerber to Graphtec")
Gerber_name = StringVar()
Output_name = StringVar()
gerbv_path = StringVar()
pstoedit_path = StringVar()
offset_str = StringVar()
border_str = StringVar()
matrix_str = StringVar()
speed_str = StringVar()
force_str = StringVar()
cut_mode_str = StringVar()
cutter_shared_name_str = StringVar()
CONFPATH='./g2g_gui.cnf'
input_filename = ''
output_filename = ''
gerbv_filename = ''
pstoedit_filename = ''
offset_text = ''
border_text = ''
matrix_text = ''
speed_text = ''
force_text = ''
cut_mode_text = ''
cutter_shared_name_text = ''
offset = (4,0.5)
border = (1,1)
matrix = (1,0,0,1)
speed = [2,2]
force = [8,30]
cut_mode = 0
def floats(s):
return map(float,string.split(s,','))
def test_forces():
original_stdout = sys.stdout # keep a reference to STDOUT
if Output_name.get():
sys.stdout = open(Output_name.get(), 'w')
if not offset_str.get():
default_offset_str()
if not matrix_str.get():
default_matrix_str()
if not speed_str.get():
default_speed_str()
offset = floats(offset_str.get())
matrix = floats(matrix_str.get())
speed = floats(speed_str.get())
#
# main program
#
import graphtec
import pic
import optimize
# offset = (5, 1)
# matrix = (1, 0, 0, 1)
g = graphtec.graphtec()
g.start()
g.set(offset=offset, matrix=matrix)
g.set(speed=speed)
for i in range(0, 6):
for j in range(0, 5):
g.set(force=1 + j + 5 * i)
tx = 0.5 * j
ty = 0.5 * i
g.closed_path([(tx, ty), (tx + 0.3, ty), (tx + 0.3, ty + 0.3), (tx, ty + 0.3)])
g.end()
if Output_name.get():
sys.stdout = original_stdout # restore STDOUT back to its original value
tkMessageBox.showinfo("G2G_GUI Message", "File '%s' created" % (Output_name.get()))
def show_gerber():
if not os.path.exists(Gerber_name.get()):
get_input_filename()
if not os.path.exists(Gerber_name.get()):
tkMessageBox.showerror("G2G_GUI ERROR", "The path provided for the input Gerber file is invalid.")
return
head, tail = os.path.split(Gerber_name.get())
if not os.path.exists(gerbv_path.get()):
tkMessageBox.showerror("G2G_GUI ERROR", "The path provided for gerbv is invalid.")
return
if not os.path.exists(pstoedit_path.get()):
tkMessageBox.showerror("G2G_GUI ERROR", "The path provided for pstoedit is invalid.")
return
subprocess.Popen([os.path.normpath(gerbv_path.get()), os.path.normpath(Gerber_name.get())])
def main_program():
#
# convert file to pic format
#
if not os.path.exists(Gerber_name.get()):
get_input_filename()
if not os.path.exists(Gerber_name.get()):
tkMessageBox.showerror("G2G_GUI ERROR", "The path provided for the input Gerber file is invalid.")
return
head, tail = os.path.split(Gerber_name.get())
if os.name=='nt':
temp_pdf = os.path.normpath("%s\_tmp_gerber.pdf" % (head))
temp_pic = os.path.normpath("%s\_tmp_gerber.pic" % (head))
temp_bat = os.path.normpath("%s\_tmp_gerber.bat" % (head))
else:
temp_pdf = "_tmp_gerber.pdf"
temp_pic = "_tmp_gerber.pic"
if os.name=='nt':
if not os.path.exists(gerbv_path.get()):
tkMessageBox.showerror("G2G_GUI ERROR", "The path provided for gerbv is invalid.")
return
if not os.path.exists(pstoedit_path.get()):
tkMessageBox.showerror("G2G_GUI ERROR", "The path provided for pstoedit is invalid.")
return
if os.name=='nt':
os.system("echo \"%s\" --export=pdf --output=%s --border=0 \"%s\" > \"%s\"" % (os.path.normpath(gerbv_path.get()),temp_pdf,os.path.normpath(Gerber_name.get()),temp_bat))
os.system("echo \"%s\" -q -f pic \"%s\" \"%s\" >> \"%s\"" % (os.path.normpath(pstoedit_path.get()),temp_pdf,temp_pic, temp_bat))
os.system("\"%s\"" % temp_bat)
else:
os.system("%s --export=pdf --output=%s --border=0 \"%s\"" % (os.path.normpath(gerbv_path.get()),temp_pdf,os.path.normpath(Gerber_name.get())))
os.system("%s -q -f pic \"%s\" \"%s\"" % (os.path.normpath(pstoedit_path.get()),temp_pdf,temp_pic))
original_stdout = sys.stdout # keep a reference to STDOUT
if Output_name.get():
sys.stdout = open(Output_name.get(), 'w')
if not offset_str.get():
default_offset_str()
if not border_str.get():
default_border_str()
if not matrix_str.get():
default_matrix_str()
if not speed_str.get():
default_speed_str()
if not force_str.get():
default_force_str()
if not cut_mode_str.get():
default_cut_mode_str()
offset = floats(offset_str.get())
border = floats(border_str.get())
matrix = floats(matrix_str.get())
speed = floats(speed_str.get())
force = floats(force_str.get())
cut_mode = int(cut_mode_str.get())
#
# main program
#
import graphtec
import pic
import optimize
g = graphtec.graphtec()
g.start()
g.set(offset=(offset[0]+border[0]+0.5,offset[1]+border[1]+0.5), matrix=matrix)
strokes = pic.read_pic(temp_pic)
max_x,max_y = optimize.max_extent(strokes)
tx,ty = 0.5,0.5
border_path = [
(-border[0], -border[1]),
(max_x+border[0], -border[1]),
(max_x+border[0], max_y+border[1]),
(-border[0], max_y+border[1])
]
if cut_mode==0:
lines = optimize.optimize(strokes, border)
for (s,f) in zip(speed,force):
g.set(speed=s, force=f)
for x in lines:
g.line(*x)
g.closed_path(border_path)
else:
for (s,f) in zip(speed,force):
g.set(speed=s, force=f)
for s in strokes:
g.closed_path(s)
g.closed_path(border_path)
g.end()
if Output_name.get():
sys.stdout = original_stdout # restore STDOUT back to its original value
tkMessageBox.showinfo("G2G_GUI Message", "File '%s' created" % (Output_name.get()) )
def Save_Configuration():
f = open(CONFPATH,'w')
f.write(Gerber_name.get() + '\n')
f.write(Output_name.get() + '\n')
f.write(gerbv_path.get() + '\n')
f.write(pstoedit_path.get() + '\n')
f.write(offset_str.get() + '\n')
f.write(border_str.get() + '\n')
f.write(matrix_str.get() + '\n')
f.write(speed_str.get() + '\n')
f.write(force_str.get() + '\n')
f.write(cut_mode_str.get() + '\n')
f.write(cutter_shared_name_str.get() + '\n')
f.close()
def Just_Exit():
top.destroy()
def Send_to_Cutter():
if not Output_name.get():
get_output_filename()
if not Output_name.get():
return
src=os.path.normpath(Output_name.get())
if not cutter_shared_name_str.get():
tkMessageBox.showerror("G2G_GUI ERROR", "The name of the cutter (as a shared printer) was not provided.")
return
#if not os.path.exists(cutter_shared_name_str.get()):
# tkMessageBox.showerror("G2G_GUI ERROR", "The name of the cutter (as a shared printer) does not exist.")
# return
dst=os.path.normpath(cutter_shared_name_str.get())
if os.name=='nt':
os.system("copy /B \"%s\" \"%s\"" % (src, dst))
else:
os.system("cat %s > %s" % (src, dst))
def get_input_filename():
input_filename=tkFileDialog.askopenfilename(title='Select paste mask Gerber file', filetypes=[('Gerber File', '*.g*'),("All files", "*.*")] )
if input_filename:
Gerber_name.set(input_filename)
def get_output_filename():
output_filename=tkFileDialog.asksaveasfilename(title='Select output filename', filetypes=[('Output files', '*.txt'),("All files", "*.*")] )
if output_filename:
Output_name.set(output_filename)
def get_gerbv_path():
gerbv_filename=tkFileDialog.askopenfilename(title='Select gerbv program', initialfile='gerbv.exe', filetypes=[('Programs', '*.exe')] )
if gerbv_filename:
gerbv_path.set(gerbv_filename)
def get_pstoedit_path():
pstoedit_filename=tkFileDialog.askopenfilename(title='Select gerbv program', initialfile='pstoedit.exe', filetypes=[('Programs', '*.exe')] )
if pstoedit_filename:
pstoedit_path.set(pstoedit_filename)
def default_offset_str():
offset_str.set("4.0,0.5")
def default_border_str():
border_str.set("1,1")
def default_matrix_str():
matrix_str.set("1,0,0,1")
def default_speed_str():
speed_str.set("2,2")
def default_force_str():
force_str.set("8,30")
def default_cut_mode_str():
cut_mode_str.set("0")
Label(top, text="Gerber File ").grid(row=1, column=0, sticky=W)
Entry(top, bd =1, width=60, textvariable=Gerber_name).grid(row=1, column=1)
Tkinter.Button(top, width=9, text = "Browse", command = get_input_filename).grid(row=1, column=2)
Label(top, text="Output File ").grid(row=2, column=0, sticky=W)
Entry(top, bd =1, width=60, textvariable=Output_name).grid(row=2, column=1)
Tkinter.Button(top, width=9, text = "Browse", command = get_output_filename).grid(row=2, column=2)
if os.name=='nt':
Label(top, text="gerbv path ").grid(row=3, column=0, sticky=W)
Entry(top, bd =1, width=60, textvariable=gerbv_path).grid(row=3, column=1)
Tkinter.Button(top, width=9, text = "Browse", command = get_gerbv_path).grid(row=3, column=2)
Label(top, text="pstoedit path ").grid(row=4, column=0, sticky=W)
Entry(top, bd =1, width=60, textvariable=pstoedit_path).grid(row=4, column=1)
Tkinter.Button(top, width=9, text = "Browse", command = get_pstoedit_path).grid(row=4, column=2)
Label(top, text="Offset ").grid(row=5, column=0, sticky=W)
Entry(top, bd =1, width=60, textvariable=offset_str).grid(row=5, column=1)
Tkinter.Button(top, width=9, text = "Default", command = default_offset_str).grid(row=5, column=2)
Label(top, text="Border ").grid(row=6, column=0, sticky=W)
Entry(top, bd =1, width=60, textvariable=border_str).grid(row=6, column=1)
Tkinter.Button(top, width=9, text = "Default", command = default_border_str).grid(row=6, column=2)
Label(top, text="Matrix ").grid(row=7, column=0, sticky=W)
Entry(top, bd =1, width=60, textvariable=matrix_str).grid(row=7, column=1)
Tkinter.Button(top, width=9, text = "Default", command = default_matrix_str).grid(row=7, column=2)
Label(top, text="Speed ").grid(row=8, column=0, sticky=W)
Entry(top, bd =1, width=60, textvariable=speed_str).grid(row=8, column=1)
Tkinter.Button(top, width=9, text = "Default", command = default_speed_str).grid(row=8, column=2)
Label(top, text="Force ").grid(row=9, column=0, sticky=W)
Entry(top, bd =1, width=60, textvariable=force_str).grid(row=9, column=1)
Tkinter.Button(top, width=9, text = "Default", command = default_force_str).grid(row=9, column=2)
Label(top, text="Cut Mode ").grid(row=10, column=0, sticky=W)
Entry(top, bd =1, width=60, textvariable=cut_mode_str).grid(row=10, column=1)
Tkinter.Button(top, width=9, text = "Default", command = default_cut_mode_str).grid(row=10, column=2)
if os.name=='nt':
Label(top, text="Cutter Shared Name").grid(row=11, column=0, sticky=W)
else:
Label(top, text="Cutter Device Name").grid(row=11, column=0, sticky=W)
Entry(top, bd =1, width=60, textvariable=cutter_shared_name_str).grid(row=11, column=1, sticky=E)
Tkinter.Button(top, width=40, text="Show Gerber in Gerbv", command=show_gerber).grid(row=12, column=1)
Tkinter.Button(top, width=40, text="Create Graphtec File from Input", command=main_program).grid(row=13, column=1)
Tkinter.Button(top, width=40, text="Send Graphtec File to Silhouette Cutter", command=Send_to_Cutter).grid(row=14, column=1)
Tkinter.Button(top, width=40, text="Save Configuration", command=Save_Configuration).grid(row=15, column=1)
Tkinter.Button(top, width=40, text="Exit", command=Just_Exit).grid(row=16, column=1)
Tkinter.Button(top, width=40, text="Create test_forces Graphtec file", command=test_forces).grid(row=17, column=1)
if path.isfile(CONFPATH) and access(CONFPATH, R_OK):
f = open(CONFPATH,'r')
input_filename = f.readline()
input_filename = input_filename.strip()
output_filename = f.readline()
output_filename = output_filename.strip()
gerbv_filename = f.readline()
gerbv_filename = gerbv_filename.strip()
pstoedit_filename = f.readline()
pstoedit_filename = pstoedit_filename.strip()
offset_text = f.readline()
offset_text = offset_text.strip()
border_text = f.readline()
border_text = border_text.strip()
matrix_text = f.readline()
matrix_text = matrix_text.strip()
speed_text = f.readline()
speed_text = speed_text.strip()
force_text = f.readline()
force_text = force_text.strip()
cut_mode_text = f.readline()
cut_mode_text = cut_mode_text.strip()
cutter_shared_name_text = f.readline()
cutter_shared_name_text = cutter_shared_name_text.strip()
f.close()
if not input_filename:
input_filename=""
if not output_filename:
output_filename="result.txt"
if not gerbv_filename:
if os.name=='nt':
gerbv_filename="C:/Program Files (x86)/gerbv-2.6.0/bin/gerbv.exe"
else:
gerbv_filename="gerbv"
if not pstoedit_filename:
if os.name=='nt':
pstoedit_filename="C:/Program Files/pstoedit/pstoedit.exe"
else:
pstoedit_filename="pstoedit"
if not offset_text:
offset_text="4.0,0.5"
if not border_text:
border_text="1,1"
if not matrix_text:
matrix_text="1,0,0,1"
if not speed_text:
speed_text="2,2"
if not force_text:
force_text="8,30"
if not cut_mode_text:
cut_mode_text="0"
if not cutter_shared_name_text:
if os.name=='nt':
cutter_shared_name_text="\\\\[Computer Name]\\[Shared Name]"
else:
cutter_shared_name_text="/dev/usb/lp0"
Gerber_name.set(input_filename)
Output_name.set(output_filename)
gerbv_path.set(gerbv_filename)
pstoedit_path.set(pstoedit_filename)
offset_str.set(offset_text)
border_str.set(border_text)
matrix_str.set(matrix_text)
speed_str.set(speed_text)
force_str.set(force_text)
cut_mode_str.set(cut_mode_text)
cutter_shared_name_str.set(cutter_shared_name_text)
top.mainloop()