using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Geometry; using Autodesk.ProcessPower.DataLinks; using PRecAnnot.Abstractions; namespace PRecAnnot.Plant3D2026; // Converts an explicit AutoCAD selection to the small, AutoCAD-free candidate // used by the line-number preview. It deliberately performs no DataLinks or // DWG write and reports unavailable Plant mappings as typed failures. internal sealed class PlantLineNumberCandidateReader(DataLinksManager dataLinks) { public LineNumberCandidateReadResult Read(ObjectId selectedId, string layer, string textStyle) { if (selectedId.IsNull || !selectedId.IsValid) return new(false, null, "LINE_NUMBER_SELECTION_INVALID"); if (string.IsNullOrWhiteSpace(layer) || string.IsNullOrWhiteSpace(textStyle)) return new(false, null, "LINE_NUMBER_ASSET_CONTRACT_INVALID"); try { var rowId = dataLinks.FindAcPpRowId(selectedId); if (rowId <= 0) return new(false, null, "LINE_NUMBER_DATALINKS_ROW_NOT_FOUND"); var database = selectedId.Database; using var transaction = database.TransactionManager.StartOpenCloseTransaction(); if (transaction.GetObject(selectedId, OpenMode.ForRead) is not Entity entity) return new(false, null, "LINE_NUMBER_SELECTION_NOT_ENTITY"); var extents = entity.GeometricExtents; var center = new Point3d( (extents.MinPoint.X + extents.MaxPoint.X) / 2d, (extents.MinPoint.Y + extents.MaxPoint.Y) / 2d, (extents.MinPoint.Z + extents.MaxPoint.Z) / 2d); transaction.Commit(); return new(true, new($"acad:{selectedId.Handle}", rowId, new(center.X, center.Y, center.Z), 0, layer, textStyle, new Dictionary())); } catch (Autodesk.AutoCAD.Runtime.Exception) { return new(false, null, "LINE_NUMBER_SELECTION_GEOMETRY_UNAVAILABLE"); } catch (DLException) { return new(false, null, "LINE_NUMBER_DATALINKS_ROW_NOT_FOUND"); } catch (Exception) { return new(false, null, "LINE_NUMBER_CANDIDATE_READ_FAILED"); } } }