错误的边界框标注提取pythonocc

2024-10-03 11:19:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图从STP文件中提取盒子尺寸,它对一些示例有效,但不幸的是,我对其他示例的提取错误,例如下面的压缩STP文件

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

我得到了“x”的结果值:6.80200000001 但正确的值是6.24,y和z值也是如此

这是我的密码

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)

Tags: fromcoreimportreadstepreaderfileprint
1条回答
网友
1楼 · 发布于 2024-10-03 11:19:57

我也遇到了同样的问题

我认为原因是,正如documentation of Get也指出的那样,包括了某种差距

您也可以在code here中看到它

这一差距似乎正在被扩大

由于只需在边界框周围添加间隙,您只需通过GetGap()获取间隙,然后从所有侧面再次移除间隙。或者使用SetGap将其设置为0

我个人不知道为什么会增加这个差距,但是BRepBndLib::Add的文档甚至这样说:The resulting bounding box may be somewhat larger than the object.

相关问题 更多 >