Nick Gilbert
Nick Gilbert

Reputation: 4240

Using AutoCAD API's Document.SendStringToExecute method correctly

I'm trying to mark my drawing before I programmatically insert a block so that I can programmatically undo the action if it only partially completes because of an error. Right now that insert method looks like this

public void askForInsertionPoint
{
StateManagementExtensions.MarkPosition();
try
{
    PromptPointResult pr = ed.GetPoint("\nSelect insertion point: ");
    Point3d insPt = pr.Value;
}
catch(Exception e)
{
 //TODO handle exception with undo
}
}

MarkPosition is defined as

public static void MarkPosition()
{
doc.SendStringToExecute("MARKPOS ", true, false, true);
}

Finally, sending MARKPOS to the command line as i do above calls this method

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

The pointprompt somehow beats the MARKPOS call to the AutoCAD commandline so it tries to enter MARKPOS as the insertion point instead of pausing the C# method to wait for MARKPOS to execute as a command. How do I signal the program to wait and execute the MARKPOS command before prompting for the insertion point? I've tried Thread.sleep() after the SendStringToExecute call and that didn't work.

Upvotes: 2

Views: 5261

Answers (2)

Augusto Goncalves
Augusto Goncalves

Reputation: 8574

Call one command inside another will require the first to be Transparent

[CommandMethod("nameHere", CommandFlags.Transparent)]

But as the SendStringToExecute is async, you still have problems... You'll probably need to use Editor.Commad instead.

Upvotes: 1

Miiir
Miiir

Reputation: 831

Wrap your function in a Transaction. If the Transaction doesn't commit then nothing will have to be rolled back.

If you want to be a perfectionist, store the current view location at the start of the routine so you can reset the zoom/location after your command.

Upvotes: 2

Related Questions