由于笔者是初学者 经过几天的折腾,终于把 step&stp格式转化成stl 并使用py2exe打包成 exe文件,
以下是代码:
#!/usr/bin/python
# -*- coding: utf-8 -*-
# ============================
# @Author : TwoBrother Zhu
# @Time : 2018/7/29 19:32
# @File : trans
# @Software: PyCharm
# ============================
import sys
sys.path.append('C:\\Software\\FreeCAD 0.17\\lib')
import FreeCAD
import math
import Part # 此模块在freeCAD中,虽然在pycharm中有红色波浪线,仍然可以使用
import Mesh # 此模块需要在pycharm安装
import logging
import time
import os
# 第一步 创建一个logger
logger = logging.getLogger()
logger.setLevel(logging.DEBUG) # 等级总开关
# 第二步 创建一个handler 用于写入日志文件
rq = time.strftime('%Y%m%d%H', time.localtime(time.time()))
log_path = "./logs/"
if not os.path.exists(log_path):
os.mkdir(log_path)
log_name = log_path + rq + '.log'
logfile = log_name
fh = logging.FileHandler(logfile, mode='a')
fh.setLevel(logging.DEBUG) # 输出到file的log等级的开关
# 第三步 定义handler的输出格式
formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
fh.setFormatter(formatter)
# 第四步,将logger添加到handler里面
logger.addHandler(fh)
class Transform(object):
@classmethod
def trans_file(self):
try:
start_files, out_path = self.trans(path)
for start_file in start_files:
name = os.path.basename(start_file)
end_file = out_path + '\\{}.stl'.format(name)
shape = Part.Shape()
shape.read(start_file)
mesh = Mesh.Mesh()
mesh.addFacets(shape.tessellate(0.01))
mesh.write(end_file)
except Exception as e:
logger.error(e)
@classmethod
def trans(self, path):
try:
directory_path, out_path = path
file_paths = []
out_stls = []
file_list = os.walk(directory_path)
for (dir_path, dir_list, file_names) in file_list:
for file_name in file_names:
if file_name.endswith('STEP') or file_name.endswith('step') or file_name.endswith('stp') or file_name.endswith('STP'):
file_path = os.path.join(dir_path, file_name)
file_paths.append(file_path)
# print(file_path)
if not os.path.exists(out_path):
os.mkdir(out_path)
out_stl = out_path + file_name + '.stl'
out_stls.append(out_stl)
# print(out_stl)
return file_paths, out_path
except Exception as e:
logger.error(e)
if __name__ == '__main__':
path = ('D:\\work\\step', 'D:\\work\\stl')
Transform.trans(path)
Transform.trans_file()
以下是py2exe打包代码
#!/usr/bin/python
# -*- coding: utf-8 -*-
# ============================
# @Author : TwoBrother Zhu
# @Time : 2018/7/29 22:32
# @File : trans
# @Software: PyCharm
# ============================
FREECADPATH = 'C:\\Software\\FreeCAD 0.17\\lib'
import sys
sys.path.append(FREECADPATH)
import py2exe
from distutils.core import setup
import FreeCAD
import Part
import Mesh
setup(
console=["C:\\Users\\TwoBrother\\projects\\spider\\day16\\trans.py"],
options={
'py2exe': {
"dll_excludes": [
"MSVCP90.dll","libzmq.pyd","geos_c.dll","api-ms-win-core-string-l1-1-0.dll",
"api-ms-win-core-rtlsupport-l1-2-0.dll","api-ms-win-core-registry-l1-1-0.dll",
"api-ms-win-core-errorhandling-l1-1-1.dll","api-ms-win-core-string-l2-1-0.dll",
"api-ms-win-core-profile-l1-1-0.dll","api-ms-win*.dll",
"api-ms-win-core-processthreads-l1-1-2.dll","api-ms-win-core-libraryloader-l1-2-1.dll",
"api-ms-win-core-file-l1-2-1.dll","api-ms-win-security-base-l1-2-0.dll",
"api-ms-win-eventing-provider-l1-1-0.dll","api-ms-win-core-heap-l2-1-0.dll",
"api-ms-win-core-libraryloader-l1-2-0.dll","api-ms-win-core-localization-l1-2-1.dll",
"api-ms-win-core-sysinfo-l1-2-1.dll","api-ms-win-core-synch-l1-2-0.dll",
"api-ms-win-core-heap-l1-2-0.dll","api-ms-win-core-handle-l1-1-0.dll",
"api-ms-win-core-io-l1-1-1.dll","api-ms-win-core-com-l1-1-1.dll",
"api-ms-win-core-memory-l1-1-2.dll","api-ms-win-core-version-l1-1-1.dll",
"api-ms-win-core-version-l1-1-0.dll", 'api-ms-win-core-rtlsupport-l1-1-0.dll',
'api-ms-win-core-processthreads-l1-1-0.dll', 'api-ms-win-core-errorhandling-l1-1-0.dll',
'api-ms-win-core-sysinfo-l1-1-0.dll'
]
}
}
)