从 STEP 文件中提取体积

Extract volume from a STEP file

我的目标是编写一个 Python 程序来提取 STEP 文件中对象的体积。我能够找到 steputils and aoxchange are the two libraries present in Python but neither of them seemed to contain enough documentation about extracting the volume/properties from the file. Is there any document available that can explain this? I tried a similar use-case for STL files and was able to achieve it successfully using numpy-stl。我正在为 STEP 文件搜索类似 numpy-stl 的东西。下面是我如何为 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)

旧版本,使用STEPSTL转换。


绝对不是最优雅的解决方案,但它完成了工作:使用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")