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

C# Cancel active prompts

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
teckstrand
2853 Views, 6 Replies

C# Cancel active prompts

I have a custom palette that has a bunch of blocks on it, when the user selects a block it prompts for a point using doc.Editor.GetPoint and loops through so that they can keep clicking and inserting the same block in different places.  The issue I have is that this prompt doesn't seem to be considered a command, so doc.CommandInProgress returns "" when it is running through the loop.

 

I want the current command/prompt to cancel when the user clicks on the different block, and then continue looping through the new block for insertion points.

 

I have tried the following, but since autocad doesn't consider the prompt a command, the CommandCancelled event is never called.

 

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

 

Is there a way I can either cancel all current prompts / or have the CommandCancelled even called when it is one of my prompt being run?  Or another way to cancel all commands / prompts and wait until complete before continuing with the code (other options I've tried haven't worked and I can't find a solution).

 

Thanks,

6 REPLIES 6
Message 2 of 7

Hi,

 

doc.SendStringToExecute("\x03\x03", false, true, false); should cancel your current command. Try setting focus to AutoCAD using "Focus" as shown below.

 

private void button1_Click(object sender, EventArgs e)
{
    //cancel the commands
    Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
    doc.Window.Focus();
    doc.SendStringToExecute("\x03\x03", false, true, false);
}

also, add the "CommandCancelled" in your command which shows the palette.



Virupaksha Aithal KM
Developer Technical Services
Autodesk Developer Network

Message 3 of 7

Thanks, but it does cancel the current command, the problem is that it doesn't fire the CommandCancelled event, and doc.CommandInProgress is always blank if I'm waiting on Editor.GetPoint from the user.

 

So I'm not able to put my code for the new command in doc.CommandCancelled, and since 
doc.SendStringToExecute is asynchronous that means that the code for my new block is fired before the cancel is called which cancels out of the Editor.GetPoint for the new block as well, I just want to cancel out for the old block.

 

So I need a way to have doc.SendStringToExecute("\x03\x03", false, true, false) fire my doc.CommandCancelled event when using Editor.GetPoint prompt or a different way to send the cancel command synchronously.  Most of the options I've seen posted are older and I can't get working in 2016+ AutoCAD.

 

The palette is always showing, this is the full process:

1) User clicks on a button on the palette.  Should cancel out of any current command before continuing with code.  This works for commands like LINE if they are running and calls the CommandCancelled event, but doesn't work when it's one of my own Prompts waiting.

2) Code grabs information for the corresponding block

3) Prompt user for the insertion point of the block

4) Loop until cancelled -> If user cancels themselves there is no issue.

5) User clicks on new button -> Needs to cancel out of last command before running new code.

6) Repeat. 

 

It looks like Editor.Command() sends synchronously but I can't find what to send it for cancel.

Message 4 of 7
teckstrand
in reply to: teckstrand

I've tried the following as well but can't seem to get the right parameters, they all cause eInvalidInput or 'Value does not fall within the expected range' errors. 

 

await ed.CommandAsync("\x03\x03", false, true, false);

await ed.CommandAsync("\x03\x03");

 

With code in the onComplete

Editor.CommandResult cmdResult = ed.CommandAsync("\x03\x03", false, true, false);
cmdResult.OnCompleted(() =>{});

Message 5 of 7
norman.yuan
in reply to: teckstrand

Well, since you did not show the code of how the block inserting process begins because of user clicking something in the palette, it is a bit difficult to say what and where some code could be placed to achieve what you want. There could be different ways to do it, depending on how you create the PaletteSet (i.e. subclassing the Autodesk.AutoCAD.Windows.PaletteSet). It is probably not easy to show a fully run-able code solution as a reply to your question. I decided to post a full code solution in my blog to make it easier to read for everyone who might be interested in this discussion. the article is here:

 

http://drive-cad-with-code.blogspot.com/2018/10/executing-command-from-paletteset.html

 

HTH

 

Message 6 of 7
teckstrand
in reply to: norman.yuan

Thanks, I wasn't actually calling a command on the button click event, instead I was just calling a method.  I changed it to call a command instead and now it works as expected.  I was trying to avoid having to do that, but it seems like that is by far the easiest solution.

Message 7 of 7

Can you tell me what you referenced in your project that enabled you to instantiate  types as Autodesk.AutoCAD.ApplicationServices. . . ??

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

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost