def split_solid(base, plane): splt = BRepFeat_SplitShape() splt.Init(base) sect = BRepAlgoAPI_Section(base, plane, False) sect.ComputePCurveOn1(True) sect.Approximation(True) sect.Build() edge = sect.Shape() rdict = set() # print(Topo(edge).number_of_edges()) Ex = TopExp_Explorer(edge, TopAbs_EDGE) while Ex.More(): # print('edge', Ex.Current()) base_iter = TopExp_Explorer(base, TopAbs_FACE) curr = Ex.Current() while base_iter.More(): # print('face', base_iter.Current()) bface = base_iter.Current() if sect.HasAncestorFaceOn1(curr, bface): # print('has1', curr, bface) k, v = hash(bface), hash(curr) if (k, v) not in rdict: rdict.add((k, v)) e = topods.Edge(curr) f = topods.Face(bface) splt.Add(e, f) if sect.HasAncestorFaceOn2(curr, bface): # print('has2', curr, bface) pass base_iter.Next() Ex.Next() splt.Build() return splt.Shape()
class Intersector: """ given shape1 and shape2, form the intersection need to be able to return which 'topo' of """ def __init__(self, shp1, shp2): # self.shp1 = shp1 # self.shp2 = shp2 self.algo = BRepAlgoAPI_Section(shp1, shp2, True) self.algo.ComputePCurveOn1(True) self.algo.ComputePCurveOn2(True) # self.algo.Approximation(True) @property def shp1(self): return self.algo.Shape1() @property def shp2(self): return self.algo.Shape2() def Shape(self): return self.algo.Shape() 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 commonface(self): w = Construct.make_wirex(*Topo(self.Shape()).edges()) f = Construct.make_face(w) return f