Cancel active command C#

Cancel active command C#

tamara
Advocate Advocate
5,449 Views
6 Replies
Message 1 of 7

Cancel active command C#

tamara
Advocate
Advocate

Hello, 

 

although I've seen many questions and answers on this subject none of them works for me except this one:

Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.SendStringToExecute("\x03\x03", false, true, false); 

but it runs asynchronously and that is not what I want.

Some answers that I found and tried out are more than 10 years old so I guess maybe they used to work on older version of CAD but now they don't. 

So to be clear, I need a way to cancel any active command synchronously, using C# and I work in AutoCAD 2018. 

I tried to use the Editor.Command() and Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication;.ActiveDocument.SendCommand(), but i don't know what string to pass. Tried so many without success.

 

Please help, this is driving me crazy.

 

0 Likes
5,450 Views
6 Replies
Replies (6)
Message 2 of 7

FRFR1426
Collaborator
Collaborator

Just install an handler on Document.CommandCancelled event before calling SendStringToExecute(). This way, you'll be able to run your code after the command is cancelled.

Maxence DELANNOY
Manager
Add-ins development for Autodesk software products
http://wiip.fr
0 Likes
Message 3 of 7

tamara
Advocate
Advocate

Interesting suggestion. Thank you very much, but I must say that I'm still hoping to get an answer on how to use Editor.Command(). If no one replays with that I will certainly use your suggestion.

0 Likes
Message 4 of 7

dgorsman
Consultant
Consultant

Just to be clear, are you looking to cancel the active command, regardless of what it is?  Or to run something when the active command is canceled by whatever method?

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


0 Likes
Message 5 of 7

tamara
Advocate
Advocate

This is the situation:

In my app, i have a Palette Set with Tool Strip. When user clicks on a button on a Tool Strip a new command is activated. But the problem is that if there is already a command running it won't be canceled. So when a button is pressed in my Pallete Set i need to check if there are any active commands and if so cancel them, and then start the new command. 

0 Likes
Message 6 of 7

FRFR1426
Collaborator
Collaborator

Why are you not putting the command you want to launch after the ETX chars?

 

Application.DocumentManager.MdiActiveDocument.SendStringToExecute("\x03\x03YOURCOMMAND\n", false, false, false); 

 

And I think that in your case, you can pass false for wrapUpInactiveDoc as you are working on the active document.

 

Editor.Command() does not work in an applicationContext (I think it throws an invalid input exception).

 

Maxence DELANNOY
Manager
Add-ins development for Autodesk software products
http://wiip.fr
0 Likes
Message 7 of 7

Anonymous
Not applicable

When exactly does this event get called?  Only if a command is cancelled before finishing or whenever a cancel happens?

 

I ask because I have this code and it never calls Document_CommandCancelled

Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
doc.CommandCancelled += new CommandEventHandler(Document_CommandCancelled);
doc.SendStringToExecute("\x03\x03", false, true, false);

If this isn't the way to do it, how do I get my code to fire Document_CommandCancelled after doc.SendStringToExecute?

 

 

0 Likes