def fix_tolerance(shape, tolerance=TOLERANCE): from OCC.ShapeFix import ShapeFix_ShapeTolerance ShapeFix_ShapeTolerance().SetTolerance(shape, tolerance)
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(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)
def write_face(self, points_face, list_points_edge, topo_face, toledge): """ Method to recreate a Face associated to a geometric surface after the modification of Face points. It returns a TopoDS_Face. :param points_face: the new face points array. :param list_points_edge: new edge points :param topo_face: the face to be modified :param toledge: tolerance on the surface creation after modification :return: TopoDS_Face (Shape) :rtype: TopoDS_Shape """ # convert Face to Geom B-spline Surface nurbs_converter = BRepBuilderAPI_NurbsConvert(topo_face) nurbs_converter.Perform(topo_face) nurbs_face = nurbs_converter.Shape() topo_nurbsface = topods.Face(nurbs_face) h_geomsurface = BRep_Tool.Surface(topo_nurbsface) h_bsurface = geomconvert_SurfaceToBSplineSurface(h_geomsurface) bsurface = h_bsurface.GetObject() nb_u = bsurface.NbUPoles() nb_v = bsurface.NbVPoles() # check consistency if points_face.shape[0] != nb_u * nb_v: raise ValueError("Input control points do not have not have the " "same number as the geometric face!") # cycle on the face points indice_cpt = 0 for iu in range(1, nb_u + 1): for iv in range(1, nb_v + 1): cpt = points_face[indice_cpt] bsurface.SetPole(iu, iv, gp_Pnt(cpt[0], cpt[1], cpt[2])) indice_cpt += 1 # create modified new face new_bspline_tface = BRepBuilderAPI_MakeFace() toler = precision_Confusion() new_bspline_tface.Init(bsurface.GetHandle(), False, toler) # cycle on the wires face_wires_explorer = TopExp_Explorer( topo_nurbsface.Oriented(TopAbs_FORWARD), TopAbs_WIRE) ind_edge_total = 0 while face_wires_explorer.More(): # get old wire twire = topods_Wire(face_wires_explorer.Current()) # cycle on the edges ind_edge = 0 wire_explorer_edge = TopExp_Explorer( twire.Oriented(TopAbs_FORWARD), TopAbs_EDGE) # check edges order on the wire mode3d = True tolerance_edges = toledge wire_order = ShapeAnalysis_WireOrder(mode3d, tolerance_edges) # an edge list deformed_edges = [] # cycle on the edges while wire_explorer_edge.More(): tedge = topods_Edge(wire_explorer_edge.Current()) new_bspline_tedge = self.write_edge( list_points_edge[ind_edge_total], tedge) deformed_edges.append(new_bspline_tedge) analyzer = topexp() vfirst = analyzer.FirstVertex(new_bspline_tedge) vlast = analyzer.LastVertex(new_bspline_tedge) pt1 = BRep_Tool.Pnt(vfirst) pt2 = BRep_Tool.Pnt(vlast) wire_order.Add(pt1.XYZ(), pt2.XYZ()) ind_edge += 1 ind_edge_total += 1 wire_explorer_edge.Next() # grouping the edges in a wire, then in the face # check edges order and connectivity within the wire wire_order.Perform() # new wire to be created stol = ShapeFix_ShapeTolerance() new_bspline_twire = BRepBuilderAPI_MakeWire() for order_i in range(1, wire_order.NbEdges() + 1): deformed_edge_i = wire_order.Ordered(order_i) if deformed_edge_i > 0: # insert the deformed edge to the new wire new_edge_toadd = deformed_edges[deformed_edge_i - 1] stol.SetTolerance(new_edge_toadd, toledge) new_bspline_twire.Add(new_edge_toadd) if new_bspline_twire.Error() != 0: stol.SetTolerance(new_edge_toadd, toledge * 10.0) new_bspline_twire.Add(new_edge_toadd) else: deformed_edge_revers = deformed_edges[ np.abs(deformed_edge_i) - 1] stol.SetTolerance(deformed_edge_revers, toledge) new_bspline_twire.Add(deformed_edge_revers) if new_bspline_twire.Error() != 0: stol.SetTolerance(deformed_edge_revers, toledge * 10.0) new_bspline_twire.Add(deformed_edge_revers) # add new wire to the Face new_bspline_tface.Add(new_bspline_twire.Wire()) face_wires_explorer.Next() return topods.Face(new_bspline_tface.Face())
def fix_tolerance(shape, tolerance=TOLERANCE): ShapeFix_ShapeTolerance().SetTolerance(shape, tolerance)