def ProjectOnSrfLoose():
    crv = rs.GetObject("select curve to loosely project", rs.filter.curve)
    srf = rs.GetObject("Select surface to loosely project onto",
                       rs.filter.surface)
    if crv == None or srf == None:
        return
    degree = rs.CurveDegree(crv)
    bPeriodic = False
    pts = rs.CurvePoints(crv)
    if rs.IsCurvePeriodic(crv):
        pts = rs.CullDuplicatePoints(pts, 0.01)
        bPeriodic = True

    pts_projected = []
    curveplane = rs.CurvePlane(crv)
    projection_dir = curveplane.ZAxis

    for pt in pts:

        pp = rs.ProjectPointToSurface(pt, srf, projection_dir)
        if len(pp) > 0:
            pt_projected = pp[0]
            pts_projected.append(pt_projected)
    if len(pts_projected) <= 2:
        return
    if len(pts_projected) < len(pts):
        bPeriodic = False
    nc = Rhino.Geometry.NurbsCurve.Create(bPeriodic, degree, pts_projected)
    sc.doc.Objects.AddCurve(nc)
    sc.doc.Views.Redraw()
def SampleExportCurvesAsJSON():

    # Select curves to export
    ids = rs.GetObjects('Select curves to export', 4, True, True)
    if not ids: return

    # Name of filename to creat
    fname = rs.SaveFileName('Save', 'JSON File (*.json)|*.json||')
    if not fname: return

    # The json data (dictionary)
    data = {}

    # The version of this data format
    data['version'] = 1.0

    # The Rhino version
    data['rhino'] = rs.ExeVersion()

    # The date
    data['date'] = time.strftime('%d/%m/%Y')

    # The number of curve records
    data['curve_count'] = len(ids)

    # The curve records (list)
    data['curves'] = []

    for id in ids:

        # Create a curve record (dictionary)
        rec = {}

        # The id
        rec['id'] = id.ToString()

        # The dimension
        rec['dim'] = rs.CurveDim(id)

        # Is rational
        rec['rational'] = rs.IsCurveRational(id)

        # The degree
        rec['degree'] = rs.CurveDegree(id)

        # The control point count
        rec['cv_count'] = rs.CurvePointCount(id)

        # The control points
        rec['cvs'] = []
        pts = rs.CurvePoints(id)
        wht = rs.CurveWeights(id)
        for i in range(len(pts)):
            pt = pts[i]
            rec['cvs'].append([pt[0], pt[1], pt[2], wht[i]])

        # The knot count
        rec['knot_count'] = rs.CurveKnotCount(id)

        # The knots
        rec['knots'] = []
        knots = rs.CurveKnots(id)
        for i in range(len(knots)):
            rec['knots'].append(knots[i])

        # Some other (unnecessary) properties
        rec['closed'] = rs.IsCurveClosed(id)
        rec['periodic'] = rs.IsCurvePeriodic(id)
        rec['planar'] = rs.IsCurvePlanar(id)

        # Append the curve record
        data['curves'].append(rec)

    # Write the json data
    with open('c:/users/dale/desktop/data.json', 'w') as outfile:
        json.dump(data, outfile)
Beispiel #3
0
def Crv2ViewLoose():
    curve1 = rs.GetObject("select first curve", rs.filter.curve)
    if curve1 != None:
        rs.LockObject(curve1)
    curve2 = rs.GetObject("select second curve", rs.filter.curve)
    if curve1 == None or curve2 == None:
        return

    degree1 = rs.CurveDegree(curve1)
    degree2 = rs.CurveDegree(curve2)
    pts1 = rs.CurvePoints(curve1)
    pts2 = rs.CurvePoints(curve2)
    error = False
    errors = []
    if rs.IsPolyCurve(curve1) or rs.IsPolyCurve(curve2):
        errors.append("Error: This script only works for single open curves")
        error = True
    if not rs.IsCurvePlanar(curve1) or not rs.IsCurvePlanar(curve2):
        errors.append("Error: One or more of the input curves is not planar.")
        error = True
    if rs.IsCurvePeriodic(curve1) or rs.IsCurvePeriodic(curve2):
        errors.append("Error: This script only works with open curves")
        error = True
    if len(pts1) != len(pts2):
        errors.append(
            "Error: Input curves need to have same amount of control points")
        error = True
    if rs.CurveDegree(curve1) != rs.CurveDegree(curve2):
        errors.append("Error: Input curves need to be of same degree")
        error = True
    if error:
        for err in errors:
            print err
        rs.UnlockObject(curve1)
        return

    top = 0
    right = 0
    front = 0
    if rs.CurvePlane(curve1).ZAxis[2] != 0:  #top view curve
        top = 1

    if rs.CurvePlane(curve2).ZAxis[2] != 0:  #top view curve
        top = 2

    if rs.CurvePlane(curve1).ZAxis[0] != 0:  #right view curve
        right = 1

    if rs.CurvePlane(curve2).ZAxis[0] != 0:  #right view curve
        right = 2

    if rs.CurvePlane(curve1).ZAxis[1] != 0:  #front view curve
        front = 1

    if rs.CurvePlane(curve2).ZAxis[1] != 0:  #front view curve
        front = 2

    pts3 = []  #array to store the points for the new curve
    if top == 1 and right == 2:
        for i in range(0, len(pts1)):
            pts1[i][2] = pts2[i][2]
            pts1[i][1] = (pts1[i][1] + pts2[i][1]
                          ) / 2  #average out y-coordinate of each point
            pts3.append(pts1[i])
    if top == 2 and right == 1:
        for i in range(0, len(pts1)):
            pts2[i][2] = pts1[i][2]
            pts2[i][1] = (pts1[i][1] + pts2[i][1]
                          ) / 2  #average out y-coordinate of each point
            pts3.append(pts2[i])
    if top == 1 and front == 2:
        for i in range(0, len(pts1)):
            pts1[i][2] = pts2[i][2]
            pts1[i][0] = (pts1[i][0] + pts2[i][0]
                          ) / 2  #average out x-coordinate of each point
            pts3.append(pts1[i])
    if top == 2 and front == 1:
        for i in range(0, len(pts1)):
            pts2[i][2] = pts1[i][2]
            pts2[i][0] = (pts1[i][0] + pts2[i][0]
                          ) / 2  #average out x-coordinate of each point
            pts3.append(pts2[i])
    rs.UnlockObject(curve1)

    if (right == 0 and front == 0) or (top == 0
                                       and right == 0) or (top == 0
                                                           and front == 0):
        print "Error: Curves need to be placed on orthogonal views"
        return
    else:

        rs.AddCurve(pts3, degree1)
def OffsetCrvLoose():

    crv = rs.GetObject("select curve to offset loosely", rs.filter.curve, True)
    if crv == None:
        return
    if not rs.IsCurvePlanar(crv):
        print "Sorry, but that curve is not planar."
        return
    if rs.IsPolyCurve(crv):
        print "This simple script works only for single open or closed curves"
        return
    offset = rs.GetReal("offset amount", 5)

    if offset == None or offset == 0:
        return
    both_sides = rs.GetBoolean("Offset both sides?",
                               ["both_sides", "off", "on"], False)[0]
    bPeriodic = False
    #rs.EnableRedraw(False)
    pts = rs.CurvePoints(crv)
    degree = rs.CurveDegree(crv)
    if rs.IsCurvePeriodic(crv):
        pts = rs.CullDuplicatePoints(pts, 0.01)
        bPeriodic = True
    offset_pts = []
    offset_pts2 = []  #if both_sides=true
    plane = rs.CurvePlane(crv)
    axis = plane.ZAxis
    for pt in pts:
        cp = rs.CurveClosestPoint(crv, pt)
        v = rs.CurveTangent(crv, cp)
        v = rs.VectorUnitize(v)
        v *= offset
        v = rs.VectorRotate(v, 90, axis)
        pt_ = rs.AddPoint(pt)
        #create points for offset on one side of the curve
        movedpt = rs.MoveObject(pt_, v)
        newpt = rs.coerce3dpoint(movedpt)
        offset_pts.append(newpt)
        #create points for offset on other side of the curve
        movedpt = rs.MoveObject(pt_, -2 * v)
        newpt = rs.coerce3dpoint(movedpt)
        offset_pts2.append(newpt)
        rs.DeleteObject(pt_)
    nc = Rhino.Geometry.NurbsCurve.Create(bPeriodic, degree, offset_pts)
    nc2 = Rhino.Geometry.NurbsCurve.Create(bPeriodic, degree, offset_pts2)

    if not both_sides:
        if nc.GetLength(0.1) > nc2.GetLength(0.1):  #get the longest curve...
            if offset > 0:  #...and add it to the document for positive offsets...
                sc.doc.Objects.AddCurve(nc)
            else:  #...or the shortest for negative offsets.
                sc.doc.Objects.AddCurve(nc2)
        else:
            if offset > 0:
                sc.doc.Objects.AddCurve(nc2)
            else:
                sc.doc.Objects.AddCurve(nc)
    else:  #add both curves to the document
        sc.doc.Objects.AddCurve(nc)
        sc.doc.Objects.AddCurve(nc2)

    rs.EnableRedraw(True)
    sc.doc.Views.Redraw()