|
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
- 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
- );
- }
- }
- }
- }
复制代码
|
|