#!/usr/bin/env python # # img2dxf.py # (C)BA Neil Gershenfeld # convert a bitmap image to a 3D DXF surface # DATE = "4/27/03" from Tkinter import * from string import * import Image # # window size # WIDTH = 500 HEIGHT = 500 # # machine horizontal size and units scale factor # SIZE = 10 SCALE = 1000 # # default cutting force and speed # force = 150 vel = 40 path = [] ipath = [] xoff = 0 yoff = 0 scale = 1 def read(event): global path # # read in DXF file # #text = infile.get() #img = Image.open(text) #print image photo = PhotoImage(file="j.gif") #item = c.create_image(100,100,anchor=NW,image=photo) item = Image.create_image(100,100,anchor=NW,image=photo) item.pack() def plot(event): # # scale and plot path # scale = float(sscale.get())*WIDTH/SIZE xoff = float(sxoff.get())*WIDTH/SIZE yoff = float(syoff.get())*HEIGHT/SIZE if (path != []): c.delete("path") for i in range(len(path)/4): x0 = int(path[4*i]*scale + xoff) y0 = HEIGHT - int(path[4*i+1]*scale + yoff) x1 = int(path[4*i+2]*scale + xoff) y1 = HEIGHT - int(path[4*i+3]*scale + yoff) c.create_line(x0,y0,x1,y1,tag="path") def write(event): # # write CAMM file # text = outfile.get() file = open(text, 'w') file.write("PA;PA;!ST1;!FS"+sforce.get()+";VS"+svel.get()+";") x1 = y1 = -1e10 scale = float(sscale.get()) xoff = float(sxoff.get()) yoff = float(syoff.get()) for i in range(len(path)/4): x0 = int(SCALE*(path[4*i]*scale + xoff)) y0 = int(SCALE*(path[4*i+1]*scale + yoff)) if ((x0 != x1) & (y0 != y1)): file.write("PU"+str(x0)+","+str(y0)+";") x1 = int(SCALE*(path[4*i+2]*scale + xoff)) y1 = int(SCALE*(path[4*i+3]*scale + yoff)) file.write("PD"+str(x1)+","+str(y1)+";") file.write("PU0,0;") file.close() c.delete("path") root = Tk() root.title('img2dxf') inframe = Frame(root) infile = StringVar() Label(inframe, text="input image file: ").pack(side="left") winfile = Entry(inframe, width=20, textvariable=infile) winfile.pack(side="left") winfile.bind('',read) inframe.pack() coordframe = Frame(root) sxoff = StringVar() sxoff.set(str(xoff)) syoff = StringVar() syoff.set(str(yoff)) sscale = StringVar() sscale.set(str(scale)) Label(coordframe, text="x offset:").pack(side="left") wxoff = Entry(coordframe, width=10, textvariable=sxoff) wxoff.pack(side="left") wxoff.bind('',plot) Label(coordframe, text="y offset:").pack(side="left") wyoff = Entry(coordframe, width=10, textvariable=syoff) wyoff.pack(side="left") wyoff.bind('',plot) Label(coordframe, text="scale factor:").pack(side="left") wscale = Entry(coordframe, width=10, textvariable=sscale) wscale.pack(side="left") wscale.bind('',plot) coordframe.pack() c = Canvas(root, width=WIDTH, height=HEIGHT, background='white') c.pack() outframe = Frame(root) outfile = StringVar() outfile.set('out.camm') Label(outframe, text="output CAMM file: ").pack(side="left") woutfile = Entry(outframe, width=20, textvariable=outfile) woutfile.pack(side="left") woutfile.bind('',write) Label(outframe, text="force: ").pack(side="left") sforce = StringVar() sforce.set(str(force)) Entry(outframe, width=10, textvariable=sforce).pack(side="left") Label(outframe, text="velocity:").pack(side="left") svel = StringVar() svel.set(str(vel)) Entry(outframe, width=10, textvariable=svel).pack(side="left") outframe.pack() botframe = Frame(root) Button(botframe, text="quit", command='exit').pack(side="left") Label(botframe, text=" img2dxf "+DATE).pack(side="right") botframe.pack() root.mainloop()