diff --git a/Plant3D2026/P_Rec_Annot/src/PRecAnnot.Plant3D2026/FirstLimitedApplyAutomation.cs b/Plant3D2026/P_Rec_Annot/src/PRecAnnot.Plant3D2026/FirstLimitedApplyAutomation.cs index 970fd09..5b2fdd6 100644 --- a/Plant3D2026/P_Rec_Annot/src/PRecAnnot.Plant3D2026/FirstLimitedApplyAutomation.cs +++ b/Plant3D2026/P_Rec_Annot/src/PRecAnnot.Plant3D2026/FirstLimitedApplyAutomation.cs @@ -195,6 +195,16 @@ internal sealed class FirstLimitedApplyAutomationCoordinator document.CommandCancelled += OnCommandCancelled; documentHandlersSubscribed = true; Transition(CoordinatorState.Running, "AutomationRunning"); + // DocumentActivated can be raised synchronously by Open before the + // coordinator receives the returned Document. Confirm the current + // active document after assignment so that one missed event cannot + // strand the workflow waiting for another startup Idle. + if (ReferenceEquals(AcadApplication.DocumentManager.MdiActiveDocument, document)) + { + fixtureDocumentActivated = true; + transitions.Add("FixtureDocumentActiveAfterOpen"); + TryActivateProjectAndVerifyContext(); + } } catch (System.Exception ex) { FailFromCallback("Start", ex, "AUTOMATION_START_FAILED"); } } 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 ead611b..87a693c 100644 --- a/Plant3D2026/P_Rec_Annot/tests/PRecAnnot.Core.Tests/FoundationTests.cs +++ b/Plant3D2026/P_Rec_Annot/tests/PRecAnnot.Core.Tests/FoundationTests.cs @@ -98,6 +98,15 @@ public sealed class FoundationTests Assert.Contains("FixtureDocumentActivated", source); } + [Fact] public void AutomationDocumentActivation_RecoversSynchronousOpenEvent() + { + var source = File.ReadAllText(Path.Combine(ProjectRoot, "src", "PRecAnnot.Plant3D2026", "FirstLimitedApplyAutomation.cs")); + var start = source[source.IndexOf("public void Start()", StringComparison.Ordinal)..source.IndexOf("private void CompleteLifecycleProbe", StringComparison.Ordinal)]; + Assert.Contains("FixtureDocumentActiveAfterOpen", start); + Assert.Contains("ReferenceEquals(AcadApplication.DocumentManager.MdiActiveDocument, document)", start); + Assert.Contains("TryActivateProjectAndVerifyContext();", start); + } + [Fact] public void AutomationControlledExit_QueuesNativeQuitAfterPublicCommand() { var source = File.ReadAllText(Path.Combine(ProjectRoot, "src", "PRecAnnot.Plant3D2026", "FirstLimitedApplyAutomation.cs"));