前言
传统建模领域,有各种各样的格式文件,相对于three.js来说不一定友好,后者推荐的是gltf格式文件,笔者人为即json+obj资源文件的格式,优点是更小,层次更分明,所以在这个需求基础上,一旦涉及到模型上云,这个转化工具必不可少,而传统行业中,建模工具在windows使用的频次更高,所以,开始吧
准备环境
目前能找到的解决方案
pythonocc ----> stl
gltf-pipeline --> gltf
- AnaConda 安装,python3.7 链接
- 安装nvm-windows,进行版本管理,这里需要node 12.0.0版本
nvm.exe install 12.0.0 nvm.exe use 12.0.0
- 安装pythonocc,
conda create -n pythonocct -c dlr-sc -c pythonocc pythonocc-core=7.4.0rc1
或者根据github中的conda install -c conda-forge pythonocc-core=7.4.0
,对应的demo 项目地址 demo,校验下是否执行成功cd examples;python core_helloworld.py;
执行之前pip install pyqt5
- https://github.com/MyMiniFactory/stl2gltf
- python stl2gltf.py out.stl out.glb -b(压缩后感觉文件有点问题)
转换方法
import os
from OCC.Extend.DataExchange import read_iges_file,read_step_file,write_stl_file
input_file = 'temp2.igs'
output_file = 'out2.stl'
if not os.path.exists(input_file):
print('Input file need exists')
exit()
shapes=read_iges_file(input_file)
write_stl_file(shapes, output_file, 'binary', 0.03, 0.5)
import os
from OCC.Extend.DataExchange import read_iges_file,read_step_file,write_stl_file
input_file = 'temp2.stp'
output_file = 'out.stl'
if not os.path.exists(input_file):
print('Input file need exists')
exit()
shapes=read_step_file(input_file)
write_stl_file(shapes, output_file, 'binary', 0.03, 0.5)
结尾感言
这个项目是之前接的一个私活,基本上全网找了下没有很合适的躺平办法,包括occ等所谓开源最终也是被人卡着脖子不了了之,最终选择了破解cadexchanger,实现命令行调用,惭愧惭愧