Files
AohDrllTools/DrillUsageReportExporter.cs
2026-05-16 12:07:32 +08:00

102 lines
3.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
namespace DrillTools
{
/// <summary>
/// 生成 CAM350 风格的钻孔使用报告。
/// </summary>
internal static class DrillUsageReportExporter
{
public static string GenerateReport(IReadOnlyList<ToolItem> tools, string layerName, DateTime exportTime)
{
if (tools == null)
throw new ArgumentNullException(nameof(tools));
var rows = tools.Select((tool, index) => CreateRow(tool, index + 1)).ToList();
var report = new StringBuilder();
AppendHeader(report, layerName, exportTime);
AppendRows(report, rows);
AppendTotals(report, rows);
return report.ToString();
}
private static ReportRow CreateRow(ToolItem tool, int order)
{
// 当前导出顺序与 Tool Ref 相同;未来如有独立钻孔执行顺序可拆分维护。
return new ReportRow(
order,
tool.ToolNumber,
FormatDiameter(tool.Diameter),
order,
tool.RegularHoles,
tool.SlotHoles,
tool.TotalHoles);
}
private static void AppendHeader(StringBuilder report, string layerName, DateTime exportTime)
{
report.AppendLine("Project file name: ");
report.AppendLine($"Date: {FormatDate(exportTime)}");
report.AppendLine($"Table: DrillTable_1 Layer: {layerName}");
report.AppendLine("Drill Usage:");
report.AppendLine("Table # Tool Ref Tool # Size Exp Ord Plated Hits Unplated Hits Total Hits");
report.AppendLine("======= ======== ====== ==== ======= =========== ============= ==========");
}
private static void AppendRows(StringBuilder report, IReadOnlyList<ReportRow> rows)
{
foreach (var row in rows)
{
report.AppendLine(string.Format(
CultureInfo.InvariantCulture,
"{0,8}{1,10}{2,10}{3,17}{4,16}{5,14}{6,15}{7,12}",
1,
row.ToolRef,
row.ToolNumber,
row.Size,
row.ExportOrder,
row.PlatedHits,
row.UnplatedHits,
row.TotalHits));
}
}
private static void AppendTotals(StringBuilder report, IReadOnlyList<ReportRow> rows)
{
report.AppendLine("=========================================================== =========== ============= ==========");
report.AppendLine(string.Format(
CultureInfo.InvariantCulture,
"{0,69}{1,14}{2,15}{3,12}",
"Totals:",
rows.Sum(row => row.PlatedHits),
rows.Sum(row => row.UnplatedHits),
rows.Sum(row => row.TotalHits)));
}
private static string FormatDate(DateTime value)
{
return $"{value:HH:mm:ss} {value.Year}年{value.Month}月{value.Day}日";
}
private static string FormatDiameter(double value)
{
return value.ToString("0.###", CultureInfo.InvariantCulture);
}
private sealed record ReportRow(
int ToolRef,
int ToolNumber,
string Size,
int ExportOrder,
int PlatedHits,
int UnplatedHits,
int TotalHits);
}
}