Another way to switch to the document context is to prompt the user within the button event handler, but in this case, you're responsible to lock the document (before calling TraceBoundary).
Command side:
[CommandMethod("DLG", CommandFlags.Modal)]
public static void ShowDialog()
{
var dlg = new Dialog1();
Application.ShowModelessDialog(null, dlg, false);
}
Dialog side:
private void button1_Click(object sender, EventArgs e)
{
var doc = AcAp.DocumentManager.MdiActiveDocument;
if (doc == null) return;
var ed = doc.Editor;
doc.Window.Focus();
var pointResult = ed.GetPoint("\nPick a point: "); // <- switch to document context
if (pointResult.Status != Autodesk.AutoCAD.EditorInput.PromptStatus.OK) return;
using (doc.LockDocument())
{
var bound = ed.TraceBoundary(pointResult.Value, true);
if (0 < bound.Count)
{
var db = doc.Database;
using (var tr = db.TransactionManager.StartTransaction())
{
var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
foreach (DBObject obj in bound)
{
var ent = (Entity)obj;
ent.ColorIndex = 1;
curSpace.AppendEntity(ent);
tr.AddNewlyCreatedDBObject(ent, true);
}
tr.Commit();
}
}
}
}
Even in this case, the more robust way is not more complex and allow to recall the last command by hiting enter.
Command side:
[CommandMethod("DLG", CommandFlags.Modal)]
public static void ShowDialog()
{
var dlg = new Dialog1();
Application.ShowModelessDialog(null, dlg, false);
}
[CommandMethod("DRAWBOUND")]
public static void DrawBoundary()
{
var doc = Application.DocumentManager.MdiActiveDocument;
var ed = doc.Editor;
var pointResult = ed.GetPoint("\nPick a point: ");
if (pointResult.Status != PromptStatus.OK) return;
var bound = ed.TraceBoundary(pointResult.Value, true);
if (0 < bound.Count)
{
var db = doc.Database;
using (var tr = db.TransactionManager.StartTransaction())
{
var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
foreach (DBObject obj in bound)
{
var ent = (Entity)obj;
ent.ColorIndex = 1;
curSpace.AppendEntity(ent);
tr.AddNewlyCreatedDBObject(ent, true);
}
tr.Commit();
}
}
}
Dialog side:
private void button2_Click(object sender, EventArgs e)
{
var doc = AcAp.DocumentManager.MdiActiveDocument;
if (doc == null) return;
doc.Window.Focus();
doc.SendStringToExecute("DRAWBOUND\n", false, false, false); // <- switch to document context
}