def __call__(self, plane: gp.gp_Pln, vertex=None, edge_dict=None, **kwargs): splt = BRepFeat_SplitShape() if isinstance(self._base, TopoDS_Shape): base_shape = self._base else: base_shape = self._base.Shape() splt.Init(base_shape) sect = BRepAlgoAPI_Section(base_shape, plane, False) sect.ComputePCurveOn1(True) sect.Approximation(True) sect.Build() self.cutting_edge = sect.Shape() ancestors = set() new_faces = [] edge_iter = TopExp_Explorer(self.cutting_edge, TopAbs_EDGE) while edge_iter.More(): base_iter = TopExp_Explorer(base_shape, TopAbs_FACE) curr_edge = edge_iter.Current() while base_iter.More(): curr_face = base_iter.Current() if sect.HasAncestorFaceOn1(curr_edge, curr_face): k, v = hash(curr_face), hash(curr_edge) if (k, v) not in self.ancestors: ancestors.add((k, v)) e = topods.Edge(curr_edge) f = topods.Face(curr_face) splt.Add(e, f) # todo - only add the closest one !!!! new_faces.append(f) self.added.append(e) break # if sect.HasAncestorFaceOn2(curr_edge, curr_face): # print('has2', curr_edge, curr_face) # pass base_iter.Next() edge_iter.Next() # ------------------------------------- splt.Build() new_shape = splt.Shape() sect.Destroy() return new_shape
def split_shape(event=None): S = BRepPrimAPI_MakeBox(gp_Pnt(-100, -60, -80), 150, 200, 170).Shape() asect = BRepAlgoAPI_Section(S, gp_Pln(1, 2, 1, -15), False) asect.ComputePCurveOn1(True) asect.Approximation(True) asect.Build() R = asect.Shape() asplit = BRepFeat_SplitShape(S) for edg in Topo(R).edges(): face = TopoDS_Face() if asect.HasAncestorFaceOn1(edg, face): asplit.Add(edg, face) asplit.Build() display.EraseAll() display.DisplayShape(asplit.Shape()) display.FitAll()
def splitwire(base, in_edge): sect = BRepAlgoAPI_Section(base, in_edge) sect.Build() sect.RefineEdges() edge = sect.Shape() splt = BRepFeat_SplitShape(base) Ex = TopExp_Explorer(edge, TopAbs_VERTEX) while Ex.More(): # print(Ex.Current()) Sx = TopExp_Explorer(base, TopAbs_EDGE) while Sx.More(): if sect.HasAncestorFaceOn1(Ex.Current(), Sx.Current()): print('add', Ex.Current(), Sx.Current()) splt.Add(Ex.Current(), Sx.Current()) Sx.Next() Ex.Next() splt.Build() return splt.Shape()
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