3

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))
vishal
  • 1,646
  • 5
  • 28
  • 56
  • 2
    @itprorh66 This question seems specific enough that there might be a *unique* (or at least "canonical") tool suited for this task. If questions about how to find the length of a list in Python can get hundreds of upvotes, then for consistency, SO should tolerate questions about how to achieve more specific tasks like "extracting the volume of the object in a `STEP` file." To this end, I've upvoted the question. – Asker Apr 03 '21 at 14:58
  • 2
    @itprorh66 Minimum reproducible example added. Question also edited with better grammar. – vishal Apr 03 '21 at 15:18
  • 1
    Not python, but an algorithm and a paper to check out if you want to form a function https://stackoverflow.com/questions/1406029/how-to-calculate-the-volume-of-a-3d-mesh-object-the-surface-of-which-is-made-up – Tim Jim Apr 17 '21 at 11:05
  • Found a repo where they seem to be using pythonocc to convert the step file to stl, and then computing the volume: [https://github.com/jdlubrano/cad_volume](https://github.com/jdlubrano/cad_volume) – Seon Apr 18 '21 at 18:18
  • 1
    Note that STEP file may define "validation" properties - e.g. volume/surface values encoded directly in the STEP file. Computing volume from geometry anew and extracting this information from STEP file itself is not the same thing (though values should be the same or close to each other in case of a valid STEP file and fine STEP geometry import without issues). – gkv311 Apr 19 '21 at 10:59

1 Answers1

4

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")
Seon
  • 3,332
  • 8
  • 27
  • 1
    This is indeed a solution. Prior to this question, I have already looked at the link you posted. I'm having troubles installing pythonOcc itself. Can you share any latest link that you followed to get pythonOcc installed? – vishal Apr 19 '21 at 10:38
  • 2
    You may further try extending the answer and compute the volume via `BRepGProp` tool from Open CASCADE Technology itself. `bgprop = BRepGProp()| prop = GProp_GProps()| err = bgprop.VolumeProperties(myshape, prop)| volume = prop.Mass()` – gkv311 Apr 19 '21 at 10:53
  • 1
    I used [this binder](https://github.com/tpaviot/pythonocc-binderhub) to do the experiments as I don't currently have it installed on my station. The [conda install](https://anaconda.org/pythonocc/pythonocc-core) did work for me a few months ago, otherwise there are instructions on how to compile from source [here](https://github.com/tpaviot/pythonocc-core/blob/master/INSTALL.md). – Seon Apr 19 '21 at 11:17
  • I didn't know about that tool @gkv311, thanks! Edited the answer to add this much more more elegant solution. – Seon Apr 19 '21 at 11:37
  • @Seon Okay will wait for two more days to see if anyone comes with a better solution. If not the bounty shall be yours as this one is a valid canonical solution. – vishal Apr 19 '21 at 12:24