Site Tools


How To: Extend a Curve Object

C++, .NET

Summary: Demonstrates how to extend a curve by a line, arc or smooth extension until it intersects a collection of objects.

The following source code sample demonstrates how to extend a curve by a line, arc or smooth extension until it intersects a collection of objects.

C++

CRhinoCommand::result CCommandTest::RunCommand(
        const CRhinoCommandContext& context )
{
  CRhinoGetObject gc;
  gc.SetCommandPrompt( L"Select line to extend" );
  gc.SetGeometryFilter( CRhinoGetObject::curve_object );
  gc.GetObjects( 1, 1 );
  if( gc.CommandResult() != CRhinoCommand::success )
    return gc.CommandResult();
 
  const CRhinoObjRef& objref = gc.Object(0);
  const ON_Curve* pC = ON_Curve::Cast( objref.Geometry() );
  if( !pC )
    return CRhinoCommand::failure;
 
  int side = 0; // start of curve
  ON_3dPoint pt;
  if( objref.SelectionPoint(pt) )
  {
    ON_3dPoint p0 = pC->PointAtStart();
    ON_3dPoint p1 = pC->PointAtEnd();
    if( (p0-pt).Length() > (p1-pt).Length() )
      side = 1; // end of curve
  }
 
  CRhinoGetObject go;
  go.SetCommandPrompt( L"Select boundary surfaces" );
  go.SetGeometryFilter( CRhinoGetObject::surface_object |
                        CRhinoGetObject::polysrf_object );
  go.EnablePreSelect( false );
  go.EnableDeselectAllBeforePostSelect( false );
  go.GetObjects( 1, 0 );
  if( go.CommandResult() != CRhinoCommand::success )
    return go.CommandResult();
 
  int object_count = go.ObjectCount();
  ON_SimpleArray<const ON_Geometry*> geom( object_count );
 
  for( int i = 0; i < object_count; i++ )
  {
    const CRhinoObject* obj = go.Object(i).Object();
    if( obj )
      geom.Append( obj->Geometry() );
  }
 
  if( geom.Count() <= 0 )
    return CRhinoCommand::cancel;
 
  ON_Curve* crv = pC->DuplicateCurve();
  if( !crv )
    return CRhinoCommand::failure;
 
  // Do the curve extension
  bool rc = RhinoExtendCurve(crv, CRhinoExtend::Line, side, geom);
 
  if( rc )
  {
    // CRhinoDoc::ReplaceObject() will copy our curve
    // so, we will need to clean up when finshed.
    context.m_doc.ReplaceObject( objref, *crv );
    context.m_doc.Redraw();
  }
 
  // Clean up or leak...
  delete crv;
  crv = 0;
  return CRhinoCommand::success;
}

VB.NET

Public Overrides Function RunCommand(ByVal context As IRhinoCommandContext) _
  As IRhinoCommand.result
  Dim gc As New MRhinoGetObject()
  gc.SetCommandPrompt("Select line to extend")
  gc.SetGeometryFilter(IRhinoGetObject.GEOMETRY_TYPE_FILTER.curve_object)
  gc.GetObjects(1, 1)
  If (gc.CommandResult() <> IRhinoCommand.result.success) Then
    Return gc.CommandResult()
  End If
 
  Dim objref As IRhinoObjRef = gc.Object(0)
  Dim pC As IOnCurve = OnCurve.ConstCast(objref.Geometry())
  If (pC Is Nothing) Then Return IRhinoCommand.result.failure
 
  Dim side As Integer = 0 'start of curve
  Dim pt As New On3dPoint()
  If (objref.SelectionPoint(pt)) Then
    Dim p0 As On3dPoint = pC.PointAtStart()
    Dim p1 As On3dPoint = pC.PointAtEnd()
    If ((p0 - pt).Length() > (p1 - pt).Length()) Then
      side = 1 ' end of curve
    End If
  End If
 
  Dim go As New MRhinoGetObject()
  go.SetCommandPrompt("Select boundary surfaces")
  Dim filter_list() As IRhinoGetObject.GEOMETRY_TYPE_FILTER = _
        {IRhinoGetObject.GEOMETRY_TYPE_FILTER.surface_object, _
         IRhinoGetObject.GEOMETRY_TYPE_FILTER.polysrf_object}
  go.SetGeometryFilter(filter_list)
  go.EnablePreSelect(False)
  go.EnableDeselectAllBeforePostSelect(False)
  go.GetObjects(1, 0)
  If (go.CommandResult() <> IRhinoCommand.result.success) Then
    Return go.CommandResult()
  End If
 
  Dim object_count As Integer = go.ObjectCount()
  Dim geom As New System.Collections.Generic.List(Of IOnGeometry)
  For i As Integer = 0 To object_count - 1
    Dim obj As IRhinoObject = go.Object(i).Object()
    If (obj IsNot Nothing) Then
      geom.Add(obj.Geometry())
    End If
  Next i
 
  If (geom.Count() <= 0) Then Return IRhinoCommand.result.cancel
 
  Dim crv As OnCurve = pC.DuplicateCurve()
  If (crv Is Nothing) Then Return IRhinoCommand.result.failure
 
  ' Do the curve extension
  Dim rc As Boolean = RhUtil.RhinoExtendCurve(crv, _
           IRhinoExtend.Type.Line, side, geom.ToArray())
  If (rc) Then
    context.m_doc.ReplaceObject(objref, crv)
    context.m_doc.Redraw()
  End If
 
  Return IRhinoCommand.result.success
End Function

C#

public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
{
  MRhinoGetObject gc = new MRhinoGetObject();
  gc.SetCommandPrompt("Select line to extend");
  gc.SetGeometryFilter(IRhinoGetObject.GEOMETRY_TYPE_FILTER.curve_object);
  gc.GetObjects(1, 1);
  if(gc.CommandResult() != IRhinoCommand.result.success)
    return gc.CommandResult();
 
  IRhinoObjRef objref = gc.Object(0);
  IOnCurve pC = OnCurve.ConstCast(objref.Geometry());
  if(pC == null)
    return IRhinoCommand.result.failure;
 
  int side = 0; //start of curve
  On3dPoint pt = new On3dPoint();
  if(objref.SelectionPoint(ref pt))
  {
    On3dPoint p0 = pC.PointAtStart();
    On3dPoint p1 = pC.PointAtEnd();
    if((p0 - pt).Length() > (p1 - pt).Length())
      side = 1; // end of curve
  }
 
  MRhinoGetObject go = new MRhinoGetObject();
  go.SetCommandPrompt("Select boundary surfaces");
  IRhinoGetObject.GEOMETRY_TYPE_FILTER[] filter_list =
      {IRhinoGetObject.GEOMETRY_TYPE_FILTER.surface_object,
       IRhinoGetObject.GEOMETRY_TYPE_FILTER.polysrf_object};
  go.SetGeometryFilter(filter_list);
  go.EnablePreSelect(false);
  go.EnableDeselectAllBeforePostSelect(false);
  go.GetObjects(1, 0);
  if(go.CommandResult() != IRhinoCommand.result.success)
    return go.CommandResult();
 
  int object_count = go.ObjectCount();
  System.Collections.Generic.List<IOnGeometry> geom = 
    new System.Collections.Generic.List<IOnGeometry>();
  for( int i=0; i<object_count; i++ )
  {
    IRhinoObject obj = go.Object(i).Object();
    if( obj != null )
      geom.Add(obj.Geometry());
  }
 
  if(geom.Count <= 0)
    return IRhinoCommand.result.cancel;
 
  OnCurve crv = pC.DuplicateCurve();
  if(crv == null)
    return IRhinoCommand.result.failure;
 
  // Do the curve extension
  bool rc = RhUtil.RhinoExtendCurve(ref crv,
            IRhinoExtend.Type.Line, side, geom.ToArray());
  if(rc)
  {
    context.m_doc.ReplaceObject(objref, crv);
    context.m_doc.Redraw();
  }
  return IRhinoCommand.result.success;
}
developer/sdksamples/extendcurve.txt ยท Last modified: 2010/01/26 (external edit)