def get_faces(shp): ex = TopExp_Explorer(shp, TopAbs_FACE) seq = [] while ex.More(): s1 = ex.Current() seq.append(s1) ex.Next() return seq
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 iter_edges(shape): """ Generator / Iterator over the edges of a shape """ exp = TopExp_Explorer(shape, TopAbs_EDGE) while exp.More(): yield topods_Edge(exp.Current()) exp.Next()
def color_the_edges(shp, display, color, width): shapeList = [] Ex = TopExp_Explorer(shp, TopAbs_EDGE) ctx = display.Context while Ex.More(): aEdge = topods.Edge(Ex.Current()) ais_shape = AIS_Shape(aEdge).GetHandle() ctx.SetColor(ais_shape, color, False) ctx.SetWidth(ais_shape, width, False) ctx.Display(ais_shape, False) Ex.Next()
def process_shape(shape): '''extracts faces from a shape note: if this doesnt work you should try process_face(shape) returns a list of faces''' faces = [] explorer = TopExp_Explorer(shape, TopAbs_FACE) while explorer.More(): face = TopoDS().Face(explorer.Current()) faces.append(face) explorer.Next() return faces
def process_face(face): '''traverses a face for wires returns a list of wires''' wires = [] explorer = TopExp_Explorer(face, TopAbs_EDGE) while explorer.More(): edge = TopoDS().Edge(explorer.Current()) wire = BRepBuilderAPI_MakeWire(edge).Wire() wires.append(wire) explorer.Next() return wires
def _entities(self, topo_type): out = {} # using dict to prevent duplicates explorer = TopExp_Explorer(self.wrapped, inverse_shape_LUT[topo_type]) while explorer.More(): item = explorer.Current() out[item.__hash__()] = item # some implementations use __hash__ explorer.Next() return list(out.values())
def get_faces(_shape): """ return the faces from `_shape` :param _shape: TopoDS_Shape, or a subclass like TopoDS_Solid :return: a list of faces found in `_shape` """ topExp = TopExp_Explorer() topExp.Init(_shape, TopAbs_FACE) _faces = [] while topExp.More(): fc = topods_Face(topExp.Current()) _faces.append(fc) topExp.Next() return _faces
def draft_angle(event=None): S = BRepPrimAPI_MakeBox(200., 300., 150.).Shape() adraft = BRepOffsetAPI_DraftAngle(S) topExp = TopExp_Explorer() topExp.Init(S, TopAbs_FACE) while topExp.More(): face = topods_Face(topExp.Current()) surf = Handle_Geom_Plane_DownCast(BRep_Tool_Surface(face)).GetObject() dirf = surf.Pln().Axis().Direction() ddd = gp_Dir(0, 0, 1) if dirf.IsNormal(ddd, precision_Angular()): adraft.Add(face, ddd, math.radians(15), gp_Pln(gp_Ax3(gp_XOY()))) topExp.Next() adraft.Build() display.DisplayShape(adraft.Shape(), update=True)
def get_edges_from_shape(a_topods_shape): """ Returns a list of edges from a topods_shape """ edge_explorer = TopExp_Explorer() edge_explorer.Init(a_topods_shape, TopAbs_EDGE) edges = [] hashes = [] while edge_explorer.More(): current_edge = edge_explorer.Current() current_item_hash = current_edge.__hash__() if not current_item_hash in hashes: hashes.append(current_item_hash) edges.append(current_edge) edge_explorer.Next() return edges
def get_faces_from_shape(a_topods_shape): """ Returns a list of faces from a TopoDS_Shape """ faces = [] # get faces face_explorer = TopExp_Explorer() face_explorer.Init(a_topods_shape, TopAbs_FACE) faces = [] hashes = [] while face_explorer.More(): current_face = face_explorer.Current() current_item_hash = current_face.__hash__() if not current_item_hash in hashes: hashes.append(current_item_hash) faces.append(current_face) face_explorer.Next() return faces
def GetFacesSurfaces(BRepShape): FaceExplorer = TopExp_Explorer(BRepShape, TopAbs_FACE) Faces = [] while FaceExplorer.More(): ShapeFace = FaceExplorer.Current() # a TopoDS_Shape # Convert TopoDS_Shape to a TopoDS_Face Face = topods.Face(ShapeFace) Faces.append(Face) FaceExplorer.Next() pass Surfaces = [BRep_Tool().Surface(Face) for Face in Faces] SurfObjs = [Surface.GetObject() for Surface in Surfaces] return (Faces, Surfaces, SurfObjs)
def compute(self): # get faces explorer = TopExp_Explorer() explorer.Init(self._shape, TopAbs_FACE) if self._map_faces_to_mesh: # one mesh per face faces = [] while explorer.More(): current_face = explorer.Current() faces.append(current_face) explorer.Next() # loop over faces for face in faces: face_tesselator = Tesselator(face) self._indexed_face_sets.append(face_tesselator.ExportShapeToX3DIndexedFaceSet()) else: # only one mesh for the whole shape shape_tesselator = Tesselator(self._shape) self._indexed_face_sets.append(shape_tesselator.ExportShapeToX3DIndexedFaceSet())
def combine_faces(compshape, sew_tolerance): """ Method to combine faces in a shell by adding connectivity and continuity :param compshape: TopoDS_Shape :param sew_tolerance: tolerance for sewing :return: Topo_Shell """ offsew = BRepOffsetAPI_FindContigousEdges(sew_tolerance) sew = BRepBuilderAPI_Sewing(sew_tolerance) face_explorers = TopExp_Explorer(compshape, TopAbs_FACE) n_faces = 0 # cycle on Faces while face_explorers.More(): tface = topods.Face(face_explorers.Current()) sew.Add(tface) offsew.Add(tface) n_faces += 1 face_explorers.Next() offsew.Perform() offsew.Dump() sew.Perform() shell = sew.SewedShape() sew.Dump() shell = topods.Shell(shell) shell_fixer = ShapeFix_Shell() shell_fixer.FixFaceOrientation(shell) if shell_fixer.Perform(): print("{} shells fixed! ".format(shell_fixer.NbShells())) else: print "Shells not fixed! " new_shell = shell_fixer.Shell() if brepalgo_IsValid(new_shell): print "Shell valid! " else: print "Shell failed! " return new_shell
def simple_mesh(): # # Create the shape # shape = BRepPrimAPI_MakeBox(200, 200, 200).Shape() theBox = BRepPrimAPI_MakeBox(200, 60, 60).Shape() theSphere = BRepPrimAPI_MakeSphere(gp_Pnt(100, 20, 20), 80).Shape() shape = BRepAlgoAPI_Fuse(theSphere, theBox).Shape() # # Mesh the shape # BRepMesh_IncrementalMesh(shape, 0.8) builder = BRep_Builder() comp = TopoDS_Compound() builder.MakeCompound(comp) bt = BRep_Tool() ex = TopExp_Explorer(shape, TopAbs_FACE) while ex.More(): face = topods_Face(ex.Current()) location = TopLoc_Location() facing = (bt.Triangulation(face, location)).GetObject() tab = facing.Nodes() tri = facing.Triangles() for i in range(1, facing.NbTriangles()+1): trian = tri.Value(i) index1, index2, index3 = trian.Get() for j in range(1, 4): if j == 1: m = index1 n = index2 elif j == 2: n = index3 elif j == 3: m = index2 me = BRepBuilderAPI_MakeEdge(tab.Value(m), tab.Value(n)) if me.IsDone(): builder.Add(comp, me.Edge()) ex.Next() display.EraseAll() display.DisplayShape(shape) display.DisplayShape(comp, update=True)
def createModel(self): # edges = makeEdgesFromPoints(self.points) # self.a1 = edge1 = BRepBuilderAPI_MakeEdge(getGpPt(self.a1), getGpPt(self.a2)) edge2 = BRepBuilderAPI_MakeEdge(getGpPt(self.a2), getGpPt(self.a3)) arc1 = GC_MakeArcOfCircle(getGpPt(self.a3), getGpPt(self.a4), getGpPt(self.a5)) edge3 = BRepBuilderAPI_MakeEdge(arc1.Value()) edge4 = BRepBuilderAPI_MakeEdge(getGpPt(self.a5), getGpPt(self.a6)) arc2 = GC_MakeArcOfCircle(getGpPt(self.a6), getGpPt(self.a7), getGpPt(self.a8)) edge5 = BRepBuilderAPI_MakeEdge(arc2.Value()) edge6 = BRepBuilderAPI_MakeEdge(getGpPt(self.a8), getGpPt(self.a9)) arc3 = GC_MakeArcOfCircle(getGpPt(self.a9), getGpPt(self.a10), getGpPt(self.a11)) edge7 = BRepBuilderAPI_MakeEdge(arc3.Value()) edge8 = BRepBuilderAPI_MakeEdge(getGpPt(self.a11), getGpPt(self.a12)) edge9 = BRepBuilderAPI_MakeEdge(getGpPt(self.a12), getGpPt(self.a1)) # wire = makeWireFromEdges(edge1,edge2,edge3,edge4,edge5,edge6,edge7,edge8,edge9) wire = BRepBuilderAPI_MakeWire(edge1.Edge(), edge2.Edge(), edge3.Edge(), edge4.Edge()) wire = BRepBuilderAPI_MakeWire(wire.Wire(), edge5.Edge()) wire = BRepBuilderAPI_MakeWire(wire.Wire(), edge6.Edge()) wire = BRepBuilderAPI_MakeWire(wire.Wire(), edge7.Edge()) wire = BRepBuilderAPI_MakeWire(wire.Wire(), edge8.Edge()) wire = BRepBuilderAPI_MakeWire(wire.Wire(), edge9.Edge()) aFace = makeFaceFromWire(wire.Wire()) extrudeDir = self.L * self.wDir # extrudeDir is a numpy array prism = makePrismFromFace(aFace, extrudeDir) mkFillet = BRepFilletAPI_MakeFillet(prism) anEdgeExplorer = TopExp_Explorer(prism, TopAbs_EDGE) while anEdgeExplorer.More(): aEdge = topods.Edge(anEdgeExplorer.Current()) mkFillet.Add(self.T / 17., aEdge) anEdgeExplorer.Next() prism = mkFillet.Shape() return prism
def createModel(self): edges = makeEdgesFromPoints(self.points) wire = makeWireFromEdges(edges) aFace = makeFaceFromWire(wire) extrudeDir = self.T * self.wDir # extrudeDir is a numpy array prism = makePrismFromFace(aFace, extrudeDir) mkFillet = BRepFilletAPI_MakeFillet(prism) anEdgeExplorer = TopExp_Explorer(prism, TopAbs_EDGE) while anEdgeExplorer.More(): aEdge = topods.Edge(anEdgeExplorer.Current()) mkFillet.Add(self.T / 17. , aEdge) anEdgeExplorer.Next() prism = mkFillet.Shape() cylOrigin = self.secOrigin innerCyl = BRepPrimAPI_MakeCylinder(gp_Ax2(getGpPt(cylOrigin), getGpDir(self.wDir)), self.r1, self.H).Shape() result_shape = BRepAlgoAPI_Cut(prism, innerCyl).Shape() return result_shape
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
def createModel(self): edges = makeEdgesFromPoints(self.points) wire = makeWireFromEdges(edges) aFace = makeFaceFromWire(wire) extrudeDir = -self.T * self.shaftDir # extrudeDir is a numpy array boltHead = makePrismFromFace(aFace, extrudeDir) mkFillet = BRepFilletAPI_MakeFillet(boltHead) anEdgeExplorer = TopExp_Explorer(boltHead, TopAbs_EDGE) while anEdgeExplorer.More(): aEdge = topods.Edge(anEdgeExplorer.Current()) mkFillet.Add(self.T / 17. , aEdge) anEdgeExplorer.Next() boltHead = mkFillet.Shape() cylOrigin = self.origin boltCylinder = BRepPrimAPI_MakeCylinder(gp_Ax2(getGpPt(cylOrigin), getGpDir(self.shaftDir)), self.r, self.H).Shape() whole_Bolt = BRepAlgoAPI_Fuse(boltHead,boltCylinder).Shape() mkFillet = BRepFilletAPI_MakeFillet(whole_Bolt) return whole_Bolt
def discretize(shape, tol): """This method discretizes the OpenCascade shape. :param shape: Shape to discretize :type shape: :return: discretized face; profile coordinates; id of the surface the\ coordinates belong to :rtype: OCC.TopoDS.TopoDS_Compound; numpy.ndarray; numpy.ndarray """ BRepMesh_IncrementalMesh(shape, tol, False, 5) builder = BRep_Builder() comp = TopoDS_Compound() builder.MakeCompound(comp) bt = BRep_Tool() ex = TopExp_Explorer(shape, TopAbs_EDGE) edge_coords = np.zeros([0, 3]) edge_ids = np.zeros([0], dtype=int) edge_id = 0 while ex.More(): edge = topods_Edge(ex.Current()) location = TopLoc_Location() edging = (bt.Polygon3D(edge, location)).GetObject() tab = edging.Nodes() for i in range(1, edging.NbNodes() + 1): p = tab.Value(i) edge_coords = np.append(edge_coords, [[p.X(), p.Y(), p.Z()]], axis=0) edge_ids = np.append(edge_ids, edge_id) mv = BRepBuilderAPI_MakeVertex(p) if mv.IsDone(): builder.Add(comp, mv.Vertex()) edge_id += 1 ex.Next() edge_coords = np.round(edge_coords, 8) return edge_coords, edge_ids
lim_coord1 = (xmin, xmax) lim_coord2 = (zmin, zmax) section_width = ymin + 1e-3 # A horizontal plane is created from which a face is constructed to intersect with # the building. The face is transparently displayed along with the building. section_plane = gp_Pln(gp_Pnt(xmax + xmin, section_width, 0.0), gp_Dir(0, 1, 0)) plt.figure() plt.xlim(lim_coord1) plt.ylim(lim_coord2) # Explore the faces of the shape (these are known to be named) exp = TopExp_Explorer(shape, TopAbs_FACE) while exp.More(): s = exp.Current() tp = Topo(s) for face in tp.faces(): for edge in list(Topo(face).edges()): obj = EdgeOnSurface(edge, section_plane, lim_coord2, lim_coord1, XYZ) if type(obj) == Line3D: x1, y1 = obj.get_v1(XYZ) x2, y2 = obj.get_v2(XYZ) plt.plot([x1, x2], [y1, y2], color="red") elif type(obj) == Arc3D:
builder = BRep_Builder() breptools_Read(shape, "cubeWithHole.brep", builder) # Loop over the shape pulled from the input and look for solids. solids = [] # A list for storing solids from the file i = 0 # A counter for writing vertices/points allPointsDataFrame = pd.DataFrame([]) # A dataframe for ALL points # OpenCASCADE can only successfully execute point inclusion queries if the # shape in question is a full solid, which is defined in their topology as # TopAbs_SOLID. Each solid in the file can sampled individually by pulling # it from the compound. if shape.ShapeType() != TopAbs_SOLID: # Create a topology explorer and pull all the solids from the shape explorer = TopExp_Explorer(shape, TopAbs_SOLID) # Loop over all the solids while explorer.More(): solid = explorer.Current() #pointsInSolid = sampleSolid(n,solid) pointsInSolid = sampleSolid(n, solid, vertexList) # Write the coordinates that are saved to disk. i = i + 1 fileName = 'solid_' + str(i) + '.csv' pointsInSolid.to_csv(fileName, index=False) # Store the points for later allPointsDataFrame = allPointsDataFrame.append( pointsInSolid.copy(), ignore_index=False) # Store the solid for later reference (i.e. - visualization) solids.append(solid) # Go to the next solid if one exists explorer.Next() else:
def parse_shape(self, filename): """ Method to parse a Shape with multiple objects (1 compound = multi-shells and 1 shell = multi-faces) It returns a list of matrix with all the coordinates of control points of each Face and a second list with all the control points related to Edges of each Face. :param filename: the input filename. :return: list of (mesh_points: `n_points`-by-3 matrix containing the coordinates of the control points of the Face (surface), edge_points: it is a list of numpy.narray) :rtype: a list of shells """ self.infile = filename self.shape = self.load_shape_from_file(filename) self.check_topology() # parse and get control points l_shells = [] # an empty list of shells n_shells = 0 if self.check_topo == 0: shells_explorer = TopExp_Explorer(self.shape, TopAbs_SHELL) # cycle on shells while shells_explorer.More(): topo_shell = topods.Shell(shells_explorer.Current()) shell_faces_explorer = TopExp_Explorer(topo_shell, TopAbs_FACE) l_faces = [] # an empty list of faces per shell # cycle on faces while shell_faces_explorer.More(): topo_face = topods.Face(shell_faces_explorer.Current()) mesh_point, edge_point = self.parse_face(topo_face) l_faces.append((mesh_point, edge_point)) shell_faces_explorer.Next() l_shells.append(l_faces) n_shells += 1 shells_explorer.Next() else: # cycle only on faces shell_faces_explorer = TopExp_Explorer(self.shape, TopAbs_FACE) l_faces = [] # an empty list of faces per shell while shell_faces_explorer.More(): topo_face = topods.Face(shell_faces_explorer.Current()) mesh_point, edge_point = self.parse_face(topo_face) l_faces.append((mesh_point, edge_point)) shell_faces_explorer.Next() l_shells.append(l_faces) n_shells += 1 return l_shells
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(self, filename): """ Method to parse the file `filename`. It returns a matrix with all the coordinates. :param string filename: name of the input file. :return: mesh_points: it is a `n_points`-by-3 matrix containing the coordinates of the points of the mesh :rtype: numpy.ndarray """ self.infile = filename self.shape = self.load_shape_from_file(filename) # cycle on the faces to get the control points # init some quantities n_faces = 0 control_point_position = [0] faces_explorer = TopExp_Explorer(self.shape, TopAbs_FACE) mesh_points = np.zeros(shape=(0, 3)) while faces_explorer.More(): # performing some conversions to get the right format (BSplineSurface) face = OCC.TopoDS.topods_Face(faces_explorer.Current()) nurbs_converter = BRepBuilderAPI_NurbsConvert(face) nurbs_converter.Perform(face) nurbs_face = nurbs_converter.Shape() brep_face = BRep_Tool.Surface(OCC.TopoDS.topods_Face(nurbs_face)) bspline_face = geomconvert_SurfaceToBSplineSurface(brep_face) # openCascade object occ_face = bspline_face.GetObject() # extract the Control Points of each face n_poles_u = occ_face.NbUPoles() n_poles_v = occ_face.NbVPoles() control_polygon_coordinates = np.zeros(\ shape=(n_poles_u * n_poles_v, 3)) # cycle over the poles to get their coordinates i = 0 for pole_u_direction in range(n_poles_u): for pole_v_direction in range(n_poles_v): control_point_coordinates = occ_face.Pole(\ pole_u_direction + 1, pole_v_direction + 1) control_polygon_coordinates[i, :] = [control_point_coordinates.X(),\ control_point_coordinates.Y(),\ control_point_coordinates.Z()] i += 1 # pushing the control points coordinates to the mesh_points array (used for FFD) mesh_points = np.append(mesh_points, control_polygon_coordinates, axis=0) control_point_position.append(control_point_position[-1] + n_poles_u * n_poles_v) n_faces += 1 faces_explorer.Next() self._control_point_position = control_point_position return mesh_points
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)
class Topo(object): ''' Topology traversal ''' def __init__(self, myShape, ignore_orientation=False): """ implements topology traversal from any TopoDS_Shape this class lets you find how various topological entities are connected from one to another find the faces connected to an edge, find the vertices this edge is made from, get all faces connected to a vertex, and find out how many topological elements are connected from a source *note* when traversing TopoDS_Wire entities, its advised to use the specialized ``WireExplorer`` class, which will return the vertices / edges in the expected order :param myShape: the shape which topology will be traversed :param ignore_orientation: filter out TopoDS_* entities of similar TShape but different Orientation for instance, a cube has 24 edges, 4 edges for each of 6 faces that results in 48 vertices, while there are only 8 vertices that have a unique geometric coordinate in certain cases ( computing a graph from the topology ) its preferable to return topological entities that share similar geometry, though differ in orientation by setting the ``ignore_orientation`` variable to True, in case of a cube, just 12 edges and only 8 vertices will be returned for further reference see TopoDS_Shape IsEqual / IsSame methods """ self.myShape = myShape self.ignore_orientation = ignore_orientation # the topoFactory dicts maps topology types and functions that can # create this topology self.topoFactory = { TopAbs_VERTEX: topods.Vertex, TopAbs_EDGE: topods.Edge, TopAbs_FACE: topods.Face, TopAbs_WIRE: topods.Wire, TopAbs_SHELL: topods.Shell, TopAbs_SOLID: topods.Solid, TopAbs_COMPOUND: topods.Compound, TopAbs_COMPSOLID: topods.CompSolid } def _loop_topo(self, topologyType, topologicalEntity=None, topologyTypeToAvoid=None): ''' this could be a faces generator for a python TopoShape class that way you can just do: for face in srf.faces: processFace(face) ''' topoTypes = { TopAbs_VERTEX: TopoDS_Vertex, TopAbs_EDGE: TopoDS_Edge, TopAbs_FACE: TopoDS_Face, TopAbs_WIRE: TopoDS_Wire, TopAbs_SHELL: TopoDS_Shell, TopAbs_SOLID: TopoDS_Solid, TopAbs_COMPOUND: TopoDS_Compound, TopAbs_COMPSOLID: TopoDS_CompSolid } assert topologyType in topoTypes.keys(), '%s not one of %s' % ( topologyType, topoTypes.keys()) self.topExp = TopExp_Explorer() # use self.myShape if nothing is specified if topologicalEntity is None and topologyTypeToAvoid is None: self.topExp.Init(self.myShape, topologyType) elif topologicalEntity is None and topologyTypeToAvoid is not None: self.topExp.Init(self.myShape, topologyType, topologyTypeToAvoid) elif topologyTypeToAvoid is None: self.topExp.Init(topologicalEntity, topologyType) elif topologyTypeToAvoid: self.topExp.Init(topologicalEntity, topologyType, topologyTypeToAvoid) seq = [] hashes = [] # list that stores hashes to avoid redundancy occ_seq = TopTools_ListOfShape() while self.topExp.More(): current_item = self.topExp.Current() current_item_hash = current_item.__hash__() if not current_item_hash in hashes: hashes.append(current_item_hash) occ_seq.Append(current_item) self.topExp.Next() # Convert occ_seq to python list occ_iterator = TopTools_ListIteratorOfListOfShape(occ_seq) while occ_iterator.More(): topo_to_add = self.topoFactory[topologyType](occ_iterator.Value()) seq.append(topo_to_add) occ_iterator.Next() if self.ignore_orientation: # filter out those entities that share the same TShape # but do *not* share the same orientation filter_orientation_seq = [] for i in seq: _present = False for j in filter_orientation_seq: if i.IsSame(j): _present = True break if _present is False: filter_orientation_seq.append(i) return filter_orientation_seq else: return iter(seq) def faces(self): ''' loops over all faces ''' return self._loop_topo(TopAbs_FACE) def _number_of_topo(self, iterable): n = 0 for i in iterable: n += 1 return n def number_of_faces(self): return self._number_of_topo(self.faces()) def vertices(self): ''' loops over all vertices ''' return self._loop_topo(TopAbs_VERTEX) def number_of_vertices(self): return self._number_of_topo(self.vertices()) def edges(self): ''' loops over all edges ''' return self._loop_topo(TopAbs_EDGE) def number_of_edges(self): return self._number_of_topo(self.edges()) def wires(self): ''' loops over all wires ''' return self._loop_topo(TopAbs_WIRE) def number_of_wires(self): return self._number_of_topo(self.wires()) def shells(self): ''' loops over all shells ''' return self._loop_topo(TopAbs_SHELL, None) def number_of_shells(self): return self._number_of_topo(self.shells()) def solids(self): ''' loops over all solids ''' return self._loop_topo(TopAbs_SOLID, None) def number_of_solids(self): return self._number_of_topo(self.solids()) def comp_solids(self): ''' loops over all compound solids ''' return self._loop_topo(TopAbs_COMPSOLID) def number_of_comp_solids(self): return self._number_of_topo(self.comp_solids()) def compounds(self): ''' loops over all compounds ''' return self._loop_topo(TopAbs_COMPOUND) def number_of_compounds(self): return self._number_of_topo(self.compounds()) def ordered_vertices_from_wire(self, wire): ''' @param wire: TopoDS_Wire ''' we = WireExplorer(wire) return we.ordered_vertices() def number_of_ordered_vertices_from_wire(self, wire): return self._number_of_topo(self.ordered_vertices_from_wire(wire)) def ordered_edges_from_wire(self, wire): ''' @param wire: TopoDS_Wire ''' we = WireExplorer(wire) return we.ordered_edges() def number_of_ordered_edges_from_wire(self, wire): return self._number_of_topo(self.ordered_edges_from_wire(wire)) def _map_shapes_and_ancestors(self, topoTypeA, topoTypeB, topologicalEntity): ''' using the same method @param topoTypeA: @param topoTypeB: @param topologicalEntity: ''' topo_set = set() _map = TopTools_IndexedDataMapOfShapeListOfShape() topexp_MapShapesAndAncestors(self.myShape, topoTypeA, topoTypeB, _map) results = _map.FindFromKey(topologicalEntity) if results.IsEmpty(): yield None topology_iterator = TopTools_ListIteratorOfListOfShape(results) while topology_iterator.More(): topo_entity = self.topoFactory[topoTypeB]( topology_iterator.Value()) # return the entity if not in set # to assure we're not returning entities several times if not topo_entity in topo_set: if self.ignore_orientation: unique = True for i in topo_set: if i.IsSame(topo_entity): unique = False break if unique: yield topo_entity else: yield topo_entity topo_set.add(topo_entity) topology_iterator.Next() def _number_shapes_ancestors(self, topoTypeA, topoTypeB, topologicalEntity): '''returns the number of shape ancestors If you want to know how many edges a faces has: _number_shapes_ancestors(self, TopAbs_EDGE, TopAbs_FACE, edg) will return the number of edges a faces has @param topoTypeA: @param topoTypeB: @param topologicalEntity: ''' topo_set = set() _map = TopTools_IndexedDataMapOfShapeListOfShape() topexp_MapShapesAndAncestors(self.myShape, topoTypeA, topoTypeB, _map) results = _map.FindFromKey(topologicalEntity) if results.IsEmpty(): return None topology_iterator = TopTools_ListIteratorOfListOfShape(results) while topology_iterator.More(): topo_set.add(topology_iterator.Value()) topology_iterator.Next() return len(topo_set) # ====================================================================== # EDGE <-> FACE # ====================================================================== def faces_from_edge(self, edge): """ :param edge: :return: """ return self._map_shapes_and_ancestors(TopAbs_EDGE, TopAbs_FACE, edge) def number_of_faces_from_edge(self, edge): """ :param edge: :return: """ return self._number_shapes_ancestors(TopAbs_EDGE, TopAbs_FACE, edge) def edges_from_face(self, face): """ :param face: :return: """ return self._loop_topo(TopAbs_EDGE, face) def number_of_edges_from_face(self, face): cnt = 0 for i in self._loop_topo(TopAbs_EDGE, face): cnt += 1 return cnt # ====================================================================== # VERTEX <-> EDGE # ====================================================================== def vertices_from_edge(self, edg): return self._loop_topo(TopAbs_VERTEX, edg) def number_of_vertices_from_edge(self, edg): cnt = 0 for i in self._loop_topo(TopAbs_VERTEX, edg): cnt += 1 return cnt def edges_from_vertex(self, vertex): return self._map_shapes_and_ancestors(TopAbs_VERTEX, TopAbs_EDGE, vertex) def number_of_edges_from_vertex(self, vertex): return self._number_shapes_ancestors(TopAbs_VERTEX, TopAbs_EDGE, vertex) # ====================================================================== # WIRE <-> EDGE # ====================================================================== def edges_from_wire(self, wire): return self._loop_topo(TopAbs_EDGE, wire) def number_of_edges_from_wire(self, wire): cnt = 0 for i in self._loop_topo(TopAbs_EDGE, wire): cnt += 1 return cnt def wires_from_edge(self, edg): return self._map_shapes_and_ancestors(TopAbs_EDGE, TopAbs_WIRE, edg) def wires_from_vertex(self, edg): return self._map_shapes_and_ancestors(TopAbs_VERTEX, TopAbs_WIRE, edg) def number_of_wires_from_edge(self, edg): return self._number_shapes_ancestors(TopAbs_EDGE, TopAbs_WIRE, edg) # ====================================================================== # WIRE <-> FACE # ====================================================================== def wires_from_face(self, face): return self._loop_topo(TopAbs_WIRE, face) def number_of_wires_from_face(self, face): cnt = 0 for i in self._loop_topo(TopAbs_WIRE, face): cnt += 1 return cnt def faces_from_wire(self, wire): return self._map_shapes_and_ancestors(TopAbs_WIRE, TopAbs_FACE, wire) def number_of_faces_from_wires(self, wire): return self._number_shapes_ancestors(TopAbs_WIRE, TopAbs_FACE, wire) # ====================================================================== # VERTEX <-> FACE # ====================================================================== def faces_from_vertex(self, vertex): return self._map_shapes_and_ancestors(TopAbs_VERTEX, TopAbs_FACE, vertex) def number_of_faces_from_vertex(self, vertex): return self._number_shapes_ancestors(TopAbs_VERTEX, TopAbs_FACE, vertex) def vertices_from_face(self, face): return self._loop_topo(TopAbs_VERTEX, face) def number_of_vertices_from_face(self, face): cnt = 0 for i in self._loop_topo(TopAbs_VERTEX, face): cnt += 1 return cnt # ====================================================================== # FACE <-> SOLID # ====================================================================== def solids_from_face(self, face): return self._map_shapes_and_ancestors(TopAbs_FACE, TopAbs_SOLID, face) def number_of_solids_from_face(self, face): return self._number_shapes_ancestors(TopAbs_FACE, TopAbs_SOLID, face) def faces_from_solids(self, solid): return self._loop_topo(TopAbs_FACE, solid) def number_of_faces_from_solids(self, solid): cnt = 0 for i in self._loop_topo(TopAbs_FACE, solid): cnt += 1 return cnt
def write_shape(self, l_shells, filename, tol): """ Method to recreate a TopoDS_Shape associated to a geometric shape after the modification of points of each Face. It returns a TopoDS_Shape (Shape). :param l_shells: the list of shells after initial parsing :param filename: the output filename :param tol: tolerance on the surface creation after modification :return: None """ self.outfile = filename # global compound containing multiple shells global_compound_builder = BRep_Builder() global_comp = TopoDS_Compound() global_compound_builder.MakeCompound(global_comp) if self.check_topo == 0: # cycle on shells (multiple objects) shape_shells_explorer = TopExp_Explorer( self.shape.Oriented(TopAbs_FORWARD), TopAbs_SHELL) ishell = 0 while shape_shells_explorer.More(): per_shell = topods_Shell(shape_shells_explorer.Current()) # a local compound containing a shell compound_builder = BRep_Builder() comp = TopoDS_Compound() compound_builder.MakeCompound(comp) # cycle on faces faces_explorer = TopExp_Explorer( per_shell.Oriented(TopAbs_FORWARD), TopAbs_FACE) iface = 0 while faces_explorer.More(): topoface = topods.Face(faces_explorer.Current()) newface = self.write_face(l_shells[ishell][iface][0], l_shells[ishell][iface][1], topoface, tol) # add face to compound compound_builder.Add(comp, newface) iface += 1 faces_explorer.Next() new_shell = self.combine_faces(comp, 0.01) itype = TopoDS_Shape.ShapeType(new_shell) # add the new shell to the global compound global_compound_builder.Add(global_comp, new_shell) print("Shell {0} of type {1} Processed ".format(ishell, itype)) print "==============================================" ishell += 1 shape_shells_explorer.Next() else: # cycle on faces # a local compound containing a shell compound_builder = BRep_Builder() comp = TopoDS_Compound() compound_builder.MakeCompound(comp) # cycle on faces faces_explorer = TopExp_Explorer( self.shape.Oriented(TopAbs_FORWARD), TopAbs_FACE) iface = 0 while faces_explorer.More(): topoface = topods.Face(faces_explorer.Current()) newface = self.write_face(l_shells[0][iface][0], l_shells[0][iface][1], topoface, tol) # add face to compound compound_builder.Add(comp, newface) iface += 1 faces_explorer.Next() new_shell = self.combine_faces(comp, 0.01) itype = TopoDS_Shape.ShapeType(new_shell) # add the new shell to the global compound global_compound_builder.Add(global_comp, new_shell) print("Shell {0} of type {1} Processed ".format(0, itype)) print "==============================================" self.write_shape_to_file(global_comp, self.outfile)
mkWire.Add(aWire.Wire()) mkWire.Add(aMirroredWire) myWireProfile = mkWire.Wire() # The face that we'll sweep to make the prism myFaceProfile = BRepBuilderAPI_MakeFace(myWireProfile) # We want to sweep the face along the Z axis to the height aPrismVec = gp_Vec(0, 0, height) myBody = BRepPrimAPI_MakePrism(myFaceProfile.Face(), aPrismVec) # Add fillets to all edges through the explorer mkFillet = BRepFilletAPI_MakeFillet(myBody.Shape()) anEdgeExplorer = TopExp_Explorer(myBody.Shape(), TopAbs_EDGE) while anEdgeExplorer.More(): anEdge = topods.Edge(anEdgeExplorer.Current()) mkFillet.Add(thickness / 12.0, anEdge) anEdgeExplorer.Next() myBody = mkFillet # Create the neck of the bottle neckLocation = gp_Pnt(0, 0, height) neckAxis = gp_DZ() neckAx2 = gp_Ax2(neckLocation, neckAxis) myNeckRadius = thickness / 4.0 myNeckHeight = height / 10.0
mkWire = MakeWire() mkWire.Add(aWire.Wire()) mkWire.Add(aMirroredWire) myWireProfile = mkWire.Wire() # Body : Prism the Profile myFaceProfile = MakeFace(myWireProfile) if myFaceProfile.IsDone(): bottomFace = myFaceProfile.Face() aPrismVec = gp_Vec(0, 0, myHeight) myBody = MakePrism(myFaceProfile.Shape(), aPrismVec) # Body : Apply Fillets mkFillet = MakeFillet(myBody.Shape()) aEdgeExplorer = Explorer(myBody.Shape(), OCC.TopAbs.TopAbs_EDGE) while aEdgeExplorer.More(): aEdge = OCC.TopoDS.topods_Edge(aEdgeExplorer.Current()) mkFillet.Add(myThickness / 12, aEdge) aEdgeExplorer.Next() myBody = mkFillet.Shape() # Body : Add the Neck neckLocation = OCC.gp.gp_Pnt(0, 0, myHeight) neckNormal = OCC.gp.gp_DZ() neckAx2 = OCC.gp.gp_Ax2(neckLocation, neckNormal) myNeckRadius = myThickness / 4 myNeckHeight = myHeight / 10 MKCylinder = MakeCylinder(neckAx2, myNeckRadius, myNeckHeight) myNeck = MKCylinder.Shape() myBody = Fuse(myBody, myNeck).Shape()