Site Tools


Differences

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

Link to this comparison view

developer:rhinocommonsamples:intertknot [2015/09/14] (current)
Line 1: Line 1:
 +===== Sample: Insert knot in curve =====
 +
 +===== C# =====
 +<code c#>
 +public static Rhino.Commands.Result InsertKnot(Rhino.RhinoDoc doc)
 +{
 +  Rhino.Commands.Result rc = Rhino.Commands.Result.Success;​
 +  Rhino.DocObjects.ObjectType filter = Rhino.DocObjects.ObjectType.Curve;​
 +  Rhino.DocObjects.ObjRef objref;
 +  rc = Rhino.Input.RhinoGet.GetOneObject("​Select curve for knot insertion",​ false, filter, out objref);
 +  if (rc != Rhino.Commands.Result.Success)
 +    return rc;
 +  Rhino.Geometry.Curve curve = objref.Curve();​
 +  if (null == curve)
 +    return Rhino.Commands.Result.Failure;​
 +  Rhino.Geometry.NurbsCurve nurb = curve.ToNurbsCurve();​
 +  if (null == nurb)
 +    return Rhino.Commands.Result.Failure;​
 +
 +  Rhino.Input.Custom.GetPoint gp = new Rhino.Input.Custom.GetPoint();​
 +  gp.SetCommandPrompt("​Point on curve to add knot"​);​
 +  gp.Constrain(nurb,​ false);
 +  gp.Get();
 +  if (gp.CommandResult() == Rhino.Commands.Result.Success)
 +  {
 +    double t;
 +    Rhino.Geometry.Curve crv = gp.PointOnCurve(out t);
 +    if( crv!=null && nurb.Knots.InsertKnot(t) )
 +    {
 +      doc.Objects.Replace(objref,​ nurb);
 +      doc.Views.Redraw();​
 +    }
 +  }
 +  return Rhino.Commands.Result.Success;  ​
 +}
 +</​code>​
 +===== VB.NET =====
 +<code vb>
 +Public Shared Function InsertKnot(ByVal doc As Rhino.RhinoDoc) As Rhino.Commands.Result
 +  Dim rc As Rhino.Commands.Result = Rhino.Commands.Result.Success
 +  Dim filter As Rhino.DocObjects.ObjectType = Rhino.DocObjects.ObjectType.Curve
 +  Dim objref As Rhino.DocObjects.ObjRef
 +  rc = Rhino.Input.RhinoGet.GetOneObject("​Select curve for knot insertion",​ False, filter, objref)
 +  If rc <> Rhino.Commands.Result.Success Then
 +    Return rc
 +  End If
 +  Dim curve As Rhino.Geometry.Curve = objref.Curve()
 +  If curve Is Nothing Then
 +    Return Rhino.Commands.Result.Failure
 +  End If
 +  Dim nurb As Rhino.Geometry.NurbsCurve = curve.ToNurbsCurve()
 +  If nurb Is Nothing Then
 +    Return Rhino.Commands.Result.Failure
 +  End If
 +
 +  Dim gp As New Rhino.Input.Custom.GetPoint()
 +  gp.SetCommandPrompt("​Point on curve to add knot")
 +  gp.Constrain(nurb,​ False)
 +  gp.[Get]()
 +  If gp.CommandResult() = Rhino.Commands.Result.Success Then
 +    Dim t As Double
 +    Dim crv As Rhino.Geometry.Curve = gp.PointOnCurve(t)
 +    If crv IsNot Nothing AndAlso nurb.Knots.InsertKnot(t) Then
 +      doc.Objects.Replace(objref,​ nurb)
 +      doc.Views.Redraw()
 +    End If
 +  End If
 +  Return Rhino.Commands.Result.Success
 +End Function
 +</​code>​
 +===== Python =====
 +<code python>
 +import Rhino
 +import scriptcontext
 +
 +def InsertKnot():​
 +    filter = Rhino.DocObjects.ObjectType.Curve
 +    rc, objref = Rhino.Input.RhinoGet.GetOneObject("​Select curve for knot insertion",​ False, filter)
 +    if rc != Rhino.Commands.Result.Success:​ return rc
 +    ​
 +    curve = objref.Curve()
 +    if not curve: return Rhino.Commands.Result.Failure
 +    nurb = curve.ToNurbsCurve()
 +    if not nurb: return Rhino.Commands.Result.Failure
 +
 +    gp = Rhino.Input.Custom.GetPoint()
 +    gp.SetCommandPrompt("​Point on curve to add knot")
 +    gp.Constrain(nurb,​ False)
 +    gp.Get()
 +    if gp.CommandResult() == Rhino.Commands.Result.Success:​
 +        crv, t = gp.PointOnCurve()
 +        if crv and nurb.Knots.InsertKnot(t):​
 +            scriptcontext.doc.Objects.Replace(objref,​ nurb)
 +            scriptcontext.doc.Views.Redraw()
 +    return Rhino.Commands.Result.Success
 +
 +if __name__=="​__main__":​
 +    InsertKnot()
 +</​code>​
 +
 +{{tag>​Developer rhinocommon}}
  
developer/rhinocommonsamples/intertknot.txt ยท Last modified: 2015/09/14 (external edit)