Cancel other command

Cancel other command

dallas.a.adams
Explorer Explorer
675 Views
7 Replies
Message 1 of 8

Cancel other command

dallas.a.adams
Explorer
Explorer

I am using a toolbar to display a number of grid sizes that I commonly switch between. I am using this function to loop though all viewports and change the gird size on all of them when the button is pressed on the toolbar.

 

void SetGrid(double size, short spacing)
        {
            // Get AutoCAD objects.
            Document doc = Application.DocumentManager.MdiActiveDocument;
            KernelDescriptor kd = new KernelDescriptor();
            Database database = doc.Database;
            Autodesk.AutoCAD.EditorInput.Editor ed = doc.Editor;

            // Get the current CVPORT.
            int currentCVPORT = System.Convert.ToInt32(Application.GetSystemVariable("CVPORT"));

            // Get the other View Ports.
            List<int> viewPortNumbers = new List<int>();
            using (Transaction transaction = database.TransactionManager.StartTransaction())
            {
                SymbolTable symTable = (SymbolTable)transaction.GetObject(database.ViewportTableId, OpenMode.ForRead);
                foreach (ObjectId id in symTable)
                {
                    ViewportTableRecord symbol = (ViewportTableRecord)transaction.GetObject(id, OpenMode.ForWrite);
                    viewPortNumbers.Add(symbol.Number);
                    symbol.GridEnabled = true;
                    symbol.GridIncrements = new Autodesk.AutoCAD.Geometry.Point2d(size, size);
                    symbol.GridMajor = spacing;
                    symbol.SnapIncrements = new Autodesk.AutoCAD.Geometry.Point2d(size, size);
                }
                transaction.Commit();
                doc.Editor.UpdateTiledViewportsFromDatabase();
            }

            // Switch back to original view port.
            Application.SetSystemVariable("CVPORT", currentCVPORT);
        }

 

It seems to work except for if there is another command already in progress. I was wondering how I can cancel the previous command if there is one via .net?

0 Likes
Accepted solutions (1)
676 Views
7 Replies
Replies (7)
Message 2 of 8

_gile
Consultant
Consultant

Hi,

 

Assuming the SetGrid method is called from methods colored with the ComandMethod attribute, if the command macro of the button starts with: ^C^C, it should cancel the running command.

 

Command:

[CommandMethod("GRID15", CommandFlags.Modal)]
public void SetGrid_1_5()
{
     SetGrid(1.0, 5);
}

 

Macro:

^C^CGRID15


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 8

dallas.a.adams
Explorer
Explorer

Yes, you assumed correctly about the functions, although I had to add the command flag to all of them. They look like this now:

 

/// <summary>
        /// Method to set the grid and snap to 0.01mm.
        /// </summary>
        [Autodesk.AutoCAD.Runtime.CommandMethod("SNAPUNIT_0_01", CommandFlags.Modal)]
        public void SnapUnit_0_01_Command()
        {
            SetGrid(0.01, 10);
        }

 

But I tried to add the C^C^ to macro in the AcadToolbar.AddToolbarButton function but AutoCAD replies with:

 

Unknown command "^C^CSNAPUNIT_10".  Press F1 for help.

 

Here is the full Initialize function where I am adding the C^C^:

 

/// <summary>
        /// AutoCAD calls this method to initialize the extension.
        /// </summary>
        public void Initialize()
        {
            AcadApplication cadApp = (AcadApplication)Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication;
            AcadToolbar tb = cadApp.MenuGroups.Item(0).Toolbars.Add("GridSnap");
            tb.Dock(Autodesk.AutoCAD.Interop.Common.AcToolbarDockStatus.acToolbarDockLeft);
            
            button0_01mm = tb.AddToolbarButton(0, "0.01", "Sets the Snap/Grid 0.01mm", "^C^CSnapUnit_0_01\n", null);
            button0_05mm = tb.AddToolbarButton(1, "0.05", "Sets the Snap/Grid 0.05mm", "^C^CSnapUnit_0_05\n", null);
            button0_1mm = tb.AddToolbarButton(2, "0.1", "Sets the Snap/Grid 0.1mm", "^C^CSnapUnit_0_1\n", null);
            button0_5mm = tb.AddToolbarButton(3, "0.5", "Sets the Snap/Grid 0.5mm", "^C^CSnapUnit_0_5\n", null);
            button1mm = tb.AddToolbarButton(4, "1", "Sets the Snap/Grid 1mm", "^C^CSnapUnit_1\n", null);
            button5mm = tb.AddToolbarButton(5, "5", "Sets the Snap/Grid 5mm", "^C^CSnapUnit_5\n", null);
            button10mm = tb.AddToolbarButton(6, "10", "Sets the Snap/Grid 10mm", "^C^CSnapUnit_10\n", null);
            button50mm = tb.AddToolbarButton(7, "50", "Sets the Snap/Grid 50mm", "^C^CSnapUnit_50\n", null);
            button100mm = tb.AddToolbarButton(8, "100", "Sets the Snap/Grid 100mm", "^C^CSnapUnit_100\n", null);
            button500mm = tb.AddToolbarButton(9, "500", "Sets the Snap/Grid 500mm", "^C^CSnapUnit_500\n", null);
            button1000mm = tb.AddToolbarButton(10, "1000", "Sets the Snap/Grid 1000mm", "^C^CSnapUnit_1000\n", null);

            SetBitMaps();
        }

 

Is this not the right place to add this?

 

 

 

 

0 Likes
Message 4 of 8

_gile
Consultant
Consultant
Accepted solution

My mistake.

From the example in the documentation about AddToolbarButton AcitveX method, the macro string should be:

"\u0003\u0003SNAPUNIT_0_01\n"

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 8

kerry_w_brown
Advisor
Advisor

Bookmarked !

Thanks Gilles


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
0 Likes
Message 6 of 8

dallas.a.adams
Explorer
Explorer

Its working perfectly now. Thank you for your help, it is greatly appreciated. I am now inspired to try a few more functions now.

0 Likes
Message 7 of 8

kerry_w_brown
Advisor
Advisor

Hi, 

For curiosity . . 

What was the purpose of this ??

 

KernelDescriptor kd = new KernelDescriptor();

 

 

. . . Something to do with hardware Acceleration ??

Regards,

 


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
0 Likes
Message 8 of 8

dallas.a.adams
Explorer
Explorer

I'm not really sure.  Two of those lines at the start of the function are not used at all. Sit down, this might shock you, but I don't know what I'm doing. If I was to hazard a guess it comes from a previous attempt at trying to find the viewport objects. If I remember correctly, you need it to get the Graphics Kernel object to then call other functions like CreateAutoCADDevice and CreateAutoCADModel which is where I was originally thinking I be able to find the Viewports.

 

But as you can see, I found them in the Viewport table in the database.

 

 

0 Likes