My aim is to write a Python program that extracts the volume of the object in a STEP
file. I was able to find that 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. I'm searching for something like numpy-stl for STEP
files. Below is a sample code of how I achieved it for STL
files.
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))
Edited to take gkv311's suggestion into account: pythonOCC
can be used to compute the volume directly.
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)
Old version, using STEP
to STL
conversion.
Definitely not the most elegant solution, but it gets the job done: using Pythonocc (the library aoxchange is based on), you can convert a STEP
file to STL
, then use the solution from your question to compute the STL
's volume.
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")