def collect_dumpable_shapes(self, shape): if shape.ShapeType() == TopAbs_EDGE: points = [] try: points = discretize_edge(shape, 0.25) except: points = [] else: debug("Discretize done !") return points debug("edge: %s/%s"%(str(shape), self.describe_edge(shape))) it = TopoDS_Iterator(shape) points = [] while it.More(): shp = it.Value() brt = BRep_Tool() debug("vertex: %s"%(str(shp),)) pnt = brt.Pnt(topods_Vertex(shp)) points.append((pnt.X(), pnt.Y(), pnt.Z())) it.Next() return points else: lines = [] it = TopoDS_Iterator(shape) while it.More(): shp = it.Value() lines.append(self.collect_dumpable_shapes(shp)) it.Next() return lines
def topo2topotype(occtopology): """ This function converts the original OCCtopology of the given topology. e.g. an OCCcompound that is originally an OCCface etc. Parameters ---------- occtopology : OCCtopology The OCCtopology to be converted. OCCtopology includes: OCCshape, OCCcompound, OCCcompsolid, OCCsolid, OCCshell, OCCface, OCCwire, OCCedge, OCCvertex Returns ------- original topology : OCCtopology The original OCCtopology of the input. """ shapetype = occtopology.ShapeType() if shapetype == TopAbs_COMPOUND: #compound orig_topo = topods_Compound(occtopology) if shapetype == TopAbs_COMPSOLID: #compsolid orig_topo = topods_CompSolid(occtopology) if shapetype == TopAbs_SOLID: #solid orig_topo = topods_Solid(occtopology) if shapetype == TopAbs_SHELL: #shell orig_topo = topods_Shell(occtopology) if shapetype == TopAbs_FACE: #face orig_topo = topods_Face(occtopology) if shapetype == TopAbs_WIRE: #wire orig_topo = topods_Wire(occtopology) if shapetype == TopAbs_EDGE: #edge orig_topo = topods_Edge(occtopology) if shapetype == TopAbs_VERTEX: #vertex orig_topo = topods_Vertex(occtopology) return orig_topo
def topo_explorer(occtopo2explore, topotype2find): """ This function explores and fetches the specified topological type from the given OCCtopology. e.g. find a list of OCCfaces in an OCCcompound. Parameters ---------- occtopo2explore : OCCtopology The OCCtopology to be explored. OCCtopology includes: OCCshape, OCCcompound, OCCcompsolid, OCCsolid, OCCshell, OCCface, OCCwire, OCCedge, OCCvertex topotype2find : str The string describing the topology to find. The strings can be e.g. "compound", "compsolid", "solid", "shell", "face", "wire", "edge", "vertex". Returns ------- list of topology : list of OCCtopology The list of OCCtopology found in the specified OCCtopology. """ geom_list = [] if topotype2find == "compound": shapetype2find_topABS = TopAbs_COMPOUND if topotype2find == "compsolid": shapetype2find_topABS = TopAbs_COMPSOLID if topotype2find == "solid": shapetype2find_topABS = TopAbs_SOLID if topotype2find == "shell": shapetype2find_topABS = TopAbs_SHELL if topotype2find == "face": shapetype2find_topABS = TopAbs_FACE if topotype2find == "wire": shapetype2find_topABS = TopAbs_WIRE if topotype2find == "edge": shapetype2find_topABS = TopAbs_EDGE if topotype2find == "vertex": shapetype2find_topABS = TopAbs_VERTEX ex = TopExp_Explorer(occtopo2explore, shapetype2find_topABS) while ex.More(): if shapetype2find_topABS == 0: geom = topods_Compound(ex.Current()) if shapetype2find_topABS == 1: geom = topods_CompSolid(ex.Current()) if shapetype2find_topABS == 2: geom = topods_Solid(ex.Current()) if shapetype2find_topABS == 3: geom = topods_Shell(ex.Current()) if shapetype2find_topABS == 4: geom = topods_Face(ex.Current()) if shapetype2find_topABS == 5: geom = topods_Wire(ex.Current()) if shapetype2find_topABS == 6: geom = topods_Edge(ex.Current()) if shapetype2find_topABS == 7: geom = topods_Vertex(ex.Current()) geom_list.append(geom) ex.Next() return geom_list
def distPtPtC(self, shapeList, *args): # callback (collector) for distPtPt logger.debug('Edges selected: %s', shapeList) logger.debug('args: %s', args) # args = x, y mouse coords for shape in shapeList: vrtx = topods_Vertex(shape) gpPt = BRep_Tool.Pnt(vrtx) # convert vertex to gp_Pnt self.ptStack.append(gpPt) if len(self.ptStack) == 2: self.distPtPt()
def vertex_clicked(shp, *kwargs): """ This function is called whenever a vertex is selected """ for shape in shp: # this should be a TopoDS_Vertex print("Face selected: ", shape) v = topods_Vertex(shape) pnt = BRep_Tool.Pnt(v) print("3d gp_Pnt selected coordinates : X=", pnt.X(), "Y=", pnt.Y(), "Z=", pnt.Z()) # then convert to screen coordinates screen_coord = display.View.Convert(pnt.X(), pnt.Y(), pnt.Z()) print("2d screen coordinates : ", screen_coord)
def wpBy3PtsC(shapeList, *args): # callback (collector) for wpBy3Pts for shape in shapeList: vrtx = topods_Vertex(shape) gpPt = BRep_Tool.Pnt(vrtx) # convert vertex to gp_Pnt win.ptStack.append(gpPt) if len(win.ptStack) == 1: statusText = "Now select point 2 (wp origin)." win.statusBar().showMessage(statusText) elif len(win.ptStack) == 2: statusText = "Now select point 3 to set uDir." win.statusBar().showMessage(statusText) elif len(win.ptStack) == 3: wpBy3Pts()
def add_vertex_to_xyPtStack(shapeList): """Helper function to convert vertex to gp_Pnt and put on ptStack.""" wp = win.activeWp for shape in shapeList: if isinstance(shape, TopoDS_Vertex): # Guard against wrong type vrtx = topods_Vertex(shape) pnt = BRep_Tool.Pnt(vrtx) # convert vertex to type <gp_Pnt> trsf = wp.Trsf.Inverted() # New transform. Don't invert wp.Trsf pnt.Transform(trsf) pt2d = (pnt.X(), pnt.Y()) # 2d point win.xyPtStack.append(pt2d) else: print(f"(Unwanted) shape type: {type(shape)}")
def revolveC(shapeList, *args): for shape in shapeList: vrtx = topods_Vertex(shape) gpPt = BRep_Tool.Pnt(vrtx) # convert vertex to gp_Pnt win.ptStack.append(gpPt) if len(win.ptStack) == 1: statusText = "Select 2nd point on revolve axis." win.statusBar().showMessage(statusText) elif len(win.ptStack) == 2 and not win.lineEditStack: statusText = "Enter part name." win.statusBar().showMessage(statusText) win.lineEdit.setFocus() if win.lineEditStack and len(win.ptStack) == 2: revolve()
def dump_topology_to_string(shape, level=0, buffer=""): """ Reutnrs the details of an object from the top down """ brt = BRep_Tool() s = shape.ShapeType() if s == TopAbs_VERTEX: pnt = brt.Pnt(topods_Vertex(shape)) print( ".." * level + "<Vertex %i: %s %s %s>\n" % (hash(shape), pnt.X(), pnt.Y(), pnt.Z())) else: print(".." * level, end ="") print(shape_type_string(shape)) it = TopoDS_Iterator(shape) while it.More() and level < 5: # LEVEL MAX shp = it.Value() it.Next() print(dump_topology_to_string(shp, level + 1, buffer))
def dump_topology_to_string(shape, level=0, buffer=""): """ Reutnrs the details of an object from the top down """ brt = BRep_Tool() s = shape.ShapeType() if s == TopAbs_VERTEX: pnt = brt.Pnt(topods_Vertex(shape)) print(".." * level + "<Vertex %i: %s %s %s>\n" % (hash(shape), pnt.X(), pnt.Y(), pnt.Z())) else: print(".." * level, end="") print(shape_type_string(shape)) it = TopoDS_Iterator(shape) while it.More() and level < 5: # LEVEL MAX shp = it.Value() it.Next() dump_topology_to_string(shp, level + 1, buffer)
def dumpTopology(shape, level=0): """ Print the details of an object from the top down """ brt = BRep_Tool() s = shape.ShapeType() if s == TopAbs_VERTEX: pnt = brt.Pnt(topods_Vertex(shape)) print(".." * level + "<Vertex %i: %s %s %s>" % (hash(shape), pnt.X(), pnt.Y(), pnt.Z())) else: print(".." * level, end="") print(shapeTypeString(shape)) it = TopoDS_Iterator(shape) while it.More(): shp = it.Value() it.Next() dumpTopology(shp, level + 1)
def check_remaining(array): from OCC.Extend.TopologyUtils import TopologyExplorer from OCC.Core.BRepClass3d import BRepClass3d_SolidClassifier from OCC.Core.TopoDS import topods_Vertex from OCC.Core.BRep import BRep_Tool for i, si in enumerate(array): while si.to_check: face, isol = si.to_check.pop(0) brt = BRep_Tool() states = [] for v in TopologyExplorer(face).vertices(): pnt = brt.Pnt(topods_Vertex(v)) classifier = BRepClass3d_SolidClassifier( array[isol].solid, gp_Pnt(pnt.X(), pnt.Y(), pnt.Z()), 1.e-10) states.append(classifier.State()) if (0 not in states): #if 'fore' in array[isol].name: print states si.kept.append(face)
def dump_topology_to_string(shape: TopoDS_Shape, level: Optional[int] = 0, buffer: Optional[str] = "") -> None: """ Return the details of an object from the top down """ brt = BRep_Tool() s = shape.ShapeType() if s == TopAbs_VERTEX: pnt = brt.Pnt(topods_Vertex(shape)) print(".." * level + f"<Vertex {hash(shape)}: {pnt.X()} {pnt.Y()} {pnt.Z()}>\n") else: print(".." * level, end="") print(shape) it = TopoDS_Iterator(shape) while it.More() and level < 5: # LEVEL MAX shp = it.Value() it.Next() dump_topology_to_string(shp, level + 1, buffer)
def DumpTop(self, shape, level=0): """ Print the details of an object from the top down """ brt = BRep_Tool() s = shape.ShapeType() if s == TopAbs_VERTEX: pnt = brt.Pnt(topods_Vertex(shape)) dmp = " " * level dmp += "%s - " % shapeTypeString(shape) dmp += "%.5e %.5e %.5e" % (pnt.X(), pnt.Y(), pnt.Z()) print(dmp) else: dmp = " " * level dmp += shapeTypeString(shape) print(dmp) it = TopoDS_Iterator(shape) while it.More(): shp = it.Value() it.Next() self.DumpTop(shp, level + 1)
def parts_in_others_boxes(array): from OCC.Extend.TopologyUtils import TopologyExplorer from OCC.Core.BRepClass3d import BRepClass3d_SolidClassifier from OCC.Core.TopoDS import topods_Vertex from OCC.Core.BRep import BRep_Tool for i, si in enumerate(array): for faci in si.splitsolid: for j, sj in enumerate(array): if i != j: boxj = sj.box.Shape() states = [] for v in TopologyExplorer(faci).vertices(): brt = BRep_Tool() pnt = brt.Pnt(topods_Vertex(v)) classifier = BRepClass3d_SolidClassifier( boxj, gp_Pnt(pnt.X(), pnt.Y(), pnt.Z()), 1.e-12) states.append(classifier.State()) if 0 in states: si.to_check.append([faci, j]) if faci not in [ftc[0] for ftc in si.to_check]: si.kept.append(faci)
def max_counter(self, facesS_2): section_edges = list(Topo(facesS_2).edges()) # print(len(section_edges)) if len(section_edges) > 0: Wire_c = BRepBuilderAPI_MakeWire() prep_list = [] Wire_c.Add(section_edges[0]) prep_list.append(section_edges[0]) ex = TopExp_Explorer(section_edges[0], TopAbs_VERTEX) # no need for a loop since we know for a fact that # the edge has only one start and one end c = ex.Current() cv = topods_Vertex(c) v0 = BRep_Tool_Pnt(cv) ex.Next() c = ex.Current() cv = topods_Vertex(c) v1 = BRep_Tool_Pnt(cv) section_edges.pop(0) flag = 0 wires = [] while len(section_edges) > 0: new_list = [] for edges in section_edges: # Wire_c.Add(edges) ex = TopExp_Explorer(edges, TopAbs_VERTEX) c = ex.Current() cv = topods_Vertex(c) End_1 = BRep_Tool_Pnt(cv) ex.Next() c = ex.Current() cv = topods_Vertex(c) End_2 = BRep_Tool_Pnt(cv) if End_1.X() == v0.X() and End_1.Y() == v0.Y() and End_1.Z( ) == v0.Z(): Wire_c.Add(edges) v0 = End_2 flag = 0 elif End_1.X() == v1.X() and End_1.Y() == v1.Y( ) and End_1.Z() == v1.Z(): Wire_c.Add(edges) v1 = End_2 flag = 0 elif End_2.X() == v0.X() and End_2.Y() == v0.Y( ) and End_2.Z() == v0.Z(): Wire_c.Add(edges) v0 = End_1 flag = 0 elif End_2.X() == v1.X() and End_2.Y() == v1.Y( ) and End_2.Z() == v1.Z(): Wire_c.Add(edges) v1 = End_1 flag = 0 else: new_list.append(edges) flag += 1 section_edges = new_list if flag >= 5: # print('number_ostalos', len(section_edges)) wires.append(Wire_c.Wire()) Wire_c = BRepBuilderAPI_MakeWire() Wire_c.Add(section_edges[0]) ex = TopExp_Explorer(section_edges[0], TopAbs_VERTEX) # no need for a loop since we know for a fact that # the edge has only one start and one end c = ex.Current() cv = topods_Vertex(c) v0 = BRep_Tool_Pnt(cv) ex.Next() c = ex.Current() cv = topods_Vertex(c) v1 = BRep_Tool_Pnt(cv) section_edges.pop(0) flag = 0 wires.append(Wire_c.Wire()) areas = [] props = GProp_GProps() for wire in wires: brown_face = BRepBuilderAPI_MakeFace(wire) brown_face = brown_face.Face() # props = GProp_GProps() brepgprop_SurfaceProperties(brown_face, props) areas.append(props.Mass()) return max(areas) else: return 0
topexp_LastVertex) from OCC.Core.TopAbs import TopAbs_VERTEX, TopAbs_EDGE from OCC.Core.TopTools import (TopTools_IndexedDataMapOfShapeListOfShape, TopTools_ListIteratorOfListOfShape) from OCC.Core.TopoDS import topods_Vertex, topods_Edge from OCC.Display.SimpleGui import init_display display, start_display, add_menu, add_function_to_menu = init_display() # 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_shp, vert): # apply a fillet on incident edges on a vertex afillet = BRepFilletAPI_MakeFillet(cube_shp) cnt = 0 # find edges from vertex _map = TopTools_IndexedDataMapOfShapeListOfShape() topexp_MapShapesAndAncestors(cube_shp, TopAbs_VERTEX, TopAbs_EDGE, _map) results = _map.FindFromKey(vert) topology_iterator = TopTools_ListIteratorOfListOfShape(results) while topology_iterator.More(): edge = topods_Edge(topology_iterator.Value())
def vertex(self, vertex) -> None: self._vertex = topods_Vertex(vertex)
#section.SetFuzzyValue(1.e-18) section.ComputePCurveOn1(True) section.ComputePCurveOn2(True) section.Approximation(True) section.Build() #dumpTopology(section.Shape()) display.DisplayColoredShape(section.Shape(), 'GREEN') all_points = [] all_edges = [] for edg in Topo(section.Shape()).edges(): brt = BRep_Tool() print '--- edge -----' edge_points = [] tpedg = Topo(edg) for v in tpedg.vertices(): pnt = brt.Pnt(topods_Vertex(v)) edge_points.append(Point([pnt.X(), pnt.Y(), pnt.Z()])) display.DisplayColoredShape(pnt, 'GREEN') all_points.append(edge_points) if len(all_points) != 0: pol = segments_to_polyline(all_points) all_edges = polyline3d_to_edges(pol) for edg in all_edges: face_explorer = TopExp_Explorer(faces1[f].Shape(), TopAbs_FACE) while False: #face_explorer.More(): try: TopOpeBRepTool_CurveTool_MakePCurveOnFace( edg, topods_Face(face_explorer.Current())) print 'done' except:
def glue_solids(event=None): display.EraseAll() display.Context.RemoveAll(True) # Without common edges S1 = BRepPrimAPI_MakeBox(gp_Pnt(500., 500., 0.), gp_Pnt(100., 250., 300.)).Shape() # S2 = BRepPrimAPI_MakeBox(gp_Pnt(300., 300., 300.), gp_Pnt(600., 600., 600.)).Shape() S2 = read_step_file(os.path.join('..', 'part_of_sattelate', 'pribore', 'Camara_WS16.STEP')) bbox = Bnd_Box() brepbndlib_Add(S2, bbox) xmin, ymin, zmin, xmax, ymax, zmax = bbox.Get() print(bbox.Get()) p0 = gp_Pnt(xmin + (xmax - xmin) / 2, ymin + (ymax - ymin) / 2, zmin+0.01) vnorm = gp_Dir(0, 0, 1) pln = gp_Pln(p0, vnorm) face = BRepBuilderAPI_MakeFace(pln, -(xmax - xmin) / 2 - 1, (xmax - xmin) / 2 + 1, -(ymax - ymin) / 2 - 1, (ymax - ymin) / 2 + 1).Face() # face = BRepBuilderAPI_MakeFace(pln, -10, 10, -10,10).Face() '''planeZ = BRepBuilderAPI_MakeFace( gp_Pln(gp_Pnt(xmin, ymin, zmin), gp_Pnt(xmax, ymax, zmin), gp_Pnt(xmin, ymax, zmin))).Face()''' facesS_2 = BRepAlgoAPI_Section(face, S2).Shape() # print(facesS_2) display.DisplayShape(face, update=True) display.DisplayShape(S2, update=True) display.DisplayShape(facesS_2, update=True) section_edges = list(Topo(facesS_2).edges()) print(len(section_edges)) '''toptool_seq_shape = TopTools_SequenceOfShape() for edge in section_edges: toptool_seq_shape.Append(edge)''' Wire_c = BRepBuilderAPI_MakeWire() prep_list = [] Wire_c.Add(section_edges[0]) prep_list.append(section_edges[0]) ex = TopExp_Explorer(section_edges[0], TopAbs_VERTEX) # no need for a loop since we know for a fact that # the edge has only one start and one end c = ex.Current() cv = topods_Vertex(c) v0 = BRep_Tool_Pnt(cv) ex.Next() c = ex.Current() cv = topods_Vertex(c) v1 = BRep_Tool_Pnt(cv) section_edges.pop(0) flag = 0 wires = [] while len(section_edges) > 0: new_list = [] for edges in section_edges: #Wire_c.Add(edges) ex = TopExp_Explorer(edges, TopAbs_VERTEX) c = ex.Current() cv = topods_Vertex(c) End_1 = BRep_Tool_Pnt(cv) ex.Next() c = ex.Current() cv = topods_Vertex(c) End_2 = BRep_Tool_Pnt(cv) if End_1.X() == v0.X() and End_1.Y() == v0.Y() and End_1.Z() == v0.Z(): Wire_c.Add(edges) v0 = End_2 flag = 0 elif End_1.X() == v1.X() and End_1.Y() == v1.Y() and End_1.Z() == v1.Z(): Wire_c.Add(edges) v1 = End_2 flag = 0 elif End_2.X() == v0.X() and End_2.Y() == v0.Y() and End_2.Z() == v0.Z(): Wire_c.Add(edges) v0 = End_1 flag = 0 elif End_2.X() == v1.X() and End_2.Y() == v1.Y() and End_2.Z() == v1.Z(): Wire_c.Add(edges) v1 = End_1 flag = 0 else: new_list.append(edges) flag += 1 section_edges = new_list if flag >= 5: print('number_ostalos', len(section_edges)) wires.append(Wire_c.Wire()) #wir = Wire_c.Wire() print('ttttt') Wire_c = BRepBuilderAPI_MakeWire() Wire_c.Add(section_edges[0]) ex = TopExp_Explorer(section_edges[0], TopAbs_VERTEX) # no need for a loop since we know for a fact that # the edge has only one start and one end c = ex.Current() cv = topods_Vertex(c) v0 = BRep_Tool_Pnt(cv) ex.Next() c = ex.Current() cv = topods_Vertex(c) v1 = BRep_Tool_Pnt(cv) section_edges.pop(0) flag = 0 # wires.append(Wire_c.Wire()) wires.append(bild_wire(Wire_c)) props = GProp_GProps() yellow_wire = wires[0] brown_face = BRepBuilderAPI_MakeFace(yellow_wire) brown_face = brown_face.Face() brepgprop_SurfaceProperties(brown_face, props) face_surf = props.Mass() print(face_surf) #display.DisplayColoredShape(brown_face.Face(), 'BLUE') areas = [] props = GProp_GProps() for wire in wires: brown_face = BRepBuilderAPI_MakeFace(wire) brown_face = brown_face.Face() #props = GProp_GProps() brepgprop_SurfaceProperties(brown_face, props) areas.append(props.Mass()) print(areas) print(len(wires))
def shape_to_pts(sh): return [BRep_Tool().Pnt(topods_Vertex(v)) for v in Topo(sh).vertices()]
# ============================================================================== # Explore # ============================================================================== polygons = [] tool = BRep_Tool() wires = TopExp_Explorer(shell, TopAbs_WIRE) while wires.More(): wire = topods_Wire(wires.Current()) vertices = TopExp_Explorer(wire, TopAbs_VERTEX) points = [] while vertices.More(): vertex = topods_Vertex(vertices.Current()) point = tool.Pnt(vertex) x = point.X() y = point.Y() z = point.Z() points.append([x, y, z]) vertices.Next() polygons.append(points) wires.Next() # ============================================================================== # Viz # ============================================================================== mesh = Mesh.from_polygons(polygons)