/
importDWG.py
130 lines (119 loc) · 5.46 KB
/
importDWG.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
# -*- coding: utf8 -*-
#***************************************************************************
#* *
#* Copyright (c) 2009 Yorik van Havre <yorik@uncreated.net> *
#* *
#* This program is free software; you can redistribute it and/or modify *
#* it under the terms of the GNU Lesser General Public License (LGPL) *
#* 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 *
#* *
#***************************************************************************
"FreeCAD Draft Workbench - DWG importer/exporter"
## @package importDWG
# \ingroup DRAFT
# \brief DWG file importer & exporter
#
# This module provides support for importing and exporting Autodesk DWG files.
# This module is only a thin layer that uses the Teigha Converter application
# to convert to/from DXF. Then the real work is done by importDXF
if open.__module__ == '__builtin__':
pythonopen = open # to distinguish python built-in open function from the one declared here
def open(filename):
"called when freecad opens a file."
dxf = convertToDxf(filename)
if dxf:
import importDXF
doc = importDXF.open(dxf)
return doc
return
def insert(filename,docname):
"called when freecad imports a file"
dxf = convertToDxf(filename)
if dxf:
import importDXF
doc = importDXF.insert(dxf,docname)
return doc
return
def export(objectslist,filename):
"called when freecad exports a file"
import importDXF,os,tempfile
outdir = tempfile.mkdtemp()
dxf = outdir + os.sep + os.path.splitext(os.path.basename(filename))[0] + ".dxf"
importDXF.export(objectslist,dxf)
convertToDwg(dxf,filename)
return filename
def getTeighaConverter():
import FreeCAD,os,platform
"finds the Teigha Converter executable"
p = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Draft")
p = p.GetString("TeighaFileConverter")
if p:
# path set manually
teigha = p
else:
# try to find teigha
teigha = None
if platform.system() == "Linux":
teigha = "/usr/bin/TeighaFileConverter"
elif platform.system() == "Windows":
odadir = os.path.expandvars("%ProgramFiles%\ODA")
if os.path.exists(odadir):
subdirs = os.walk(odadir).next()[1]
for sub in subdirs:
t = odadir + os.sep + sub + os.sep + "TeighaFileConverter.exe"
if os.path.exists(t):
teigha = t
if teigha:
if os.path.exists(teigha):
return teigha
from DraftTools import translate
FreeCAD.Console.PrintMessage(translate("draft","Teigha File Converter not found, DWG support is disabled.\n"))
return None
def convertToDxf(dwgfilename):
"converts a DWG file to DXF"
import os,tempfile,subprocess #import os,tempfile
teigha = getTeighaConverter()
if teigha:
indir = os.path.dirname(dwgfilename)
outdir = tempfile.mkdtemp()
basename = os.path.basename(dwgfilename)
cmdline = '"%s" "%s" "%s" "ACAD2000" "DXF" "0" "1" "%s"' % (teigha, indir, outdir, basename)
print("Converting: " + cmdline)
if isinstance(cmdline,unicode):
import sys
encoding = sys.getfilesystemencoding()
cmdline = cmdline.encode(encoding)
subprocess.call(cmdline, shell=True) #os.system(cmdline)
result = outdir + os.sep + os.path.splitext(basename)[0] + ".dxf"
if os.path.exists(result):
print("Conversion successful")
return result
else:
print("Error during DWG to DXF conversion. Try moving the DWG file to a directory path")
print("without spaces and non-english characters, or try saving to a lower DWG version")
return None
def convertToDwg(dxffilename,dwgfilename):
"converts a DXF file to DWG"
import os,subprocess #import os
teigha = getTeighaConverter()
if teigha:
indir = os.path.dirname(dxffilename)
outdir = os.path.dirname(dwgfilename)
basename = os.path.basename(dxffilename)
cmdline = '"%s" "%s" "%s" "ACAD2000" "DWG" "0" "1" "%s"' % (teigha, indir, outdir, basename)
print("converting " + cmdline)
subprocess.call(cmdline, shell=True) #os.system(cmdline)
return dwgfilename
return None