.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Cancel active command C#

6 REPLIES 6
Reply
Message 1 of 7
tamara
4224 Views, 6 Replies

Cancel active command C#

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.

 

6 REPLIES 6
Message 2 of 7
FRFR1426
in reply to: tamara

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
Message 3 of 7
tamara
in reply to: FRFR1426

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.

Message 4 of 7
dgorsman
in reply to: tamara

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.


Message 5 of 7
tamara
in reply to: dgorsman

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. 

Message 6 of 7
FRFR1426
in reply to: tamara

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
Message 7 of 7
Anonymous
in reply to: FRFR1426

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?

 

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report