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 __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()
def face2(): u"""Муфта""" f1=rect(d.dm-d.d1m, d.lm, [0,0,(3,0.8),(1.15,2)]) # муфта f1=translate(f1,0,0,d.d1m,-(d.lm-d.l1n)) h=d.d1m-d.d1_m # фаска f2=rect(d.dm-d.d1_m, d.lm-10, [0,0,0,(h,1.73*h)]) # заготовка різьбової частини f2=translate(f2, 0, 0, d.d1_m, -(d.lm-d.l1n)) f2=BRepAlgo_Fuse(f2, f1).Shape() tn1=math.tan(math.radians(30)) # tan верхнього кута профілю tn2=math.tan(math.radians(30)) # tan нижнього кута профілю H=2.2 # висота різця #f3=poly([[(0,0),0],[(0.3,-0.1),0],[(0.3,-0.11),0],[(0,-0.21),0]]) f3=poly([[(0,H*tn1),0],[(H,0),(0.275/0.866, 0.275/0.866)],[(0,-H*tn2),0]]) # різець #display.DisplayShape(f3,update=True,color='red') f3=translate(f3, 0, 0, d.dm_, d.l2m_) a=thread_points_array((d.dm_, d.l2m_), d.lm_, -d.p_m) f=cut_array(f3,f2,a) # муфта з різьбою #print f.ShapeType() # COMPOUND !!! # ребро для поділу грані на дві частини e1=BRepBuilderAPI_MakeEdge(gp_Pnt(d.d1m,d.l1n-5,0),gp_Pnt(d.dm,d.l1n-5,0)).Edge() # отримати грань ex = TopExp_Explorer(f, TopAbs_FACE) ff=topods_Face(ex.Current()) # розділити грань ребром from OCC.BRepFeat import BRepFeat_SplitShape ss=BRepFeat_SplitShape(f) ss.Add(e1,ff) f=ss.Shape() #print f.ShapeType() # COMPOUND # отримати поверхню ex = TopExp_Explorer(f, TopAbs_SHELL) f=topods_Shell(ex.Current()) return f