def transform_nonuniformal(brep, factors, vec=[0, 0, 0], copy=False): """Nonuniformly scale brep with respect to pnt by the x y z scaling factors provided in 'factors', and translate by vector 'vec' Parameters ---------- factors : List of factors [Fx, Fy, Fz] Scaling factors with respect to origin (0,0,0) vec : List of x,y,z or gp_Vec the translation vector (default is [0,0,0]) Notes ----- * Only tested on 3d shapes * Assumes factors are define with respect to the origin (0,0,0) """ assert (len(factors) == 3), \ ("factors should have [Fx, Fy, Fz] scaling factors: Found length ", len(factors)) M = np.diag(factors).flatten() trns_M = gp_Mat(*M) try: V = gp_XYZ(*vec) except NotImplementedError: V = gp_XYZ(vec.X(), vec.Y(), vec.Z()) trns = gp_GTrsf(trns_M, V) brep_trns = BRepBuilderAPI_GTransform(brep, trns, copy) brep_trns.Build() return brep_trns.Shape()
def transform_nonuniformal(brep, factors, vec=[0, 0, 0], copy=False): """Nonuniformly scale brep with respect to pnt by the x y z scaling factors provided in 'factors', and translate by vector 'vec' Parameters ---------- factors : List of factors [Fx, Fy, Fz] Scaling factors with respect to origin (0,0,0) vec : List of x,y,z or gp_Vec the translation vector (default is [0,0,0]) Notes ----- * Only tested on 3d shapes * Assumes factors are define with respect to the origin (0,0,0) """ assert(len(factors) == 3),\ ("factors should have [Fx, Fy, Fz] scaling factors: Found length ", len(factors)) M = np.diag(factors).flatten() trns_M = gp_Mat(*M) try: V = gp_XYZ(*vec) except NotImplementedError: V = gp_XYZ(vec.X(), vec.Y(), vec.Z()) trns = gp_GTrsf(trns_M, V) brep_trns = BRepBuilderAPI_GTransform(brep, trns, copy) brep_trns.Build() return brep_trns.Shape()
def aabb(shape, optimal_OBB=True): """ return the oriented 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 optimal_OBB : bool, True by default. If set to True, compute the optimal (i.e. the smallest oriented bounding box). Optimal OBB is a bit longer. Returns ------- a list with center, x, y and z sizes a shape """ obb = Bnd_OBB() if optimal_OBB: is_triangulationUsed = True is_optimal = True is_shapeToleranceUsed = False brepbndlib_AddOBB(shape, obb, is_triangulationUsed, is_optimal, is_shapeToleranceUsed) else: brepbndlib_AddOBB(shape, obb) # converts the bounding box to a shape aBaryCenter = obb.Center() aXDir = obb.XDirection() aYDir = obb.YDirection() aZDir = obb.ZDirection() aHalfX = obb.XHSize() aHalfY = obb.YHSize() aHalfZ = obb.ZHSize() ax = gp_XYZ(aXDir.X(), aXDir.Y(), aXDir.Z()) ay = gp_XYZ(aYDir.X(), aYDir.Y(), aYDir.Z()) az = gp_XYZ(aZDir.X(), aZDir.Y(), aZDir.Z()) p = gp_Pnt(aBaryCenter.X(), aBaryCenter.Y(), aBaryCenter.Z()) anAxes = gp_Ax2(p, gp_Dir(aZDir), gp_Dir(aXDir)) anAxes.SetLocation( gp_Pnt(p.XYZ() - ax * aHalfX - ay * aHalfY - az * aHalfZ)) aBox = BRepPrimAPI_MakeBox(anAxes, 2.0 * aHalfX, 2.0 * aHalfY, 2.0 * aHalfZ).Shape() return aBaryCenter, [aHalfX, aHalfY, aHalfZ], aBox
def test_bndBox(): # simple interference check through bounding box shp = read_step_file("/home/lenty/exchange/tempData/models/lf064-01.stp") brepbnd = brepbndlib() bndBox = Bnd_Box() # display = Display(shp, run_display=True) brepbnd.Add(shp, bndBox) p1 = gp_Pnt(55, 30, 7) print(bndBox.IsOut(p1)) b3f = Bnd_B3f() p2 = gp_Pnt(0, 0, 0) b3f.Add(p2) print(b3f.IsOut(gp_XYZ(0, 0, 1))) ipdb.set_trace() print(b3f.IsOut(gp_Pnt(0, 0, 1)))
def vstep(step_str): step = int(step_str) positions = dpos_data[nbobjs*step:nbobjs*step+nbobjs, 2:] builder = BRep_Builder() comp = TopoDS_Compound() builder.MakeCompound(comp) for _id in range(positions.shape[0]): q0, q1, q2, q3, q4, q5, q6 = [float(x) for x in positions[_id,:]] obj = obj_by_id[_id+1] q = Quaternion((q3, q4, q5, q6)) for shape_name, avatar in zip(io.instances()[obj], avatars(obj)): offset = get_offset(obj, shape_name) p = q.rotate(offset[0]) r = q*Quaternion(offset[1]) tr = gp_Trsf() qocc = gp_Quaternion(r[1], r[2], r[3], r[0]) tr.SetRotation(qocc) xyz = gp_XYZ(q0 + p[0], q1 + p[1], q2 + p[2]) vec = gp_Vec(xyz) tr.SetTranslationPart(vec) loc = TopLoc_Location(tr) display.Context.SetLocation(avatar, loc) moved_shape = BRepBuilderAPI_Transform(avatar.GetObject().Shape(), tr, True).Shape() builder.Add(comp, moved_shape) display.Context.UpdateCurrentViewer() write_step((step_str, comp))
def vstep(step_str): step = int(step_str) positions = dpos_data[nbobjs * step:nbobjs * step + nbobjs, 2:] builder = BRep_Builder() comp = TopoDS_Compound() builder.MakeCompound(comp) for _id in range(positions.shape[0]): q0, q1, q2, q3, q4, q5, q6 = [float(x) for x in positions[_id, :]] obj = obj_by_id[_id + 1] q = Quaternion((q3, q4, q5, q6)) for shape_name, avatar in zip(io.instances()[obj], avatars(obj)): offset = get_offset(obj, shape_name) p = q.rotate(offset[0]) r = q * Quaternion(offset[1]) tr = gp_Trsf() qocc = gp_Quaternion(r[1], r[2], r[3], r[0]) tr.SetRotation(qocc) xyz = gp_XYZ(q0 + p[0], q1 + p[1], q2 + p[2]) vec = gp_Vec(xyz) tr.SetTranslationPart(vec) loc = TopLoc_Location(tr) display.Context.SetLocation(avatar, loc) moved_shape = BRepBuilderAPI_Transform( avatar.GetObject().Shape(), tr, True).Shape() builder.Add(comp, moved_shape) display.Context.UpdateCurrentViewer() write_step((step_str, comp))
def testVectorConstructors(self): v1 = Vector(1, 2, 3) v2 = Vector((1, 2, 3)) v3 = Vector(gp_Vec(1, 2, 3)) v4 = Vector([1, 2, 3]) v5 = Vector(gp_XYZ(1, 2, 3)) for v in [v1, v2, v3, v4, v5]: self.assertTupleAlmostEquals((1, 2, 3), v.toTuple(), 4) v6 = Vector((1, 2)) v7 = Vector([1, 2]) v8 = Vector(1, 2) for v in [v6, v7, v8]: self.assertTupleAlmostEquals((1, 2, 0), v.toTuple(), 4) v9 = Vector() self.assertTupleAlmostEquals((0, 0, 0), v9.toTuple(), 4) v9.x = 1. v9.y = 2. v9.z = 3. self.assertTupleAlmostEquals((1, 2, 3), (v9.x, v9.y, v9.z), 4)
from OCC.gp import gp_Pnt, gp_XYZ, gp_Mat, gp_GTrsf from OCC.BRepPrimAPI import BRepPrimAPI_MakeSphere, BRepPrimAPI_MakeBox from OCC.BRepBuilderAPI import BRepBuilderAPI_GTransform from OCC.BRepAlgoAPI import BRepAlgoAPI_Common from OCC.Display.SimpleGui import init_display display, start_display, add_menu, add_function_to_menu = init_display() orig = gp_Pnt(0., 0., 0.) sphere = BRepPrimAPI_MakeSphere(orig, 50.).Solid() # be careful that the following scale numbers are "not too big", # otherwise boolean operations can be buggy # see isue a1 = 17.1 a2 = 17.1 a3 = 3.5 gTrsf = gp_GTrsf(gp_Mat(a1, 0, 0, 0, a2, 0, 0, 0, a3), gp_XYZ(0., 112.2, 0.)) ellipsoid = BRepBuilderAPI_GTransform(sphere, gTrsf).Shape() # then perform a boolean intersection with a box box = BRepPrimAPI_MakeBox(gp_Pnt(-1000, -1000, -1000), gp_Pnt(1000, 112.2, 1000)).Shape() common = BRepAlgoAPI_Common(box, ellipsoid).Shape() assert not common.IsNull() display.DisplayShape(box, color = "BLACK", transparency = 0.8) display.DisplayShape(ellipsoid, color = "BLACK", transparency = 0.8) display.DisplayShape(common, color = "BLACK", transparency = 0.8, update=True) start_display()
def Translate(self, dx, dy, dz): self.SetTranslationPart(gp.gp_XYZ(dx,dy,dz))
def write(self, mesh_points, filename, tolerance=None): """ Writes a output file, called filename, copying all the structures from self.filename but the coordinates. mesh_points is a matrix that contains the new coordinates to write in the output file. :param numpy.ndarray mesh_points: it is a `n_points`-by-3 matrix containing the coordinates of the points of the mesh :param string filename: name of the output file. :param float tolerance: tolerance for the construction of the faces and wires in the write function. If not given it uses `self.tolerance`. """ self._check_filename_type(filename) self._check_extension(filename) self._check_infile_instantiation() self.outfile = filename if tolerance is not None: self.tolerance = tolerance # cycle on the faces to update the control points position # init some quantities faces_explorer = TopExp_Explorer(self.shape, TopAbs_FACE) n_faces = 0 control_point_position = self._control_point_position compound_builder = BRep_Builder() compound = OCC.TopoDS.TopoDS_Compound() compound_builder.MakeCompound(compound) while faces_explorer.More(): # similar to the parser method face = OCC.TopoDS.topods_Face(faces_explorer.Current()) nurbs_converter = BRepBuilderAPI_NurbsConvert(face) nurbs_converter.Perform(face) nurbs_face = nurbs_converter.Shape() face_aux = OCC.TopoDS.topods_Face(nurbs_face) brep_face = BRep_Tool.Surface(OCC.TopoDS.topods_Face(nurbs_face)) bspline_face = geomconvert_SurfaceToBSplineSurface(brep_face) occ_face = bspline_face.GetObject() n_poles_u = occ_face.NbUPoles() n_poles_v = occ_face.NbVPoles() i = 0 for pole_u_direction in range(n_poles_u): for pole_v_direction in range(n_poles_v): control_point_coordinates = mesh_points[ i + control_point_position[n_faces], :] point_xyz = gp_XYZ(*control_point_coordinates) gp_point = gp_Pnt(point_xyz) occ_face.SetPole(pole_u_direction + 1, pole_v_direction + 1, gp_point) i += 1 # construct the deformed wire for the trimmed surfaces wire_maker = BRepBuilderAPI_MakeWire() tol = ShapeFix_ShapeTolerance() brep = BRepBuilderAPI_MakeFace(occ_face.GetHandle(), self.tolerance).Face() brep_face = BRep_Tool.Surface(brep) # cycle on the edges edge_explorer = TopExp_Explorer(nurbs_face, TopAbs_EDGE) while edge_explorer.More(): edge = OCC.TopoDS.topods_Edge(edge_explorer.Current()) # edge in the (u,v) coordinates edge_uv_coordinates = BRep_Tool.CurveOnSurface(edge, face_aux) # evaluating the new edge: same (u,v) coordinates, but different (x,y,x) ones edge_phis_coordinates_aux = BRepBuilderAPI_MakeEdge(\ edge_uv_coordinates[0], brep_face) edge_phis_coordinates = edge_phis_coordinates_aux.Edge() tol.SetTolerance(edge_phis_coordinates, self.tolerance) wire_maker.Add(edge_phis_coordinates) edge_explorer.Next() # grouping the edges in a wire wire = wire_maker.Wire() # trimming the surfaces brep_surf = BRepBuilderAPI_MakeFace(occ_face.GetHandle(), wire).Shape() compound_builder.Add(compound, brep_surf) n_faces += 1 faces_explorer.Next() self.write_shape_to_file(compound, self.outfile)
def write(self, mesh_points, filename, tolerance=None): """ Writes a iges file, called filename, copying all the structures from self.filename but the coordinates. mesh_points is a matrix that contains the new coordinates to write in the iges file. :param numpy.ndarray mesh_points: it is a `n_points`-by-3 matrix containing the coordinates of the points of the mesh :param string filename: name of the output file. :param float tolerance: tolerance for the construction of the faces and wires in the write function. If not given it uses `self.tolerance`. """ self._check_filename_type(filename) self._check_extension(filename) self._check_infile_instantiation(self.infile) self.outfile = filename if tolerance is not None: self.tolerance = tolerance # init the ouput file writer writer = IGESControl_Writer() # read in the IGES file reader = IGESControl_Reader() reader.ReadFile(self.infile) reader.TransferRoots() shape_read = reader.Shape() # cycle on the faces to update the control points position # init some quantities faces_explorer = TopExp_Explorer(shape_read, TopAbs_FACE) n_faces = 0 control_point_position = self._control_point_position compound_builder = BRep_Builder() compound = OCC.TopoDS.TopoDS_Compound() compound_builder.MakeCompound(compound) while faces_explorer.More(): # similar to the parser method iges_face = OCC.TopoDS.topods_Face(faces_explorer.Current()) iges_nurbs_converter = BRepBuilderAPI_NurbsConvert(iges_face) iges_nurbs_converter.Perform(iges_face) nurbs_face = iges_nurbs_converter.Shape() face_aux = OCC.TopoDS.topods_Face(nurbs_face) brep_face = BRep_Tool.Surface(OCC.TopoDS.topods_Face(nurbs_face)) bspline_face = geomconvert_SurfaceToBSplineSurface(brep_face) occ_face = bspline_face.GetObject() n_poles_u = occ_face.NbUPoles() n_poles_v = occ_face.NbVPoles() i = 0 for pole_u_direction in xrange(n_poles_u): for pole_v_direction in xrange(n_poles_v): control_point_coordinates = mesh_points[i+control_point_position[n_faces], :] point_xyz = gp_XYZ(control_point_coordinates[0], control_point_coordinates[1], \ control_point_coordinates[2]) gp_point = gp_Pnt(point_xyz) occ_face.SetPole(pole_u_direction+1, pole_v_direction+1, gp_point) i += 1 # construct the deformed wire for the trimmed surfaces wire_maker = BRepBuilderAPI_MakeWire() tol = ShapeFix_ShapeTolerance() brep = BRepBuilderAPI_MakeFace(occ_face.GetHandle(), self.tolerance).Face() brep_face = BRep_Tool.Surface(brep) # cycle on the edges edge_explorer = TopExp_Explorer(nurbs_face, TopAbs_EDGE) while edge_explorer.More(): edge = OCC.TopoDS.topods_Edge(edge_explorer.Current()) # edge in the (u,v) coordinates edge_uv_coordinates = OCC.BRep.BRep_Tool.CurveOnSurface(edge, face_aux) # evaluating the new edge: same (u,v) coordinates, but different (x,y,x) ones edge_phis_coordinates_aux = BRepBuilderAPI_MakeEdge(edge_uv_coordinates[0], brep_face) edge_phis_coordinates = edge_phis_coordinates_aux.Edge() tol.SetTolerance(edge_phis_coordinates, self.tolerance) wire_maker.Add(edge_phis_coordinates) edge_explorer.Next() # grouping the edges in a wire wire = wire_maker.Wire() # trimming the surfaces brep_surf = BRepBuilderAPI_MakeFace(occ_face.GetHandle(), wire).Shape() compound_builder.Add(compound, brep_surf) n_faces += 1 faces_explorer.Next() writer.AddShape(compound) writer.Write(self.outfile)
def Translate(self, dx, dy, dz): self.SetTranslationPart(gp.gp_XYZ(dx, dy, dz))
def as_xyz(self): '''returns a gp_XYZ version of self''' return gp_XYZ(*self._pnt.Coord())
def write(cls, filename, data, tolerance=1e-6): # cycle on the faces to update the control points position # init some quantities shape = data.shape control_point_position = data.control_point_position mesh_points = data.points faces_explorer = TopExp_Explorer(shape, TopAbs_FACE) n_faces = 0 compound_builder = BRep_Builder() compound = TopoDS_Compound() compound_builder.MakeCompound(compound) while faces_explorer.More(): # similar to the parser method face = topods_Face(faces_explorer.Current()) nurbs_converter = BRepBuilderAPI_NurbsConvert(face) nurbs_converter.Perform(face) nurbs_face = nurbs_converter.Shape() face_aux = topods_Face(nurbs_face) brep_face = BRep_Tool.Surface(topods_Face(nurbs_face)) bspline_face = geomconvert_SurfaceToBSplineSurface(brep_face) occ_face = bspline_face.GetObject() n_poles_u = occ_face.NbUPoles() n_poles_v = occ_face.NbVPoles() i = 0 for pole_u_direction in range(n_poles_u): for pole_v_direction in range(n_poles_v): control_point_coordinates = mesh_points[ +control_point_position[n_faces], :] point_xyz = gp_XYZ(*control_point_coordinates) gp_point = gp_Pnt(point_xyz) occ_face.SetPole(pole_u_direction + 1, pole_v_direction + 1, gp_point) i += 1 # construct the deformed wire for the trimmed surfaces wire_maker = BRepBuilderAPI_MakeWire() tol = ShapeFix_ShapeTolerance() brep = BRepBuilderAPI_MakeFace(occ_face.GetHandle(), tolerance).Face() brep_face = BRep_Tool.Surface(brep) # cycle on the edges edge_explorer = TopExp_Explorer(nurbs_face, TopAbs_EDGE) while edge_explorer.More(): edge = topods_Edge(edge_explorer.Current()) # edge in the (u,v) coordinates edge_uv_coordinates = BRep_Tool.CurveOnSurface(edge, face_aux) # evaluating the new edge: same (u,v) coordinates, but # different (x,y,x) ones edge_phis_coordinates_aux = BRepBuilderAPI_MakeEdge( edge_uv_coordinates[0], brep_face) edge_phis_coordinates = edge_phis_coordinates_aux.Edge() tol.SetTolerance(edge_phis_coordinates, tolerance) wire_maker.Add(edge_phis_coordinates) edge_explorer.Next() # grouping the edges in a wire wire = wire_maker.Wire() # trimming the surfaces brep_surf = BRepBuilderAPI_MakeFace(occ_face.GetHandle(), wire).Shape() compound_builder.Add(compound, brep_surf) n_faces += 1 faces_explorer.Next() IGESControl_Controller_Init() writer = IGESControl_Writer() writer.AddShape(compound) writer.Write(filename)