python - 从 STEP 文件中提取体积
问题描述
我的目标是编写一个 Python 程序来提取文件中对象的体积STEP
。我能够发现steputils和aoxchange是 Python 中存在的两个库,但它们似乎都没有包含足够的关于从文件中提取卷/属性的文档。是否有任何文件可以解释这一点?我为文件尝试了一个类似的用例,STL
并且能够使用numpy-stl成功实现它。我正在寻找类似 numpy-stl 的STEP
文件。下面是我如何为STL
文件实现它的示例代码。
import numpy
from stl import mesh
your_mesh = mesh.Mesh.from_file('/path/to/myfile.stl')
volume, cog, inertia = your_mesh.get_mass_properties()
print("Volume = {0}".format(volume))
解决方案
编辑以考虑 gkv311 的建议:pythonOCC
可用于直接计算音量。
from OCC.Core.GProp import GProp_GProps
from OCC.Core.BRepGProp import brepgprop_VolumeProperties
from OCC.Extend.DataExchange import read_step_file
my_shape = read_step_file(path_to_file)
prop = GProp_GProps()
tolerance = 1e-5 # Adjust to your liking
volume = brepgprop_VolumeProperties(myshape, prop, tolerance)
print(volume)
旧版本,STEP
用于STL
转换。
绝对不是最优雅的解决方案,但它完成了工作:使用Pythonocc(库 aoxchange 所基于),您可以将STEP
文件转换为STL
,然后使用您问题中的解决方案来计算STL
的体积。
from OCC.Core.STEPControl import STEPControl_Reader
from OCC.Core.StlAPI import StlAPI_Writer
input_file = 'myshape.stp'
output_file = 'myshape.stl'
# Load STEP file
step_reader = STEPControl_Reader()
step_reader.ReadFile( input_file )
step_reader.TransferRoot()
myshape = step_reader.Shape()
print("File loaded")
# Export to STL
stl_writer = StlAPI_Writer()
stl_writer.SetASCIIMode(True)
stl_writer.Write(myshape, output_file)
print("Done")
推荐阅读
- sql - PostgreSQL 查询更新返回 UPDATE 0
- sql - 在 PL/SQL 过程中创建数据库表
- bash - 使用 FFMPEG 递归扫描和识别视频文件
- pandas - Plotly 基值以百分比表示
- r - 在ggforce中绘制圆圈的顺序
- python-3.x - discord.py 我得到错误'NoneType'对象没有属性'发送'
- transactions - 如何使用 Python 与币安智能链合约进行交互
- r - 从列名创建一个字符(在 R 中)
- sql - 将sql查询转换为EF
- spring - 如何使用 Spring 嵌入式 ActiveMQ 代理指定自定义 activemq.xml?