Sdk Screen To World
Last changed: dale@mcneel.com-204.177.179.95

.
DeveloperC++
VersionRhino 4
SummaryDemonstrates how to transform Screen coordinates to world coordinates using Rhino 4 SDK.

Question

How to I get the world coordinates of a view? That is, I need to transforms client-area coordinates of a specified view to world coordinates. The resulting coordinates are represented as a 3-D point. How can I do this?

Answer

Every Rhino view is capable of returning the transformation from one coordiate system to another. This is accomplished by the ON_Viewport::GetXform member. The following demonstrates how to use this to covert screen coordinates to world coordinates.

C++

  CRhinoCommand::result CCommandTest::RunCommand( const CRhinoCommandContext& context )
  {
    CRhinoCommand::result rc = failure;


    // Get the active view
    CRhinoView* view = RhinoApp().ActiveView();
    if( view )
    {
      // Get the current cursor position
      POINT point;
      if( GetCursorPos(&point ) )
      {
        // Convert the screen coordinates to client coordinates
        view->ScreenToClient( &point );


        // Obtain the view's screen-to-world transformation
        ON_Xform screen_to_world;
        view->ActiveViewport().VP().GetXform( ON::screen_cs, ON::world_cs, screen_to_world );


        // Create a 3-D point
        ON_3dPoint pt( point.x, point.y, 0 );
        // Transform it
        pt.Transform( screen_to_world );


        // Add it to the document
        context.m_doc.AddPointObject( pt );
        context.m_doc.Redraw();


        rc = success;
      }
    }


    return rc;
  }