https://www.cnblogs.com/Sketch-liu/p/8065497.html
FreeCAD stp文件基于python脚本操作
FreeCAD对于3D模型处理这块的东西封装的还是很完善的,所以移植这块的东西还是有必要的!
首先下载FreeCAD编译好的库:
https://www.freecadweb.org/wiki/Download#FreeCAD_LibPack
打开,并解压,在bin中有相应的FreeCAD.exe工具,这里也有大量的python库,Q库等等;
lib文件夹里面也有大量的库文件;
这里说一下模型操作相应的python脚本的使用:
首先我通过一个stp模型转obj模型的例子来讲解,通常给我们一个stl的模型,我放在E盘下(文件的取名不要在意,我之前做测试忘记改名了):
由于bin文件存在python的库,我们可将该python.exe文件设置环境变量。或者可自己独立安装相应的库。
创建一个.py的脚本(为了方便我直接在lib文件夹里写了,这里没有位置的限定):
step_stl.py:
复制代码
#!/Applications/FreeCAD.app/Contents/MacOS/FreeCADCmd
FREECADPATH = 'E:/FreeCAD/FreeCAD/lib/' # path to your FreeCAD.so or FreeCAD.dll file
import sys
sys.path.append(FREECADPATH)
import math
import FreeCAD
import Part
import Mesh
import sys
in_fn, out_fn = sys.argv[1], sys.argv[2]
Type=sys.argv[3]
Part.open(in_fn)
strlist = in_fn.split('\\')
stllist=strlist[len(strlist)-1].split('.')
print(strlist[len(strlist)-1])
print(stllist[0])
print(len(FreeCAD.getDocument("Unnamed").Objects))
size=len(FreeCAD.getDocument("Unnamed").Objects)
for a in range(len(FreeCAD.getDocument("Unnamed").Objects)):
o = [ FreeCAD.getDocument("Unnamed").Objects[a] ]
out_model=out_fn+'\\'+stllist[0]+'_part'+str(a)+Type
Mesh.export(o, out_model)
out_advance=str(round((float(a+1)/size),4)*100)+'%'
print(out_advance)
需要注意的是需要使用的库文件的导入,用到什么库就导入什么(设置环境变量和在代码设置路径都是可行的)。我的库目录就是在代码设置的(FreeCAD.pyd 在bin文件夹内,可复制过去)。
这个python脚本是个命令行程序:python.exe step_stl.py(.py文件的路径) E:\1.stp(需要转换文件的路径) E:(保存文件的位置) .obj(保存文件的格式)
需要注意的是:这个程序是按文件的mesh进行输出,假如需要转换成单个模型,去官网或者博客查找相应的API.
我们都知道,这样操作借助的库多而且麻烦,所以打包成exe是必要的:
py2exe解决了这个问题:
这是py2exe下载链接(保证python版本一致):http://www.py2exe.org/
py2exe使用:http://www.cnblogs.com/jans2002/archive/2006/09/30/519393.html
import py2exe—————–pyt2exe是一个文件夹,将它的上级设置成环境变量就好
后序编译会遇到一些库文件依赖没找到的问题,”dll_excludes”是很好的处理手段(下面是我的.py文件):
FREECADPATH = 'E:/Python/Lib/site-packages' # path to your FreeCAD.so or FreeCAD.dll file
import sys
sys.path.append(FREECADPATH)
from distutils.core import setup
import py2exe
setup(console=["E:\FreeCAD\FreeCAD\lib\step_stl.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",<br>"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",<br>"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",<br>"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",<br>"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"]
}
})
编译那块py2exe有很详细的解释;
最后编辑:秦晓川 更新时间:2024-11-14 14:45