Example #1
0
def CutSect(Shape, SpanStation):
    """
    Parameters
    ----------
    Shape : TopoDS_Shape
        The Shape to find planar cut section (parallel to xz plane)

    SpanStation : scalar in range (0, 1)
        y-direction location at which to cut Shape

    Returns
    -------
    Section : result of OCC.BRepAlgoAPI.BRepAlgoAPI_Section (TopoDS_Shape)
        The cut section of shape given a cut plane parallel to xz at input
        Spanstation.

    Chord : result of OCC.GC.GC_MakeSegment.Value (Geom_TrimmedCurve)
        The Chord line between x direction extremeties
    """
    (Xmin, Ymin, Zmin, Xmax, Ymax, Zmax) = ObjectsExtents([Shape])

    YStation = Ymin + (Ymax - Ymin) * SpanStation
    OriginX = Xmin - 1
    OriginZ = Zmin - 1

    P = gp_Pln(gp_Pnt(OriginX, YStation, OriginZ), gp_Dir(gp_Vec(0, 1, 0)))
    # Note: using 2*extents here as previous +1 trimmed plane too short
    CutPlaneSrf = make_face(P, 0, Zmax + 2, 0, Xmax + 2)

    I = BRepAlgoAPI_Section(Shape, CutPlaneSrf)
    I.ComputePCurveOn1(True)
    I.Approximation(True)
    I.Build()
    Section = I.Shape()

    (Xmin, Ymin, Zmin, Xmax, Ymax, Zmax) = ObjectsExtents([Section])

    #     Currently assume only one edge exists in the intersection:
    exp = TopExp_Explorer(Section, TopAbs_EDGE)
    edge = topods_Edge(exp.Current())

    #    Find the apparent chord of the section (that is, the line connecting the
    #    fore most and aftmost points on the curve
    DivPoints = Uniform_Points_on_Curve(edge, 200)

    Xs = np.array([pt.X() for pt in DivPoints])

    min_idx = np.argmin(Xs)
    LeadingPoint = gp_Pnt(Xs[min_idx], DivPoints[min_idx].Y(),
                          DivPoints[min_idx].Z())

    max_idx = np.argmax(Xs)
    TrailingPoint = gp_Pnt(Xs[max_idx], DivPoints[max_idx].Y(),
                           DivPoints[max_idx].Z())

    HChord = GC_MakeSegment(TrailingPoint, LeadingPoint).Value()
    #    Chord = HChord.GetObject()
    return Section, HChord
Example #2
0
    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()
Example #4
0
def section(event=None):
    Torus = BRepPrimAPI_MakeTorus(120, 20).Shape()
    radius = 120.0
    sections = []
    for i in range(-3, 4):
        # Create Sphere
        Sphere = BRepPrimAPI_MakeSphere(gp_Pnt(26 * 3 * i, 0, 0), radius).Shape()
        # Computes Torus/Sphere section
        section = BRepAlgoAPI_Section(Torus, Sphere, False)
        section.ComputePCurveOn1(True)
        section.Approximation(True)
        section.Build()
        sections.append(section)

    display.EraseAll()
    display.DisplayShape(Torus)
    for section in sections:
        display.DisplayShape(section.Shape())
    display.FitAll()
Example #5
0
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()