def occ_topo_list(shape): """ return the edges & faces from `shape` :param shape: a TopoDS_Shape :return: a list of edges and faces """ from OCC.TopAbs import TopAbs_FACE from OCC.TopAbs import TopAbs_EDGE from OCC.TopExp import TopExp_Explorer from OCC.TopoDS import topods_Face, topods_Edge topExp = TopExp_Explorer() topExp.Init(shape, TopAbs_FACE) faces = [] edges = [] while topExp.More(): face = topods_Face(topExp.Current()) faces.append(face) topExp.Next() topExp.Init(shape, TopAbs_EDGE) while topExp.More(): edge = topods_Edge(topExp.Current()) edges.append(edge) topExp.Next() return faces, edges
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 FilletFaceCorners(face, radius): """Fillets the corners of the input face Parameters ---------- face : TopoDS_Face radius : the Fillet radius Returns ------- """ vert_explorer = TopExp_Explorer(face, TopAbs_VERTEX) from OCC.BRepFilletAPI import BRepFilletAPI_MakeFillet2d fillet = BRepFilletAPI_MakeFillet2d(face) while vert_explorer.More(): vertex = topods_Vertex(vert_explorer.Current()) fillet.AddFillet(vertex, radius) # Note: Needed two next statements here as faces have a vertex on # either side vert_explorer.Next() vert_explorer.Next() fillet.Build() face = fillet.Shape() return 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 split_edge_with_face(event=None): display.EraseAll() p0 = gp_Pnt() vnorm = gp_Dir(1, 0, 0) pln = gp_Pln(p0, vnorm) face = BRepBuilderAPI_MakeFace(pln, -10, 10, -10, 10).Face() p1 = gp_Pnt(0, 0, 15) p2 = gp_Pnt(0, 0, -15) edge = BRepBuilderAPI_MakeEdge(p1, p2).Edge() # Initialize splitter splitter = GEOMAlgo_Splitter() # Add the edge as an argument and the face as a tool. This will split # the edge with the face. splitter.AddArgument(edge) splitter.AddTool(face) splitter.Perform() edges = [] exp = TopExp_Explorer(splitter.Shape(), TopAbs_EDGE) while exp.More(): edges.append(exp.Current()) exp.Next() print('Number of edges in split shape: ', len(edges)) display.DisplayShape(edges[0], color='red') display.DisplayShape(edges[1], color='green') display.DisplayShape(edges[2], color='yellow') display.FitAll()
def occ_triangle_mesh(event=None): # # Mesh the shape # BRepMesh_IncrementalMesh(aShape, 0.1) builder = BRep_Builder() Comp = TopoDS_Compound() builder.MakeCompound(Comp) ex = TopExp_Explorer(aShape, TopAbs_FACE) while ex.More(): F = topods_Face(ex.Current()) L = TopLoc_Location() facing = (BRep_Tool().Triangulation(F, L)).GetObject() tab = facing.Nodes() tri = facing.Triangles() for i in range(1, facing.NbTriangles() + 1): trian = tri.Value(i) #print trian 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.DisplayShape(Comp, update=True)
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 CutSect(Shape, SpanStation): """ Parameters ---------- Shape : TopoDS_Shape The Shape to find planar cut section (parallel to xz plane) SpanStation : scalar in range (0, 1) y-direction location at which to cut Shape Returns ------- Section : result of OCC.BRepAlgoAPI.BRepAlgoAPI_Section (TopoDS_Shape) The cut section of shape given a cut plane parallel to xz at input Spanstation. Chord : result of OCC.GC.GC_MakeSegment.Value (Geom_TrimmedCurve) The Chord line between x direction extremeties """ (Xmin, Ymin, Zmin, Xmax, Ymax, Zmax) = ObjectsExtents([Shape]) YStation = Ymin + (Ymax - Ymin) * SpanStation OriginX = Xmin - 1 OriginZ = Zmin - 1 P = gp_Pln(gp_Pnt(OriginX, YStation, OriginZ), gp_Dir(gp_Vec(0, 1, 0))) # Note: using 2*extents here as previous +1 trimmed plane too short CutPlaneSrf = make_face(P, 0, Zmax + 2, 0, Xmax + 2) I = BRepAlgoAPI_Section(Shape, CutPlaneSrf) I.ComputePCurveOn1(True) I.Approximation(True) I.Build() Section = I.Shape() (Xmin, Ymin, Zmin, Xmax, Ymax, Zmax) = ObjectsExtents([Section]) # Currently assume only one edge exists in the intersection: exp = TopExp_Explorer(Section, TopAbs_EDGE) edge = topods_Edge(exp.Current()) # Find the apparent chord of the section (that is, the line connecting the # fore most and aftmost points on the curve DivPoints = Uniform_Points_on_Curve(edge, 200) Xs = np.array([pt.X() for pt in DivPoints]) min_idx = np.argmin(Xs) LeadingPoint = gp_Pnt(Xs[min_idx], DivPoints[min_idx].Y(), DivPoints[min_idx].Z()) max_idx = np.argmax(Xs) TrailingPoint = gp_Pnt(Xs[max_idx], DivPoints[max_idx].Y(), DivPoints[max_idx].Z()) HChord = GC_MakeSegment(TrailingPoint, LeadingPoint).Value() # Chord = HChord.GetObject() return Section, HChord
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 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 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 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 _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 SplitShapeFromProjection(shape, wire, direction, return_section=True): """Splits shape by the projection of wire onto its face Parameters ---------- shape : TopoDS_Shape the brep to subtract from wire : TopoDS_Wire the tool to use for projection and splitting direction: OCC.gp.gp_Dir the direction to project the wire return_section : bool returns the split shape Returns ------- newshape : TopoDS_Shape input shape with wire subtracted section : the shape which was substracted (returned only if return_section is true) Notes ----- Currently assumes splits the first face only """ # get the face from the shape exp = TopExp_Explorer(shape, TopAbs_FACE) face = topods_Face(exp.Current()) # Perform the projection proj = BRepProj_Projection(wire, face, direction) wire = proj.Current() splitter = BRepFeat_SplitShape(face) splitter.Add(wire, face) splitter.Build() section_list = splitter.DirectLeft() iterator = TopTools_ListIteratorOfListOfShape(section_list) section = iterator.Value() # assume here that only 1 section is produced mod_list = splitter.Modified(face) iterator = TopTools_ListIteratorOfListOfShape(mod_list) newshape = iterator.Value() if return_section: return newshape, wire else: return newshape
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 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 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 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 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 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 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_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: radius = obj.radius
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: # Otherwise, just quit