def clipAtPlane():
    """Clip the selection with a plane."""
    FL = selection.check()
    if not FL:
        return
    
    dsize = bbox(FL).dsize()
    esize = 10 ** (niceLogSize(dsize)-5)

    res = askItems([['Point',(0.0,0.0,0.0)],
                    ['Normal',(1.0,0.0,0.0)],
                    ['Keep side','positive', 'radio', ['positive','negative']],
                    ['Nodes','all','radio',['all','any','none']],
                    ['Tolerance',esize],
                    ['Property',1],
                    ],caption = 'Define the clipping plane')
    if res:
        P = res['Point']
        N = res['Normal']
        side = res['Keep side']
        nodes = res['Nodes']
        atol = res['Tolerance']
        prop = res['Property']
        selection.remember(True)
        if side == 'positive':
            func = TriSurface.clip
        else:
            func = TriSurface.cclip
        FL = [ func(F,F.test(nodes=nodes,dir=N,min=P,atol=atol)) for F in FL]
        FL = [ F.setProp(prop) for F in FL ]
        export(dict([('%s/clip' % n,F) for n,F in zip(selection,FL)]))
        selection.set(['%s/clip' % n for n in selection])
        selection.draw()
def cutWithPlane():
    """Cut the selection with a plane."""
    FL = selection.check()
    if not FL:
        return

    dsize = bbox(FL).dsize()
    esize = 10 ** (niceLogSize(dsize) - 5)

    res = askItems(
        [
            _I("Point", [0.0, 0.0, 0.0], itemtype="point"),
            _I("Normal", [1.0, 0.0, 0.0], itemtype="point"),
            _I("New props", [1, 2, 2, 3, 4, 5, 6]),
            _I("Side", "positive", itemtype="radio", choices=["positive", "negative", "both"]),
            _I("Tolerance", esize),
        ],
        caption="Define the cutting plane",
    )
    if res:
        P = res["Point"]
        N = res["Normal"]
        p = res["New props"]
        side = res["Side"]
        atol = res["Tolerance"]
        selection.remember(True)
        if side == "both":
            G = [F.toFormex().cutWithPlane(P, N, side=side, atol=atol, newprops=p) for F in FL]
            G_pos = []
            G_neg = []
            for F in G:
                G_pos.append(TriSurface(F[0]))
                G_neg.append(TriSurface(F[1]))
            export(dict([("%s/pos" % n, g) for n, g in zip(selection, G_pos)]))
            export(dict([("%s/neg" % n, g) for n, g in zip(selection, G_neg)]))
            selection.set(["%s/pos" % n for n in selection] + ["%s/neg" % n for n in selection])
            selection.draw()
        else:
            selection.changeValues([F.cutWithPlane(P, N, newprops=p, side=side, atol=atol) for F in FL])
            selection.drawChanges()
def cutSelection():
    """Cut the selection with a plane."""
    FL = selection.check()
    FLnot = [ F for F in FL if F.nplex() not in [2,3] ]
    if FLnot:
        warning("Currently I can only cut Formices with plexitude 2 or 3.\nPlease change your selection.")
        return
    
    dsize = bbox(FL).dsize()
    if dsize > 0.:
        esize = 10 ** (niceLogSize(dsize)-5)
    else:
        esize = 1.e-5
    
    res = askItems([['Point',(0.0,0.0,0.0)],
                    ['Normal',(1.0,0.0,0.0)],
                    ['New props',[1,2,2,3,4,5,6]],
                    ['Side','positive', 'radio', ['positive','negative','both']],
                    ['Tolerance',esize],
                    ],caption = 'Define the cutting plane')
    if res:
        P = res['Point']
        N = res['Normal']
        atol = res['Tolerance']
        p = res['New props']
        side = res['Side']
        if side == 'both':
            G = [F.cutWithPlane(P,N,side=side,atol=atol,newprops=p) for F in FL]
            #print(G[0][0].p)
            draw(G[0])
            G_pos = [ g[0] for g in G ]
            G_neg = [ g[1] for g in G ]
            export(dict([('%s/pos' % n,g) for n,g in zip(selection,G_pos)]))
            export(dict([('%s/neg' % n,g) for n,g in zip(selection,G_neg)]))
            selection.set(['%s/pos' % n for n in selection] + ['%s/neg' % n for n in selection])
            selection.draw()
        else:
            selection.changeValues([ F.cutWithPlane(P,N,side=side,atol=atol,newprops=p) for F in FL ])
            selection.drawChanges()
def clipAtPlane():
    """Clip the selection with a plane."""
    FL = selection.check()
    if not FL:
        return

    dsize = bbox(FL).dsize()
    esize = 10 ** (niceLogSize(dsize) - 5)

    res = askItems(
        [
            _I("Point", [0.0, 0.0, 0.0], itemtype="point"),
            _I("Normal", [1.0, 0.0, 0.0], itemtype="point"),
            _I("Keep side", itemtype="radio", choices=["positive", "negative"]),
            _I("Nodes", itemtype="radio", choices=["all", "any", "none"]),
            _I("Tolerance", esize),
            _I("Property", 1),
        ],
        caption="Define the clipping plane",
    )
    if res:
        P = res["Point"]
        N = res["Normal"]
        side = res["Keep side"]
        nodes = res["Nodes"]
        atol = res["Tolerance"]
        prop = res["Property"]
        selection.remember(True)
        if side == "positive":
            func = TriSurface.clip
        else:
            func = TriSurface.cclip
        FL = [func(F, F.test(nodes=nodes, dir=N, min=P, atol=atol)) for F in FL]
        FL = [F.setProp(prop) for F in FL]
        export(dict([("%s/clip" % n, F) for n, F in zip(selection, FL)]))
        selection.set(["%s/clip" % n for n in selection])
        selection.draw()
def cutAtPlane():
    """Cut the selection with a plane."""
    FL = selection.check()
    if not FL:
        return
    
    dsize = bbox(FL).dsize()
    esize = 10 ** (niceLogSize(dsize)-5)

    res = askItems([['Point',(0.0,0.0,0.0)],
                    ['Normal',(1.0,0.0,0.0)],
                    ['New props',[1,2,2,3,4,5,6]],
                    ['Side','positive', 'radio', ['positive','negative','both']],
                    ['Tolerance',esize],
                    ],caption = 'Define the cutting plane')
    if res:
        P = res['Point']
        N = res['Normal']
        p = res['New props']
        side = res['Side']
        atol = res['Tolerance']
        selection.remember(True)
        if side == 'both':
            G = [F.toFormex().cutWithPlane(P,N,side=side,atol=atol,newprops=p) for F in FL]
            G_pos = []
            G_neg  =[]
            for F in G:
                G_pos.append(TriSurface(F[0]))
                G_neg.append(TriSurface(F[1]))
            export(dict([('%s/pos' % n,g) for n,g in zip(selection,G_pos)]))
            export(dict([('%s/neg' % n,g) for n,g in zip(selection,G_neg)]))
            selection.set(['%s/pos' % n for n in selection] + ['%s/neg' % n for n in selection])
            selection.draw()
        else:
            [F.cutWithPlane(P,N,newprops=p,side=side,atol=atol) for F in FL]
            selection.drawChanges()