using System;
using DrillTools;
namespace DrillTools.Examples
{
///
/// 槽孔计算器使用示例
///
public class SlotHoleCalculatorExamples
{
///
/// 示例1:计算线段槽孔孔数
///
public static void CalculateLineSlotHoleCountExample()
{
Console.WriteLine("=== 线段槽孔孔数计算示例 ===");
// 创建线段槽孔(参考readme.md中的测试数据)
var slot = new LineSlot(
new Point2D(-69.659, 16.450), // 起点
new Point2D(-94.159, 16.450), // 终点
1.601 // 孔径
);
// 计算孔数
int holeCount = SlotHoleCalculator.CalculateLineSlotHoleCount(slot);
Console.WriteLine($"槽孔长度: {slot.Length:F3} mm");
Console.WriteLine($"槽孔宽度: {slot.Width} mm");
Console.WriteLine($"计算孔数: {holeCount}");
Console.WriteLine($"预期孔数: 88 (CAM350标准)");
Console.WriteLine($"结果验证: {(holeCount == 88 ? "✓ 通过" : "✗ 失败")}");
Console.WriteLine();
}
///
/// 示例2:从G85命令解析并计算
///
public static void ParseFromG85Example()
{
Console.WriteLine("=== G85命令解析示例 ===");
// G85命令字符串(来自钻带文件)
string g85Command = "X-069659Y016450G85X-094159Y016450";
double width = 1.601;
try
{
// 解析G85命令
var slot = SlotHoleCalculator.ParseLineSlotFromG85(g85Command, width);
// 计算孔数
int holeCount = SlotHoleCalculator.CalculateLineSlotHoleCount(slot);
Console.WriteLine($"G85命令: {g85Command}");
Console.WriteLine($"解析起点: ({slot.StartPoint.X}, {slot.StartPoint.Y})");
Console.WriteLine($"解析终点: ({slot.EndPoint.X}, {slot.EndPoint.Y})");
Console.WriteLine($"槽孔宽度: {slot.Width} mm");
Console.WriteLine($"计算孔数: {holeCount}");
Console.WriteLine();
}
catch (Exception ex)
{
Console.WriteLine($"解析失败: {ex.Message}");
}
}
///
/// 示例3:计算钻孔位置
///
public static void CalculateHolePositionsExample()
{
Console.WriteLine("=== 钻孔位置计算示例 ===");
// 创建线段槽孔
var slot = new LineSlot(
new Point2D(0, 0),
new Point2D(10, 0),
1.0
);
// 计算钻孔位置
var positions = SlotHoleCalculator.CalculateLineSlotHolePositions(slot);
Console.WriteLine($"槽孔长度: {slot.Length} mm");
Console.WriteLine($"钻孔数量: {positions.Count}");
Console.WriteLine("钻孔位置列表:");
for (int i = 0; i < positions.Count; i++)
{
Console.WriteLine($" 孔 {i + 1}: ({positions[i].X:F3}, {positions[i].Y:F3})");
}
Console.WriteLine();
}
///
/// 示例4:弧段槽孔计算
///
public static void CalculateArcSlotExample()
{
Console.WriteLine("=== 弧段槽孔计算示例 ===");
// 创建弧段槽孔(半圆)
var arcSlot = new ArcSlot(
new Point2D(10, 0), // 起点
new Point2D(0, 10), // 终点
new Point2D(0, 0), // 圆心
1.0, // 宽度
false // 顺时针
);
// 计算孔数
int holeCount = SlotHoleCalculator.CalculateArcSlotHoleCount(arcSlot);
// 计算钻孔位置
var positions = SlotHoleCalculator.CalculateArcSlotHolePositions(arcSlot);
Console.WriteLine($"弧段半径: {arcSlot.Radius:F3} mm");
Console.WriteLine($"弧段长度: {SlotHoleCalculator.CalculateArcLength(arcSlot):F3} mm");
Console.WriteLine($"计算孔数: {holeCount}");
Console.WriteLine($"钻孔位置数量: {positions.Count}");
Console.WriteLine();
}
///
/// 示例5:批量计算不同孔径的槽孔
///
public static void BatchCalculationExample()
{
Console.WriteLine("=== 批量计算示例 ===");
// 不同孔径的槽孔(参考readme.md中的测试数据)
var diameters = new[] { 1.601, 1.701, 1.801, 1.901, 2.001, 1.501, 1.401, 1.301, 1.201, 1.101, 1.001, 0.706, 0.506 };
var expectedCounts = new[] { 88, 85, 83, 81, 79, 91, 94, 97, 101, 106, 111, 132, 156 };
Console.WriteLine("孔径(mm)\t计算孔数\t预期孔数\t验证结果");
Console.WriteLine("----------------------------------------");
for (int i = 0; i < diameters.Length; i++)
{
var slot = new LineSlot(
new Point2D(-69.659, 16.450),
new Point2D(-94.159, 16.450),
diameters[i]
);
int actualCount = SlotHoleCalculator.CalculateLineSlotHoleCount(slot);
int expectedCount = expectedCounts[i];
bool passed = actualCount == expectedCount;
Console.WriteLine($"{diameters[i]}\t{actualCount}\t{expectedCount}\t{(passed ? "✓" : "✗")}");
}
Console.WriteLine();
}
///
/// 示例6:自定义凸位高度值
///
public static void CustomToleranceExample()
{
Console.WriteLine("=== 自定义凸位高度值示例 ===");
var slot = new LineSlot(
new Point2D(0, 0),
new Point2D(10, 0),
1.0
);
// 使用默认凸位高度值
int defaultHoleCount = SlotHoleCalculator.CalculateLineSlotHoleCount(slot);
// 使用自定义凸位高度值
double customTolerance = 0.01; // 0.01mm
int customHoleCount = SlotHoleCalculator.CalculateLineSlotHoleCount(slot, customTolerance);
Console.WriteLine($"默认凸位高度值 (0.0127mm): {defaultHoleCount} 个孔");
Console.WriteLine($"自定义凸位高度值 (0.01mm): {customHoleCount} 个孔");
Console.WriteLine($"差异: {Math.Abs(defaultHoleCount - customHoleCount)} 个孔");
Console.WriteLine();
}
///
/// 示例7:处理实际钻带数据
///
public static void ProcessRealDrillTapeExample()
{
Console.WriteLine("=== 处理实际钻带数据示例 ===");
// 模拟钻带数据中的槽孔命令
var g85Commands = new[]
{
"X-069659Y016450G85X-094159Y016450", // T01 - 1.601mm
"X-181341Y195550G85X-156841Y195550", // T02 - 1.601mm
"X-181341Y389550G85X-156841Y389550", // T03 - 1.601mm
};
var toolDiameters = new[] { 1.601, 1.601, 1.601 };
var expectedHoleCounts = new[] { 88, 88, 88 };
Console.WriteLine("刀具\t孔径(mm)\tG85命令\t\t\t\t计算孔数\t预期孔数\t验证结果");
Console.WriteLine("--------------------------------------------------------------------------------");
for (int i = 0; i < g85Commands.Length; i++)
{
try
{
var slot = SlotHoleCalculator.ParseLineSlotFromG85(g85Commands[i], toolDiameters[i]);
int holeCount = SlotHoleCalculator.CalculateLineSlotHoleCount(slot);
bool passed = holeCount == expectedHoleCounts[i];
Console.WriteLine($"T{i + 1:D2}\t{toolDiameters[i]}\t{g85Commands[i]}\t{holeCount}\t\t{expectedHoleCounts[i]}\t\t{(passed ? "✓" : "✗")}");
}
catch (Exception ex)
{
Console.WriteLine($"T{i + 1:D2}\t{toolDiameters[i]}\t{g85Commands[i]}\t解析失败: {ex.Message}");
}
}
Console.WriteLine();
}
///
/// 运行所有示例
///
public static void RunAllExamples()
{
Console.WriteLine("槽孔计算器使用示例");
Console.WriteLine("==================");
Console.WriteLine();
CalculateLineSlotHoleCountExample();
ParseFromG85Example();
CalculateHolePositionsExample();
CalculateArcSlotExample();
BatchCalculationExample();
CustomToleranceExample();
ProcessRealDrillTapeExample();
Console.WriteLine("所有示例运行完成!");
}
}
}