.NET

Reply
Valued Contributor
dynamicscope
Posts: 95
Registered: ‎04-04-2012
Message 1 of 13 (663 Views)
Accepted Solution

CommandEnded Event handling with OVERKILL

663 Views, 12 Replies
01-31-2013 12:29 AM
public void A()
{
   Document doc = Application.DocumentManager.MdiActiveDocument;
   doc.SendStringToExecute("-overkill\np\n\n\n", true, false, false);
   doc.CommandEnded += new CommandEventHandler(doc_CommandEnded);
}

public void doc_CommandEnded(object sender, CommandEventArgs e)
{
   Document doc = sender as Document;
   if (!doc.Editor.IsQuiescent) return;

   DoSomeWork();
}

 As you can see above, I am trying to call DoSomeWork() after "-overkill" command finishes.

It looks like "-overkill" command is consits of a number of other commands because CommandEnded event gets triggered many times.

So I had to check if Editor is quiescent.

 

But the problem is CommandEnded event does not get tirggered when Editor.IsQuiescent.

So DoSomeWork() never gets called.

 

I also haved tried EnteringQuiescentState event instead of CommandEnded event, but it did not help.

EnteringQuiescentState event also gets called many times before "-overkill" command ends.

 

How could I solve this problem?

Any idea??

Moderator
Alexander.Rivilis
Posts: 1,398
Registered: ‎04-09-2008
Message 2 of 13 (654 Views)

Re: CommandEnded Event handling with OVERKILL

01-31-2013 01:56 AM in reply to: dynamicscope

dynamicscope wrote:

How could I solve this problem?

Any idea??


How to run AutoCAD interactive commands and run code after command completes

Synchronously Send (and wait for) commands in AutoCAD using C# .NET


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Valued Mentor
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 3 of 13 (648 Views)

Re: CommandEnded Event handling with OVERKILL

01-31-2013 04:03 AM in reply to: dynamicscope

I'm going to respectfully disagree with Alex's advice to do what that article he referred to shows.  

 

That is the absolutely worse possible way to solve a problem like the one you face.

 

How to best solve it depends on if you are trying to execute the command from a modal command handler or from the Application's execution context. In either case, you can use the Editor's RunCommand() method to execute commands synchronously, so that you can easily have control after they've finished.

 

RunCommand() is undocumented and must be invoked via reflection, but that's not hard to do, and you should be able to rely on it continuing to be there, as along as acedCmd() and acedCommand() continue to exist.

 

See this swamp post for more info on using RunCommand.

 

When you are trying to execute commands from the application context (e.g., from an event handler for a control on a Palette, modeless dialog, or the ribbon), you can define your own modal command that you can start using SendStringToExecute(), and that modal command can then use RunCommand() to execute commands syncronously, and allow you to easily get control after the commands have finished.

 

 

Moderator
Alexander.Rivilis
Posts: 1,398
Registered: ‎04-09-2008
Message 4 of 13 (644 Views)

Re: CommandEnded Event handling with OVERKILL

01-31-2013 04:23 AM in reply to: DiningPhilosopher

Hi, Tony!


DiningPhilosopher wrote:

I'm going to respectfully disagree with Alex's advice to do what that article he referred to shows.


Then the correct answer would be to totally rewrite the code -OVERKILL command by means of AutoCAD .NET API :smileyhappy:


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Valued Contributor
dynamicscope
Posts: 95
Registered: ‎04-04-2012
Message 5 of 13 (625 Views)

Re: CommandEnded Event handling with OVERKILL

02-01-2013 12:24 AM in reply to: Alexander.Rivilis

Sadly, this approach still does not wait for the "OVERKILL" to end... =(

Moderator
Alexander.Rivilis
Posts: 1,398
Registered: ‎04-09-2008
Message 6 of 13 (622 Views)

Re: CommandEnded Event handling with OVERKILL

02-01-2013 01:28 AM in reply to: dynamicscope

dynamicscope wrote:

Sadly, this approach still does not wait for the "OVERKILL" to end... =(


Which of me and Tony approaches do not work? And what code do you use?


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Valued Mentor
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 7 of 13 (608 Views)

Re: CommandEnded Event handling with OVERKILL

02-01-2013 09:19 AM in reply to: dynamicscope

dynamicscope wrote:

Sadly, this approach still does not wait for the "OVERKILL" to end... =(


Not sure what approach you're referring to, but the one that I wrote about works.

 

Here is the LISP (command) function call to run OVERKILL using the current options:

 

(command "._-OVERKILL" "ALL" "" "")

 Doing the same using the Command() extension method:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices;

namespace Namespace1
{
   public static class SyncOverkillExample
   {
      [CommandMethod( "MYOVERKILL" )]
      public static void MyOverkill()
      {
         Document doc = Application.DocumentManager.MdiActiveDocument;
         doc.Editor.Command( "._-OVERKILL", "_ALL", "", "" );

         doc.Editor.WriteMessage( "\nOverkill finished," );
         doc.Editor.WriteMessage( "\ndoing more stuff now...8-)" );

         // TODO: Here you can do what must be done after
         //       the OVERKILL command is finished.
      }
   }
}

namespace Autodesk.AutoCAD.EditorInput
{
   public static class MyEditorExtensions
   {
      static MethodInfo runCommand = typeof( Editor ).GetMethod(
         "RunCommand", BindingFlags.Instance 
            | BindingFlags.NonPublic | BindingFlags.Public );

      public static PromptStatus Command( this Editor ed, params object[] args )
      {
         if( Application.DocumentManager.IsApplicationContext )
            throw new InvalidOperationException( 
               "Invalid execution context for Command()" );
         if( ed.Document != Application.DocumentManager.MdiActiveDocument )
            throw new InvalidOperationException( "Document is not active" );
         return (PromptStatus) runCommand.Invoke( ed, new object[] { args } );
      }
   }
}

 

 

 

Valued Mentor
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 8 of 13 (597 Views)

Re: CommandEnded Event handling with OVERKILL

02-01-2013 10:35 AM in reply to: Alexander.Rivilis

hI


Alexander.Rivilis wrote:

Hi, Tony!


DiningPhilosopher wrote:

I'm going to respectfully disagree with Alex's advice to do what that article he referred to shows.


Then the correct answer would be to totally rewrite the code -OVERKILL command by means of AutoCAD .NET API :smileyhappy:


Well, I don't know if there's a correct o:smileywink:r incorrect answer, but the best solution IMO, is the one shown by the above example. 

Moderator
Alexander.Rivilis
Posts: 1,398
Registered: ‎04-09-2008
Message 9 of 13 (578 Views)

Re: CommandEnded Event handling with OVERKILL

02-02-2013 01:59 AM in reply to: DiningPhilosopher

Hi, Tony!

A small clarification. The first version of AutoCAD in which ._-OVERKILL can be invoke with (command ...) is AutoCAD 2012. Previous versions of AutoCAD has lisp-defined command -OVERKILL and can not been called with (command ...) Only with COM-method SendCommand or .NET method SendStringToExecute.

AutoCAD 2011:

Command: (command "._-OVERKILL" "ALL" "" "") ._-OVERKILL Unknown command 
"-OVERKILL".  Press F1 for help.
Command: ALL Unknown command "ALL".  Press F1 for help.

Command: ._-OVERKILL
Initializing...
Select objects: _ALL
0 found
Select objects:

 

 


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Valued Mentor
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 10 of 13 (569 Views)

Re: CommandEnded Event handling with OVERKILL

02-02-2013 09:52 AM in reply to: Alexander.Rivilis

Hi Alex, and thanks.

 

I didn't notice the OP mention what release(s) he needs to work with.

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community