improved import/export compatibility w prop cads

thread related
https://forum.freecadweb.org/viewtopic.php?f=3&t=37469#p318651
This commit is contained in:
easyw
2019-07-04 14:49:51 +02:00
committed by Yorik van Havre
parent 265faf6c59
commit 7de89cf62e

View File

@@ -21,17 +21,24 @@ import PySide
from PySide import QtGui, QtCore
import tempfile
___stpZversion___ = "1.3.4"
___stpZversion___ = "1.3.7"
# support both gz and zipfile archives
# Catia seems to use gz, Inventor zipfile
# improved import, open and export
if six.PY3:
import gzip as gz
import builtins as builtin #py3
import importlib
else: # six.PY2
import gzip_utf8 as gz
import __builtin__ as builtin #py2
# import stepZ; reload(stepZ); import gzip_utf8; reload(gzip_utf8)
import zipfile as zf
# import stepZ; import importlib; importlib.reload(stepZ); stepZ.open(u"C:/Temp/brick.stpz")
# import stepZ; import importlib; importlib.reload(stepZ); import gzip_utf8; importlib.reload(gzip_utf8)
def mkz_string(input):
if six.PY3:
@@ -73,56 +80,60 @@ def sayzerr(msg):
FreeCAD.Console.PrintError(msg)
FreeCAD.Console.PrintWarning('\n')
####
def open(filename):
sayz("stpZ version "+___stpZversion___)
with gz.open(filename, 'rb') as f:
file_content = f.read()
def import_stpz(fn,fc,doc):
ext = os.path.splitext(os.path.basename(filename))[1]
fname=os.path.splitext(os.path.basename(filename))[0]
basepath=os.path.split(filename)[0]
# sayz(fn)
ext = os.path.splitext(os.path.basename(fn))[1]
fname=os.path.splitext(os.path.basename(fn))[0]
basepath=os.path.split(fn)[0]
filepath = os.path.join(basepath,fname + u'.stp')
tempdir = tempfile.gettempdir() # get the current temporary directory
tempfilepath = os.path.join(tempdir,fname + u'.stp')
#with six.builtins.open(tempfilepath, 'wb') as f: #py3
with builtin.open(tempfilepath, 'wb') as f: #py3
f.write(file_content)
f.write(fc)
#ImportGui.insert(filepath)
ImportGui.open(tempfilepath)
if doc is None:
ImportGui.open(tempfilepath)
else:
ImportGui.open(tempfilepath,doc.Name)
FreeCADGui.SendMsgToActiveView("ViewFit")
try:
os.remove(tempfilepath)
except OSError:
sayzerr("error on removing "+tempfilepath+" file")
pass
###
def open(filename,doc=None):
sayz("stpZ version "+___stpZversion___)
if zf.is_zipfile(filename):
with zf.ZipFile(filename, 'r') as fz:
file_names = fz.namelist()
for fn in file_names:
sayz(fn)
with fz.open(fn) as zfile:
file_content = zfile.read()
import_stpz(filename,file_content,doc)
else:
with gz.open(filename, 'rb') as f:
fnm=os.path.splitext(os.path.basename(filename))[0]
sayz(fnm)
file_content = f.read()
import_stpz(filename,file_content,doc)
####
def insert(filename,doc):
doc = FreeCAD.ActiveDocument
open(filename, doc)
sayz("stpZ version "+___stpZversion___)
with gz.open(filename, 'rb') as f:
file_content = f.read()
ext = os.path.splitext(os.path.basename(filename))[1]
fname=os.path.splitext(os.path.basename(filename))[0]
basepath=os.path.split(filename)[0]
filepath = os.path.join(basepath,fname + u'.stp')
tempdir = tempfile.gettempdir() # get the current temporary directory
tempfilepath = os.path.join(tempdir,fname + u'.stp')
# with six.builtins.open(tempfilepath, 'wb') as f: #py3
with builtin.open(tempfilepath, 'wb') as f: #py3
f.write(file_content)
ImportGui.insert(tempfilepath, doc)
#ImportGui.open(tempfilepath)
try:
os.remove(tempfilepath)
except OSError:
sayzerr("error on removing "+tempfilepath+" file")
pass
####
def export(objs,filename):
@@ -144,7 +155,7 @@ def export(objs,filename):
outfpath_stp = os.path.join(basepath,fname)+u'.stp'
outfpath_base = basepath
#outfpath_str = mkz_string(os.path.join(basepath,fname))
outfpath_str = os.path.join(basepath,fname)
outfpath_str = os.path.join(basepath,fname)+u'.stp'
if os.path.exists(outfpath_stp):
@@ -153,25 +164,20 @@ def export(objs,filename):
reply = QtGui.QMessageBox.information(None,"info", "File cannot be compressed because\na file with the same name exists\n'"+ outfpath_stp + "'")
else:
ImportGui.export(objs,outfpath_stp)
if 0: #os.path.exists(namefpath):
sayzw("File cannot be compressed because a file with the same name exists '" + namefpath + "'")
QtGui.QApplication.restoreOverrideCursor()
reply = QtGui.QMessageBox.information(None,"info", "File cannot be compressed because\na file with the same name exists\n'"+ namefpath+ "'")
with builtin.open(outfpath_stp, 'rb') as f_in:
file_content = f_in.read()
new_f_content = file_content
f_in.close()
with gz.open(outfpath_str, 'wb') as f_out:
f_out.write(new_f_content)
f_out.close()
if os.path.exists(outfpath):
os.remove(outfpath)
os.rename(outfpath_str, outfpath)
#os.remove(outfpath_stp)
else:
with builtin.open(outfpath_stp, 'rb') as f_in:
file_content = f_in.read()
new_f_content = file_content
f_in.close()
with gz.open(outfpath_str, 'wb') as f_out:
f_out.write(new_f_content)
f_out.close()
if os.path.exists(outfpath):
os.remove(outfpath)
os.rename(outfpath_str, outfpath)
os.remove(outfpath_stp)
else:
os.rename(outfpath_str, outfpath)
os.remove(outfpath_stp)
os.rename(outfpath_str, outfpath)
#os.remove(outfpath_stp)
####