def get_boundingbox(shape, tol=1e-6, use_mesh=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_mesh : bool a flag that tells whether or not the shape has first to be meshed before the bbox computation. This produces more accurate results """ bbox = Bnd_Box() bbox.SetGap(tol) if use_mesh: mesh = BRepMesh_IncrementalMesh() mesh.SetParallel(True) mesh.SetShape(shape) mesh.Perform() if not mesh.IsDone(): raise AssertionError("Mesh not done.") brepbndlib_Add(shape, bbox, use_mesh) xmin, ymin, zmin, xmax, ymax, zmax = bbox.Get() return xmin, ymin, zmin, xmax, ymax, zmax, xmax - xmin, ymax - ymin, zmax - zmin
def get_boundingbox(shape, tol=1e-6, as_vec=False): """ 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 as_vec : bool wether to return the lower and upper point of the bounding box as gp_Vec instances Returns ------- if `as_vec` is True, return a tuple of gp_Vec instances for the lower and another for the upper X,Y,Z values representing the bounding box if `as_vec` is False, return a tuple of lower and then upper X,Y,Z values representing the bounding box """ bbox = Bnd_Box() bbox.SetGap(tol) brepbndlib_Add(shape, bbox) xmin, ymin, zmin, xmax, ymax, zmax = bbox.Get() if as_vec is False: return xmin, ymin, zmin, xmax, ymax, zmax else: return gp_Vec(xmin, ymin, zmin), gp_Vec(xmax, ymax, zmax)
def get_boundingbox(shape: TopoDS_Shape, tol=1e-6, use_mesh=True) -> Tuple[tuple, tuple]: """ :param shape: TopoDS_Shape or a subclass such as TopoDS_Face the shape to compute the bounding box from :param tol: tolerance of the computed boundingbox :param use_mesh: a flag that tells whether or not the shape has first to be meshed before the bbox computation. This produces more accurate results :return: return the bounding box of the TopoDS_Shape `shape` """ bbox = Bnd_Box() bbox.SetGap(tol) if use_mesh: mesh = BRepMesh_IncrementalMesh() mesh.SetParallelDefault(True) mesh.SetShape(shape) mesh.Perform() if not mesh.IsDone(): raise AssertionError("Mesh not done.") brepbndlib_Add(shape, bbox, use_mesh) xmin, ymin, zmin, xmax, ymax, zmax = bbox.Get() return (xmin, ymin, zmin), (xmax, ymax, zmax)
def _bounding_box(self, obj, tol=1e-5): bbox = Bnd_Box() bbox.SetGap(self.tol) brepbndlib_Add(obj, bbox, True) values = bbox.Get() return (values[0], values[3], values[1], values[4], values[2], values[5])
def __init__(self, shape_or_values, tol=1.e-5): if isinstance(shape_or_values, tuple): self.values = shape_or_values else: bbox = Bnd_Box() bbox.SetGap(tol) brepbndlib_Add(shape_or_values, bbox, True) # use the shape triangulation self.values = bbox.Get()
def get_boundingbox(shape, tol=TOLERANCE): ''' :param shape: TopoDS_Shape such as TopoDS_Face :param tol: tolerance :return: xmin, ymin, zmin, xmax, ymax, zmax ''' bbox = Bnd_Box() bbox.SetGap(tol) brepbndlib_Add(shape, bbox) xmin, ymin, zmin, xmax, ymax, zmax = bbox.Get() return xmin, ymin, zmin, xmax, ymax, zmax
def point_in_boundingbox(solid, pnt, tolerance=1e-5): """returns True if *pnt* lies in *boundingbox*, False if not this is a much speedier test than checking the TopoDS_Solid Args: solid TopoDS_Solid pnt: gp_Pnt Returns: bool """ bbox = Bnd_Box() bbox.SetGap(tolerance) brepbndlib_Add(solid, bbox) return not bbox.IsOut(pnt)
def get_boundingbox(shape: TopoDS_Shape, tol=1e-6, use_mesh=True): bbox = Bnd_Box() bbox.SetGap(tol) if use_mesh: mesh = BRepMesh_IncrementalMesh() mesh.SetParallelDefault(True) mesh.SetShape(shape) mesh.Perform() if not mesh.IsDone(): raise AssertionError("Mesh not done.") brepbndlib_Add(shape, bbox, use_mesh) xmin, ymin, zmin, xmax, ymax, zmax = bbox.Get() return xmin, ymin, zmin, xmax, ymax, zmax, xmax - xmin, ymax - ymin, zmax - zmin
def z_max_finder(stl_shp,tol=1e-6,use_mesh=True): """first change the model to mesh form in order to get an accurate MAX and Min bounfing box from topology""" bbox = Bnd_Box() bbox.SetGap(tol) if use_mesh: mesh = BRepMesh_IncrementalMesh() mesh.SetParallelDefault(True) mesh.SetShape(stl_shp) mesh.Perform() if not mesh.IsDone(): raise AssertionError("Mesh not done.") brepbndlib_Add(stl_shp, bbox, use_mesh) xmin, ymin, zmin, xmax, ymax, zmax = bbox.Get() return zmax
def get_boundingbox(shape, tol=1e-6, use_mesh=True): bbox = Bnd_Box() bbox.SetGap(tol) if use_mesh: mesh = BRepMesh_IncrementalMesh() mesh.SetParallel(True) mesh.SetShape(shape) mesh.Perform() assert mesh.IsDone() brepbndlib_Add(shape, bbox, use_mesh) xmin, ymin, zmin, xmax, ymax, zmax = bbox.Get() return [ xmin, ymin, zmin, xmax, ymax, zmax, xmax - xmin, ymax - ymin, zmax - zmin ]
def _fromTopoDS(cls, shape, tol=None, optimal=False): ''' Constructs a bounding box from a TopoDS_Shape ''' tol = TOL if tol is None else tol # tol = TOL (by default) bbox = Bnd_Box() bbox.SetGap(tol) if optimal: raise NotImplementedError # brepbndlib_AddOptimal(shape, bbox) #this is 'exact' but expensive - not yet wrapped by PythonOCC else: mesh = BRepMesh_IncrementalMesh(shape, TOL, True) mesh.Perform() # this is adds +margin but is faster brepbndlib_Add(shape, bbox, True) return cls(bbox)
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 __init__(self, name, dict_of_shells, solid): from OCC.Core.Bnd import Bnd_Box from OCC.Core.BRepBndLib import brepbndlib_Add from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox bbox = Bnd_Box() bbox.SetGap(1.e-6) self.name = name self.solid = solid self.dico = {} self.dicosplit = {} self.kept = [] self.to_check = [] self.splitsolid = [] brepbndlib_Add(solid, bbox, False) xmin, ymin, zmin, xmax, ymax, zmax = bbox.Get() self.box = BRepPrimAPI_MakeBox(gp_Pnt(xmin, ymin, zmin), gp_Pnt(xmax, ymax, zmax)) for k in dict_of_shells: new_k = name + k self.dico[k] = dict_of_shells[k] self.dicosplit[k] = None
def add(self, obj, tol=1e-8): """Returns a modified (expanded) bounding box obj can be one of several things: 1. a 3-tuple corresponding to x,y, and z amounts to add 2. a vector, containing the x,y,z values to add 3. another bounding box, where a new box will be created that encloses both. This bounding box is not changed. """ tmp = Bnd_Box() tmp.SetGap(tol) tmp.Add(self.wrapped) if isinstance(obj, tuple): tmp.Update(*obj) elif isinstance(obj, Vector): tmp.Update(*obj.toTuple()) elif isinstance(obj, BoundBox): tmp.Add(obj.wrapped) return BoundBox(tmp)
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