C# Cancel active prompts

Anonymous

C# Cancel active prompts

Anonymous
Not applicable

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,

0 Likes
Reply
Accepted solutions (1)
3,640 Views
6 Replies
Replies (6)

Virupaksha_aithal
Autodesk Support
Autodesk Support

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

0 Likes

Anonymous
Not applicable

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.

0 Likes

Anonymous
Not applicable

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(() =>{});

0 Likes

norman.yuan
Mentor
Mentor
Accepted solution

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

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes

Anonymous
Not applicable

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.

0 Likes

Anonymous
Not applicable

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

0 Likes