Site Tools


Differences

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

Link to this comparison view

developer:rhinocommonsamples:createsurfaceexample [2015/09/14] (current)
Line 1: Line 1:
 +===== Sample: Create a NURBS Surface =====
 +
 +===== C# =====
 +<code c#>
 +[System.Runtime.InteropServices.Guid("​29530E9C-8A5B-47BD-A2F8-0A7BF311D8D3"​)]
 +public class CreateSurfaceFromPointsAndKnotsCommand : Command
 +{
 +  public override string EnglishName { get { return "​csCreateSurfaceFromPointsAndKnots";​ } }
 +
 +  protected override Result RunCommand(RhinoDoc doc, RunMode mode)
 +  {
 +    const bool isRational = false;
 +    const int numberOfDimensions = 3;
 +    const int uDegree = 2;
 +    const int vDegree = 3;
 +    const int uControlPointCount = 3;
 +    const int vControlPointCount = 5;
 +   
 +    // The knot vectors do NOT have the 2 superfluous knots
 +    // at the start and end of the knot vector. ​ If you are
 +    // coming from a system that has the 2 superfluous knots,
 +    // just ignore them when creating NURBS surfaces.
 +    var uKnots = new double[uControlPointCount + uDegree - 1];
 +    var vKnots = new double[vControlPointCount + vDegree - 1];
 +   
 +    // make up a quadratic knot vector with no interior knots
 +    uKnots[0] = uKnots[1] = 0.0;
 +    uKnots[2] = uKnots[3] = 1.0;
 +   
 +    // make up a cubic knot vector with one simple interior knot
 +    vKnots[0] = vKnots[1] = vKnots[2] = 0.0;
 +    vKnots[3] = 1.5;
 +    vKnots[4] = vKnots[5] = vKnots[6] = 2.0;
 +   
 +    // Rational control points can be in either homogeneous
 +    // or euclidean form. Non-rational control points do not
 +    // need to specify a weight.  ​
 +    var controlPoints = new Point3d[uControlPointCount,​ vControlPointCount];​
 +
 +    for (int u = 0; u < uControlPointCount;​ u++)
 +    {
 +      for (int v = 0; v < vControlPointCount;​ v++)
 +      {
 +        controlPoints[u,​v] = new Point3d(u, v, u-v);
 +      }
 +    }
 +   
 +    // creates internal uninitialized arrays for 
 +    // control points and knots
 +    var nurbsSurface = NurbsSurface.Create(
 +      numberOfDimensions,​
 +      isRational,
 +      uDegree + 1,
 +      vDegree + 1,
 +      uControlPointCount,​
 +      vControlPointCount
 +      );
 +   
 +    // add the knots
 +    for (int u = 0;  u < nurbsSurface.KnotsU.Count;​ u++)
 +      nurbsSurface.KnotsU[u] = uKnots[u];
 +    for (int v = 0; v < nurbsSurface.KnotsV.Count;​ v++)
 +      nurbsSurface.KnotsV[v] = vKnots[v];
 +
 +    // add the control points
 +    for (int u = 0; u < nurbsSurface.Points.CountU;​ u++)
 +    {
 +      for (int v = 0; v < nurbsSurface.Points.CountV;​ v++)
 +      {
 +        nurbsSurface.Points.SetControlPoint(u,​ v, controlPoints[u,​ v]);
 +      }
 +    }
 +
 +    if (nurbsSurface.IsValid)
 +    {
 +      doc.Objects.AddSurface(nurbsSurface);​
 +      doc.Views.Redraw();​
 +      return Result.Success;​
 +    }
 +    else
 +    {
 +      return Result.Failure;​
 +    }
 +  }
 +}
 +</​code>​
 +===== VB.NET =====
 +<code vb>
 +<​System.Runtime.InteropServices.Guid("​652FCBE5-D8DC-4472-AB94-5A70998A3895"​)>​ _
 +Public Class CreateSurfaceFromPointsAndKnotsCommand
 +  Inherits Command
 +  Public Overrides ReadOnly Property EnglishName() As String
 +    Get
 +      Return "​vbCreateSurfaceFromPointsAndKnots"​
 +    End Get
 +  End Property
 +
 +  Protected Overrides Function RunCommand(doc As RhinoDoc, mode As RunMode) As Result
 +    Const isRational As Boolean = False
 +    Const numberOfDimensions As Integer = 3
 +    Const uDegree As Integer = 2
 +    Const vDegree As Integer = 3
 +    Const uControlPointCount As Integer = 3
 +    Const vControlPointCount As Integer = 5
 +
 +    ' The knot vectors do NOT have the 2 superfluous knots
 +    ' at the start and end of the knot vector. ​ If you are
 +    ' coming from a system that has the 2 superfluous knots,
 +    ' just ignore them when creating NURBS surfaces.
 +    Dim uKnots = New Double(uControlPointCount + uDegree - 2) {}
 +    Dim vKnots = New Double(vControlPointCount + vDegree - 2) {}
 +
 +    ' make up a quadratic knot vector with no interior knots
 +    uKnots(0) = InlineAssignHelper(uKnots(1),​ 0.0)
 +    uKnots(2) = InlineAssignHelper(uKnots(3),​ 1.0)
 +
 +    ' make up a cubic knot vector with one simple interior knot
 +    vKnots(0) = InlineAssignHelper(vKnots(1),​ InlineAssignHelper(vKnots(2),​ 0.0))
 +    vKnots(3) = 1.5
 +    vKnots(4) = InlineAssignHelper(vKnots(5),​ InlineAssignHelper(vKnots(6),​ 2.0))
 +
 +    ' Rational control points can be in either homogeneous
 +    ' or euclidean form. Non-rational control points do not
 +    ' need to specify a weight.  ​
 +    Dim controlPoints = New Point3d(uControlPointCount - 1, vControlPointCount - 1) {}
 +
 +    For u As Integer = 0 To uControlPointCount - 1
 +      For v As Integer = 0 To vControlPointCount - 1
 +        controlPoints(u,​ v) = New Point3d(u, v, u - v)
 +      Next
 +    Next
 +
 +    ' creates internal uninitialized arrays for 
 +    ' control points and knots
 +    Dim nurbsSurface__1 = NurbsSurface.Create(numberOfDimensions,​ isRational, uDegree + 1, vDegree + 1, uControlPointCount,​ vControlPointCount)
 +
 +    ' add the knots
 +    For u As Integer = 0 To nurbsSurface__1.KnotsU.Count - 1
 +      nurbsSurface__1.KnotsU(u) = uKnots(u)
 +    Next
 +    For v As Integer = 0 To nurbsSurface__1.KnotsV.Count - 1
 +      nurbsSurface__1.KnotsV(v) = vKnots(v)
 +    Next
 +
 +    ' add the control points
 +    For u As Integer = 0 To nurbsSurface__1.Points.CountU - 1
 +      For v As Integer = 0 To nurbsSurface__1.Points.CountV - 1
 +        nurbsSurface__1.Points.SetControlPoint(u,​ v, controlPoints(u,​ v))
 +      Next
 +    Next
 +
 +    If nurbsSurface__1.IsValid Then
 +      doc.Objects.AddSurface(nurbsSurface__1)
 +      doc.Views.Redraw()
 +      Return Result.Success
 +    Else
 +      Return Result.Failure
 +    End If
 +  End Function
 +  Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, value As T) As T
 +    target = value
 +    Return value
 +  End Function
 +End Class
 +d Namespace
 +</​code>​
 +===== Python =====
 +<code python>
 +from Rhino.Geometry import Point3d, NurbsSurface,​ ControlPoint
 +from scriptcontext import doc
 +
 +def RunCommand():​
 +  bIsRational = False
 +  dim = 3
 +  u_degree = 2
 +  v_degree = 3
 +  u_cv_count = 3
 +  v_cv_count = 5
 + 
 +  # make up a quadratic knot vector with no interior knots
 +  u_knot = [0.0, 0.0, 1.0, 1.0] 
 + 
 +  # make up a cubic knot vector with one simple interior knot
 +  v_knot = [0.0, 0.0, 0.0, 1.5, 2.0, 2.0, 2.0]
 + 
 +  # Rational control points can be in either homogeneous
 +  # or euclidean form. Non-rational control points do not
 +  # need to specify a weight.  ​
 +  CV = dict( ((i,​j),​None) for i in range(2) for j in range(3) )
 +  for i in range(0, u_cv_count):​
 +    for j in range(0, v_cv_count):​
 +      CV[i,j] = Point3d(i, j, i-j)
 + 
 +  # creates internal uninitialized arrays for 
 +  # control points and knots
 +  nurbs_surface = NurbsSurface.Create(
 +    dim,
 +    bIsRational,​
 +    u_degree + 1,
 +    v_degree + 1,
 +    u_cv_count,
 +    v_cv_count
 +    )
 + 
 +  # add the knots
 +  for i in range(0, nurbs_surface.KnotsU.Count):​
 +    nurbs_surface.KnotsU[i] = u_knot[i]
 +  for j in range(0, nurbs_surface.KnotsV.Count):​
 +    nurbs_surface.KnotsV[j] = v_knot[j]
 +
 +  # add the control points
 +  for i in range(0, nurbs_surface.Points.CountU):​
 +    for j in range(0, nurbs_surface.Points.CountV):​
 +      nurbs_surface.Points.SetControlPoint(i,​ j, ControlPoint(CV[i,​ j]))
 +
 +  if nurbs_surface.IsValid:​
 +    doc.Objects.AddSurface(nurbs_surface)
 +    doc.Views.Redraw()
 +
 +if __name__ == "​__main__":​
 +  RunCommand()
 +</​code>​
 +
 +{{tag>​Developer rhinocommon}}
  
developer/rhinocommonsamples/createsurfaceexample.txt ยท Last modified: 2015/09/14 (external edit)