Author Topic: Kean's using shift and control keys during jig - But I need the Tab key.  (Read 3463 times)

0 Members and 1 Guest are viewing this topic.

dugk

  • Guest
Hi All,

I've looked at this post:
http://through-the-interface.typepad.com/through_the_interface/2009/11/detecting-the-use-of-the-shift-and-control-keys-during-an-autocad-jig-using-net.html#comment-6a00d83452464869e20134819ca5a9970c

and this post:
http://through-the-interface.typepad.com/through_the_interface/2007/02/allowing_users_.html

I've gotten that latter of the two to stop looping at the pressing of the Tab key.  That was easy.  But I want to catch the Tab key being press inside an Insert Jig.

I've tried combining the two but I'm not having any success.  I've posted my attempt below:
Code: [Select]
    namespace JigTextPlanarToScreen
    {
        public class TextJig : DrawJig
        {
            private Point3d _position;
            public bool tab;
            public string tabValue = "Tab Key OFF";
            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 = AcApSrvApp.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 IsInputKey(Keys keyData)
            //{
            //    if (keyData == Keys.Tab) return true;
            //    return base.IsInputKey(keyData);
            //}

            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;
                    wg.Text(
                      new Point3d(0, 0, 0),  // Position
                      new Vector3d(0, 0, 1), // Normal
                      new Vector3d(1, 0, 0), // Direction
                      tabValue,
                      true,                  // Rawness
                      _normal
                    );
                    // Remember to pop our transforms off the stack
                    wg.PopModelTransform();
                    wg.PopModelTransform();
                    // Create and add our message filter
                    MyMessageFilter filter = new MyMessageFilter();
                    System.Windows.Forms.Application.AddMessageFilter(filter);
                    // Start the loop
                    System.Windows.Forms.Application.DoEvents();
                    // Check whether the filter has set the flag
                    if (filter.bCanceled == true)
                    {
                        AcApSrvApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nLoop cancelled.");
                        //break;
                    }
                    // We're done - remove the message filter
                    System.Windows.Forms.Application.RemoveMessageFilter(filter);
                }
                return true;
            }

            [CommandMethod("SELPT")]
            static public void SelectPointWithJig()
            {
                Document doc = AcApSrvApp.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);
                }
            }

            // Our message filter class
            public class MyMessageFilter : IMessageFilter
            {
                public const int WM_KEYDOWN = 0x0100;
                public bool bCanceled = false;
                public bool PreFilterMessage(ref Message m)
                {
                    AcApSrvApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage(
                        "\n\tWM_KEYDOWN=" + WM_KEYDOWN.ToString() +
                        "\n\tm.Msg=" + m.Msg +
                        "\nm=" + m.ToString());
                    if (m.Msg == WM_KEYDOWN)
                    {
                        AcApSrvApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\n2");
                        // Check for the Escape keypress
                        Keys kc = (Keys)(int)m.WParam & Keys.KeyCode;
                        AcApSrvApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\n3");
                        if (m.Msg == WM_KEYDOWN && kc == Keys.Tab)
                        {
                            AcApSrvApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\n4");
                            bCanceled = true;
                        }
                        // Return true to filter all keypresses
                        return true;
                    }
                    // Return false to let other messages through
                    return false;
                }
            }
        }
    }

Any suggestions?

Thanks!
Doug

dugk

  • Guest
It isn't as elegant a solution as I was hoping for but I've used the following to get my needs met:
Code: [Select]
        public static string nodeNext = "Next node";
        public static string inserStr = "\nSelect insertion point:";

Code: [Select]
                JigPromptPointOptions opts = new JigPromptPointOptions(inserStr);
                opts.BasePoint = new Point3d(0, 0, 0);
                opts.UserInputControls = UserInputControls.NoZeroResponseAccepted;
                opts.AppendKeywordsToMessage = true;
                opts.Keywords.Add(nodeNext);

Code: [Select]
                    PromptResult selPt;
                    do
                    {
                        BlockJig myJig = new BlockJig(tr, br2d, attInfo, nodePoints[nodePtCntr]);
                        AcApSrvApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage(nodePtCntr.ToString());
                        selPt = myJig.Run();
                        if (selPt.Status == PromptStatus.Keyword)
                        {
                            if (selPt.StringResult == nodeNext)
                            {
                                nodePtCntr++;
                                if (nodePtCntr == nodePoints.Count)
                                    nodePtCntr = 0;
                            }
                        }
                        else if (selPt.Status != PromptStatus.OK)
                            return;
                    } while (selPt.Status != PromptStatus.OK);
                    tr.Commit();

dugk

  • Guest
Here is code I got from someone at ADN (hopefully I can share it) that worked perfectly per my needs.  It also addresses my request in the following post: http://www.theswamp.org/index.php?topic=33519.0

Code: [Select]
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
 
[assembly: CommandClass(typeof(DotNetJig.CBlockInsertControlJig))]
 
namespace DotNetJig
{
    public class CBlockInsertControlJig : EntityJig
    {
        private static CBlockInsertControlJig currentJig;
 
        private Point3d mPosition;
     
        private int mOffsetIdx;
 
        private Vector3d[] mOffset;
 
        private JigMessageFilter mMsgFilter;
 
        public CBlockInsertControlJig(ObjectId btrId)
            : base(new BlockReference(Point3d.Origin, btrId))
        {
            mPosition = Point3d.Origin;
         
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
 
            Matrix3d ucsRotation = ed.CurrentUserCoordinateSystem;
 
            //cancel translation component of UCS matrix
            ucsRotation = ucsRotation.PostMultiplyBy(Matrix3d.Displacement(ucsRotation.Translation));
 
            //Respects UCS orientation
            Entity.TransformBy(ucsRotation);
 
            mOffsetIdx = 0;
 
            //Our predefined offsets computed in advance depending of your custom block
            //Here we just use some dummy values
            mOffset = new Vector3d[]{new Vector3d(0,0,0),
                                     new Vector3d(5,0,0),
                                     new Vector3d(-5,0,0),
                                     new Vector3d(0,5,0),
                                     new Vector3d(0,-5,0)};
 
            //We need to transform also those offsets depending of the UCS
            for (int idx=0; idx<mOffset.Length; ++idx)
            {
                mOffset[idx] = mOffset[idx].TransformBy(ucsRotation);
            }
 
            //Setup message filter for SPACE keydown -> change the offset
            //(could be any other key or controlled by jig key word...)
            mMsgFilter = new JigMessageFilter();
            System.Windows.Forms.Application.AddMessageFilter(mMsgFilter);
 
            currentJig = this;
        }
 
        public new BlockReference Entity
        {
            get
            {
                return base.Entity as BlockReference;
            }
        }
 
        protected override bool Update()
        {
            Entity.Position = mPosition;
            return true;
        }
 
        protected override SamplerStatus Sampler(JigPrompts prompts)
        {
            JigPromptPointOptions jigOpts = new JigPromptPointOptions();
 
            jigOpts.UserInputControls = UserInputControls.NoZeroResponseAccepted |
                                        UserInputControls.GovernedByOrthoMode |
                                        UserInputControls.Accept3dCoordinates;
 
            PromptPointResult res = prompts.AcquirePoint(jigOpts);
 
            if (res.Status == PromptStatus.Cancel)
                return SamplerStatus.Cancel;
 
            Point3d newPos = res.Value.Add(mOffset[mOffsetIdx]);
 
            if (mPosition == newPos)
                return SamplerStatus.NoChange;
 
            mPosition = newPos;
 
            return SamplerStatus.OK;
        }
 
        public PromptStatus Run()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
 
            PromptResult promptResult = ed.Drag(this);
 
            return promptResult.Status;
        }
 
        public void MoveOffsetIndex()
        {
           mOffsetIdx = (++mOffsetIdx)%5;
        }
 
        public void Cleanup()
        {
            System.Windows.Forms.Application.RemoveMessageFilter(mMsgFilter);
        }
 
        [CommandMethod("DoBlockInsertControlJig")]
        static public void DoBlockInsertControlJig()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
 
            PromptStringOptions pso = new PromptStringOptions("\nEnter block name: ");
 
            PromptResult pr = ed.GetString(pso);
 
            if (pr.Status != PromptStatus.OK) return;
 
            using (Transaction Tx = doc.TransactionManager.StartTransaction())
            {
                BlockTable bT = Tx.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
 
                if (!bT.Has(pr.StringResult))
                {
                    ed.WriteMessage("\nBlock do not exist :(");
                    return;
                }
 
                ObjectId btrId = bT[pr.StringResult];
 
                CBlockInsertControlJig jig = new CBlockInsertControlJig(btrId);
 
                if (jig.Run() != PromptStatus.OK)
                {
                    jig.Entity.Dispose();
                }
                else
                {
                    BlockTableRecord model = Tx.GetObject(bT[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
 
                    model.AppendEntity(jig.Entity);
                    Tx.AddNewlyCreatedDBObject(jig.Entity, true);
                }
 
                jig.Cleanup();
                Tx.Commit();
            }
        }
 
        class JigMessageFilter : System.Windows.Forms.IMessageFilter
        {
            int WM_KEYDOWN = 0x100;
           
            public bool PreFilterMessage(ref System.Windows.Forms.Message m)
            {
                if (m.Msg == WM_KEYDOWN)
                {
                    if ((int)m.WParam == 32) //is this a space?
                    {
                        currentJig.MoveOffsetIndex();
 
                        //triggers graphic update without the need to move the mouse
                        System.Windows.Forms.Cursor.Position = System.Windows.Forms.Cursor.Position;
 
                        //Block message for AutoCAD
                        return true;
                    }
                }
 
                return false;
            }
        }
    }
}

vegbruiser

  • Guest
Re: Kean's using shift and control keys during jig - But I need the Tab key.
« Reply #3 on: January 25, 2011, 07:15:00 AM »
Hi Dug,

This looks like a potentially useful piece of code, but I was wondering if it might be possible to modify it to make the block scale instead of rotating; and if so, what would I need to modify?

Thanks,

Alex.