def stitch(self, other): """Attempt to stich wires""" wire_builder = BRepBuilderAPI_MakeWire() wire_builder.Add(topods_Wire(self.wrapped)) wire_builder.Add(topods_Wire(other.wrapped)) wire_builder.Build() return self.__class__(wire_builder.Wire())
def topo2topotype(occtopology): """ This function converts the original OCCtopology of the given topology. e.g. an OCCcompound that is originally an OCCface etc. Parameters ---------- occtopology : OCCtopology The OCCtopology to be converted. OCCtopology includes: OCCshape, OCCcompound, OCCcompsolid, OCCsolid, OCCshell, OCCface, OCCwire, OCCedge, OCCvertex Returns ------- original topology : OCCtopology The original OCCtopology of the input. """ shapetype = occtopology.ShapeType() if shapetype == TopAbs_COMPOUND:#compound orig_topo = topods_Compound(occtopology) if shapetype == TopAbs_COMPSOLID:#compsolid orig_topo = topods_CompSolid(occtopology) if shapetype == TopAbs_SOLID:#solid orig_topo = topods_Solid(occtopology) if shapetype == TopAbs_SHELL:#shell orig_topo = topods_Shell(occtopology) if shapetype == TopAbs_FACE:#face orig_topo = topods_Face(occtopology) if shapetype == TopAbs_WIRE:#wire orig_topo = topods_Wire(occtopology) if shapetype == TopAbs_EDGE:#edge orig_topo = topods_Edge(occtopology) if shapetype == TopAbs_VERTEX:#vertex orig_topo = topods_Vertex(occtopology) return orig_topo
def topo_explorer(occtopo2explore, topotype2find): """ This function explores and fetches the specified topological type from the given OCCtopology. e.g. find a list of OCCfaces in an OCCcompound. Parameters ---------- occtopo2explore : OCCtopology The OCCtopology to be explored. OCCtopology includes: OCCshape, OCCcompound, OCCcompsolid, OCCsolid, OCCshell, OCCface, OCCwire, OCCedge, OCCvertex topotype2find : str The string describing the topology to find. The strings can be e.g. "compound", "compsolid", "solid", "shell", "face", "wire", "edge", "vertex". Returns ------- list of topology : list of OCCtopology The list of OCCtopology found in the specified OCCtopology. """ geom_list = [] if topotype2find == "compound": shapetype2find_topABS = TopAbs_COMPOUND if topotype2find == "compsolid": shapetype2find_topABS = TopAbs_COMPSOLID if topotype2find == "solid": shapetype2find_topABS = TopAbs_SOLID if topotype2find == "shell": shapetype2find_topABS = TopAbs_SHELL if topotype2find == "face": shapetype2find_topABS = TopAbs_FACE if topotype2find == "wire": shapetype2find_topABS = TopAbs_WIRE if topotype2find == "edge": shapetype2find_topABS = TopAbs_EDGE if topotype2find == "vertex": shapetype2find_topABS = TopAbs_VERTEX ex = TopExp_Explorer(occtopo2explore, shapetype2find_topABS) while ex.More(): if shapetype2find_topABS == 0: geom = topods_Compound(ex.Current()) if shapetype2find_topABS == 1: geom = topods_CompSolid(ex.Current()) if shapetype2find_topABS == 2: geom = topods_Solid(ex.Current()) if shapetype2find_topABS == 3: geom = topods_Shell(ex.Current()) if shapetype2find_topABS == 4: geom = topods_Face(ex.Current()) if shapetype2find_topABS == 5: geom = topods_Wire(ex.Current()) if shapetype2find_topABS == 6: geom = topods_Edge(ex.Current()) if shapetype2find_topABS == 7: geom = topods_Vertex(ex.Current()) geom_list.append(geom) ex.Next() return geom_list
def iter_wires(shape): """ Generator / Iterator over the wire of a shape """ exp = TopExp_Explorer(shape, TopAbs_WIRE) while exp.More(): yield topods_Wire(exp.Current()) exp.Next()
def shape2shapetype(occ_shape): shapetype = occ_shape.ShapeType() if shapetype == TopAbs_COMPOUND: #compound orig_topo = topods_Compound(occ_shape) if shapetype == TopAbs_COMPSOLID: #compsolid orig_topo = topods_CompSolid(occ_shape) if shapetype == TopAbs_SOLID: #solid orig_topo = topods_Solid(occ_shape) if shapetype == TopAbs_SHELL: #shell orig_topo = topods_Shell(occ_shape) if shapetype == TopAbs_FACE: #face orig_topo = topods_Face(occ_shape) if shapetype == TopAbs_WIRE: #wire orig_topo = topods_Wire(occ_shape) if shapetype == TopAbs_EDGE: #edge orig_topo = topods_Edge(occ_shape) if shapetype == TopAbs_VERTEX: #vertex orig_topo = topods_Vertex(occ_shape) return orig_topo
def geom_explorer(geom2explore, shapetype2find): geom_list = [] if shapetype2find == "compound": shapetype2find_topABS = TopAbs_COMPOUND if shapetype2find == "compsolid": shapetype2find_topABS = TopAbs_COMPSOLID if shapetype2find == "solid": shapetype2find_topABS = TopAbs_SOLID if shapetype2find == "shell": shapetype2find_topABS = TopAbs_SHELL if shapetype2find == "face": shapetype2find_topABS = TopAbs_FACE if shapetype2find == "wire": shapetype2find_topABS = TopAbs_WIRE if shapetype2find == "edge": shapetype2find_topABS = TopAbs_EDGE if shapetype2find == "vertex": shapetype2find_topABS = TopAbs_VERTEX ex = TopExp_Explorer(geom2explore, shapetype2find_topABS) while ex.More(): if shapetype2find_topABS == 0: geom = topods_Compound(ex.Current()) if shapetype2find_topABS == 1: geom = topods_CompSolid(ex.Current()) if shapetype2find_topABS == 2: geom = topods_Solid(ex.Current()) if shapetype2find_topABS == 3: geom = topods_Shell(ex.Current()) if shapetype2find_topABS == 4: geom = topods_Face(ex.Current()) if shapetype2find_topABS == 5: geom = topods_Wire(ex.Current()) if shapetype2find_topABS == 6: geom = topods_Edge(ex.Current()) if shapetype2find_topABS == 7: geom = topods_Vertex(ex.Current()) geom_list.append(geom) ex.Next() return geom_list
step_reader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity) ok = step_reader.TransferRoot(1) _nbs = step_reader.NbShapes() aResShape = step_reader.Shape(1) else: print("Error: can't read file.") sys.exit(0) mydumpTopology(aResShape) print(len(lstWires)) face = BRepBuilderAPI_MakeFace(topods_Wire(lstWires[-1])) starting_point = gp_Pnt(0., 0., 0.) end_point = gp_Pnt(0., 0., 100.) vec = gp_Vec(starting_point, end_point) prism = BRepPrimAPI_MakePrism(face.Shape(), vec).Shape() freecadObj = FreeCADPart.Wire(FreeCADPart.__fromPythonOCC__(lstWires[-1])) print(str(type(freecadObj))) cqObj = cq.CQ(freecadObj) cqObj = Wire(cqObj)
bushing = BRepAlgoAPI_Fuse(bushing, BconeSm).Shape() BSIN = BOR * math.sin(math.radians(BA)) BCOS = BOR * math.cos(math.radians(BA)) # create and set up transformation for leftBush ltrsf = gp_Trsf() ltrsf.SetRotation(gp_Ax1(gp_Pnt(0, 0, 0), gp_Dir(1, 0, 0)), math.radians(BA)) leftBush = BRepBuilderAPI_Transform(bushing, ltrsf).Shape() ltrsf.SetTranslation(gp_Vec(BIn + BOR, float(TW) / 2 - BS - BCOS, TH - BSIN)) leftBush = BRepBuilderAPI_Transform(leftBush, ltrsf).Shape() leftBush = BRepAlgoAPI_Cut(leftBush, tank).Shape() # cut common part of bushing and tank mooh = BRepAlgoAPI_Section(leftBush, tank).Shape() wire = topods_Wire(mooh) face = BRepBuilderAPI_MakeFace(wire).Face() #leftBush = BRepAlgoAPI_Cut(leftBush,face).Shape() # create and set up transformation for midBush mtrsf = gp_Trsf() mtrsf.SetTranslation(gp_Vec(BIn + BOR, float(TW) / 2, TH)) midBush = BRepBuilderAPI_Transform(bushing, mtrsf).Shape() # create and set up transformation for rightBush rtrsf = gp_Trsf() rtrsf.SetRotation(gp_Ax1(gp_Pnt(0, 0, 0), gp_Dir(1, 0, 0)), math.radians(360 - BA)) rightBush = BRepBuilderAPI_Transform(bushing, rtrsf).Shape() rtrsf.SetTranslation(gp_Vec(BIn + BOR, float(TW) / 2 + BS + BCOS, TH - BSIN)) rightBush = BRepBuilderAPI_Transform(rightBush, rtrsf).Shape()
def bordering_wires_of_face(shape, face_shape): """ Return dictionary of bordering wires with face shape. This function also returns list of common edges of bordering wires. Definition of bordering wire is following: such wire shares at least one edge with face_shape, but it does not create face_shape. """ primitives = brep_explorer.shape_disassembly(shape) # Create list of wires "to be fixed" (potentially) wire_shapes_tbf = [] for old_wire_shape in primitives[5].values(): wire_shapes_tbf.append(topods_Wire(old_wire_shape)) old_wires, old_edges, old_verts = entities_of_wires(wire_shapes_tbf) # Get list of wires at border of shape new_wire_shapes = wires_of_face(face_shape) new_wires, new_edges, new_verts = entities_of_wires(new_wire_shapes) # Dictionary of bordering wires bordering_wires = {} # Go through all wires of face_shape (usually only one wire) for new_wire_key, new_wire_shape in new_wires.items(): print('>>>> Wire of face_shape:', new_wire_shape) # Go through all edges of bordering wire for edge_coords in new_wire_key: new_edge_coords = tuple(sorted(edge_coords)) new_edge = new_edges[new_edge_coords] print('@@@@ Bordering edge:', new_edge_coords, new_edge) # Try to find this edge in other wires for old_wire_key, old_wire in old_wires.items(): # Old wire can't be same as wire from bordering face # This wire is all right and there is no need to fix it if new_wire_key == old_wire_key: continue # Try to find the edge in all edges of old wire for old_edge_coord in old_wire_key: # Is it the edge with same verticies? if tuple(sorted(old_edge_coord)) == new_edge_coords: # Old wire containing edge from face_shape was found print('#### Old Wire to be fixed:', old_wire_key, old_wire) bordering_wires[old_wire_key] = old_wire break print('<<<< Bordering wires:', bordering_wires) # Try to get list of common edges of bordering wires all_edges = [] common_edges = {} for old_wire_key, old_wire in old_wires.items(): if old_wire_key in bordering_wires.keys(): print('^^^^ Edges of wrong wire:', old_wire_key) if len(all_edges) > 0: for edge in old_wire_key: if edge in all_edges: common_edges[edge] = old_edges[edge] all_edges.extend(old_wire_key) all_edges = list(set(all_edges)) print('<<<< Common edges:', common_edges) return bordering_wires, common_edges
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 parse_face(topo_face): """ Method to parse a single `Face` (a single patch nurbs surface). It returns a matrix with all the coordinates of control points of the `Face` and a second list with all the control points related to the `Edges` of the `Face.` :param Face topo_face: the input Face. :return: control points of the `Face`, control points related to `Edges`. :rtype: tuple(numpy.ndarray, list) """ # get some Face - Edge - Vertex data map information mesh_points_edge = [] face_exp_wire = TopExp_Explorer(topo_face, TopAbs_WIRE) # loop on wires per face while face_exp_wire.More(): twire = topods_Wire(face_exp_wire.Current()) wire_exp_edge = TopExp_Explorer(twire, TopAbs_EDGE) # loop on edges per wire while wire_exp_edge.More(): edge = topods_Edge(wire_exp_edge.Current()) bspline_converter = BRepBuilderAPI_NurbsConvert(edge) bspline_converter.Perform(edge) bspline_tshape_edge = bspline_converter.Shape() h_geom_edge = BRep_Tool_Curve( topods_Edge(bspline_tshape_edge))[0] h_bspline_edge = geomconvert_CurveToBSplineCurve(h_geom_edge) bspline_geom_edge = h_bspline_edge.GetObject() nb_poles = bspline_geom_edge.NbPoles() # Edge geometric properties edge_ctrlpts = TColgp_Array1OfPnt(1, nb_poles) bspline_geom_edge.Poles(edge_ctrlpts) points_single_edge = np.zeros((0, 3)) for i in range(1, nb_poles + 1): ctrlpt = edge_ctrlpts.Value(i) ctrlpt_position = np.array( [[ctrlpt.Coord(1), ctrlpt.Coord(2), ctrlpt.Coord(3)]]) points_single_edge = np.append( points_single_edge, ctrlpt_position, axis=0) mesh_points_edge.append(points_single_edge) wire_exp_edge.Next() face_exp_wire.Next() # extract mesh points (control points) on Face mesh_points_face = np.zeros((0, 3)) # convert Face to Geom B-spline Face nurbs_converter = BRepBuilderAPI_NurbsConvert(topo_face) nurbs_converter.Perform(topo_face) nurbs_face = nurbs_converter.Shape() h_geomsurface = BRep_Tool.Surface(topods.Face(nurbs_face)) h_bsurface = geomconvert_SurfaceToBSplineSurface(h_geomsurface) bsurface = h_bsurface.GetObject() # get access to control points (poles) nb_u = bsurface.NbUPoles() nb_v = bsurface.NbVPoles() ctrlpts = TColgp_Array2OfPnt(1, nb_u, 1, nb_v) bsurface.Poles(ctrlpts) for indice_u_direction in range(1, nb_u + 1): for indice_v_direction in range(1, nb_v + 1): ctrlpt = ctrlpts.Value(indice_u_direction, indice_v_direction) ctrlpt_position = np.array( [[ctrlpt.Coord(1), ctrlpt.Coord(2), ctrlpt.Coord(3)]]) mesh_points_face = np.append( mesh_points_face, ctrlpt_position, axis=0) return mesh_points_face, mesh_points_edge
def parse_face(topo_face): """ Method to parse a single Face (a single patch nurbs surface). It returns a matrix with all the coordinates of control points of the Face and a second list with all the control points related to the Edges of the Face. :param topo_face: the input Face :return: mesh_points_face: it is a `n_points`-by-3 matrix containing the coordinates of the control points of the Face (a nurbs surface) :return: mesh_points_edge: it is a list of `n_points`-by-3 matrix :rtype: tuple(numpy.ndarray, list) """ # get some Face - Edge - Vertex data map information mesh_points_edge = [] face_exp_wire = TopExp_Explorer(topo_face, TopAbs_WIRE) # loop on wires per face while face_exp_wire.More(): twire = topods_Wire(face_exp_wire.Current()) wire_exp_edge = TopExp_Explorer(twire, TopAbs_EDGE) # loop on edges per wire while wire_exp_edge.More(): edge = topods_Edge(wire_exp_edge.Current()) bspline_converter = BRepBuilderAPI_NurbsConvert(edge) bspline_converter.Perform(edge) bspline_tshape_edge = bspline_converter.Shape() h_geom_edge = BRep_Tool_Curve( topods_Edge(bspline_tshape_edge))[0] h_bspline_edge = geomconvert_CurveToBSplineCurve(h_geom_edge) bspline_geom_edge = h_bspline_edge.GetObject() nb_poles = bspline_geom_edge.NbPoles() # Edge geometric properties edge_ctrlpts = TColgp_Array1OfPnt(1, nb_poles) bspline_geom_edge.Poles(edge_ctrlpts) points_single_edge = np.zeros((0, 3)) for i in range(1, nb_poles + 1): ctrlpt = edge_ctrlpts.Value(i) ctrlpt_position = np.array( [[ctrlpt.Coord(1), ctrlpt.Coord(2), ctrlpt.Coord(3)]]) points_single_edge = np.append(points_single_edge, ctrlpt_position, axis=0) mesh_points_edge.append(points_single_edge) wire_exp_edge.Next() face_exp_wire.Next() # extract mesh points (control points) on Face mesh_points_face = np.zeros((0, 3)) # convert Face to Geom B-spline Face nurbs_converter = BRepBuilderAPI_NurbsConvert(topo_face) nurbs_converter.Perform(topo_face) nurbs_face = nurbs_converter.Shape() h_geomsurface = BRep_Tool.Surface(topods.Face(nurbs_face)) h_bsurface = geomconvert_SurfaceToBSplineSurface(h_geomsurface) bsurface = h_bsurface.GetObject() # get access to control points (poles) nb_u = bsurface.NbUPoles() nb_v = bsurface.NbVPoles() ctrlpts = TColgp_Array2OfPnt(1, nb_u, 1, nb_v) bsurface.Poles(ctrlpts) for indice_u_direction in range(1, nb_u + 1): for indice_v_direction in range(1, nb_v + 1): ctrlpt = ctrlpts.Value(indice_u_direction, indice_v_direction) ctrlpt_position = np.array( [[ctrlpt.Coord(1), ctrlpt.Coord(2), ctrlpt.Coord(3)]]) mesh_points_face = np.append(mesh_points_face, ctrlpt_position, axis=0) return mesh_points_face, mesh_points_edge