CRhinoCommand::result CCommandSampleTriangulatePolygon::RunCommand( const CRhinoCommandContext& context )
{
  CRhinoGetObject go;
  go.SetCommandPrompt( L"Select closed planar polygon to triangulate" );
  go.SetGeometryFilter( CRhinoGetObject::curve_object );
  go.SetGeometryFilter( CRhinoGetObject::closed_curve );
  go.EnableSubObjectSelect( FALSE );
  go.GetObjects( 1, 1 );
  if( go.CommandResult() != CRhinoCommand::success )
    return go.CommandResult();

  const CRhinoObjRef& ref = go.Object(0);

  ON_3dPointArray vertices;

  const ON_PolylineCurve* pc = ON_PolylineCurve::Cast( ref.Curve() );
  if( pc )
  {
    vertices = pc->m_pline;
  }
  else
  {
    const ON_NurbsCurve* nc = ON_NurbsCurve::Cast( ref.Curve() );
    if( nc )
      nc->IsPolyline( &vertices );
  }

  if( vertices.Count() < 5 )
  {
    RhinoApp().Print( L"Curve not polygon with at least four sides.\n" );
    return CRhinoCommand::nothing;
  }

  int* triangles = (int*)onmalloc( (vertices.Count()-3) * sizeof(int) * 3 );
  if( 0 == triangles )
    return CRhinoCommand::failure; // out of memory
  
  memset( triangles, 0, (vertices.Count()-3) * sizeof(int) * 3 );

  int rc = RhinoTriangulate3dPolygon( vertices.Count()-1, 3, (const double*)vertices.Array(), 3, triangles);
  if( 0 == rc )
  {
    int i;
    for( i = 0; i < vertices.Count()-3; i++ )
    {
      ON_Polyline pline;
      pline.Append( vertices[triangles[i * 3]] );
      pline.Append( vertices[triangles[i * 3 + 1]] );
      pline.Append( vertices[triangles[i * 3 + 2]] );
      pline.Append( pline[0] );
      context.m_doc.AddCurveObject( pline );
    }
    context.m_doc.Redraw();
  }

  onfree( triangles );

  return CRhinoCommand::success;
}
void CGetPolylinePoints::DynamicDraw( HDC hdc, CRhinoViewport& vp, const ON_3dPoint& pt )
{
  if( m_point_array.Count() > 0 )
  {
    int i;
    for( i = 1; i < m_point_array.Count(); i++ )
    {
      vp.DrawPoint( m_point_array[i-1] );
      vp.DrawLine( m_point_array[i-1], m_point_array[i] );
    }

    vp.DrawPoint( m_point_array[i-1] );
    vp.DrawPoint( pt );
    vp.DrawLine( m_point_array[i-1], pt );
  }
  
  CRhinoGetPoint::DynamicDraw( hdc, vp, pt);
}
int CGetPolylinePoints::GetPoints()
{
  m_point_array.Empty();

  SetCommandPrompt( L"Start of polyline" );
  AcceptNothing();

  CRhinoGet::result res = GetPoint();

  if( res == CRhinoGet::point )
  {
    m_point_array.Append( Point() );

    SetCommandPrompt( L"Next point of polyline" );

    for( ;; )
    {
      SetBasePoint( Point() );

      res = GetPoint();

      if( res == CRhinoGet::point )
      {
        ON_3dPoint pt = Point();
        if( pt.DistanceTo(m_point_array[m_point_array.Count()-1]) > ON_ZERO_TOLERANCE )
          m_point_array.Append( Point() );
        continue;
      }

      if( res == CRhinoGet::nothing )
        break;

      return 0;
    }
  }

  return m_point_array.Count();
}
示例#4
0
void ON_TextLog::Print( const ON_3dPointArray& a, const char* sPreamble )
{
  const double* p = (a.Array() ? &a.Array()[0].x : NULL );
  PrintPointList( 3, false, a.Count(), 3, p, sPreamble );
}