6

I'm trying to write two methods which call AutoCAD's UNDO command and pass in different parameters. The first method calls UNDO and passes M which means mark the position of the drawing. The second method calls UNDO and passes B which means undo all the way back to the marked position (or the end if there isnt one). So far they are pretty simple

        /// <summary>
        /// Method to mark the current position of the AutoCAD program
        /// </summary>
        public static void MarkPosition()
        {
            doc.SendStringToExecute("._UNDO M", true, false, true);
        }

        /// <summary>
        /// Method to step AutoCAD back int steps
        /// </summary>
        public static void BigUndo()
        {
            doc.SendStringToExecute("._UNDO B", true, false, true);
        }

These look like they should work but for some reason they don't. When I call MarkPosition() and then BigUndo() I get an error saying Start of Group encountered; enter Undo End to go back further. To test my syntax. I changed MarkPosition to

public static void MarkPosition() 
{
    doc.SendStringToExecute("circle 2,2,0 4 ", true, false, true);
}

which successfully draws a circle. That means my syntax is right but something weird is going on with Undo.

Nick Gilbert
  • 4,159
  • 8
  • 43
  • 90
  • Presumably typing the command directly in AutoCAD gives you the desired response? – ZombieSheep Mar 31 '15 at 16:56
  • Yes, that is correct – Nick Gilbert Mar 31 '15 at 17:08
  • SendStringtoExecute is asynchronous. They are executed after the .net code has ended. Are you sure You are testing the right stuff? try to draw the circle via this method between the mark and back http://help.autodesk.com/view/ACD/2016/ENU/?guid=GUID-F4A36181-39FB-4923-A2AF-3333945DB289 – Alain Apr 01 '15 at 05:54

3 Answers3

2

When you use SendCommand you always need a space at the end, that will make sure the command runs.

Also, on AutoCAD 2015 (and newer) you can user Editor.Command or Editor.CommandAsync, which is much better.

http://adndevblog.typepad.com/autocad/2014/04/migration-after-fiber-is-removed-in-autocad-2015.html

Augusto Goncalves
  • 8,493
  • 2
  • 17
  • 44
0

The space you're sending isn't being recognized as a new line by AutoCAD. You have to add the new line first then send the next character on another line like below:

            doc.SendStringToExecute("._UNDO\n", true, false, true);
            doc.SendStringToExecute("M\n", true, false, true);

            doc.SendStringToExecute("._UNDO\n", true, false, true);
            doc.SendStringToExecute("B\n", true, false, true);
prestonsmith
  • 758
  • 1
  • 9
  • 29
0

You're missing a space @END of the command string.

 doc.SendStringToExecute("._UNDO B", true, false, true); // Instead of this
 doc.SendStringToExecute("._UNDO B ", true, false, true); // use this
illright
  • 3,991
  • 2
  • 29
  • 54