FreeCAD

Форк
0
/
stepZ.py 
182 строки · 4.8 Кб
1
# -*- coding: utf-8 -*-
2
# ****************************************************************************
3
# *  Copyright (c) 2018 Maurice <easyw@katamail.com>                         *
4
# *                                                                          *
5
# *  StepZ Import Export compressed STEP files for FreeCAD                   *
6
# *  License: LGPLv2+                                                        *
7
# *                                                                          *
8
# ****************************************************************************
9

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
13

14

15
import FreeCAD
16
import FreeCADGui
17
import shutil
18
import os
19
import re
20
import ImportGui
21
import PySide
22
from PySide import QtCore
23
from PySide import QtGui
24
import tempfile
25

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
30

31

32
import gzip as gz
33
import builtins
34
import importlib
35

36

37
import zipfile as zf
38

39
# import stepZ; import importlib; importlib.reload(stepZ); stepZ.open(u"C:/Temp/brick.stpz")
40

41

42
def mkz_string(input):
43
    if isinstance(input, str):
44
        return input
45
    else:
46
        input = input.encode("utf-8")
47
        return input
48

49

50
####
51
def mkz_unicode(input):
52
    if isinstance(input, str):
53
        return input
54
    else:
55
        input = input.decode("utf-8")
56
        return input
57

58

59
####
60
def sayz(msg):
61
    FreeCAD.Console.PrintMessage(msg)
62
    FreeCAD.Console.PrintMessage("\n")
63

64

65
####
66
def sayzw(msg):
67
    FreeCAD.Console.PrintWarning(msg)
68
    FreeCAD.Console.PrintWarning("\n")
69

70

71
####
72
def sayzerr(msg):
73
    FreeCAD.Console.PrintError(msg)
74
    FreeCAD.Console.PrintWarning("\n")
75

76

77
####
78

79

80
def import_stpz(fn, fc, doc):
81

82
    # sayz(fn)
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")
87

88
    tempdir = tempfile.gettempdir()  # get the current temporary directory
89
    tempfilepath = os.path.join(tempdir, fname + ".stp")
90

91
    with builtins.open(tempfilepath, "wb") as f:  # py3
92
        f.write(fc)
93
    # ImportGui.insert(filepath)
94
    if doc is None:
95
        ImportGui.open(tempfilepath)
96
    else:
97
        ImportGui.open(tempfilepath, doc.Name)
98
    FreeCADGui.SendMsgToActiveView("ViewFit")
99
    try:
100
        os.remove(tempfilepath)
101
    except OSError:
102
        sayzerr("error on removing " + tempfilepath + " file")
103

104

105
###
106

107

108
def open(filename, doc=None):
109

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:
114
                sayz(fn)
115
                with fz.open(fn) as zfile:
116
                    file_content = zfile.read()
117
                    import_stpz(filename, file_content, doc)
118
    else:
119
        with gz.open(filename, "rb") as f:
120
            fnm = os.path.splitext(os.path.basename(filename))[0]
121
            sayz(fnm)
122
            file_content = f.read()
123
            import_stpz(filename, file_content, doc)
124

125

126
####
127

128

129
def insert(filename, doc):
130

131
    doc = FreeCAD.ActiveDocument
132
    open(filename, doc)
133

134

135
####
136

137

138
def export(objs, filename):
139
    """exporting to file folder"""
140

141
    # sayz(filename)
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
147

148
    filepath = os.path.join(basepath, fname) + ".stp"
149
    filepath_base = os.path.join(basepath, fname)
150

151
    namefpath = os.path.join(basepath, fname)
152

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"
157

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"
162

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
170
        f_in.close()
171
    with gz.open(outfpathT_str, "wb") as f_out:
172
        f_out.write(new_f_content)
173
        f_out.close()
174
    if os.path.exists(outfpath):
175
        shutil.move(outfpathT_str, outfpath)
176
        # os.remove(outfpathT_stp)
177
    else:
178
        shutil.move(outfpathT_str, outfpath)
179
        # os.remove(outfpathT_stp)
180

181

182
####
183

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.