forked from yorikvanhavre/CADExchanger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCADExchangerIO.py
163 lines (124 loc) · 6.01 KB
/
CADExchangerIO.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
#***************************************************************************
#* *
#* Copyright (c) 2017 Yorik van Havre <[email protected]> *
#* *
#* 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 *
#* *
#***************************************************************************
__title__="FreeCAD CAD Exchanger importer/exporter"
__author__ = "Yorik van Havre"
__url__ = "http://www.freecadweb.org"
import subprocess,tempfile,os,FreeCAD
preferences = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/CADExchanger")
converter = preferences.GetString("ConverterPath","")
if FreeCAD.GuiUp:
from PySide import QtCore, QtGui
# Qt tanslation handling
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def translate(context, text, disambig=None):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def translate(context, text, disambig=None):
return QtGui.QApplication.translate(context, text, disambig)
else:
def translate(ctxt,txt):
return txt
def addPreferencePage():
"Adds the CAD Exchanger preferences page"
if FreeCAD.GuiUp:
import FreeCADGui
FreeCADGui.addPreferencePage(os.path.join(os.path.dirname(__file__),"CADExchangerIO.ui"),"Import-Export")
def getExtensions():
"Get the list of supported file formats from CadExchangerConv executable"
# this is currently not used but might prove handy some day...
if not converter:
FreeCAD.Console.PrintError(translate("CADExchanger","CAD Exchanger converter path is not set. Please check Preferences -> Import/Export/CAD Exchanger\n"))
return
if subprocess.call(converter) != 1:
FreeCAD.Console.PrintError(translate("CADExchanger","Error while running CAD Exchanger\n"))
return
extensions = {}
try:
subprocess.check_output(converter)
except subprocess.CalledProcessError as e:
if e.output.startswith("Usage"):
rec = False
rest = ""
for l in e.output.split("\n"):
if rec:
if not l:
rec = False
else:
l = l.split(":\t")
if len(l) == 1:
rest = l[0].strip() + " "
else:
extensions[l[-1].strip()] = rest + l[0].strip()
rest = ""
elif "recognized extensions" in l.lower():
rec = True
else:
FreeCAD.Console.PrintError(translate("CADExchanger","Unable to retrieve file extensions from CAD Exchanger\n"))
return extensions
def open(filename):
"called by FreeCAD on opening a file"
docname = (os.path.splitext(os.path.basename(filename))[0]).encode("utf8")
doc = FreeCAD.newDocument(docname)
doc.Label = docname
FreeCAD.ActiveDocument = doc
return insert(filename,doc.Name)
def insert(filename,docname):
"called on importing a file in an existing document"
if not converter:
FreeCAD.Console.PrintError(translate("CADExchanger","CAD Exchanger converter path is not set. Please check Preferences -> Import/Export/CAD Exchanger\n"))
return
if subprocess.call(converter) != 1:
FreeCAD.Console.PrintError(translate("CADExchanger","Error while running CAD Exchanger\n"))
return
try:
doc = FreeCAD.getDocument(docname)
except NameError:
doc = FreeCAD.newDocument(docname)
FreeCAD.ActiveDocument = doc
tempname = tempfile.mkstemp(suffix=".brep")[1]
exstr = [converter, " -i ", filename, " -e ", tempname]
print "executing "+"".join(exstr)
result = subprocess.call(exstr)
if result != 0:
FreeCAD.Console.PrintError(translate("CADExchanger","Error during CAD Exchanger conversion\n"))
return
import Part
Part.show(Part.read(tempname))
return doc
def export(exportList,filename):
"called on exporting a file"
if not converter:
FreeCAD.Console.PrintError(translate("CADExchanger","CAD Exchanger converter path is not set. Please check Preferences -> Import/Export/CAD Exchanger\n"))
return
if subprocess.call(converter) != 1:
FreeCAD.Console.PrintError(translate("CADExchanger","Error while running CAD Exchanger\n"))
return
import Part
tempname = tempfile.mkstemp(suffix=".brep")[1]
Part.export(exportList,tempname)
exstr = [converter, " -i ", tempname, " -e ", filename]
print "executing "+"".join(exstr)
result = subprocess.call(exstr)
if result != 0:
FreeCAD.Console.PrintError(translate("CADExchanger","Error during CAD Exchanger conversion\n"))
return