Wrong BoundingBox dimension extraction pythonocc

447 views Asked by At

I am trying to extract box dimensions from the STP file and it's worked for a some of the samples, but unfortunately, I got the wrong extraction for the other samples for example below zipped STP file

https://github.com/tpaviot/pythonocc-demos/files/5272793/Test.zip

I got this results value for "x": 6.802000200000001 but the right value is 6.24, and so on for y and z values.

and here is my code

from future import print_function

from OCC.Extend.DataExchange import read_step_file
from OCC.Core.IFSelect import IFSelect_RetDone
from OCC.Core.Bnd import Bnd_Box
from OCC.Core.BRepBndLib import brepbndlib_Add
from OCC.Core.BRepMesh import BRepMesh_IncrementalMesh
from OCC.Core.STEPControl import STEPControl_Reader

shapes = read_step_file('path/to/stpfile')

def read_stp_file(file_path):
step_reader = STEPControl_Reader()
status = step_reader.ReadFile(file_path)
if status == IFSelect_RetDone:
fails_only = False
step_reader.TransferRoots()
shape = step_reader.Shape(1)
return shape
else:
print("Error: can't read file.")

bbox = Bnd_Box()

use_mesh = True
mesh = BRepMesh_IncrementalMesh()
mesh.SetParallelDefault(True)
mesh.SetShape(shapes)
mesh.Perform()
assert mesh.IsDone()
brepbndlib_Add(shapes, bbox, use_mesh)
xmin, ymin, zmin, xmax, ymax, zmax = bbox.Get()
print('x value : >>>> ', xmax - xmin)
1

There are 1 answers

0
CodeSalad On

I just ran into the same problem.

I think the reason is, as the documentation of Get also states, that some kind of gap is included.

You can also see it in the code here.

It seems the gap is being added by Enlarge in this function.

As the gap is simply added around the bounding box, you can just get it via GetGap() and remove it again from all sides. Or you use SetGap to set it to 0.

I personally have no idea why this gap is being added, but the docs of BRepBndLib::Add even say so: The resulting bounding box may be somewhat larger than the object.