From e7765cbddab576ee3f177c7fad3f79a886bf0b73 Mon Sep 17 00:00:00 2001 From: codex Date: Mon, 3 Aug 2026 17:04:56 +0300 Subject: [PATCH] fix: close live document before automation exit --- .../FirstLimitedApplyAutomation.cs | 12 +++++++++--- .../tests/PRecAnnot.Core.Tests/FoundationTests.cs | 8 +++++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Plant3D2026/P_Rec_Annot/src/PRecAnnot.Plant3D2026/FirstLimitedApplyAutomation.cs b/Plant3D2026/P_Rec_Annot/src/PRecAnnot.Plant3D2026/FirstLimitedApplyAutomation.cs index 2bbbbaf..222914d 100644 --- a/Plant3D2026/P_Rec_Annot/src/PRecAnnot.Plant3D2026/FirstLimitedApplyAutomation.cs +++ b/Plant3D2026/P_Rec_Annot/src/PRecAnnot.Plant3D2026/FirstLimitedApplyAutomation.cs @@ -316,9 +316,15 @@ internal sealed class FirstLimitedApplyAutomationCoordinator // Do not close a cached automation document and then reuse it. Resolve // the currently-live document only at the final quiescent callback. var active = AcadApplication.DocumentManager.MdiActiveDocument; - if (active is null) { AcadApplication.Quit(); return; } - transitions.Add("NativeQuitDispatched"); - active.SendStringToExecute("_.QUIT _N ", true, false, false); + if (active is not null) + { + transitions.Add("ActiveDocumentCloseRequested"); + active.CloseAndDiscard(); + } + // Application.Quit alone leaves the script host alive. Close the + // live document first, then request normal application shutdown. + transitions.Add("ApplicationQuitRequested"); + AcadApplication.Quit(); } catch (System.Exception exception) { diff --git a/Plant3D2026/P_Rec_Annot/tests/PRecAnnot.Core.Tests/FoundationTests.cs b/Plant3D2026/P_Rec_Annot/tests/PRecAnnot.Core.Tests/FoundationTests.cs index 3b1f713..edb5bd2 100644 --- a/Plant3D2026/P_Rec_Annot/tests/PRecAnnot.Core.Tests/FoundationTests.cs +++ b/Plant3D2026/P_Rec_Annot/tests/PRecAnnot.Core.Tests/FoundationTests.cs @@ -82,12 +82,13 @@ public sealed class FoundationTests Assert.Contains("PostStartupReady", source); } - [Fact] public void AutomationControlledExit_UsesNativeQuitFromIdle() + [Fact] public void AutomationControlledExit_ClosesLiveDocumentThenQuitsFromIdle() { var source = File.ReadAllText(Path.Combine(ProjectRoot, "src", "PRecAnnot.Plant3D2026", "FirstLimitedApplyAutomation.cs")); Assert.Contains("RequestControlledExit", source); Assert.Contains("AcadApplication.Idle += OnExitIdle", source); - Assert.Contains("active.SendStringToExecute(\"_.QUIT _N \", true, false, false)", source); + Assert.Contains("active.CloseAndDiscard();", source); + Assert.Contains("AcadApplication.Quit();", source); } [Fact] public void Callback_AfterDocumentDestroyed_DoesNotAccessDocument() @@ -100,7 +101,8 @@ public sealed class FoundationTests [Fact] public void Idle_WhenActiveDocumentNull_DoesNotThrow() { var source = File.ReadAllText(Path.Combine(ProjectRoot, "src", "PRecAnnot.Plant3D2026", "FirstLimitedApplyAutomation.cs")); - Assert.Contains("if (active is null) { AcadApplication.Quit(); return; }", source); + Assert.Contains("if (active is not null)", source); + Assert.Contains("AcadApplication.Quit();", source); } [Fact] public void TerminalState_IgnoresFurtherCallbacks()