From 2a9ac632cbc3b3391f41fa76d0803b730a7ad1c2 Mon Sep 17 00:00:00 2001 From: easyw Date: Sun, 28 Jan 2018 20:35:05 +0100 Subject: [PATCH] adding stpZ format: STEP compression format support --- src/Mod/Import/CMakeLists.txt | 2 + src/Mod/Import/Gui/CMakeLists.txt | 2 +- src/Mod/Import/gzip_utf8.py | 526 ++++++++++++++++++++++++++++++ src/Mod/Import/stepZ.py | 170 ++++++++++ 4 files changed, 699 insertions(+), 1 deletion(-) create mode 100644 src/Mod/Import/gzip_utf8.py create mode 100644 src/Mod/Import/stepZ.py diff --git a/src/Mod/Import/CMakeLists.txt b/src/Mod/Import/CMakeLists.txt index 740f84d33f..a20f9ad804 100644 --- a/src/Mod/Import/CMakeLists.txt +++ b/src/Mod/Import/CMakeLists.txt @@ -24,6 +24,8 @@ INSTALL( FILES Init.py InitGui.py + gzip_utf8.py + stepZ.py ${Import_QRC_SRCS} DESTINATION Mod/Import diff --git a/src/Mod/Import/Gui/CMakeLists.txt b/src/Mod/Import/Gui/CMakeLists.txt index aee68f7c4d..1c135dab16 100644 --- a/src/Mod/Import/Gui/CMakeLists.txt +++ b/src/Mod/Import/Gui/CMakeLists.txt @@ -43,7 +43,7 @@ target_link_libraries(ImportGui ${ImportGui_LIBS}) fc_target_copy_resource(ImportGui ${CMAKE_SOURCE_DIR}/src/Mod/Import ${CMAKE_BINARY_DIR}/Mod/Import - Init.py InitGui.py) + Init.py InitGui.py gzip_utf8.py stepZ.py) SET_BIN_DIR(ImportGui ImportGui /Mod/Import) SET_PYTHON_PREFIX_SUFFIX(ImportGui) diff --git a/src/Mod/Import/gzip_utf8.py b/src/Mod/Import/gzip_utf8.py new file mode 100644 index 0000000000..a8f28cadb5 --- /dev/null +++ b/src/Mod/Import/gzip_utf8.py @@ -0,0 +1,526 @@ +"""Functions that read and write gzipped files. + +The user of the file doesn't have to worry about the compression, +but random access is not allowed.""" + +# based on Andrew Kuchling's minigzip.py distributed with the zlib module + +# License: PSF, see https://www.python.org/psf/ +# original file: gzip.py from https://www.python.org/ftp/python/2.7.8/Python-2.7.8.tgz +# changed line: +# self.fileobj.write(fname + '\000') +# to: +# self.fileobj.write(fname.encode('utf-8') + '\000') +# as a workaround to gzip filenames containing utf-8 characters on win systems + +import struct, sys, time, os +import zlib +import io +import __builtin__ + +__all__ = ["GzipFile","open"] + +FTEXT, FHCRC, FEXTRA, FNAME, FCOMMENT = 1, 2, 4, 8, 16 + +READ, WRITE = 1, 2 + +def write32u(output, value): + # The L format writes the bit pattern correctly whether signed + # or unsigned. + output.write(struct.pack("' + + def _check_closed(self): + """Raises a ValueError if the underlying file object has been closed. + + """ + if self.closed: + raise ValueError('I/O operation on closed file.') + + def _init_write(self, filename): + self.name = filename + self.crc = zlib.crc32("") & 0xffffffffL + self.size = 0 + self.writebuf = [] + self.bufsize = 0 + + def _write_gzip_header(self): + self.fileobj.write('\037\213') # magic header + self.fileobj.write('\010') # compression method + fname = os.path.basename(self.name) + if fname.endswith(".gz"): + fname = fname[:-3] + flags = 0 + if fname: + flags = FNAME + self.fileobj.write(chr(flags)) + mtime = self.mtime + if mtime is None: + mtime = time.time() + write32u(self.fileobj, long(mtime)) + self.fileobj.write('\002') + self.fileobj.write('\377') + if fname: + self.fileobj.write(fname.encode('utf-8') + '\000') + + def _init_read(self): + self.crc = zlib.crc32("") & 0xffffffffL + self.size = 0 + + def _read_gzip_header(self): + magic = self.fileobj.read(2) + if magic != '\037\213': + raise IOError, 'Not a gzipped file' + method = ord( self.fileobj.read(1) ) + if method != 8: + raise IOError, 'Unknown compression method' + flag = ord( self.fileobj.read(1) ) + self.mtime = read32(self.fileobj) + # extraflag = self.fileobj.read(1) + # os = self.fileobj.read(1) + self.fileobj.read(2) + + if flag & FEXTRA: + # Read & discard the extra field, if present + xlen = ord(self.fileobj.read(1)) + xlen = xlen + 256*ord(self.fileobj.read(1)) + self.fileobj.read(xlen) + if flag & FNAME: + # Read and discard a null-terminated string containing the filename + while True: + s = self.fileobj.read(1) + if not s or s=='\000': + break + if flag & FCOMMENT: + # Read and discard a null-terminated string containing a comment + while True: + s = self.fileobj.read(1) + if not s or s=='\000': + break + if flag & FHCRC: + self.fileobj.read(2) # Read & discard the 16-bit header CRC + + def write(self,data): + self._check_closed() + if self.mode != WRITE: + import errno + raise IOError(errno.EBADF, "write() on read-only GzipFile object") + + if self.fileobj is None: + raise ValueError, "write() on closed GzipFile object" + + # Convert data type if called by io.BufferedWriter. + if isinstance(data, memoryview): + data = data.tobytes() + + if len(data) > 0: + self.size = self.size + len(data) + self.crc = zlib.crc32(data, self.crc) & 0xffffffffL + self.fileobj.write( self.compress.compress(data) ) + self.offset += len(data) + + return len(data) + + def read(self, size=-1): + self._check_closed() + if self.mode != READ: + import errno + raise IOError(errno.EBADF, "read() on write-only GzipFile object") + + if self.extrasize <= 0 and self.fileobj is None: + return '' + + readsize = 1024 + if size < 0: # get the whole thing + try: + while True: + self._read(readsize) + readsize = min(self.max_read_chunk, readsize * 2) + except EOFError: + size = self.extrasize + else: # just get some more of it + try: + while size > self.extrasize: + self._read(readsize) + readsize = min(self.max_read_chunk, readsize * 2) + except EOFError: + if size > self.extrasize: + size = self.extrasize + + offset = self.offset - self.extrastart + chunk = self.extrabuf[offset: offset + size] + self.extrasize = self.extrasize - size + + self.offset += size + return chunk + + def _unread(self, buf): + self.extrasize = len(buf) + self.extrasize + self.offset -= len(buf) + + def _read(self, size=1024): + if self.fileobj is None: + raise EOFError, "Reached EOF" + + if self._new_member: + # If the _new_member flag is set, we have to + # jump to the next member, if there is one. + # + # First, check if we're at the end of the file; + # if so, it's time to stop; no more members to read. + pos = self.fileobj.tell() # Save current position + self.fileobj.seek(0, 2) # Seek to end of file + if pos == self.fileobj.tell(): + raise EOFError, "Reached EOF" + else: + self.fileobj.seek( pos ) # Return to original position + + self._init_read() + self._read_gzip_header() + self.decompress = zlib.decompressobj(-zlib.MAX_WBITS) + self._new_member = False + + # Read a chunk of data from the file + buf = self.fileobj.read(size) + + # If the EOF has been reached, flush the decompression object + # and mark this object as finished. + + if buf == "": + uncompress = self.decompress.flush() + self._read_eof() + self._add_read_data( uncompress ) + raise EOFError, 'Reached EOF' + + uncompress = self.decompress.decompress(buf) + self._add_read_data( uncompress ) + + if self.decompress.unused_data != "": + # Ending case: we've come to the end of a member in the file, + # so seek back to the start of the unused data, finish up + # this member, and read a new gzip header. + # (The number of bytes to seek back is the length of the unused + # data, minus 8 because _read_eof() will rewind a further 8 bytes) + self.fileobj.seek( -len(self.decompress.unused_data)+8, 1) + + # Check the CRC and file size, and set the flag so we read + # a new member on the next call + self._read_eof() + self._new_member = True + + def _add_read_data(self, data): + self.crc = zlib.crc32(data, self.crc) & 0xffffffffL + offset = self.offset - self.extrastart + self.extrabuf = self.extrabuf[offset:] + data + self.extrasize = self.extrasize + len(data) + self.extrastart = self.offset + self.size = self.size + len(data) + + def _read_eof(self): + # We've read to the end of the file, so we have to rewind in order + # to reread the 8 bytes containing the CRC and the file size. + # We check the that the computed CRC and size of the + # uncompressed data matches the stored values. Note that the size + # stored is the true file size mod 2**32. + self.fileobj.seek(-8, 1) + crc32 = read32(self.fileobj) + isize = read32(self.fileobj) # may exceed 2GB + if crc32 != self.crc: + raise IOError("CRC check failed %s != %s" % (hex(crc32), + hex(self.crc))) + elif isize != (self.size & 0xffffffffL): + raise IOError, "Incorrect length of data produced" + + # Gzip files can be padded with zeroes and still have archives. + # Consume all zero bytes and set the file position to the first + # non-zero byte. See http://www.gzip.org/#faq8 + c = "\x00" + while c == "\x00": + c = self.fileobj.read(1) + if c: + self.fileobj.seek(-1, 1) + + @property + def closed(self): + return self.fileobj is None + + def close(self): + if self.fileobj is None: + return + if self.mode == WRITE: + self.fileobj.write(self.compress.flush()) + write32u(self.fileobj, self.crc) + # self.size may exceed 2GB, or even 4GB + write32u(self.fileobj, self.size & 0xffffffffL) + self.fileobj = None + elif self.mode == READ: + self.fileobj = None + if self.myfileobj: + self.myfileobj.close() + self.myfileobj = None + + def flush(self,zlib_mode=zlib.Z_SYNC_FLUSH): + self._check_closed() + if self.mode == WRITE: + # Ensure the compressor's buffer is flushed + self.fileobj.write(self.compress.flush(zlib_mode)) + self.fileobj.flush() + + def fileno(self): + """Invoke the underlying file object's fileno() method. + + This will raise AttributeError if the underlying file object + doesn't support fileno(). + """ + return self.fileobj.fileno() + + def rewind(self): + '''Return the uncompressed stream file position indicator to the + beginning of the file''' + if self.mode != READ: + raise IOError("Can't rewind in write mode") + self.fileobj.seek(0) + self._new_member = True + self.extrabuf = "" + self.extrasize = 0 + self.extrastart = 0 + self.offset = 0 + + def readable(self): + return self.mode == READ + + def writable(self): + return self.mode == WRITE + + def seekable(self): + return True + + def seek(self, offset, whence=0): + if whence: + if whence == 1: + offset = self.offset + offset + else: + raise ValueError('Seek from end not supported') + if self.mode == WRITE: + if offset < self.offset: + raise IOError('Negative seek in write mode') + count = offset - self.offset + for i in xrange(count // 1024): + self.write(1024 * '\0') + self.write((count % 1024) * '\0') + elif self.mode == READ: + if offset < self.offset: + # for negative seek, rewind and do positive seek + self.rewind() + count = offset - self.offset + for i in xrange(count // 1024): + self.read(1024) + self.read(count % 1024) + + return self.offset + + def readline(self, size=-1): + if size < 0: + # Shortcut common case - newline found in buffer. + offset = self.offset - self.extrastart + i = self.extrabuf.find('\n', offset) + 1 + if i > 0: + self.extrasize -= i - offset + self.offset += i - offset + return self.extrabuf[offset: i] + + size = sys.maxint + readsize = self.min_readsize + else: + readsize = size + bufs = [] + while size != 0: + c = self.read(readsize) + i = c.find('\n') + + # We set i=size to break out of the loop under two + # conditions: 1) there's no newline, and the chunk is + # larger than size, or 2) there is a newline, but the + # resulting line would be longer than 'size'. + if (size <= i) or (i == -1 and len(c) > size): + i = size - 1 + + if i >= 0 or c == '': + bufs.append(c[:i + 1]) # Add portion of last chunk + self._unread(c[i + 1:]) # Push back rest of chunk + break + + # Append chunk to list, decrease 'size', + bufs.append(c) + size = size - len(c) + readsize = min(size, readsize * 2) + if readsize > self.min_readsize: + self.min_readsize = min(readsize, self.min_readsize * 2, 512) + return ''.join(bufs) # Return resulting line + + +def _test(): + # Act like gzip; with -d, act like gunzip. + # The input file is not deleted, however, nor are any other gzip + # options or features supported. + args = sys.argv[1:] + decompress = args and args[0] == "-d" + if decompress: + args = args[1:] + if not args: + args = ["-"] + for arg in args: + if decompress: + if arg == "-": + f = GzipFile(filename="", mode="rb", fileobj=sys.stdin) + g = sys.stdout + else: + if arg[-3:] != ".gz": + print "filename doesn't end in .gz:", repr(arg) + continue + f = open(arg, "rb") + g = __builtin__.open(arg[:-3], "wb") + else: + if arg == "-": + f = sys.stdin + g = GzipFile(filename="", mode="wb", fileobj=sys.stdout) + else: + f = __builtin__.open(arg, "rb") + g = open(arg + ".gz", "wb") + while True: + chunk = f.read(1024) + if not chunk: + break + g.write(chunk) + if g is not sys.stdout: + g.close() + if f is not sys.stdin: + f.close() + +if __name__ == '__main__': + _test() diff --git a/src/Mod/Import/stepZ.py b/src/Mod/Import/stepZ.py new file mode 100644 index 0000000000..2875557904 --- /dev/null +++ b/src/Mod/Import/stepZ.py @@ -0,0 +1,170 @@ +# -*- coding: utf-8 -*- +#**************************************************************************** +#* * +#* StepZ Import Export compressed STEP files for FreeCAD * +#* Copyright (c) 2018 * +#* Maurice easyw@katamail.com * +#* * +#* License: LGPLv2+ * + +# workaround for unicode in gzipping filename +# OCC7 doesn't support non-ASCII characters at the moment +# https://forum.freecadweb.org/viewtopic.php?t=20815 + +import FreeCAD,FreeCADGui +import gzip_utf8, shutil +import sys, os, re +import ImportGui +import PySide +from PySide import QtGui, QtCore +import tempfile + +___stpZversion___ = "1.3.1" + +try: + import __builtin__ as builtin #py2 +except: + import builtins as builtin #py3 + +# import stepZ; reload(stepZ); import gzip_utf8; reload(gzip_utf8) + +def mkz_string(input): + if (sys.version_info > (3, 0)): #py3 + if isinstance(input, str): + return input + else: + input = input.encode('utf-8') + return input + else: #py2 + if type(input) == unicode: + input = input.encode('utf-8') + return input + else: + return input +#### +def mkz_unicode(input): + if (sys.version_info > (3, 0)): #py3 + if isinstance(input, str): + return input + else: + input = input.decode('utf-8') + return input + else: #py2 + if type(input) != unicode: + input = input.decode('utf-8') + return input + else: + return input +#### +def sayz(msg): + FreeCAD.Console.PrintMessage(msg) + FreeCAD.Console.PrintMessage('\n') +#### +def sayzw(msg): + FreeCAD.Console.PrintWarning(msg) + FreeCAD.Console.PrintWarning('\n') +#### +def sayzerr(msg): + FreeCAD.Console.PrintError(msg) + FreeCAD.Console.PrintWarning('\n') +#### +def open(filename): + + sayz("stpZ version "+___stpZversion___) + with gzip_utf8.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 builtin.open(tempfilepath, 'w') as f: #py3 + f.write(file_content) + #ImportGui.insert(filepath) + ImportGui.open(tempfilepath) + try: + os.remove(tempfilepath) + except OSError: + sayzerr("error on removing "+tempfilepath+" file") + pass +#### + +def insert(filename,doc): + + sayz("stpZ version "+___stpZversion___) + with gzip_utf8.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 builtin.open(tempfilepath, 'w') 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): + """exporting to file folder""" + + #sayz(filename) + sayz("stpZ version "+___stpZversion___) + ext = os.path.splitext(os.path.basename(filename))[1] + fname=os.path.splitext(os.path.basename(filename))[0] + basepath=os.path.split(filename)[0] + tempdir = tempfile.gettempdir() # get the current temporary directory + + filepath = os.path.join(basepath,fname) + u'.stp' + filepath_base = os.path.join(basepath,fname) + + namefpath = os.path.join(basepath,fname) + + outfpath = os.path.join(basepath,fname)+u'.stpZ' + 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) + + + if os.path.exists(outfpath_stp): + sayzw("File cannot be compressed because a file with the same name exists '"+ outfpath_stp +"'") + QtGui.qApp.restoreOverrideCursor() + 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.qApp.restoreOverrideCursor() + reply = QtGui.QMessageBox.information(None,"info", "File cannot be compressed because\na file with the same name exists\n'"+ namefpath+ "'") + else: + with builtin.open(outfpath_stp, 'rb') as f_in: + file_content = f_in.read() + new_f_content = file_content + f_in.close() + with gzip_utf8.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) + +#### +