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
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
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() # Body : Create a Hollowed Solid faceToRemove = OCC.TopoDS.TopoDS_Face() aFaceExplorer = OCC.TopExp.TopExp_Explorer(myBody, OCC.TopAbs.TopAbs_FACE)
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 # cylOrigin = self.secOrigin + self.T * self.wDir innerCyl = BRepPrimAPI_MakeCylinder( gp_Ax2(getGpPt(cylOrigin), getGpDir(self.wDir)), self.r1, self.H).Shape() # outerCyl = BRepPrimAPI_MakeCylinder(gp_Ax2(getGpPt(cylOrigin), getGpDir(self.wDir)), self.r2, self.H).Shape() # nutBody = BRepAlgoAPI_Fuse(prism, outerCyl).Shape() # my_cyl = BRepPrimAPI_MakeCylinder(9.0, 6.0).Shape() # result_shape = BRepAlgoAPI_Cut(nutBody, innerCyl).Shape() result_shape = BRepAlgoAPI_Cut(prism, innerCyl).Shape() # self.secOrigin = gp_Pnt(0 , 0 , 0) # neckNormal = gp_DZ() # # Threading : Create Surfaces # # nutAx2_bis = gp_Ax3(self.secOrigin , neckNormal) # aCyl1 = Geom_CylindricalSurface(nutAx2_bis , self.T * 0.99) # aCyl2 = Geom_CylindricalSurface(nutAx2_bis , self.T * 1.05) # #aCyl3 = Geom_CylindricalSurface(nutAx2_bis , self.T * 1.11) # aCyl1_handle = aCyl1.GetHandle() # aCyl2_handle = aCyl2.GetHandle() # #aCyl3_handle = aCyl3.GetHandle() # # # Threading : Define 2D Curves # aPnt = gp_Pnt2d(2. * math.pi , self.H / 2.) # aDir = gp_Dir2d(2. * math.pi , self.H / 4.) # aAx2d = gp_Ax2d(aPnt , aDir) # aMajor = 2. * math.pi # aMinor = self.H / 7. # anEllipse1 = Geom2d_Ellipse(aAx2d , aMajor , aMinor) # anEllipse2 = Geom2d_Ellipse(aAx2d , aMajor , aMinor / 4.) # anEllipse1_handle = anEllipse1.GetHandle() # anEllipse2_handle = anEllipse2.GetHandle() # aArc1 = Geom2d_TrimmedCurve(anEllipse1_handle, 0 , math.pi) # aArc2 = Geom2d_TrimmedCurve(anEllipse2_handle, 0 , math.pi) # aArc1_handle = aArc1.GetHandle() # aArc2_handle = aArc2.GetHandle() # anEllipsePnt1 = anEllipse1.Value(0) # anEllipsePnt2 = anEllipse1.Value(math.pi) # aSegment = GCE2d_MakeSegment(anEllipsePnt1 , anEllipsePnt2) # # # Threading : Build Edges and Wires # # aEdge1OnSurf1 = BRepBuilderAPI_MakeEdge( aArc1_handle , aCyl1_handle) # aEdge2OnSurf1 = BRepBuilderAPI_MakeEdge( aSegment.Value() , aCyl1_handle) # aEdge1OnSurf2 = BRepBuilderAPI_MakeEdge( aArc2_handle , aCyl2_handle) # aEdge2OnSurf2 = BRepBuilderAPI_MakeEdge( aSegment.Value() , aCyl2_handle) # threadingWire1 = BRepBuilderAPI_MakeWire(aEdge1OnSurf1.Edge() , aEdge2OnSurf1.Edge())#aEdge3OnSurf1.Edge()) # self.threading1 = threadingWire1 # threadingWire2 = BRepBuilderAPI_MakeWire(aEdge1OnSurf2.Edge() , aEdge2OnSurf2.Edge())#aEdge3OnSurf2.Edge()) # BRepLib.breplib.BuildCurves3d(threadingWire1.Shape()) # BRepLib.breplib.BuildCurves3d(threadingWire2.Shape()) # # # Create Threading # # aTool = BRepOffsetAPI_ThruSections(True) # aTool.AddWire(threadingWire1.Wire()) # aTool.AddWire(threadingWire2.Wire()) # aTool.CheckCompatibility(False) # myThreading = aTool.Shape() # # #Building the resulting compound # # aRes = TopoDS_Compound() # aBuilder = BRep.BRep_Builder() # aBuilder.MakeCompound(aRes) # aBuilder.Add(aRes, result_shape) # aBuilder.Add(aRes, myThreading) # final_shape = BRepAlgoAPI_Cut(result_shape, myThreading).Shape() return result_shape
def read(cls, filename): controller = IGESControl_Controller() controller.Init() reader = IGESControl_Reader() reader.ReadFile(filename) reader.TransferRoots() shape = reader.OneShape() n_faces = 0 control_point_position = [0] faces_explorer = TopExp_Explorer(shape, TopAbs_FACE) mesh_points = np.zeros(shape=(0, 3)) while faces_explorer.More(): # performing some conversions to get the right format (BSplineSurface) face = topods_Face(faces_explorer.Current()) nurbs_converter = BRepBuilderAPI_NurbsConvert(face) nurbs_converter.Perform(face) nurbs_face = nurbs_converter.Shape() brep_face = BRep_Tool.Surface(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): print(pole_u_direction, pole_v_direction) 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() return { 'shape': shape, 'points': mesh_points, 'control_point_position': control_point_position }
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 __init__(self, shape): from OCC.BRep import BRep_Tool from OCC.BRepMesh import BRepMesh_IncrementalMesh from OCC.TopAbs import TopAbs_FACE, TopAbs_VERTEX from OCC.TopExp import TopExp_Explorer from OCC.TopLoc import TopLoc_Location from OCC.TopoDS import topods_Face, topods_Vertex, TopoDS_Iterator vertices = [] # a (nested) list of vec3 triangles = [] # a (flat) list of integers normals = [] uv = [] # Mesh the shape linDeflection = 0.8 BRepMesh_IncrementalMesh(shape, linDeflection) bt = BRep_Tool() # Explore the faces of the shape # each face is triangulated, we need to collect all the parts expFac = TopExp_Explorer(shape, TopAbs_FACE) while expFac.More(): face = topods_Face(expFac.Current()) location = TopLoc_Location() facing = (bt.Triangulation(face, location)).GetObject() try: tri = facing.Triangles() nTri = facing.NbTriangles() ver = facing.Nodes() except: tri = None nTri = None ver = None # store origin of the face's local coordinates transf = face.Location().Transformation() # iterate over triangles and store indices of vertices defining each triangle # OCC uses one-based indexing for i in range(1, nTri + 1): # each triangle is defined by three points # each point is defined by its index in the list of vertices index1, index2, index3 = tri.Value(i).Get() indices = [index1, index2, index3] # python uses zero-based indexing # for each vertex of a triangle, check whether it is already known # then store it (or not) and update the index for idx in [0, 1, 2]: # read global coordinates of each point vec3 = [ ver.Value(indices[idx]).Transformed(transf).X(), ver.Value(indices[idx]).Transformed(transf).Y(), ver.Value(indices[idx]).Transformed(transf).Z() ] if vec3 not in vertices: vertices.append(vec3) indices[idx] = vertices.index(vec3) triangles.extend(indices) expFac.Next() self.shape = shape self.vertices = vertices self.triangles = triangles self.normals = normals self.uv = uv
# 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: # Otherwise, just quit exit("No solids found in input. Aborting.") # Create a matplot lib figure and display the particles in 3D # fig = plt.figure() # ax = fig.add_subplot(111, projection='3d') # ax.scatter(allPointsDataFrame.x, allPointsDataFrame.y, allPointsDataFrame.z) # plt.show() # Show the actual shape in 3D # display, start_display, add_menu, add_function_to_menu = init_display() # display.DisplayShape(vertexList, update=True) # start_display()
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)
# 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 mkCylinder = BRepPrimAPI_MakeCylinder(neckAx2, myNeckRadius, myNeckHeight) myBody = BRepAlgoAPI_Fuse(myBody.Shape(), mkCylinder.Shape())
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
from OCC.TopAbs import TopAbs_VERTEX, TopAbs_EDGE from OCC.TopTools import (TopTools_IndexedDataMapOfShapeListOfShape, TopTools_ListIteratorOfListOfShape) from OCC.TopoDS import topods_Vertex, topods_Edge from OCC.Display.SimpleGui import init_display display, start_display, add_menu, add_function_to_menu = init_display('wx') # create shape cube = BRepPrimAPI_MakeBox(100, 100, 100).Shape() topExp = TopExp_Explorer() topExp.Init(cube, TopAbs_VERTEX) # get two vertices vertA = topods_Vertex(topExp.Current()) topExp.Next() vertB = topods_Vertex(topExp.Current()) def vertex_fillet(cube, vert): # apply a fillet on incident edges on a vertex afillet = BRepFilletAPI_MakeFillet(cube) cnt = 0 # find edges from vertex _map = TopTools_IndexedDataMapOfShapeListOfShape() topexp_MapShapesAndAncestors(cube, TopAbs_VERTEX, TopAbs_EDGE, _map) results = _map.FindFromKey(vert) topology_iterator = TopTools_ListIteratorOfListOfShape(results) while topology_iterator.More(): edge = topods_Edge(topology_iterator.Value()) topology_iterator.Next()
#!/usr/bin/env python ##Copyright 2008-2015 Jelle Feringa ([email protected]) ## ##This file is part of pythonOCC. ## ##pythonOCC is free software: you can redistribute it and/or modify ##it under the terms of the GNU Lesser General Public License as published by ##the Free Software Foundation, either version 3 of the License, or ##(at your option) any later version. ## ##pythonOCC is distributed in the hope that it will be useful, ##but WITHOUT ANY WARRANTY; without even the implied warranty of ##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ##GNU Lesser General Public License for more details. ## ##You should have received a copy of the GNU Lesser General Public License ##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>. # A sample that shows how to generate the gear geometry according # to knowledge from OCC.BRepFilletAPI import BRepFilletAPI_MakeFillet from OCC.BRep import BRep_Tool from OCC.BRepPrimAPI import BRepPrimAPI_MakeBox from OCC.TopExp import (TopExp_Explorer, topexp_MapShapesAndAncestors, topexp_FirstVertex, topexp_LastVertex) from OCC.TopAbs import TopAbs_VERTEX, TopAbs_EDGE from OCC.TopTools import (TopTools_IndexedDataMapOfShapeListOfShape, TopTools_ListIteratorOfListOfShape) from OCC.TopoDS import topods_Vertex, topods_Edge from OCC.Display.SimpleGui import init_display display, start_display, add_menu, add_function_to_menu = init_display()
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_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_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)
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: radius = obj.radius t1, t2 = obj.t1, obj.t2 center = obj.get_center(XYZ) circle_width, circle_height = 2 * radius, 2 * radius arc = mpatches.Arc(xy=center, width=circle_width, height=circle_height, angle=0, theta1=t1, theta2=t2, color="red") plt.gca().add_patch(arc) elif type(obj) == BSpline3D: p = obj.get_polylines(n=100) x, y, z = p.T plt.plot(x, z, color="blue") exp.Next()
def create_model(self): ###################################################### edges = [] if self.R2 == 0.0 or self.R1 == 0.0: self.a3 = self.a4 = self.a5 edge1 = make_edge(getGpPt(self.a1), getGpPt(self.a2)) edges.append(edge1) edge2 = make_edge(getGpPt(self.a2), getGpPt(self.a3)) edges.append(edge2) edge3 = make_edge(getGpPt(self.a3), getGpPt(self.a6)) edges.append(edge3) # arc2 = GC_MakeArcOfCircle(getGpPt(self.a6), getGpPt(self.a7), getGpPt(self.a8)) # edge4 = make_edge(arc2.Value()) # edges.append(edge4) # edge5 = make_edge(getGpPt(self.a8), getGpPt(self.a9)) # edges.append(edge5) # edge6 = make_edge(getGpPt(self.a9), getGpPt(self.a12)) # edges.append(edge6) # edge7 = make_edge(getGpPt(self.a12), getGpPt(self.a1)) # edges.append(edge7) edge4 = make_edge(getGpPt(self.a6), getGpPt(self.a9)) edges.append(edge4) edge5 = make_edge(getGpPt(self.a9), getGpPt(self.a12)) edges.append(edge5) edge6 = make_edge(getGpPt(self.a12), getGpPt(self.a1)) edges.append(edge6) else: edge1 = make_edge(getGpPt(self.a1), getGpPt(self.a2)) edges.append(edge1) edge2 = make_edge(getGpPt(self.a2), getGpPt(self.a3)) edges.append(edge2) arc1 = GC_MakeArcOfCircle(getGpPt(self.a3), getGpPt(self.a4), getGpPt(self.a5)) edge3 = make_edge(arc1.Value()) edges.append(edge3) edge4 = make_edge(getGpPt(self.a5), getGpPt(self.a6)) edges.append(edge4) arc2 = GC_MakeArcOfCircle(getGpPt(self.a6), getGpPt(self.a7), getGpPt(self.a8)) edge5 = make_edge(arc2.Value()) edges.append(edge5) edge6 = make_edge(getGpPt(self.a8), getGpPt(self.a9)) edges.append(edge6) arc3 = GC_MakeArcOfCircle(getGpPt(self.a9), getGpPt(self.a10), getGpPt(self.a11)) edge7 = make_edge(arc3.Value()) edges.append(edge7) edge8 = make_edge(getGpPt(self.a11), getGpPt(self.a12)) edges.append(edge8) edge9 = make_edge(getGpPt(self.a12), getGpPt(self.a1)) edges.append(edge9) wire = makeWireFromEdges(edges) aFace = makeFaceFromWire(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