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 = Geom_Plane.DownCast(BRep_Tool_Surface(face)) 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
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 _loop_topo(self, topology_type, topological_entity=None, topology_type_to_avoid=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) """ allowed_types = self.topo_types.keys() if topology_type not in allowed_types: raise TypeError('%s not one of %s' % (topology_type, allowed_types)) shape = self.shape if shape is None: return [] topo_exp = TopExp_Explorer() # use self.myShape if nothing is specified if topological_entity is None and topology_type_to_avoid is None: topo_exp.Init(shape, topology_type) elif topological_entity is None and topology_type_to_avoid is not None: topo_exp.Init(shape, topology_type, topology_type_to_avoid) elif topology_type_to_avoid is None: topo_exp.Init(topological_entity, topology_type) elif topology_type_to_avoid: topo_exp.Init(topological_entity, topology_type, topology_type_to_avoid) items = set() # list that stores hashes to avoid redundancy occ_seq = TopTools_ListOfShape() while topo_exp.More(): current_item = topo_exp.Current() if current_item not in items: items.add(current_item) occ_seq.Append(current_item) topo_exp.Next() # Convert occ_seq to python list seq = [] factory = self.topo_factory[topology_type] occ_iterator = TopTools_ListIteratorOfListOfShape(occ_seq) while occ_iterator.More(): topo_to_add = factory(occ_iterator.Value()) seq.append(topo_to_add) occ_iterator.Next() if not self.ignore_orientation: return seq # else 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
from OCCT.BRepPrimAPI import BRepPrimAPI_MakeBox from OCCT.TopExp import (TopExp_Explorer, topexp_MapShapesAndAncestors, topexp_FirstVertex, topexp_LastVertex) from OCCT.TopAbs import TopAbs_VERTEX, TopAbs_EDGE from OCCT.TopTools import (TopTools_IndexedDataMapOfShapeListOfShape, TopTools_ListIteratorOfListOfShape) from OCCT.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)