2
# ****************************************************************************
3
# * Copyright (c) 2018 Maurice <easyw@katamail.com> *
5
# * StepZ Import Export compressed STEP files for FreeCAD *
8
# ****************************************************************************
10
# workaround for unicode in gzipping filename
11
# OCC7 doesn't support non-ASCII characters at the moment
12
# https://forum.freecad.org/viewtopic.php?t=20815
22
from PySide import QtCore
23
from PySide import QtGui
26
___stpZversion___ = "1.4.0"
27
# support both gz and zipfile archives
28
# Catia seems to use gz, Inventor zipfile
29
# improved import, open and export
39
# import stepZ; import importlib; importlib.reload(stepZ); stepZ.open(u"C:/Temp/brick.stpz")
43
if isinstance(input, str):
46
input = input.encode("utf-8")
51
def mkz_unicode(input):
52
if isinstance(input, str):
55
input = input.decode("utf-8")
61
FreeCAD.Console.PrintMessage(msg)
62
FreeCAD.Console.PrintMessage("\n")
67
FreeCAD.Console.PrintWarning(msg)
68
FreeCAD.Console.PrintWarning("\n")
73
FreeCAD.Console.PrintError(msg)
74
FreeCAD.Console.PrintWarning("\n")
80
def import_stpz(fn, fc, doc):
83
ext = os.path.splitext(os.path.basename(fn))[1]
84
fname = os.path.splitext(os.path.basename(fn))[0]
85
basepath = os.path.split(fn)[0]
86
filepath = os.path.join(basepath, fname + ".stp")
88
tempdir = tempfile.gettempdir() # get the current temporary directory
89
tempfilepath = os.path.join(tempdir, fname + ".stp")
91
with builtins.open(tempfilepath, "wb") as f: # py3
93
# ImportGui.insert(filepath)
95
ImportGui.open(tempfilepath)
97
ImportGui.open(tempfilepath, doc.Name)
98
FreeCADGui.SendMsgToActiveView("ViewFit")
100
os.remove(tempfilepath)
102
sayzerr("error on removing " + tempfilepath + " file")
108
def open(filename, doc=None):
110
if zf.is_zipfile(filename):
111
with zf.ZipFile(filename, "r") as fz:
112
file_names = fz.namelist()
113
for fn in file_names:
115
with fz.open(fn) as zfile:
116
file_content = zfile.read()
117
import_stpz(filename, file_content, doc)
119
with gz.open(filename, "rb") as f:
120
fnm = os.path.splitext(os.path.basename(filename))[0]
122
file_content = f.read()
123
import_stpz(filename, file_content, doc)
129
def insert(filename, doc):
131
doc = FreeCAD.ActiveDocument
138
def export(objs, filename):
139
"""exporting to file folder"""
142
sayz("stpZ version " + ___stpZversion___)
143
ext = os.path.splitext(os.path.basename(filename))[1]
144
fname = os.path.splitext(os.path.basename(filename))[0]
145
basepath = os.path.split(filename)[0]
146
tempdir = tempfile.gettempdir() # get the current temporary directory
148
filepath = os.path.join(basepath, fname) + ".stp"
149
filepath_base = os.path.join(basepath, fname)
151
namefpath = os.path.join(basepath, fname)
153
outfpath = os.path.join(basepath, fname) + ".stpZ"
154
outfpathT = os.path.join(tempdir, fname) + ".stpZ"
155
outfpath_stp = os.path.join(basepath, fname) + ".stp"
156
outfpathT_stp = os.path.join(tempdir, fname) + ".stp"
158
outfpath_base = basepath
159
# outfpath_str = mkz_string(os.path.join(basepath,fname))
160
outfpath_str = os.path.join(basepath, fname) + ".stp"
161
outfpathT_str = os.path.join(tempdir, fname) + ".stp"
163
if os.path.exists(outfpathT_stp):
164
os.remove(outfpathT_stp)
165
sayzw("Old temp file with the same name removed '" + outfpathT_stp + "'")
166
ImportGui.export(objs, outfpathT_stp)
167
with builtins.open(outfpathT_stp, "rb") as f_in:
168
file_content = f_in.read()
169
new_f_content = file_content
171
with gz.open(outfpathT_str, "wb") as f_out:
172
f_out.write(new_f_content)
174
if os.path.exists(outfpath):
175
shutil.move(outfpathT_str, outfpath)
176
# os.remove(outfpathT_stp)
178
shutil.move(outfpathT_str, outfpath)
179
# os.remove(outfpathT_stp)