Getting point coordinates in UCS from MessageFilter

<?xml encoding=”UTF-8″>By Balaji Ramamoorthy

Here is a code snippet to convert the mouse coordinates retreived from the Windows message in a message filter to an AutoCAD point coordinate in UCS.

 <span>class</span><span>  <span>MyMessageFilter</span><span>  : System.Windows.Forms.<span>IMessageFilter</span><span> </span></span></span>
 <span>{</span>
     <span>private</span><span>  <span>const</span><span>  <span>int</span><span>  WM_LBUTTONDBLCLK = 0x203;</span></span></span>
 
     <span>bool</span><span>  System.Windows.Forms.<span>IMessageFilter</span><span> .PreFilterMessage</span></span>
                         (<span>ref</span><span>  System.Windows.Forms.<span>Message</span><span>  m)</span></span>
     <span>{</span>
         <span>if</span><span>  (m.Msg == WM_LBUTTONDBLCLK)</span>
         <span>{</span>
             Document doc = 
                <span>Application</span><span> .DocumentManager.MdiActiveDocument;</span>
             Editor ed = doc.Editor;
 
             <span>// Double clicked point coordinates in pixels</span><span> </span>
             <span>int</span><span>  x = m.LParam.ToInt32() & 0xffff;</span>
             <span>int</span><span>  y = (m.LParam.ToInt32() >> 16);</span>
             System.Drawing.<span>Point</span><span>  p = <span>new</span><span>  <span>Point</span><span> (x, y);</span></span></span>
 
             <span>// Pixel to device independent coordinates</span><span> </span>
             System.Windows.Point p1 = <span>new</span><span>  System.Windows.Point();</span>
             
             System.Windows.Vector s = 
             Autodesk.AutoCAD.Windows.Window.GetDeviceIndependentScale
             (<span>IntPtr</span><span> .Zero);</span>
 
             p1.X = (<span>int</span><span> )(p.X / s.X);</span>
             p1.Y = (<span>int</span><span> )(p.Y / s.Y);</span>
 
             <span>// Device independent coordinates to WCS</span><span> </span>
             <span>short</span><span>  vpNum = </span>
                 (<span>short</span><span> )<span>Application</span><span> .GetSystemVariable(<span>"CVPORT"</span><span> );</span></span></span>
             Point3d bp = ed.PointToWorld(p1, vpNum);
 
             <span>// WCS to UCS</span><span> </span>
             bp = bp.TransformBy(
                         ed.CurrentUserCoordinateSystem.Inverse());
 
             ed.WriteMessage(<span>String</span><span> .Format(<span>"\n<span>{</span>0<span>}</span> <span>{</span>1<span>}</span>"</span><span> , bp.X, bp.Y));</span></span>
         <span>}</span>
         <span>return</span><span>  <span>false</span><span> ;</span></span>
     <span>}</span>
 <span>}</span>
 

Please note that there could be slight variation from the coordinates displayed by AutoCAD in its status bar. This is a similar behavior when using “acedCoordFromPixelToWorld” to do such conversion. As AutoCAD uses some internal functions that may be different from what is exposed in the API, so the reply from our engineering to a similar query in the past, was that whatever approach we use, we may never be able to retrieve the exact same values as that of what is displayed in the status bar of AutoCAD UI. 


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading