3

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()

Nick Gilbert
  • 4,159
  • 8
  • 43
  • 90
  • 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. – bjhuffine May 06 '15 at 12:01

3 Answers3

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"});
}
Augusto Goncalves
  • 8,493
  • 2
  • 17
  • 44
  • 1
    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. – bjhuffine May 06 '15 at 11:59
  • 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. – Augusto Goncalves May 07 '15 at 12:12
1

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

0

Use this:

Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.SendStringToExecute
Mathias Müller
  • 22,203
  • 13
  • 58
  • 75
Scott Rea
  • 33
  • 2
  • Putting this code in context would improve the answer – Kyeotic Apr 15 '15 at 20:54
  • 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 Gilbert Apr 16 '15 at 13:16
  • @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" – bjhuffine May 06 '15 at 11:49