August 2014

Sun Mon Tue Wed Thu Fri Sat
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            








« AU 2009 classes, both physical and virtual | Main | Shaan by name, shorn by nature »

November 05, 2009

Detecting the use of the shift and control keys during an AutoCAD jig using .NET

Another interesting little problem, that of how to detect the use of modifier keys during a jig operation (to indicate different jig behaviour). In this case the specific task was to detect the use of the Control and Shift keys, which – if held down during the jig – should cause our object to display differently.

I started with the code from this previous post which uses a DrawJig to place text in the plane of the screen during the jig operation. I initially thought I’d have to use a message filter (as shown in this previous post), but I eventually realised it wasn’t needed: it’s simply a matter of querying the state of System.Windows.Forms.Control.ModifierKeys at the appropriate moment during our WorldDraw().

Here’s the C# code I used:

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Geometry;

using Autodesk.AutoCAD.GraphicsInterface;

 

namespace JigTextPlanarToScreen

{

  public class TextJig : DrawJig

  {

    private Point3d _position;

    public Point3d Position

    {

      get { return _position; }

    }

 

    // We'll keep our styles alive rather than recreating them

 

    private TextStyle _normal;

    private TextStyle _highlight;

 

    public TextJig()

    {

      _normal = new TextStyle();

      _normal.Font =

        new FontDescriptor("Calibri", false, true, 0, 0);

      _normal.TextSize = 10;

 

      _highlight = new TextStyle();

      _highlight.Font =

        new FontDescriptor("Calibri", true, false, 0, 0);

      _highlight.TextSize = 14;

    }

 

    protected override SamplerStatus Sampler(JigPrompts prompts)

    {

      JigPromptPointOptions opts = new JigPromptPointOptions();

      opts.UserInputControls =

        UserInputControls.Accept3dCoordinates |

        UserInputControls.AcceptMouseUpAsPoint |

        UserInputControls.GovernedByUCSDetect;

 

      opts.Message = "\nSelect point: ";

      PromptPointResult res = prompts.AcquirePoint(opts);

 

      Document doc =

        Application.DocumentManager.MdiActiveDocument;

      Editor ed = doc.Editor;

 

 

      if (res.Status == PromptStatus.OK)

      {

        if (_position == res.Value)

        {

          return SamplerStatus.NoChange;

        }

        else

        {

          _position = res.Value;

          return SamplerStatus.OK;

        }

      }

      return SamplerStatus.Cancel;

    }

 

    protected override bool WorldDraw(WorldDraw draw)

    {

      // We make use of another interface to push our transforms

 

      WorldGeometry wg = draw.Geometry as WorldGeometry;

      if (wg != null)

      {

        // Push our transforms onto the stack

 

        wg.PushOrientationTransform(

          OrientationBehavior.Screen

        );

        wg.PushPositionTransform(

          PositionBehavior.Screen,

          new Point2d(30, 30)

        );

 

        System.Windows.Forms.Keys mods =

          System.Windows.Forms.Control.ModifierKeys;

 

        // Check whether the shift key is being pressed

 

        bool shift =

          (mods & System.Windows.Forms.Keys.Shift) > 0;

 

        // Check whether the control key is being pressed

 

        bool control =

          (mods & System.Windows.Forms.Keys.Control) > 0;

 

        // Draw our screen-fixed text

 

        wg.Text(

          new Point3d(0, 0, 0),  // Position

          new Vector3d(0, 0, 1), // Normal

          new Vector3d(1, 0, 0), // Direction

          _position.ToString(),  // Text

          true,                  // Rawness

          (shift &&              // Textstyle

          control ?

          _highlight :          // Highlight if Ctrl-Shift

          _normal)              // Normal, otherwise

        );

 

        // Remember to pop our transforms off the stack

 

        wg.PopModelTransform();

        wg.PopModelTransform();

      }

 

      return true;

    }

 

    [CommandMethod("SELPT")]

    static public void SelectPointWithJig()

    {

      Document doc =

        Application.DocumentManager.MdiActiveDocument;

      Editor ed = doc.Editor;

 

      TextJig jig = new TextJig();

      PromptResult res = ed.Drag(jig);

 

      if (res.Status == PromptStatus.OK)

      {

        ed.WriteMessage(

          "\nPoint selected: {0}",

          jig.Position

        );

      }

    }

  }

}

You may have noticed the jig’s point acquisition has slightly different UserInputControls flags defined, mainly because I wanted to test out the jig’s ability to automatically switch the UCS when hovering over the faces of solids. It doesn’t have direct relevance to the rest of this post, but I’ve left it in as the application is actually quite a useful testbed for the 3D point acquisition capabilities of a jig. Plus it was easier to leave the code in than to change it. :-)

Here’s what happens when we run the SELPT command and just move the cursor around:

Normal text displayed by our jig

And here’s what happens when we do the same while pressing Ctrl-Shift – we see the text shown in a larger, bold, non-italic font:

Highlighted text displayed by our jig

I haven’t attempted to filter any key-down/key-up messages (we would have to go back an implement an IMessageFilter to do so), so use of the Shift key during our point input temporarily overrides the ORTHOMODE system variable (hence the glyph to the upper-right of the cursor). It’s possible to remove this temporary override via the CUI command, if needed.

blog comments powered by Disqus

Feed/Share

10 Random Posts