def _calculate_bb_dimension(shape, tol=1e-6, triangulate=False, triangulate_tol=1e-1): """ Calculate dimensions (minima and maxima) of a box bounding the :param TopoDS_Shape shape: or a subclass such as TopoDS_Face the shape to compute the bounding box from :param float tol: tolerance of the computed bounding box :param bool triangulate: Should shape be triangulated before the boudning box is created. If ``True`` only the dimensions of the bb will take into account every part of the shape (also not *visible*) If ``False`` only the *visible* part is taken into account \*See :meth:`~params.FFDParameters.build_bounding_box` :param float triangulate_tol: tolerance of triangulation (size of created triangles) :return: coordinates of minima and maxima along XYZ :rtype: tuple """ bbox = Bnd_Box() bbox.SetGap(tol) if triangulate: BRepMesh_IncrementalMesh(shape, triangulate_tol) brepbndlib_Add(shape, bbox, triangulate) xmin, ymin, zmin, xmax, ymax, zmax = bbox.Get() xyz_min = np.array([xmin, ymin, zmin]) xyz_max = np.array([xmax, ymax, zmax]) return xyz_min, xyz_max
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, 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 build_bounding_box(self, shape, tol=1e-6, triangulate=False, triangulate_tol=1e-1): """ Builds a bounding box around the given shape. All parameters are set to match the computed box, the deformed FFD points are reset. :param shape: the shape to compute the bounding box. :type shape: TopoDS_Shape or its subclass :param float tol: tolerance of the computed bounding box. :param bool triangulate: if True, shape is triangulated before the bouning box creation. :param float triangulate_tol: tolerance of triangulation (size of created triangles). .. note:: Every UV-Surface has to be rectangular. When a solid is created surfaces are trimmed. The trimmed part, however, is still saved inside a file. It is just *invisible* when drawn in a program. """ bbox = Bnd_Box() bbox.SetGap(tol) if triangulate: BRepMesh_IncrementalMesh(shape, triangulate_tol) brepbndlib_Add(shape, bbox, triangulate) xmin, ymin, zmin, xmax, ymax, zmax = bbox.Get() min_xyz = np.array([xmin, ymin, zmin]) max_xyz = np.array([xmax, ymax, zmax]) self.origin_box = min_xyz self.lenght_box = max_xyz - min_xyz self.reset_deformation()
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 bbx(shape: TopoDS_Shape, tol=TOLERANCE): bbox = Bnd_Box() bbox.SetGap(tol) brepbndlib_Add(shape, bbox) # print(bbox.IsVoid()) if bbox.IsVoid() is True: return None return bbox
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 _fromTopoDS(cls, shape, tol=TOL, optimal=False): ''' Constructs a bounnding box from a TopoDS_Shape ''' 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_boundingbox(shape: TopoDS_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) # print(bbox.IsVoid()) if bbox.IsVoid() is True: return None # xmin, ymin, zmin, xmax, ymax, zmax = bbox.Get() cmin = bbox.CornerMin() cmax = bbox.CornerMax() xmin, ymin, zmin = cmin.X(), cmin.Y(), cmin.Z() xmax, ymax, zmax = cmax.X(), cmax.Y(), cmax.Z() return xmin, ymin, zmin, xmax, ymax, zmax
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)
class BoundingBox(AbstractBoundingBox): r"""Wrapper class for a bounding box Notes ----- Mesh the shape before instantiating a BoundingBox if required, infinite recursion would be created by calling mesh.py's mesh() method """ def __init__(self, shape, tol=OCCUTILS_DEFAULT_TOLERANCE): if isinstance(shape, TopoDS_Shape) or issubclass(shape.__class__, TopoDS_Shape): self._shape = shape else: msg = "Expecting a TopoDS_Shape (or a subclass), " \ "got a %s" % str(shape.__class__) logger.error(msg) raise WrongTopologicalType(msg) # self._shape = shape self._tol = tol self._bbox = Bnd_Box() self._bbox.SetGap(tol) brepbndlib_Add(self._shape, self._bbox) self._x_min, self._y_min, self._z_min, self._x_max, self._y_max, self._z_max = self._bbox.Get() @property def x_min(self): r"""Minimum x""" return self._x_min @property def x_max(self): r"""Maximum x""" return self._x_max @property def y_min(self): r"""Minimum y""" return self._y_min @property def y_max(self): r"""Maximum y""" return self._y_max @property def z_min(self): r"""Minimum z""" return self._z_min @property def z_max(self): r"""Maximum z""" return self._z_max @property def bnd_box(self): r"""The OCC bounding box object Returns ------- OCC.Bnd.Bnd_Box """ return self._bbox @property def x_span(self): r"""x dimension of bounding box""" return self.x_max - self.x_min @property def y_span(self): r"""y dimension of bounding box""" return self.y_max - self.y_min @property def z_span(self): r"""z dimension of bounding box""" return self.z_max - self.z_min @property def max_dimension(self): r"""Maximum dimension""" return max([self.x_span, self.y_span, self.z_span]) @property def min_dimension(self): r"""Minimum dimension""" return min([self.x_span, self.y_span, self.z_span]) @property def aspect_ratio(self): r"""Aspect ratio""" return self.max_dimension / self.min_dimension @property def as_tuple(self): r"""bounding box as the original tuple""" return (self.x_min, self.y_min, self.z_min, self.x_max, self.y_max, self.z_max) @property def centre(self): r"""Centre of the bounding box Returns ------- gp_Pnt """ return Point.midpoint(gp_Pnt(self.x_min, self.y_min, self.z_min), gp_Pnt(self.x_max, self.y_max, self.z_max))
def __init__(self, file_path, id=None): self.file = file_path.encode("utf-8") self.id = id self.shapes_simples = [] self.main_product = None basename = os.path.basename(self.file) self.fileName = os.path.splitext(basename)[0] self.STEPReader = STEPCAFControl_Reader() if self.STEPReader.ReadFile(self.file) != 1: raise OCCReadingStepError self.h_doc = TDocStd.Handle_TDocStd_Document() self.app = XCAFApp.GetApplication().GetObject() self.app.NewDocument(TCollection_ExtendedString("MDTV-XCAF"), self.h_doc) self.STEPReader.Transfer(self.h_doc) self.doc = self.h_doc.GetObject() self.h_shape_tool = XCAFDoc.XCAFDoc_DocumentTool_ShapeTool( self.doc.Main()) self.h_colors_tool = XCAFDoc.XCAFDoc_DocumentTool_ColorTool( self.doc.Main()) self.shape_tool = self.h_shape_tool.GetObject() self.color_tool = self.h_colors_tool.GetObject() self.shapes = TDF_LabelSequence() self.shape_tool.GetShapes(self.shapes) for i in range(self.shapes.Length()): shape = self.shapes.Value(i + 1) if self.shape_tool.IsSimpleShape(shape): compShape = self.shape_tool.GetShape(shape) t = Topo(compShape) if t.number_of_vertices() > 0: label = get_label_name(shape) color = find_color(shape, self.color_tool, self.shape_tool) self.shapes_simples.append( SimpleShape(label, compShape, color)) roots = TDF_LabelSequence() self.shape_tool.GetFreeShapes(roots) self.thumbnail_valid = False if roots.Length() == 1: shape = self.shape_tool.GetShape(roots.Value(1)) t = Topo(shape) if t.number_of_vertices() > 0: bbox = Bnd_Box() gap = 0 bbox.SetGap(gap) BRepMesh_Mesh(shape, get_mesh_precision(shape, 1)) faces_iterator = Topo(shape).faces() for F in faces_iterator: face_location = TopLoc_Location() BRep_Tool_Triangulation(F, face_location) BRepBndLib_Add(shape, bbox) x_min, y_min, z_min, x_max, y_max, z_max = bbox.Get() diagonal = max(x_max - x_min, y_max - y_min, z_max - z_min) if diagonal > 0: self.scale = 200. / diagonal self.trans = ((x_max - x_min) / 2. - x_max, (y_max - y_min) / 2. - y_max, (z_max - z_min) / 2. - z_max) self.thumbnail_valid = True ws = self.STEPReader.Reader().WS().GetObject() model = ws.Model().GetObject() model.Clear()