背景:做配载仪时需要批量的将IGS模型转换成STL模型
用到的方法:
- 方法1:使用3D-tool或者其他工具进行转换
- 方法2:通过python使用FreeCAD的接口批量转换(当时师弟写的程序)
FreeCAD
FreeCAD是一个基于OpenCASCADE的开源CAD/CAE工具,FreeCAD 是多平台的,可用于 Windows、Mac OS X+ 以及 Linux。
https://www.freecadweb.org/www.freecadweb.orgYour own 3D parametric modeler
https://www.freecadweb.org/www.freecadweb.org测试平台:windows
步骤:
- 安装FreeCAD
- 查看FreeCAD对应python版本号(x86 还是 x64),然后下载安装对应python版本
- 写python程序
1.安装FreeCAD比较简单,直接官网下载exe即可
2.查看python版本
安装完成后,默认不显示python脚本
通过View->Panels->python console打开python控制台
python控制台还可以学习freeCAD 的pythonAPI
比如我通过菜单打开了一个igs文件,然后导出stl 控制台中给出了相关的提示
3. 安装python
4. 写python脚本
使用VS code打开igs2stl.py文件,编辑代码
引入FreeCAD 包
import FreeCAD
运行,如果成功的话显示版本号:
5.完善Python脚本,打开igs文件所在的文件夹然后开始批量转换STL
代码如下:
# -*- coding: utf-8 -*-
#precision=input("Please input precision. default:40 :n")
precision=40
if not precision:
precision=40
else:
precision=float(precision)
import sys
#sys.path.append('win32')
import FreeCAD,Part,os
import Mesh
import tkinter as tk
from tkinter import filedialog
folder=filedialog.askdirectory()
# first we check for filenames that contain .stp, .step, .igs or .iges
cadfiles = []
for f in os.listdir(folder):
for ext in [".stp", ".step", ".igs", ".iges"]:
if f.lower().endswith(ext):
cadfiles.append((folder+os.sep+f,ext))
break
if not cadfiles:
print ("No step or iges files found in the given folder")
sys.exit()
for f,ext in cadfiles:
#f=f.decode('gbk')
s = Part.read(f)
stl = f.replace(ext,'.stl')
stl=stl.encode('utf-8')
m=Mesh.Mesh()
m.addFacets(s.tessellate(precision))
mat=FreeCAD.Matrix()
mat.scale(1/1000.0,1/1000.0,1/1000.0)
m.transform(mat)
m.write(stl)
print("finished")
运行结果:
6.善后工作
转换我们用到Mesh和Part模块,所以可以删除一些不必要的模块及DLL,不然整个包特别打
拷贝整个安装目录到自己的文件夹,一遍删除dll一遍运行,如果出错就后退
清理前大概1.65G 删除无关模块后,大概100M
以Mod文件夹为例
删除后就剩2个文件夹
7.现在程序可以独立运行了,不再依赖于FreeCAD的安装包,可以用python做任何想做的事,然后发布成exe。深藏功与名
注意:
- python版本要和FreeCAD的对应
- 如果转换后的STL模型有问题,可以用Netfabb软件进行修复,支持批量修复及导出,导出后文件名会统一添加(repaired),写个python脚本批量重命名即可,见参考目录
- 使用FreeCAD批量处理省力多了,感谢小师弟,以前我都是一个个保存,经常100+文件 眼泪 TT
- 本文是以IGS转STL模型为例,同样支持其他类型,大家可以按需修改,如下图所示
参考:
开源免费freeCAD:
https://wiki.freecadweb.org/Feature_listwiki.freecadweb.org免费的3d-tool viewer:
3D und 2D CAD Viewer und 3D-Konverterwww.3d-tool.de其他的一些转换软件:
The Best STL File Converter Tools | All3DPall3dp.compython脚本批量重命名:
刘春雷:python 批量重命名zhuanlan.zhihu.com