<gh_stars>0
#****************************************************
# Purpose: Duplicate surface edges, especially for trimmed surface.
#          In some situation, the borders get from DuplicateEdgeCurves are piecewise.
#          Solving this problem by joining curves which their Endpoints are equal and
#          the tangency is under a tolarance(tangTol=0.1).
#          Modified version of https://developer.rhino3d.com/samples/rhinocommon/duplicate-surface-border/
#          
# Inputs:  Mouse click on target surface or polysurface.
#          
# Returns: If successfully duplicate Edge of the surface, Rhino.Commands.Result.Success
#          else, return Rhino.Commands.Result.Failure.
#****************************************************
import Rhino
import scriptcontext

def DupBorder():
    filter = Rhino.DocObjects.ObjectType.Surface | Rhino.DocObjects.ObjectType.PolysrfFilter
    rc, objref = Rhino.Input.RhinoGet.GetOneObject("Select surface or polysurface", False, filter)
    if rc != Rhino.Commands.Result.Success: return rc

    rhobj = objref.Object()
    brep = objref.Brep()
    if not rhobj or not brep: return Rhino.Commands.Result.Failure
    rhobj.Select(False)
    curves = brep.DuplicateEdgeCurves(True)
    curves = list(curves)
    crvs = []
    tol = 10**-10
    tangTol = 0.1
    tag = True
    while len(crvs)<4:
        if tag:
            crv = curves[0]
            curves.remove(crv)
        tag = True
        for curve in curves:
            if crv.PointAtEnd == curve.PointAtStart:
                cross = Rhino.Geometry.Vector3d.CrossProduct(crv.TangentAtEnd, curve.TangentAtStart)
                if (cross.X**2+cross.Y**2+cross.Z**2)**0.5<tangTol:
                    crv = Rhino.Geometry.Curve.JoinCurves([crv, curve], tol)[0]
                    curves.remove(curve)
                    tag = False
                    break
            elif crv.PointAtStart == curve.PointAtEnd:
                cross = Rhino.Geometry.Vector3d.CrossProduct(crv.TangentAtStart, curve.TangentAtEnd)
                if (cross.X**2+cross.Y**2+cross.Z**2)**0.5<0.1:
                    crv = Rhino.Geometry.Curve.JoinCurves([crv, curve], tol)[0]
                    curves.remove(curve)
                    tag = False
                    break
        if tag:
            crvs.append(crv)
            id = scriptcontext.doc.Objects.AddCurve(crv)
            scriptcontext.doc.Objects.Select(id)
    
    scriptcontext.doc.Views.Redraw()
    return Rhino.Commands.Result.Success

if __name__=="__main__":
    DupBorder()
#****************************************************
# Purpose: Duplicate surface edges, especially for trimmed surface.
#          In some situation, the borders get from DuplicateEdgeCurves are piecewise.
#          Solving this problem by joining curves which their Endpoints are equal and
#          the tangency is under a tolarance(tangTol=0.1).
#          Modified version of https://developer.rhino3d.com/samples/rhinocommon/duplicate-surface-border/
#          
# Inputs:  Mouse click on target surface or polysurface.
#          
# Returns: If successfully duplicate Edge of the surface, Rhino.Commands.Result.Success
#          else, return Rhino.Commands.Result.Failure.
#****************************************************
import Rhino
import scriptcontext

def DupBorder():
    filter = Rhino.DocObjects.ObjectType.Surface | Rhino.DocObjects.ObjectType.PolysrfFilter
    rc, objref = Rhino.Input.RhinoGet.GetOneObject("Select surface or polysurface", False, filter)
    if rc != Rhino.Commands.Result.Success: return rc

    rhobj = objref.Object()
    brep = objref.Brep()
    if not rhobj or not brep: return Rhino.Commands.Result.Failure
    rhobj.Select(False)
    curves = brep.DuplicateEdgeCurves(True)
    curves = list(curves)
    crvs = []
    tol = 10**-10
    tangTol = 0.1
    tag = True
    while len(crvs)<4:
        if tag:
            crv = curves[0]
            curves.remove(crv)
        tag = True
        for curve in curves:
            if crv.PointAtEnd == curve.PointAtStart:
                cross = Rhino.Geometry.Vector3d.CrossProduct(crv.TangentAtEnd, curve.TangentAtStart)
                if (cross.X**2+cross.Y**2+cross.Z**2)**0.5<tangTol:
                    crv = Rhino.Geometry.Curve.JoinCurves([crv, curve], tol)[0]
                    curves.remove(curve)
                    tag = False
                    break
            elif crv.PointAtStart == curve.PointAtEnd:
                cross = Rhino.Geometry.Vector3d.CrossProduct(crv.TangentAtStart, curve.TangentAtEnd)
                if (cross.X**2+cross.Y**2+cross.Z**2)**0.5<0.1:
                    crv = Rhino.Geometry.Curve.JoinCurves([crv, curve], tol)[0]
                    curves.remove(curve)
                    tag = False
                    break
        if tag:
            crvs.append(crv)
            id = scriptcontext.doc.Objects.AddCurve(crv)
            scriptcontext.doc.Objects.Select(id)
    
    scriptcontext.doc.Views.Redraw()
    return Rhino.Commands.Result.Success

if __name__=="__main__":
    DupBorder()
#**************************************************** # Purpose: Duplicate surface edges, especially for trimmed surface. #          In some situation, the borders get from DuplicateEdgeCurves are piecewise. #          Solving this problem by joining curves which their Endpoints are equal and #          the tangency is under a tolarance(tangTol=0.1). #          Modified version of https://developer.rhino3d.com/samples/rhinocommon/duplicate-surface-border/ # # Inputs:  Mouse click on target surface or polysurface. # # Returns: If successfully duplicate Edge of the surface, Rhino.Commands.Result.Success #          else, return Rhino.Commands.Result.Failure. #****************************************************