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 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_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 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 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 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
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
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('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)
#!/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()