feat: port p-rec-annot ortho status and attribute maintenance

This commit is contained in:
codex
2026-08-07 13:21:32 +03:00
parent d4016237d8
commit b381f83bbd
6 changed files with 167 additions and 2 deletions
@@ -1,5 +1,9 @@
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.ProcessPower.DataObjects;
using Autodesk.ProcessPower.Drawings2d;
using Autodesk.ProcessPower.PlantInstance;
using Autodesk.ProcessPower.PnP3dOrthoDrawingsUI;
using PRecAnnot.Abstractions;
using PRecAnnot.Core;
using AcadApplication = Autodesk.AutoCAD.ApplicationServices.Core.Application;
@@ -39,13 +43,14 @@ internal static class UpdateOrthoViewBlocksCommand
ValidateFixture(document);
using var documentLock = document.LockDocument(); using var write = document.Database.TransactionManager.StartTransaction();
var layerTable = (LayerTable)write.GetObject(document.Database.LayerTableId, OpenMode.ForRead); var linetypes = (LinetypeTable)write.GetObject(document.Database.LinetypeTableId, OpenMode.ForRead);
var representationCount = ApplyRepresentationRules(document, selected.ObjectId, viewName, rules, write, linetypes);
foreach (var change in changes)
{
var layer = (LayerTableRecord)write.GetObject(layerTable[change.Layer], OpenMode.ForWrite); Apply(layer, change.Rule, linetypes);
}
var viewportToUpdate = (Viewport)write.GetObject(selected.ObjectId, OpenMode.ForWrite); if (layerTable.Has("Defpoints")) viewportToUpdate.LayerId = layerTable["Defpoints"];
write.Commit(); document.Editor.WriteMessage($"\nPXP-UOB Apply: updated-layers={changes.Count}; AutoCAD UNDO is available.");
RuntimeDiagnostics.Write("UpdateOrthoViewBlocksApplyCompleted", commandName: "PXP-UOB", resultStatus: "Applied", document: document, details: new Dictionary<string, object?> { ["LayerCount"] = changes.Count });
write.Commit(); document.Editor.WriteMessage($"\nPXP-UOB Apply: updated-layers={changes.Count}; updated-representations={representationCount}; AutoCAD UNDO is available.");
RuntimeDiagnostics.Write("UpdateOrthoViewBlocksApplyCompleted", commandName: "PXP-UOB", resultStatus: "Applied", document: document, details: new Dictionary<string, object?> { ["LayerCount"] = changes.Count, ["RepresentationCount"] = representationCount });
}
catch (Exception exception) { RuntimeDiagnostics.Write("UpdateOrthoViewBlocksFailed", "ERROR", commandName: "PXP-UOB", resultStatus: exception.Message, exception: exception, document: document); document.Editor.WriteMessage("\nPXP-UOB: " + exception.Message); }
}
@@ -56,6 +61,47 @@ internal static class UpdateOrthoViewBlocksCommand
if (rule.Red.HasValue) layer.Color = AcadColor.FromRgb(rule.Red.Value, rule.Green!.Value, rule.Blue!.Value);
else if (rule.ColorIndex.HasValue) layer.Color = AcadColor.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByColor, rule.ColorIndex.Value);
}
private static int ApplyRepresentationRules(Autodesk.AutoCAD.ApplicationServices.Document document, ObjectId viewportId, string viewName, IReadOnlyList<OrthoLayerRule> rules, Transaction transaction, LinetypeTable linetypes)
{
var manager = PlantApplication.ProjectPart("Piping")?.DataLinksManager ?? throw new InvalidOperationException("ORTHO_PIPING_DATALINKS_UNAVAILABLE");
var database = manager.GetPnPDatabase();
IDwgSheetDef sheet;
IDwgViewDef view;
List<ObjectId> representations;
if (!UIUtils.GetDwgSheetFromOrthoDrawingName(document.Database.Filename, out sheet) ||
!UIUtils.GetViewDefByViewName(viewName, sheet, out view) ||
!UIUtils.GetOrthoEntityListInView(UIUtils.GetDwgSheetDefResolvedPath(sheet), viewName, out representations))
throw new InvalidOperationException("ORTHO_VIEW_REPRESENTATIONS_UNAVAILABLE");
var changed = 0;
foreach (var id in representations.Where(value => !value.IsNull).OrderBy(value => value.Handle.Value))
{
if (transaction.GetObject(id, OpenMode.ForWrite, false) is not BlockReference block) continue;
var modelId = PnPDwg2dUtil.GetModelObjectId(id, document.Database);
var rowId = PnPDwg2dUtil.PipingRowId(modelId);
var engineering = database.Tables["EngineeringItems"].Select("PnPID=" + rowId);
var equipment = database.Tables["Equipment"].Select("PnPID=" + rowId);
if (engineering.Length == 1 && equipment.Length == 0)
{
var status = Convert.ToString(engineering[0]["Status"]) ?? string.Empty;
var statusRule = rules.FirstOrDefault(rule => rule.MatchesStatus(status));
if (statusRule is not null) Apply(block, statusRule, linetypes);
}
else if (engineering.Length == 1 && equipment.Length == 1) block.LineWeight = LineWeight.ByLineWeightDefault;
if (transaction.GetObject(block.BlockTableRecord, OpenMode.ForRead) is BlockTableRecord definition)
foreach (ObjectId nestedId in definition)
if (transaction.GetObject(nestedId, OpenMode.ForWrite, false) is Entity nested && rules.FirstOrDefault(rule => rule.MatchesLayer(viewName, nested.Layer)) is { } nestedRule)
Apply(nested, nestedRule, linetypes);
changed++;
}
return changed;
}
private static void Apply(Entity entity, OrthoLayerRule rule, LinetypeTable linetypes)
{
if (rule.LineWeight.HasValue) entity.LineWeight = (LineWeight)rule.LineWeight.Value;
if (!string.IsNullOrWhiteSpace(rule.LineType) && linetypes.Has(rule.LineType)) entity.LinetypeId = linetypes[rule.LineType];
if (rule.Red.HasValue) entity.Color = AcadColor.FromRgb(rule.Red.Value, rule.Green!.Value, rule.Blue!.Value);
else if (rule.ColorIndex.HasValue) entity.Color = AcadColor.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByColor, rule.ColorIndex.Value);
}
private static void ValidateFixture(Autodesk.AutoCAD.ApplicationServices.Document document)
{
var marker = Environment.GetEnvironmentVariable("P_REC_ANNOT_ORTHO_MAINTENANCE_FIXTURE_MARKER");