# split & clip a curve with a clipping plane (c) 2018, Clement Greiner - CG3D

import Rhino
import scriptcontext
import rhinoscriptsyntax as rs

def ClipCurveWithClippingPlane():
    
    # get curve and clipping plane
    crv_id = rs.GetObject("Curve", 4, True, False)
    if not crv_id: return
    
    cp_id = rs.GetObject("ClippingPlane", rs.filter.clippingplane, False, False)
    if not cp_id: return
    
    crv = rs.coercecurve(crv_id, -1, True)
    cp = rs.coercerhinoobject(cp_id, True)
    rc, plane = cp.Geometry.TryGetPlane()
    
    # get only point intersections 
    tol = scriptcontext.doc.ModelAbsoluteTolerance
    events = Rhino.Geometry.Intersect.Intersection.CurvePlane(crv, plane, tol)
    params = [e.ParameterA for e in events if e.IsPoint]
    if not params: return
    
    # split, keep pieces where the midpoint is on the positive side of the cp
    pieces = crv.Split(params)
    if pieces.Count == 0: return
    
    # only add curves on the positive side of the clipping plane
    for segment in pieces:
        mid = segment.PointAt(segment.Domain.Mid)
        cpt = rs.XformWorldToCPlane(mid, plane)
        if cpt.Z > 0:
            id = scriptcontext.doc.Objects.Add(segment)
            rs.SelectObject(id)
            
ClipCurveWithClippingPlane()