fix: execute automation commands in AutoCAD command context

This commit is contained in:
codex
2026-08-04 09:37:24 +03:00
parent 1fa25809de
commit bc481d4378
2 changed files with 25 additions and 17 deletions
@@ -264,23 +264,30 @@ internal sealed class FirstLimitedApplyAutomationCoordinator
catch (System.Exception ex) { FailFromCallback("ContextVerification", ex, "AUTOMATION_CONTEXT_FAILED"); }
}
private void Queue(string command)
{
if (IsTerminal || document is null) return;
transitions.Add("CommandQueued:" + command);
document.SendStringToExecute(command + "\n", true, true, false);
}
private void Queue(string command) => _ = DispatchCommandAsync(command, false);
private void QueueReadinessBarrier()
{
_ = DispatchCommandAsync("PHP_REC_AUTOMATION_READY", true);
}
private async Task DispatchCommandAsync(string command, bool readiness)
{
if (IsTerminal || document is null) return;
var active = AcadApplication.DocumentManager.MdiActiveDocument;
if (active is null || !string.Equals(active.Name, document.Name, StringComparison.OrdinalIgnoreCase))
var expected = document;
transitions.Add(readiness ? "ReadinessCommandDispatchRequested" : "CommandDispatchRequested:" + command);
try
{
Complete(false, "AUTOMATION_READINESS_DOCUMENT_NOT_ACTIVE");
return;
await AcadApplication.DocumentManager.ExecuteInCommandContextAsync(async _ =>
{
transitions.Add("CommandContextEntered:" + command);
var active = AcadApplication.DocumentManager.MdiActiveDocument;
if (active is null || !string.Equals(active.Name, expected.Name, StringComparison.OrdinalIgnoreCase))
throw new InvalidOperationException("AUTOMATION_COMMAND_DOCUMENT_NOT_ACTIVE");
transitions.Add(readiness ? "ReadinessBarrierQueued" : "CommandQueued:" + command);
await active.Editor.CommandAsync(command);
transitions.Add("CommandInvocationReturned:" + command);
}, null);
}
transitions.Add("ReadinessBarrierQueued");
document.SendStringToExecute("PHP_REC_AUTOMATION_READY\n", true, false, false);
catch (System.Exception exception) { FailFromCallback("CommandDispatcher", exception, "AUTOMATION_COMMAND_CONTEXT_FAILED"); }
}
private void OnCommandEnded(object sender, CommandEventArgs e)
{