def get_aligned_boundingbox(shape, tol=1e-6, optimal_BB=True): """return the bounding box of the TopoDS_Shape `shape` Parameters ---------- shape : TopoDS_Shape or a subclass such as TopoDS_Face the shape to compute the bounding box from tol: float tolerance of the computed boundingbox use_triangulation : bool, True by default This makes the computation more accurate Returns ------- if `as_pnt` is True, return a tuple of gp_Pnt instances for the lower and another for the upper X,Y,Z values representing the bounding box if `as_pnt` is False, return a tuple of lower and then upper X,Y,Z values representing the bounding box """ bbox = Bnd_Box() bbox.SetGap(tol) # note: useTriangulation is True by default, we set it explicitly, but t's not necessary if optimal_BB: use_triangulation = True use_shapetolerance = True brepbndlib_AddOptimal(shape, bbox, use_triangulation, use_shapetolerance) else: brepbndlib_Add(shape, bbox) xmin, ymin, zmin, xmax, ymax, zmax = bbox.Get() corner1 = gp_Pnt(xmin, ymin, zmin) corner2 = gp_Pnt(xmax, ymax, zmax) center = midpoint(corner1, corner2) dx = xmax - xmin dy = ymax - ymin dz = zmax - zmin box_shp = BRepPrimAPI_MakeBox(corner1, corner2).Shape() return center, [dx, dy, dz], box_shp
def get_aligned_boundingbox_ratio(shape, tol=1e-6, optimal_BB=True, ratio=1): """ return the bounding box of the TopoDS_Shape `shape` Parameters ---------- shape : TopoDS_Shape or a subclass such as TopoDS_Face the shape to compute the bounding box from tol: float tolerance of the computed boundingbox use_triangulation : bool, True by default This makes the computation more accurate ratio : float, 1.0 by default. Returns ------- if `as_pnt` is True, return a tuple of gp_Pnt instances for the lower and another for the upper X,Y,Z values representing the bounding box if `as_pnt` is False, return a tuple of lower and then upper X,Y,Z values representing the bounding box """ bbox = Bnd_Box() bbox.SetGap(tol) # note: useTriangulation is True by default, we set it explicitely, but t's not necessary if optimal_BB: use_triangulation = True use_shapetolerance = True brepbndlib_AddOptimal(shape, bbox, use_triangulation, use_shapetolerance) else: brepbndlib_Add(shape, bbox) xmin, ymin, zmin, xmax, ymax, zmax = bbox.Get() dx, mx = (xmax - xmin) * ratio, (xmax + xmin) / 2 dy, my = (ymax - ymin) * ratio, (ymax + ymin) / 2 dz, mz = (zmax - zmin) * ratio, (zmax + zmin) / 2 x0, x1 = mx - dx / 2, mx + dx / 2 y0, y1 = my - dy / 2, my + dy / 2 z0, z1 = mz - dz / 2, mz + dz / 2 corner1 = gp_Pnt(x0, y0, z0) corner2 = gp_Pnt(x1, y1, z1) center = midpoint(corner1, corner2) rim0 = make_polygon([ gp_Pnt(x0, y0, z0), gp_Pnt(x1, y0, z0), gp_Pnt(x1, y1, z0), gp_Pnt(x0, y1, z0) ], closed=True) rim1 = make_polygon([ gp_Pnt(x0, y0, z1), gp_Pnt(x1, y0, z1), gp_Pnt(x1, y1, z1), gp_Pnt(x0, y1, z1) ], closed=True) api = BRepOffsetAPI_ThruSections(True, False, 1.0E-9) api.AddWire(rim0) api.AddWire(rim1) box_shp = api.Shape() #box_shp = BRepPrimAPI_MakeBox(corner1, corner2).Shape() return center, [dx, dy, dz], box_shp