#!/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 converter utility" __author__ = "Yorik van Havre " __url__ = "http://yorik.uncreated.net" __version__ = '0.2 - 09.02.2010' __doc__ = ''' Linux DWG to DXF converter. Usage: dxfconvert path/to/file.dwg Options: -h, --help: prints this help message -v, --dxfversion: the version of the output dxf file. Can be ACAD2000, ACAD2004 or ACAD2007 -d, --directory: convert all dwg files found in the given path This python application converts a dwg file to a dxf 2007 file with same name. To use it, you must have wine installed, and set the correct path to the EveryDWG utility below. The EveryDWG utility can be downloaded free of charge from the opendwg website: http://www.opendwg.org/guestfiles but you must agree to their license term before downloading. History: 0.1 - 11.01.2010 - Initial release 0.2 - 09.02.2010 - Added more command-line options ''' # CONFIG ################################### EVERYDWG = '/home/yorik/Apps/EveryDWG.exe' CONFIG = 'ACAD2007 DXF 1 0' # can be overridden from the command line # END CONFIG ############################### import sys, os, tempfile, shutil, getopt, gnomevfs dirmode = False try: opt,par = getopt.getopt(sys.argv[1:],'') # inputfile = gnomevfs.get_local_path_from_uri(par[0]) inputfile = par[0] except getopt.GetoptError, err: print str(err) print __doc__ sys.exit(2) for o,a in opt: if o in ('-h','--help'): print __doc__ sys.exit() elif o in ('-v', '--dxfversion'): if a in ('ACAD2000', 'ACAD2004', 'ACAD2007'): CONFIG = a+' DXF 1 0' else: print 'unknown dxf format. Allowed formats are ACAD2000, ACAD2004 or ACAD2007' sys.exit() elif o in ('-d','--directory'): dirmode = True if dirmode: targetdir = os.path.abspath(inputfile) else: infile = os.path.split(os.path.abspath(inputfile)) targetdir = infile[0] basename = infile[1][:-4] tempin = tempfile.mkdtemp('dxfin') winepathin = os.popen("winepath "+tempin).read() tempout = tempfile.mkdtemp('dxfout') winepathout = os.popen("winepath "+tempout).read() winepathin = winepathin.split('dosdevices')[1][1:-1] winepathin = winepathin[0].upper()+winepathin[1:] winepathin = winepathin.replace('/','\\\\') winepathout = winepathout.split('dosdevices')[1][1:-1] winepathout = winepathout[0].upper()+winepathout[1:] winepathout = winepathout.replace('/','\\\\') if dirmode: files = os.listdir(targetdir) for f in file: ext = os.path.splitext(f) if ext[-1] in ('.dwg','.DWG'): shutil.copy(targetdir+os.sep+f,tempin) else: shutil.copy(infile[0]+os.sep+infile[1],tempin) dwgcommand = 'wine '+EVERYDWG+' '+winepathin+' '+winepathout+' '+CONFIG # print 'command: ',dwgcommand os.popen(dwgcommand) if dirmode: files = os.listdir(tempout) for f in files: shutil.copy(tempout+os.sep+f,targetdir) else: shutil.copy(tempout+os.sep+basename+'.dxf',targetdir) shutil.rmtree(tempin) shutil.rmtree(tempout)