using StructureHelperCommon.Infrastructures.Exceptions; using StructureHelperCommon.Models; using StructureHelperCommon.Models.Shapes; using StructureHelperCommon.Models.Tables; namespace StructureHelperLogics.NdmCalculations.Primitives { /// /// Logic for creating of trace entries of primitives /// public class TracePrimitiveFactory : ITraceEntityFactory { const int rowSize = 2; private List traceLoggerEntries; public int Priority { get; set; } = LoggerService.GetPriorityByStatus(TraceLogStatuses.Info); public IEnumerable? Collection { get; set; } public List GetTraceEntries() { Check(); traceLoggerEntries = new(); traceLoggerEntries.Add(new StringLogEntry() { Message = $"There are (is) {Collection.Count()} primitive(s)", Priority = Priority } ); ProcessCollection(); return traceLoggerEntries; } private void Check() { if (Collection is null) { throw new StructureHelperException(ErrorStrings.ParameterIsNull + ": Collection of primitives"); } } private void ProcessCollection() { foreach (var item in Collection) { var table = new TableLogEntry(rowSize) { Priority = Priority }; table.ColumnWidth[1] = 200; table.Table.AddRow(GetHeader(item)); table.Table.AddRows(GetCommonRows(item)); if (item is IRectangleNdmPrimitive rectangle) { table.Table.AddRows(ProcessRectangle(rectangle.Shape as IRectangleShape)); } if (item is IEllipseNdmPrimitive ellipse) { IRectangleShape? rectShape = ellipse.Shape as IRectangleShape; if (rectShape.Width == rectShape.Height) { table.Table.AddRows(ProcessCircle(rectShape)); } else { table.Table.AddRows(ProcessRectangle(rectShape)); } } if (item is IPointNdmPrimitive point) { table.Table.AddRows(ProcessPoint(point)); } traceLoggerEntries.Add(table); } } private IEnumerable> ProcessCircle(IRectangleShape rectShape) { List> rows = new(); ShTableRow ndmRow; ndmRow = new ShTableRow(rowSize); ndmRow.Elements[0].Value = new StringLogEntry() { Message = "Diameter", Priority = Priority }; ndmRow.Elements[1].Value = new StringLogEntry() { Message = rectShape.Width.ToString() + "(m)", Priority = Priority }; rows.Add(ndmRow); return rows; } private List> ProcessPoint(IPointNdmPrimitive point) { List> rows = new(); ShTableRow ndmRow; ndmRow = new ShTableRow(rowSize); ndmRow.Elements[0].Value = new StringLogEntry() { Message = "Area", Priority = Priority }; ndmRow.Elements[1].Value = new StringLogEntry() { Message = point.Area.ToString() + "(m^2)", Priority = Priority }; rows.Add(ndmRow); return rows; } private List> GetCommonRows(INdmPrimitive ndmPrimitive) { List> rows = new(); ShTableRow ndmRow; ndmRow = new(rowSize); ndmRow.Elements[0].Value = new StringLogEntry() { Message = "Material", Priority = Priority }; ndmRow.Elements[1].Value = new StringLogEntry() { Message = ndmPrimitive.NdmElement.HeadMaterial.Name, Priority = Priority }; rows.Add(ndmRow); ndmRow = new(rowSize); ndmRow.Elements[0].Value = new StringLogEntry() { Message = "Center", Priority = Priority }; ndmRow.Elements[1].Value = new StringLogEntry() { Message = "X = " + ndmPrimitive.Center.X.ToString() + "(m)" + ", Y = " + ndmPrimitive.Center.Y.ToString() + "(m)", Priority = Priority }; rows.Add(ndmRow); return rows; } private List> ProcessRectangle(IRectangleShape rectShape) { List> rows = new(); ShTableRow ndmRow; ndmRow = new ShTableRow(rowSize); ndmRow.Elements[0].Value = new StringLogEntry() { Message = "Height", Priority = Priority }; ndmRow.Elements[1].Value = new StringLogEntry() { Message = rectShape.Height.ToString() + "(m)", Priority = Priority }; rows.Add(ndmRow); ndmRow = new ShTableRow(rowSize); ndmRow.Elements[0].Value = new StringLogEntry() { Message = "Width", Priority = Priority }; ndmRow.Elements[1].Value = new StringLogEntry() { Message = rectShape.Width.ToString() + "(m)", Priority = Priority }; rows.Add(ndmRow); return rows; } private IShTableRow GetHeader(INdmPrimitive ndmPrimitive) { const CellRole cellRole = CellRole.Header; string[] ColumnList = new string[] { "Primitive name", ndmPrimitive.Name }; var ndmRow = new ShTableRow(rowSize); foreach (var item in ndmRow.Elements) { item.Role = cellRole; } for (int i = 0; i < rowSize; i++) { ndmRow.Elements[i].Value = new StringLogEntry() { Message = ColumnList[i], Priority = Priority }; } return ndmRow; } } }