Author Topic: Command not working in CommandEnded Event  (Read 264 times)

0 Members and 1 Guest are viewing this topic.

flyte

  • Mosquito
  • Posts: 8
Command not working in CommandEnded Event
« on: September 20, 2017, 12:06:32 am »
I have a command that calls upon some editor commands to eventually perform an export (to dwf):

Code: [Select]
[CommandMethod("X3")]
        public static void X3()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;

            var currentFilename = doc.Name;
            var currentPath = Path.GetDirectoryName(currentFilename);
            var drawing = Path.GetFileNameWithoutExtension(currentFilename);
            var fileWithDwfExt = Path.ChangeExtension(drawing, ".dwf");

            var destFile = Path.Combine(currentPath, fileWithDwfExt);

            XrefUtils.DetachAllXrefsCommand();
            LayerUtils.Layerson();
            LayerUtils.ThawAll();
            ed.Command("-xref", "d", "*");
            ed.Command("export", destFile, "a", "y");
        }

This works just fine when executed via the AutoCAD commandline, however when the same routine is called in response to the Command* events:

Code: [Select]
public void Initialize()
        {
            Application.DocumentManager.DocumentCreated += DocumentManager_DocumentCreated;
            foreach (Document doc in Application.DocumentManager)
            {
                doc.CommandEnded += OnCommandEnding;
            }

        }

Code: [Select]
public static void OnCommandEnding(object sender, CommandEventArgs e)
        {
             X3();
        }


it fails with the following exception:
Code: [Select]
System.Exception {Autodesk.AutoCAD.Runtime.Exception}

with
Quote
"eInvalidInput"
as a message.

Anyone know what I'm doing wrong?

gile

  • Water Moccasin
  • Posts: 2094
  • Marseille, France
Re: Command not working in CommandEnded Event
« Reply #1 on: September 20, 2017, 03:54:10 am »
Hi,

As far as I know, you cannot call a command from an event handler (either using SendStringToExecute, SendCommand or Command).
Speaking English as a French Frog

WILL HATCH

  • Bull Frog
  • Posts: 405
Re: Command not working in CommandEnded Event
« Reply #2 on: September 27, 2017, 01:23:53 am »
In this sort of situation the Application.Idle event can be your friend to insuring that your code is running from the proper state, just add your callback function to the list then remove it once it's been called.

Code - C#: [Select]
  1.  
  2.        {
  3.             //code not running in application context
  4.             Autodesk.AutoCAD.ApplicationServices.Application.Idle += Application_Idle;
  5.        }
  6.  
  7.        static void Application_Idle(object sender, EventArgs e)
  8.        {
  9.            Autodesk.AutoCAD.ApplicationServices.Application.Idle -= Application_Idle;
  10.            //do stuff from application context
  11.        }

flyte

  • Mosquito
  • Posts: 8
Re: Command not working in CommandEnded Event
« Reply #3 on: November 01, 2017, 05:15:12 pm »
Hi,

As far as I know, you cannot call a command from an event handler (either using SendStringToExecute, SendCommand or Command).

So my command is successfully executing when one does a qsave or clicks the save toolbar button (seems to also issue a qsave command). But how do I react when the user does it through the dialog when it prompts you if you want to save upon exit? This doesnt' seem to trigger the CommandEnded event.. how would I solve this?

flyte

  • Mosquito
  • Posts: 8
Re: Command not working in CommandEnded Event
« Reply #4 on: November 01, 2017, 06:23:03 pm »
..actually I think I solved this by hooking into the e.Document.Database.SaveComplete command instead.. so now any save method will trigger my export, (I exclude autosave tho)