vis*_*l.k 5 python step python-3.x
我的目标是编写一个 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))
Run Code Online (Sandbox Code Playgroud)
编辑考虑 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)
Run Code Online (Sandbox Code Playgroud)
旧版本,用于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")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7417 次 |
| 最近记录: |