McNeel Wiki
How To: Create Contour Curves
edit · print · help · all topics
Main Pages

AccuRender

Bongo

Brazil r/s

Developer

Flamingo

Penguin

Rhino Blogs

Rhino

Rhino Labs

Search

Languages

Česky

Deutsch

English

Español

Français

Italiano

Polish

日本語

한국어

中文(繁體)

 
.
DeveloperC++, .NET
SummaryDemonstrates how to create contour curves through surfaces, breps, and meshes using the MakeRhinoContours() SDK function.

The following sample code demonstrates how to create contour curves through surfaces, breps, and meshes using the MakeRhinoContours() SDK function.

C++

  CRhinoCommand::result CCommandTest::RunCommand(
        const CRhinoCommandContext& context )
  {
    unsigned int filter = CRhinoGetObject::surface_object | 
                          CRhinoGetObject::polysrf_object | 
                          CRhinoGetObject::mesh_object;
    // Prompt for object to contour
    CRhinoGetObject go;
    go.SetCommandPrompt( L"Select object to contour" );
    go.SetGeometryFilter( filter );
    go.GetObjects( 1, 1 );
    if( go.CommandResult() != success )
      return go.CommandResult();


    // Validate selection
    const CRhinoObjRef& objref = go.Object(0);
    const ON_Geometry* geom = objref.Geometry();
    if( !geom )
      return failure;


    // Prompt for base point
    CRhinoGetPoint gp;
    gp.SetCommandPrompt( L"Contour plane base point" );
    gp.GetPoint();
    if( gp.CommandResult() != success )
      return gp.CommandResult();


    ON_3dPoint basept = gp.Point();


    // Prompt for end point
    gp.DrawLineFromPoint( basept, true );
    gp.SetCommandPrompt( L"Direction perpendicular to contour planes" );
    gp.GetPoint();
    if( gp.CommandResult() != success )
      return gp.CommandResult();


    // Verify base and end points are not the same
    ON_3dPoint endpt = gp.Point();
    if( basept.DistanceTo(endpt) < ON_ZERO_TOLERANCE )
      return nothing;


    // Prompt for distance between contours
    CRhinoGetDistance gd;
    gd.SetCommandPrompt( L"Distance between contours" );
    gd.SetDefaultNumber( 1.0 );
    gd.GetDistance();
    if( gd.CommandResult() != success )
      return gd.CommandResult();


    // Make sure interval is reasonable
    double interval = fabs( gd.Distance() );
    if( interval < ON_ZERO_TOLERANCE )
      return nothing;


    // Create contour input
    CRhinoContourInput contour;
    contour.m_geom.Append( geom );
    contour.m_basept = basept;
    contour.m_endpt = endpt;
    contour.m_interval = interval;
    contour.m_limit_range = false;


    // Create arrays for contour output
    ON_SimpleArray<ON_Polyline*> pline_array;
    ON_SimpleArray<ON_Curve*> crv_array;


    // Make the contours. Note, this function allocates memory
    // for new curves and polylines. Thus, we are responsible
    // for clean up the memory.
    bool rc = MakeRhinoContours( contour, pline_array, crv_array );
    if( !rc )
      return failure;
    context.m_doc.UnselectAll();


    // Process crves created by contouring surfaces and polysurfaces
    for( int i = 0; i < crv_array.Count(); i++)
    {
      ON_Curve* crv = crv_array[i];
      if( crv )
      {
        CRhinoCurveObject* crvobj = context.m_doc.AddCurveObject( *crv );
        if( crvobj )
          crvobj->Select();


        // CRhinoDoc::AddCurveObject() makes a copy of the input.
        // Thus, we must delete crv, otherwise we will leak memory.
        delete crv;
        crv_array[i] = 0;
      }
    }


    // Process polylines created by contouring meshes
    for( int i = 0; i < pline_array.Count(); i++)
    {
      ON_Polyline* pline = pline_array[i];
      if( pline )
      {
        CRhinoCurveObject* crvobj = context.m_doc.AddCurveObject( *pline );
        if( crvobj )
          crvobj->Select();


        // CRhinoDoc::AddCurveObject() makes a copy of the input.
        // Thus, we must delete pline, otherwise we will leak memory.
        delete pline;
        pline_array[i] = 0;
      }
    }
    context.m_doc.Redraw();
    return success;
  }

VB.NET (Rhino 4)

  Public Overrides Function RunCommand(ByVal context As IRhinoCommandContext) _
    As IRhinoCommand.result
    Dim filter() As IRhinoGetObject.GEOMETRY_TYPE_FILTER = _
          {IRhinoGetObject.GEOMETRY_TYPE_FILTER.surface_object, _
           IRhinoGetObject.GEOMETRY_TYPE_FILTER.polysrf_object, _
           IRhinoGetObject.GEOMETRY_TYPE_FILTER.mesh_object}
    ' Prompt for object to contour
    Dim go As New MRhinoGetObject()
    go.SetCommandPrompt("Select object to contour")
    go.SetGeometryFilter(filter)
    go.GetObjects(1, 1)
    If (go.CommandResult() <> IRhinoCommand.result.success) Then
      Return go.CommandResult()
    End If


    ' Validate selection
    Dim objref As MRhinoObjRef = go.Object(0)
    Dim geom As IOnGeometry = objref.Geometry()
    If (geom Is Nothing) Then Return IRhinoCommand.result.failure


    ' Prompt for base point
    Dim gp As New MRhinoGetPoint()
    gp.SetCommandPrompt("Contour plane base point")
    gp.GetPoint()
    If (gp.CommandResult() <> IRhinoCommand.result.success) Then
      Return gp.CommandResult()
    End If
    Dim basept As On3dPoint = gp.Point()


    ' Prompt for end point
    gp.DrawLineFromPoint(basept, True)
    gp.SetCommandPrompt("Direction perpendicular to contour planes")
    gp.GetPoint()
    If (gp.CommandResult() <> IRhinoCommand.result.success) Then
      Return gp.CommandResult()
    End If
    Dim endpt As On3dPoint = gp.Point()


    ' Verify base and end points are not the same
    If (basept.DistanceTo(endpt) < OnUtil.On_ZERO_TOLERANCE) Then
      Return IRhinoCommand.result.nothing
    End If


    ' Prompt for distance between contours
    Dim gd As New MRhinoGetDistance()
    gd.SetCommandPrompt("Distance between contours")
    gd.SetDefaultNumber(1.0)
    gd.GetDistance()
    If (gd.CommandResult() <> IRhinoCommand.result.success) Then
      Return gd.CommandResult()
    End If


    ' Make sure interval is reasonable
    Dim interval As Double = System.Math.Abs(gd.Distance())
    If (interval < OnUtil.On_ZERO_TOLERANCE) Then
      Return IRhinoCommand.result.nothing
    End If


    ' Create contour input
    Dim contour As New MRhinoContourInput()
    Dim g(0) As IOnGeometry
    g(0) = geom
    contour.m_geom = g
    contour.m_basept = basept
    contour.m_endpt = endpt
    contour.m_interval = interval
    contour.m_limit_range = False


    ' Create arrays for contour output
    Dim pline_array() As OnPolyline = Nothing
    Dim curve_array() As OnCurve = Nothing
    Dim points As ArrayOn3dPoint = Nothing
    ' Make the contours.
    Dim rc As Boolean = RhUtil.MakeRhinoContours(contour, _
                                                 pline_array, _
                                                 curve_array, _
                                                 points)
    If (Not rc) Then Return IRhinoCommand.result.failure
    context.m_doc.UnselectAll()


    ' Process crves created by contouring surfaces and polysurfaces
    If (curve_array IsNot Nothing) Then
      For i As Integer = 0 To curve_array.Length - 1
        Dim crv As OnCurve = curve_array(i)
        If (crv IsNot Nothing) Then
          Dim crvobj As MRhinoCurveObject = context.m_doc.AddCurveObject(crv)
          If (crvobj IsNot Nothing) Then crvobj.Select()
        End If
      Next
    End If


    ' Process polylines created by contouring meshes
    If (pline_array IsNot Nothing) Then
      For i As Integer = 0 To pline_array.Length - 1
        Dim pline As OnPolyline = pline_array(i)
        If (pline IsNot Nothing) Then
          Dim crvobj As MRhinoCurveObject = context.m_doc.AddCurveObject(pline)
          If (crvobj IsNot Nothing) Then crvobj.Select()
        End If
        next
    End If
    context.m_doc.Redraw()


    Return IRhinoCommand.result.success
  End Function

C# (Rhino 4)

  public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
  {
    uint filter = (uint)IRhinoGetObject.GEOMETRY_TYPE_FILTER.surface_object |
                  (uint)IRhinoGetObject.GEOMETRY_TYPE_FILTER.polysrf_object |
                  (uint)IRhinoGetObject.GEOMETRY_TYPE_FILTER.mesh_object;


    // Prompt for object to contour
    MRhinoGetObject go = new MRhinoGetObject();
    go.SetCommandPrompt( "Select object to contour" );
    go.SetGeometryFilter( filter );
    go.GetObjects( 1, 1 );
    if( go.CommandResult() != IRhinoCommand.result.success )
      return go.CommandResult();


    // Validate selection
    MRhinoObjRef objref = go.Object(0);
    IOnGeometry geom = objref.Geometry();
    if( geom == null )
      return IRhinoCommand.result.failure;


    // Prompt for base point
    MRhinoGetPoint gp = new MRhinoGetPoint();
    gp.SetCommandPrompt( "Contour plane base point" );
    gp.GetPoint();
    if( gp.CommandResult() != IRhinoCommand.result.success )
      return gp.CommandResult();
    On3dPoint basept = gp.Point();


    // Prompt for end point
    gp.DrawLineFromPoint( basept, true );
    gp.SetCommandPrompt( "Direction perpendicular to contour planes" );
    gp.GetPoint();
    if( gp.CommandResult() != IRhinoCommand.result.success )
      return gp.CommandResult();
    On3dPoint endpt = gp.Point();


    // Verify base and end points are not the same
    if( basept.DistanceTo(endpt) < OnUtil.On_ZERO_TOLERANCE )
      return IRhinoCommand.result.nothing;


    // Prompt for distance between contours
    MRhinoGetDistance gd = new MRhinoGetDistance();
    gd.SetCommandPrompt( "Distance between contours" );
    gd.SetDefaultNumber( 1.0 );
    gd.GetDistance();
    if (gd.CommandResult() != IRhinoCommand.result.success)
      return gd.CommandResult();


    // Make sure interval is reasonable
    double interval = System.Math.Abs( gd.Distance() );
    if (interval < OnUtil.On_ZERO_TOLERANCE)
      return IRhinoCommand.result.nothing;


    // Create contour input
    MRhinoContourInput contour = new MRhinoContourInput();
    IOnGeometry[] g = new IOnGeometry[1];
    g[0] = geom;
    contour.m_geom = g;
    contour.m_basept = basept;
    contour.m_endpt = endpt;
    contour.m_interval = interval;
    contour.m_limit_range = false;


    // Create arrays for contour output
    OnPolyline[] pline_array = null;
    OnCurve[] curve_array = null;
    ArrayOn3dPoint points = null;
    // Make the contours.
    bool rc = RhUtil.MakeRhinoContours(contour, out pline_array, out curve_array, out points);
    if (!rc)
      return IRhinoCommand.result.failure;
    context.m_doc.UnselectAll();


    // Process crves created by contouring surfaces and polysurfaces
    if (curve_array != null)
    {
      for (int i=0; i<curve_array.Length; i++)
      {
        OnCurve crv = curve_array[i];
        if (crv != null)
        {
          MRhinoCurveObject crvobj = context.m_doc.AddCurveObject(crv);
          if (crvobj != null)
            crvobj.Select();
        }
      }
    }


    // Process polylines created by contouring meshes
    if (pline_array != null)
    {
      for (int i = 0; i < pline_array.Length; i++)
      {
        OnPolyline pline = pline_array[i];
        if (pline != null)
        {
          MRhinoCurveObject crvobj = context.m_doc.AddCurveObject(pline);
          if (crvobj != null)
            crvobj.Select();
        }
      }
    }
    context.m_doc.Redraw();
    return IRhinoCommand.result.success;
  }
rename · changes · history · subscriptions · lost and found · references · file upload