def _loop_topo(self, edges=True): if self.done: self._reinitialize() topologyType = topods_Edge if edges else topods_Vertex seq = [] hashes = [] # list that stores hashes to avoid redundancy occ_seq = TopTools_ListOfShape() while self.wire_explorer.More(): # loop edges if edges: current_item = self.wire_explorer.Current() # loop vertices else: current_item = self.wire_explorer.CurrentVertex() current_item_hash = current_item.__hash__() if not current_item_hash in hashes: hashes.append(current_item_hash) occ_seq.Append(current_item) self.wire_explorer.Next() # Convert occ_seq to python list occ_iterator = TopTools_ListIteratorOfListOfShape(occ_seq) while occ_iterator.More(): topo_to_add = topologyType(occ_iterator.Value()) seq.append(topo_to_add) occ_iterator.Next() self.done = True return iter(seq)
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() first, last = topexp_FirstVertex(edge), topexp_LastVertex(edge) vertex, first_vert, last_vert = BRep_Tool().Pnt(vert), BRep_Tool().Pnt( first), BRep_Tool().Pnt(last) if edge.Orientation(): if not vertex.IsEqual(first_vert, 0.001): afillet.Add(0, 20., edge) else: afillet.Add(20, 0, edge) cnt += 1 afillet.Build() if afillet.IsDone(): return afillet.Shape() else: raise AssertionError('you failed on me you fool!')
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 map_face_before_and_after_feat(base, feature_maker): ''' input base: TopoDS_Shape feature_maker: BRepFeat_MakePrism output fmap: {TopoDS_Face:TopoDS_Face} ''' fmap = {} base_faces = occ_utils.list_face(base) for face in base_faces: if feature_maker.IsDeleted(face): continue fmap[face] = [] modified = feature_maker.Modified(face) if modified.IsEmpty(): fmap[face].append(face) continue occ_it = TopTools_ListIteratorOfListOfShape(modified) while occ_it.More(): a_shape = occ_it.Value() assert a_shape.ShapeType() == TopAbs_FACE fmap[face].append(topods.Face(a_shape)) occ_it.Next() return fmap
def cut_solid_parts(array): from OCC.Core.BRepAlgoAPI import BRepAlgoAPI_Section from OCC.Core.BRepFeat import BRepFeat_SplitShape from OCC.TopTools import TopTools_ListIteratorOfListOfShape from OCC.Extend.TopologyUtils import TopologyExplorer from OCC.Core.TopoDS import TopoDS_Face #array contains several parts for i, si in enumerate(array): #for ki in si.dico: shpi = si.solid split = BRepFeat_SplitShape(shpi) for j, sj in enumerate(array): if i != j: shpj = sj.solid sect = BRepAlgoAPI_Section(shpi, shpj, False) sect.ComputePCurveOn1(True) sect.Approximation(True) sect.SetFuzzyValue(1.e-12) sect.Build() shpsect = sect.Shape() for edg in TopologyExplorer(shpsect).edges(): face = TopoDS_Face() if sect.HasAncestorFaceOn1(edg, face): split.Add(edg, face) split.Build() lst = TopTools_ListIteratorOfListOfShape(split.Modified(shpi)) while lst.More(): for face in TopologyExplorer(lst.Value()).faces(): array[i].splitsolid.append(face) lst.Next()
def faces_on(self): self.algo.Build() from OCC.TopTools import TopTools_ListIteratorOfListOfShape seen = set() edge_list = self.algo.Modified(self.shp1) itre = TopTools_ListIteratorOfListOfShape(edge_list) while itre.More(): print(itre.Value()) itre.Next() print('-') edge_list = self.algo.Modified(self.shp2) itre = TopTools_ListIteratorOfListOfShape(edge_list) while itre.More(): print(itre.Value()) itre.Next() # edge_list = self.algo.SectionEdges() # itr = TopTools_ListIteratorOfListOfShape(edge_list) # edge_list = self.algo.SectionEdges() res = self.Shape() itr = TopExp_Explorer(res, TopAbs_EDGE) faces1, faces2 = [], [] while itr.More(): curr_edge = itr.Current() s1_iter = TopExp_Explorer(self.shp1, TopAbs_FACE) s2_iter = TopExp_Explorer(self.shp2, TopAbs_FACE) while s1_iter.More(): curr_face1 = s1_iter.Current() if self.algo.HasAncestorFaceOn1(curr_edge, curr_face1): k, v = hash(curr_face1), hash(curr_edge) if (k, v) not in seen: seen.add((k, v)) faces1.append(curr_face1) s1_iter.Next() while s2_iter.More(): curr_face2 = s2_iter.Current() if self.algo.HasAncestorFaceOn2(curr_edge, curr_face2): k, v = hash(curr_face2), hash(curr_edge) if (k, v) not in seen: seen.add((k, v)) faces2.append(curr_face2) s2_iter.Next() # s2_iter.ReInit() # s1_iter.ReInit() itr.Next() return faces1, faces2
def sortWiresByBuildOrder(wireList, plane, result=[]): """Tries to determine how wires should be combined into faces. Assume: The wires make up one or more faces, which could have 'holes' Outer wires are listed ahead of inner wires there are no wires inside wires inside wires ( IE, islands -- we can deal with that later on ) none of the wires are construction wires Compute: one or more sets of wires, with the outer wire listed first, and inner ones Returns, list of lists. """ # check if we have something to sort at all if len(wireList) < 2: return [ wireList, ] # make a Face face = Face.makeFromWires(wireList[0], wireList[1:]) # use FixOrientation outer_inner_map = TopTools_DataMapOfShapeListOfShape() sf = ShapeFix_Face(face.wrapped) # fix wire orientation sf.FixOrientation(outer_inner_map) # Iterate through the Inner:Outer Mapping all_wires = face.Wires() result = { w: outer_inner_map.Find(w.wrapped) for w in all_wires if outer_inner_map.IsBound(w.wrapped) } # construct the result rv = [] for k, v in result.items(): tmp = [ k, ] iterator = TopTools_ListIteratorOfListOfShape(v) while iterator.More(): tmp.append(Wire(iterator.Value())) iterator.Next() rv.append(tmp) return rv
def __getitem__(self, item: TopoDS_Shape) -> List[TopoDS_Shape]: seen = set() res = [] results = self._map.FindFromKey(item) if results.IsEmpty(): return res topology_iterator = TopTools_ListIteratorOfListOfShape(results) while topology_iterator.More(): topo_entity = self.topoFactory[self._target_type](topology_iterator.Value()) # return the entity if not in set # to assure we're not returning entities several times if not hash(topo_entity) in seen: res.append(topo_entity) seen.add(hash(topo_entity)) topology_iterator.Next() return res
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)
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 _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)
#!/usr/bin/env python