From 3128568965f25df8090bc8d48f5ecffa5f8e7687 Mon Sep 17 00:00:00 2001 From: codex Date: Fri, 7 Aug 2026 08:58:38 +0300 Subject: [PATCH] feat: add controlled p-rec-annot line-number apply --- .../src/PRecAnnot.Plant3D2026/EntryPoint.cs | 13 +++++++ .../LineNumberApplyPermit.cs | 37 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 Plant3D2026/P_Rec_Annot/src/PRecAnnot.Plant3D2026/LineNumberApplyPermit.cs diff --git a/Plant3D2026/P_Rec_Annot/src/PRecAnnot.Plant3D2026/EntryPoint.cs b/Plant3D2026/P_Rec_Annot/src/PRecAnnot.Plant3D2026/EntryPoint.cs index f8a4a00..e91b0e5 100644 --- a/Plant3D2026/P_Rec_Annot/src/PRecAnnot.Plant3D2026/EntryPoint.cs +++ b/Plant3D2026/P_Rec_Annot/src/PRecAnnot.Plant3D2026/EntryPoint.cs @@ -98,6 +98,19 @@ public sealed class EntryPoint : IExtensionApplication var operations = string.Join(", ", preview.Plan!.Operations.Select(value => $"{value.Kind}:{value.Text}")); editor.WriteMessage($"\nPXP-LN preview: {operations}. DWG не изменён."); RuntimeDiagnostics.Write("LineNumberPreviewCompleted", commandName: "PXP-LN", resultStatus: "PreviewCompleted", document: document, details: new Dictionary { ["OperationCount"] = preview.Apply.Count }); + if (ParseMode(RuntimeDiagnostics.RuntimeMode) != PRecAnnotExecutionMode.Apply) return; + + var operation = preview.Apply.SingleOrDefault() ?? throw new InvalidOperationException("LINE_NUMBER_APPLY_REQUIRES_EXACTLY_ONE_OPERATION"); + var placement = editor.GetPoint("\nУкажите положение маркера номера линии: "); + if (placement.Status != PromptStatus.OK) return; + operation = operation with { Position = new AnnotationPoint(placement.Value.X, placement.Value.Y, placement.Value.Z) }; + var permit = LineNumberApplyPermitReader.Read(); + LineNumberApplyPermitReader.Validate(document, permit, operation); + // AutoCAD wraps a command's committed transaction in its native + // UNDO record. No compensating-delete path is used here. + var result = new LineNumberAnnotationWriter().Apply(document, [operation], configuration.Assets); + RuntimeDiagnostics.Write("LineNumberApplyCompleted", commandName: "PXP-LN", resultStatus: result.Created > 0 ? "Created" : result.Updated > 0 ? "Updated" : "AlreadyCurrent", document: document, details: new Dictionary { ["Created"] = result.Created, ["Updated"] = result.Updated, ["AlreadyCurrent"] = result.Unchanged }); + editor.WriteMessage($"\nPXP-LN Apply: created={result.Created}; updated={result.Updated}; already-current={result.Unchanged}."); } catch (System.Exception exception) { diff --git a/Plant3D2026/P_Rec_Annot/src/PRecAnnot.Plant3D2026/LineNumberApplyPermit.cs b/Plant3D2026/P_Rec_Annot/src/PRecAnnot.Plant3D2026/LineNumberApplyPermit.cs new file mode 100644 index 0000000..58e7083 --- /dev/null +++ b/Plant3D2026/P_Rec_Annot/src/PRecAnnot.Plant3D2026/LineNumberApplyPermit.cs @@ -0,0 +1,37 @@ +using System.Text.Json; +using Autodesk.AutoCAD.ApplicationServices; +using PRecAnnot.Abstractions; + +namespace PRecAnnot.Plant3D2026; + +internal sealed record LineNumberApplyPermit +{ + public required bool AllowApply { get; init; } + public required string FixtureMarkerPath { get; init; } + public required string ExpectedStableId { get; init; } + public required string ExpectedText { get; init; } +} + +internal static class LineNumberApplyPermitReader +{ + private const string MarkerContent = "P_REC_ANNOT_TEST_FIXTURE=LINE_NUMBER_APPLY"; + + public static void Validate(Document document, LineNumberApplyPermit permit, LineNumberApplyPreflight operation) + { + if (!permit.AllowApply || !string.Equals(permit.ExpectedStableId, operation.StableId, StringComparison.Ordinal) || !string.Equals(permit.ExpectedText, operation.Text, StringComparison.Ordinal)) + throw new InvalidOperationException("LINE_NUMBER_APPLY_PERMIT_PLAN_MISMATCH"); + if (!Path.IsPathFullyQualified(permit.FixtureMarkerPath) || permit.FixtureMarkerPath.StartsWith("\\\\", StringComparison.Ordinal) || !File.Exists(permit.FixtureMarkerPath) || !string.Equals(File.ReadAllText(permit.FixtureMarkerPath).Trim(), MarkerContent, StringComparison.Ordinal)) + throw new InvalidOperationException("LINE_NUMBER_APPLY_TEST_FIXTURE_REQUIRED"); + var fixtureRoot = Path.GetDirectoryName(Path.GetFullPath(permit.FixtureMarkerPath))! + Path.DirectorySeparatorChar; + if (!Path.GetFullPath(document.Name).StartsWith(fixtureRoot, StringComparison.OrdinalIgnoreCase)) throw new InvalidOperationException("LINE_NUMBER_APPLY_TEST_FIXTURE_REQUIRED"); + } + + public static LineNumberApplyPermit Read() + { + var path = Environment.GetEnvironmentVariable("P_REC_ANNOT_LINE_NUMBER_APPLY_PERMIT_PATH"); + if (string.IsNullOrWhiteSpace(path) || !Path.IsPathFullyQualified(path) || path.StartsWith("\\\\", StringComparison.Ordinal) || !File.Exists(path)) + throw new InvalidOperationException("LINE_NUMBER_APPLY_PERMIT_REQUIRED"); + return JsonSerializer.Deserialize(File.ReadAllText(path), new JsonSerializerOptions { PropertyNameCaseInsensitive = true }) + ?? throw new InvalidOperationException("LINE_NUMBER_APPLY_PERMIT_INVALID"); + } +}