Site Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

developer:sdksamples:testnurbscurve [2015/09/14] (current)
Line 1: Line 1:
 +====== Add a NURBS Curve to Rhino ======
 +> **Developer:​** //​[[developer:​cplusplusplugins|C++]],​ [[developer:​rhinocommon|RhinoCommon]],​ [[developer:​rhinoscript|RhinoScript]]//​
 +> **Summary:​** //​Demonstrates how to add a NURBS curve to Rhino.//
  
 +The following examples add a degree = 3 NURBS curve with 6 control points to the Rhino document.
 +==== C++ ====
 +<code c++>
 +void TestNurbsCurve(CRhinoDoc&​ doc)
 +{
 +  const int degree = 3;
 +  const int cv_count = 6;
 +  const int knot_count = cv_count + degree - 1;
 +  const int order = degree + 1;
 +
 +  ON_3dPoint cvs[cv_count];​
 +  cvs[0] = ON_3dPoint(0.0,​ 0.0, 0.0);
 +  cvs[1] = ON_3dPoint(5.0,​ 10.0, 0.0);
 +  cvs[2] = ON_3dPoint(10.0,​ 0.0, 0.0);
 +  cvs[3] = ON_3dPoint(15.0,​ 10.0, 0.0);
 +  cvs[4] = ON_3dPoint(20.0,​ 0.0, 0.0);
 +  cvs[5] = ON_3dPoint(25.0,​ 10.0, 0.0);
 + 
 +  double knots[knot_count];​
 +  knots[0] = 0.0;
 +  knots[1] = 0.0;
 +  knots[2] = 0.0;
 +  knots[3] = 1.0;
 +  knots[4] = 2.0;
 +  knots[5] = 3.0;
 +  knots[6] = 3.0;
 +  knots[7] = 3.0;
 +
 +  ON_NurbsCurve curve(3, FALSE, order, cv_count);
 +
 +  for (int i = 0; i < cv_count; i++)
 +    curve.SetCV(i,​ cvs[i]);
 +
 +  for (int i = 0; i < knot_count; i++)
 +    curve.m_knot[i] = knots[i];
 +
 +  if( curve.IsValid() )
 +  {
 +    doc.AddCurveObject(curve);​
 +    doc.Redraw();​
 +  }
 +}
 +</​code>​
 +
 +==== RhinoCommon (C#) ====
 +<code c#>
 +public void TestNurbsCurve(RhinoDoc doc)
 +{
 +  const int degree = 3;
 +  const int cv_count = 6;
 +  const int knot_count = cv_count + degree - 1;
 +  const int order = degree + 1;
 +
 +  Point3d[] cvs = new Point3d[cv_count ];
 +  cvs[0] = new Point3d(0.0,​ 0.0, 0.0);
 +  cvs[1] = new Point3d(5.0,​ 10.0, 0.0);
 +  cvs[2] = new Point3d(10.0,​ 0.0, 0.0);
 +  cvs[3] = new Point3d(15.0,​ 10.0, 0.0);
 +  cvs[4] = new Point3d(20.0,​ 0.0, 0.0);
 +  cvs[5] = new Point3d(25.0,​ 10.0, 0.0);
 +
 +  double[] knots = new double[knot_count];​
 +  knots[0] = 0.0;
 +  knots[1] = 0.0;
 +  knots[2] = 0.0;
 +  knots[3] = 1.0;
 +  knots[4] = 2.0;
 +  knots[5] = 3.0;
 +  knots[6] = 3.0;
 +  knots[7] = 3.0;
 +
 +  NurbsCurve curve = new NurbsCurve(3,​ false, order, cv_count);
 +      ​
 +  for (int i = 0; i < cv_count; i++)
 +    curve.Points.SetPoint(i,​ cvs[i]);
 +
 +  for (int i = 0; i < knot_count; i++)
 +    curve.Knots[i] = knots[i];
 +
 +  if (curve.IsValid)
 +  {
 +    doc.Objects.AddCurve(curve);​
 +    doc.Views.Redraw();​
 +  }
 +}
 +</​code>​
 +
 +==== RhinoCommon (VB) ====
 +<code vb>
 +Public Sub TestNurbsCurve(doc As RhinoDoc)
 +  Const degree As Integer = 3
 +  Const cv_count As Integer = 6
 +  Const knot_count As Integer = cv_count + degree - 1
 +  Const order As Integer = degree + 1
 +
 +  Dim cvs As Point3d() = New Point3d(cv_count - 1) {}
 +  cvs(0) = New Point3d(0.0,​ 0.0, 0.0)
 +  cvs(1) = New Point3d(5.0,​ 10.0, 0.0)
 +  cvs(2) = New Point3d(10.0,​ 0.0, 0.0)
 +  cvs(3) = New Point3d(15.0,​ 10.0, 0.0)
 +  cvs(4) = New Point3d(20.0,​ 0.0, 0.0)
 +  cvs(5) = New Point3d(25.0,​ 10.0, 0.0)
 +
 +  Dim knots As Double() = New Double(knot_count - 1) {}
 +  knots(0) = 0.0
 +  knots(1) = 0.0
 +  knots(2) = 0.0
 +  knots(3) = 1.0
 +  knots(4) = 2.0
 +  knots(5) = 3.0
 +  knots(6) = 3.0
 +  knots(7) = 3.0
 +
 +  Dim curve As New NurbsCurve(3,​ False, order, cv_count)
 +
 +  For i As Integer = 0 To cv_count - 1
 +    curve.Points.SetPoint(i,​ cvs(i))
 +  Next
 +
 +  For i As Integer = 0 To knot_count - 1
 +    curve.Knots(i) = knots(i)
 +  Next
 +
 +  If curve.IsValid Then
 +    doc.Objects.AddCurve(curve)
 +    doc.Views.Redraw()
 +  End If
 +  ​
 +End Sub
 +</​code>​
 +
 +==== Rhino.Python (RhinoCommon) ====
 +<code python>
 +import Rhino
 +import scriptcontext
 +
 +def TestNurbsCurve():​
 +    degree = 3
 +    cv_count = 6
 +    knot_count = cv_count + degree - 1
 +    order = degree + 1
 +    ​
 +    cvs = []
 +    cvs.append(Rhino.Geometry.Point3d(0.0,​ 0.0, 0.0))
 +    cvs.append(Rhino.Geometry.Point3d(5.0,​ 10.0, 0.0))
 +    cvs.append(Rhino.Geometry.Point3d(10.0,​ 0.0, 0.0))
 +    cvs.append(Rhino.Geometry.Point3d(15.0,​ 10.0, 0.0))
 +    cvs.append(Rhino.Geometry.Point3d(20.0,​ 0.0, 0.0))
 +    cvs.append(Rhino.Geometry.Point3d(25.0,​ 10.0, 0.0))
 +    ​
 +    knots = []
 +    knots.append(0.0)
 +    knots.append(0.0)
 +    knots.append(0.0)
 +    knots.append(1.0)
 +    knots.append(2.0)
 +    knots.append(3.0)
 +    knots.append(3.0)
 +    knots.append(3.0)
 +    ​
 +    curve = Rhino.Geometry.NurbsCurve(degree,​ False, order, cv_count)
 +    ​
 +    for i in xrange(cv_count):​
 +        curve.Points.SetPoint(i,​ cvs[i])
 +    ​
 +    for i in xrange(knot_count): ​
 +        curve.Knots[i] = knots[i]
 +    ​
 +    if curve.IsValid:​
 +        scriptcontext.doc.Objects.AddCurve(curve)
 +        scriptcontext.doc.Views.Redraw()
 +</​code>​
 +
 +==== RhinoScript ====
 +<code vb>
 +Sub TestNurbsCurve
 +
 +  Dim degree : degree = 3
 +  Dim cv_count : cv_count = 6
 +  Dim knot_count : knot_count = cv_count + degree - 1
 +
 +  Dim cvs() : ReDim cvs(cv_count - 1)
 +  cvs(0) = Array(0.0, 0.0, 0.0)
 +  cvs(1) = Array(5.0, 10.0, 0.0)
 +  cvs(2) = Array(10.0, 0.0, 0.0)
 +  cvs(3) = Array(15.0, 10.0, 0.0)
 +  cvs(4) = Array(20.0, 0.0, 0.0)
 +  cvs(5) = Array(25.0, 10.0, 0.0)
 +
 +  Dim knots() : ReDim knots(knot_count - 1)
 +  knots(0) = 0.0
 +  knots(1) = 0.0
 +  knots(2) = 0.0
 +  knots(3) = 1.0
 +  knots(4) = 2.0
 +  knots(5) = 3.0
 +  knots(6) = 3.0
 +  knots(7) = 3.0
 +
 +  Call Rhino.AddNurbsCurve(cvs,​ knots, degree)
 +
 +End Sub
 +</​code>​
 +
 +{{tag>​Developer cplusplus rhinocommon RhinoScript}}
developer/sdksamples/testnurbscurve.txt ยท Last modified: 2015/09/14 (external edit)