Site Tools


Differences

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

Link to this comparison view

developer:sdksamples:screentoworld [2015/09/14] (current)
Line 1: Line 1:
 +====== Transform Screen Coordinates to World Coordinates ======
 +> **Developer:​** //​[[developer:​cplusplusplugins|C++]],​ [[developer:​rhinocommon|RhinoCommon]]//​
 +> **Summary:​** //​Demonstrates how to transform screen coordinates to world coordinates using Rhino SDK.//
  
 +=====Question=====
 +How to I get the world coordinates of a view? That is, I need to transform client-area coordinates of a specified view to world coordinates. The resulting coordinates are represented as 3-D points. How can I do this?
 +
 +=====Answer=====
 +Every Rhino view is capable of returning the transformation from one coordinate system to another. This is accomplished by the ON_Viewport::​GetXform member. The following demonstrates how to use this to convert screen coordinates to world coordinates.
 +
 +====C++====
 +<code 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;
 +}
 +</​code>​
 +
 +====C#====
 +<code c#>
 +[System.Runtime.InteropServices.DllImport("​user32.dll"​)]
 +public static extern bool GetCursorPos(out System.Drawing.Point pt);
 +
 +protected override Result RunCommand(RhinoDoc doc, RunMode mode)
 +{
 +  Result rc = Result.Failure;​
 +
 +  // Get the active view
 +  Rhino.Display.RhinoView view = doc.Views.ActiveView;​
 +  if (null != view)
 +  {
 +    // Get the current cursor position
 +    System.Drawing.Point point;
 +    if (GetCursorPos(out point))
 +    {
 +      // Convert the screen coordinates to client coordinates
 +      point = view.ScreenToClient(point);​
 +
 +      // Obtain the view's screen-to-world transformation
 +      Rhino.Geometry.Transform screen_to_world = view.ActiveViewport.GetTransform(
 +        Rhino.DocObjects.CoordinateSystem.Screen, ​
 +        Rhino.DocObjects.CoordinateSystem.World
 +        );
 +
 +      // Create a 3-D point
 +      Rhino.Geometry.Point3d pt = new Rhino.Geometry.Point3d(
 +        point.X, ​
 +        point.Y, ​
 +        0.0
 +        );
 +
 +      // Transform it
 +      pt.Transform(screen_to_world);​
 +
 +      // Add it to the document
 +      doc.Objects.AddPoint(pt);​
 +      doc.Views.Redraw();​
 +
 +      rc = Result.Success;​
 +    }
 +  }
 +
 +  return rc;
 +}
 +</​code>​
 +
 +\\
 +
 +{{tag>​Developer cplusplus rhinocommon}}
developer/sdksamples/screentoworld.txt ยท Last modified: 2015/09/14 (external edit)