#!/usr/bin/python #*************************************************************************** #* * #* Copyright (c) 2010 Yorik van Havre * #* * #* This program is free software; you can redistribute it and/or modify * #* it under the terms of the GNU General Public License (GPL) * #* as published by the Free Software Foundation; either version 2 of * #* the License, or (at your option) any later version. * #* for detail see the LICENCE text file. * #* * #* This program is distributed in the hope that it will be useful, * #* but WITHOUT ANY WARRANTY; without even the implied warranty of * #* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * #* GNU Library General Public License for more details. * #* * #* You should have received a copy of the GNU Library General Public * #* License along with this program; if not, write to the Free Software * #* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * #* USA * #* * #*************************************************************************** __title__="DXF thumbnailer utility" __author__ = "Yorik van Havre " __url__ = "http://yorik.uncreated.net" __version__ = '0.1 - 11.10.2010' ''' Linux DXF thumbnailer. This application generates a 128x128px PNG image showing the contents of a DXF file. In order to speed up the image generation process, only a few types of entities are read, so complex files might not get rendered accurately or even fail to render. In order to use this program, you must configure your file manager to use it as a thumbnailer for dxf file type (mime type: image/x-dxf). ''' import sys, getopt, gnomevfs, Image, ImageDraw opt,par = getopt.getopt(sys.argv[1:],'-s:') inpfile = gnomevfs.get_local_path_from_uri(par[0]) outfile = par[1] # inpfile = par[0] # outfile = 'test.png' try: objects = [] currentobject = [] command = None store = None startentities = False xmin = 0 xmax = 0 ymin = 0 ymax = 0 dxf = open(inpfile) for l in dxf.readlines(): fline = l.strip() if fline == 'ENTITIES': startentities = True if not startentities: continue if not command: if fline == 'POLYLINE' or fline == 'LWPOLYLINE': vertexmode = False command = 'polyline' currentobject.append(command) elif fline == 'LINE': command = 'line' currentobject.append(command) elif fline == 'ARC': command = 'arc' currentobject.append(command) elif fline == 'CIRCLE': command = 'circle' currentobject.append(command) elif command == 'line': if not store: if fline == '10': store = 'x' elif fline == '20': store = 'y' elif fline == '11': store = 'x' elif fline == '21': store = 'y' else: num = float(fline) currentobject.append(num) if store == 'x': if num < xmin: xmin = num if num > xmax: xmax = num elif store == 'y': if num < ymin: ymin = num if num > ymax: ymax = num store = None if len(currentobject) == 5: objects.append(currentobject) currentobject = [] command = None elif command == 'arc': if not store: if fline == '10': store = 'x' elif fline == '20': store = 'y' elif fline == '40': store = 'other' elif fline == '50': store = 'other' elif fline == '51': store = 'other' else: num = float(fline) currentobject.append(num) if store == 'x': if num < xmin: xmin = num if num > xmax: xmax = num elif store == 'y': if num < ymin: ymin = num if num > ymax: ymax = num store = None if len(currentobject) == 6: objects.append(currentobject) currentobject = [] command = None elif command == 'circle': if not store: if fline == '10': store = 'x' elif fline == '20': store = 'y' elif fline == '40': store = 'other' else: num = float(fline) currentobject.append(num) if store == 'x': if num < xmin: xmin = num if num > xmax: xmax = num elif store == 'y': if num < ymin: ymin = num if num > ymax: ymax = num store = None if len(currentobject) == 4: objects.append(currentobject) currentobject = [] command = None elif command == 'polyline': if not store: if fline == 'VERTEX': vertexmode = True elif fline == '10' and vertexmode: store = 'x' elif fline == '20'and vertexmode: store = 'y' elif fline == 'SEQEND': objects.append(currentobject) currentobject = [] command = None else: num = float(fline) currentobject.append(num) if store == 'x': if num < xmin: xmin = num if num > xmax: xmax = num elif store == 'y': if num < ymin: ymin = num if num > ymax: ymax = num store = None print len(objects),"objects" print "bounding box",xmin,ymin,xmax,ymax sizex = xmax-xmin sizey = ymax-ymin image = Image.new('RGB',(128,128)) draw = ImageDraw.Draw(image) draw.rectangle((0,0,128,128),fill='#ffffff') for ob in objects: print "drawing",ob if ob[0] == 'line': x1 = int(((ob[1]-xmin)/sizex)*128) y1 = 128-int(((ob[2]-ymin)/sizey)*128) x2 = int(((ob[3]-xmin)/sizex)*128) y2 = 128-int(((ob[4]-ymin)/sizey)*128) draw.line((x1,y1,x2,y2),fill=0) elif ob[0] == 'polyline': x1 = int(((ob[1]-xmin)/sizex)*128) y1 = 128-int(((ob[2]-ymin)/sizey)*128) done = False for p in ob[3:]: if not done: lp = p else: x2 = int(((lp-xmin)/sizex)*128) y2 = 128-int(((p-ymin)/sizey)*128) draw.line((x1,y1,x2,y2),fill=0) x1 = x2 y1 = y2 done = not(done) elif ob[0] == 'circle': x1 = int((((ob[1]-ob[3])-xmin)/sizex)*128) y1 = 128-int((((ob[2]-ob[3])-ymin)/sizey)*128) x2 = int((((ob[1]+ob[3])-xmin)/sizex)*128) y2 = 128-int((((ob[2]+ob[3])-ymin)/sizey)*128) draw.ellipse((x1,y2,x2,y1),outline=0) elif ob[0] == 'arc': x1 = int((((ob[1]-ob[3])-xmin)/sizex)*128) y1 = 128-int((((ob[2]-ob[3])-ymin)/sizey)*128) x2 = int((((ob[1]+ob[3])-xmin)/sizex)*128) y2 = 128-int((((ob[2]+ob[3])-ymin)/sizey)*128) start = int(ob[4]) end = int(ob[5]) draw.arc((x1,y2,x2,y1),start,end,fill=0) del draw image.save(outfile,'PNG') except: sys.exit(1)