4

I'm trying to create a button that, when pressed, marks the position of the drawing. Right now the method looks like this.

[CommandMethod("MARKPOS", CommandFlags.Session)]
public void MarkPosition()
{
    Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
    ed.Command("UNDO", "M");
}

However, when I try and execute the method I get the error pictured below and cannot determine why.

enter image description here

************** Exception Text ************** Autodesk.AutoCAD.Runtime.Exception: eInvalidInput at Autodesk.AutoCAD.EditorInput.Editor.Command(Object[] parameter) at AutoCAD_Adapter.MyCommands.MarkPosition() in c:\Users\nickg\Documents\All Code\autocad-adapter\IOAutoCADHandler\myCommands.cs:line 186 at Autodesk.AutoCAD.Runtime.CommandClass.InvokeWorker(MethodInfo mi, Object commandObject, Boolean bLispFunction) at Autodesk.AutoCAD.Runtime.CommandClass.InvokeWorkerWithExceptionFilter(MethodInfo mi, Object commandObject, Boolean bLispFunction) at Autodesk.AutoCAD.Runtime.PerDocumentCommandClass.Invoke(MethodInfo mi, Boolean bLispFunction) at Autodesk.AutoCAD.Runtime.CommandClass.CommandThunk.Invoke()

1
  • See my comments to 'Augusto Goncalves'. He has the right answer, but to clarify, your eInvalidInput exception is the result of the context in which your CommandMethod is running. Commented May 6, 2015 at 12:01

4 Answers 4

1

The SendStringToExecute will work until AutoCAD 2014. On AutoCAD 2015 (and newer) this was replaced with Editor.Command or Editor.CommandAsync.

About the original code, please try with

[CommandMethod("MARKPOS")]
public static void MarkPosition()
{
  Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  ed.Command(new object[]{"UNDO", "M"});
}
Sign up to request clarification or add additional context in comments.

2 Comments

Actually, SendStringToExecute() is still available in the 2015 API. I've used it and you can find it in AcCoreMgd.dll. But the problem 'Nick G' is having is that he is running this in the application context by adding the CommandFlags.Session enum to his CommandMethod attribute. Editor.Command() only works for the DocumentContext. Really, all he has to do to fix it is remove the Session enum and it will work.
That's correct, but the Editor.Command and CommandAsync is better as we have control (sync or async), that's why I suggested to replace.
1

You can't use the ed.command() when using CommandFlags.Session

Comments

1

Another idea that you can try is to

static void Invoke(Action action)
{
   var docmgr = Application.DocumentManager;
   if(docmgr.IsApplicationContext)
   {
      docmgr.ExecuteInCommandContextAsync(unused =>
      {
         action();
         return Task.CompletedTask;
      }, null);
   }
   else
   {
      action();
   }
}

This is how you call:

Invoke(() => YourMethod());

As per here.

Comments

0

Use this:

Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.SendStringToExecute

3 Comments

Putting this code in context would improve the answer
The method is synchronous. It waits until the end of the C# method to send the string which is after when I need the command to execute
@Nick G: You may not have meant it this way, but SendStringToExecute() is Asynchronous. From the AutoCAD .NET Developer's Guide: "Commands executed with SendStringToExecute are asynchronous and are not invoked until the .NET command has ended"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.