我的目标是编写一个Python程序,该程序提取STEP文件中对象的卷。我发现尖柄和恶变是Python中的两个库,但它们似乎都没有包含足够的文档来从文件中提取卷/属性。是否有任何可用的文件可以解释这一点?我为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))发布于 2021-04-18 21:02:46
编辑,以考虑gkv311 311的建议: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 转换.
当然,不是最优雅的解决方案,但它完成了任务:使用毕多诺克 (库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")https://stackoverflow.com/questions/66929762
复制相似问题