Author Topic: How do you get Mouse Right-click to cancel Entity Jig  (Read 2952 times)

0 Members and 1 Guest are viewing this topic.

dugk

  • Guest
How do you get Mouse Right-click to cancel Entity Jig
« on: July 15, 2010, 02:59:14 PM »
Building on this post: http://www.theswamp.org/index.php?topic=33483.msg388566#msg388566

I would like to have the Mouse Right-click cancel the EntityJig being dragged.  I've tried several things as simple as returning SamplerStatus.Cancel when the bool userCancel == true to using try/catch and throwing an System.Exception("User cancelled.").  Nothing gets me to cancelling the EntityJig.

Code: [Select]
using System.Collections.Generic;
using System.Linq;

using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;

using AcApSrvApp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace psPlc
{
    class BlockJig : EntityJig
    {
        private static BlockJig currentJig;
        private Point3d mPosition;
        private int mOffsetIdx;
        private Vector3d[] mOffset;
        private JigMessageFilter mMsgFilter;
        private Point3d _pos;
        private Dictionary<ObjectId, AttInfo> _attInfo;
        private Transaction _tr;

        private static bool userCancel;

        public BlockJig(Transaction acTrans, BlockReference br, Dictionary<ObjectId, AttInfo> attInfo,
            ObjectId btrId, Vector3d[] nodeVectors)
            : base(br)
        {
            _pos = br.Position;
            _attInfo = attInfo;
            _tr = acTrans;
            mOffset = nodeVectors;
            mPosition = Point3d.Origin;
            Editor ed = AcApSrvApp.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;

            //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()
        {
            BlockReference br = Entity as BlockReference;

            Entity.Position = mPosition;
            br.Position = _pos;
            Entity.Position = mPosition;

            if (br.AttributeCollection.Count != 0)
            {
                foreach (ObjectId id in br.AttributeCollection)
                {
                    DBObject obj = _tr.GetObject(id, OpenMode.ForRead);
                    AttributeReference ar =
                      obj as AttributeReference;

                    // Apply block transform to att def position
                    if (ar != null)
                    {
                        ar.UpgradeOpen();
                        AttInfo ai = _attInfo[ar.ObjectId];
                        ar.Position =
                          ai.Position.TransformBy(br.BlockTransform);
                        if (ai.IsAligned)
                        {
                            ar.AlignmentPoint =
                              ai.Alignment.TransformBy(br.BlockTransform);
                        }
                        if (ar.IsMTextAttribute)
                        {
                            ar.UpdateMTextAttribute();
                        }
                    }
                }
            }
            return true;
        }

        protected override SamplerStatus Sampler(JigPrompts prompts)
        {
            JigPromptPointOptions jigOpts = new JigPromptPointOptions("\nInsertion point");
            jigOpts.UserInputControls = UserInputControls.NoZeroResponseAccepted |
                                        UserInputControls.GovernedByOrthoMode |
                                        UserInputControls.Accept3dCoordinates;
            PromptPointResult res = prompts.AcquirePoint(jigOpts);
            psPlc.insertPn.basePoint = new Point3d(res.Value.X, res.Value.Y, res.Value.Z);
            if (userCancel == true)// || res.Status == PromptStatus.Cancel)
            {
                //AcApSrvApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage("Right-click.");
                //if (userCancel == true)
                //{
                //userCancel = false;
                //throw new System.Exception("User cancelled.");
                    //Run();
                //}
                //try
                //{
                    //if (userCancel == true)
                    //{
                        //userCancel = false;
                        //throw new System.Exception("User cancelled.");
                    //}
                //}
                //catch { };
                //return SamplerStatus.Cancel;
            }
            if (res.Status == PromptStatus.Cancel)
            {
                aCommon.Methods.clearCommandLine();
                return SamplerStatus.Cancel;
            }
            Point3d newPos = res.Value.Add(mOffset[mOffsetIdx]);
            if (mPosition == newPos)
                return SamplerStatus.NoChange;
            mPosition = newPos;
            return SamplerStatus.OK;
        }

        public PromptStatus Run()
        {
            //try
            //{
            //    if (userCancel == true)
            //    {
            //        userCancel = false;
            //        throw new System.Exception("User cancelled.");
            //    }
            //    PromptResult promptResult = AcApSrvApp.DocumentManager.MdiActiveDocument.Editor.Drag(this);

            //    return promptResult.Status;
            //}
            //catch { };

            //return PromptStatus.Cancel;
            PromptResult promptResult = AcApSrvApp.DocumentManager.MdiActiveDocument.Editor.Drag(this);

            return promptResult.Status;
        }

        public void MoveOffsetIndex()
        {
            mOffsetIdx = (++mOffsetIdx) % mOffset.Count();
        }

        public void Cleanup()
        {
            System.Windows.Forms.Application.RemoveMessageFilter(mMsgFilter);
        }

        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 || (int)m.WParam == 9) //is this a tab or 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;
                    }
                    else if ((int)m.WParam == 13)// || (int)m.WParam == 1 || (int)m.WParam == 27)
                    {
                        userCancel = true;
                    }
                }
                return false;
            }
        }
    }
}

Thanks for you help.

Doug

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: How do you get Mouse Right-click to cancel Entity Jig
« Reply #1 on: July 17, 2010, 09:02:33 AM »
Hi,

Add UserInputControls.NullResponseAccepted to the jigOpts.UserInputControls
and just evaluates if promptResult.Status == PromptResult.OK
Speaking English as a French Frog

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: How do you get Mouse Right-click to cancel Entity Jig
« Reply #2 on: July 17, 2010, 11:56:28 AM »
Here's a little sample

Code: [Select]
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

namespace BlockJigTest
{
    class BlockJig : EntityJig
    {
        BlockReference m_blkRef;
        Point3d m_insPt;

        public BlockJig(BlockReference br) : base(br)
        {
            m_insPt = br.Position;
            m_blkRef = br;
        }

        protected override SamplerStatus Sampler(JigPrompts prompts)
        {
            JigPromptPointOptions jigOpts = new JigPromptPointOptions();
            jigOpts.UserInputControls =
              (UserInputControls.Accept3dCoordinates | UserInputControls.NullResponseAccepted);
            jigOpts.Message = "\nEnter insert point: ";
            PromptPointResult ppr = prompts.AcquirePoint(jigOpts);
            if (m_insPt == ppr.Value)
                return SamplerStatus.NoChange;
            else
                m_insPt = ppr.Value;
            return SamplerStatus.OK;
        }

        protected override bool Update()
        {
            m_blkRef.Position = m_insPt;
            return true;
        }
    }

    public class TestCommand
    {
        [CommandMethod("TEST")]
        public void Test()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            PromptStringOptions opts = new PromptStringOptions("\nEnter block name: ");
            PromptResult pr = ed.GetString(opts);
            if (pr.Status != PromptStatus.OK)
                return;
            Transaction tr = doc.TransactionManager.StartTransaction();
            using (tr)
            {
                BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                if (!bt.Has(pr.StringResult))
                {
                    ed.WriteMessage("\nBlock not found.");
                    return;
                }
                ObjectId blockId = bt[pr.StringResult];
                while (pr.Status == PromptStatus.OK)
                {
                    Point3d pt = new Point3d(0, 0, 0);
                    using (BlockReference br = new BlockReference(pt, blockId))
                    {
                        BlockJig entJig = new BlockJig(br);
                        pr = ed.Drag(entJig);
                        if (pr.Status == PromptStatus.OK)
                        {
                            BlockTableRecord btr =
                                (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                            btr.AppendEntity(br);
                            tr.AddNewlyCreatedDBObject(br, true);
                            doc.TransactionManager.QueueForGraphicsFlush();
                        }
                    }
                }
                tr.Commit();
            }
        }
    }
}
Speaking English as a French Frog

dugk

  • Guest
Re: How do you get Mouse Right-click to cancel Entity Jig
« Reply #3 on: July 23, 2010, 11:22:50 AM »
Thank you!

That did the trick.  By simply adding the line:

Code: [Select]
                UserInputControls.NullResponseAccepted |
in:

Code: [Select]
        protected override SamplerStatus Sampler(JigPrompts prompts)
        {
            JigPromptPointOptions jigOpts = new JigPromptPointOptions("\nInsertion point");
            jigOpts.UserInputControls = UserInputControls.NoZeroResponseAccepted |
                UserInputControls.NullResponseAccepted |
                UserInputControls.GovernedByOrthoMode |
                UserInputControls.Accept3dCoordinates;
            PromptPointResult res = prompts.AcquirePoint(jigOpts);
            psPlc.insertPn.basePoint = new Point3d(res.Value.X, res.Value.Y, res.Value.Z);
            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;
        }

it now works as requested.

Thanks again!

FYI - I have another issue with this EntityJig posted here:  http://www.theswamp.org/index.php?topic=34230.0