Files
AohDrllTools/Docs/README_SlotHoleCalculator.md
2025-12-07 20:25:27 +08:00

247 lines
6.6 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 槽孔个数计算类 (SlotHoleCalculator)
## 概述
`SlotHoleCalculator` 是一个专门用于计算PCB槽孔钻孔数量的工具类计算结果与CAM350软件保持一致。该类支持线段槽孔和弧段槽孔的钻孔数量计算并提供从钻带G85命令解析槽孔参数的功能。
## 主要特性
-**与CAM350一致**使用标准凸位高度值0.0127mm确保计算结果与CAM350软件完全一致
-**支持多种槽孔类型**:支持线段槽孔和弧段槽孔
-**G85命令解析**直接从钻带文件中的G85命令解析槽孔参数
-**钻孔位置计算**:可选功能,计算每个钻孔的具体坐标位置
-**完整的单元测试**:基于实际测试数据验证计算准确性
-**易于集成**设计为静态工具类提供清晰的API接口
## 核心算法
槽孔钻孔数量计算基于以下原理:
1. **凸位高度值**CAM350标准为0.0127mm
2. **孔中心距计算**`holeCenterDistance = √(r² - (r-t)²) × 2`
- r孔半径
- t凸位高度值
3. **孔数计算**`holeCount = Floor(-slotLength / holeCenterDistance) + 1`
## 快速开始
### 1. 基本使用
```csharp
using DrillTools;
// 创建线段槽孔
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($"槽孔需要 {holeCount} 个钻孔"); // 输出: 槽孔需要 88 个钻孔
```
### 2. 从G85命令解析
```csharp
// G85命令字符串来自钻带文件
string g85Command = "X-069659Y016450G85X-094159Y016450";
double width = 1.601;
// 解析G85命令
var slot = SlotHoleCalculator.ParseLineSlotFromG85(g85Command, width);
// 计算孔数
int holeCount = SlotHoleCalculator.CalculateLineSlotHoleCount(slot);
Console.WriteLine($"G85槽孔需要 {holeCount} 个钻孔"); // 输出: G85槽孔需要 88 个钻孔
```
### 3. 计算钻孔位置
```csharp
// 计算钻孔位置
var positions = SlotHoleCalculator.CalculateLineSlotHolePositions(slot);
Console.WriteLine($"钻孔位置列表:");
for (int i = 0; i < positions.Count; i++)
{
Console.WriteLine($" 孔 {i + 1}: ({positions[i].X:F3}, {positions[i].Y:F3})");
}
```
## API 参考
### 数据结构
#### Point2D
二维点结构,表示坐标位置。
```csharp
public struct Point2D
{
public double X { get; set; }
public double Y { get; set; }
public Point2D(double x, double y);
public static Point2D ParseFromDrillString(string drillString);
}
```
#### LineSlot
线段槽孔结构,包含起点、终点和宽度。
```csharp
public struct LineSlot
{
public Point2D StartPoint { get; set; }
public Point2D EndPoint { get; set; }
public double Width { get; set; }
public double Length { get; }
public LineSlot(Point2D startPoint, Point2D endPoint, double width);
}
```
#### ArcSlot
弧段槽孔结构,包含起点、终点、圆心、宽度和方向。
```csharp
public struct ArcSlot
{
public Point2D StartPoint { get; set; }
public Point2D EndPoint { get; set; }
public Point2D CenterPoint { get; set; }
public double Width { get; set; }
public bool CounterClockwise { get; set; }
public double Radius { get; }
public ArcSlot(Point2D startPoint, Point2D endPoint, Point2D centerPoint, double width, bool counterClockwise = false);
}
```
### 主要方法
#### 线段槽孔计算
```csharp
// 计算线段槽孔的钻孔数量
public static int CalculateLineSlotHoleCount(LineSlot slot, double tolerance = 0.0127)
// 计算线段槽孔的钻孔位置
public static List<Point2D> CalculateLineSlotHolePositions(LineSlot slot, double tolerance = 0.0127)
```
#### 弧段槽孔计算
```csharp
// 计算弧段槽孔的钻孔数量
public static int CalculateArcSlotHoleCount(ArcSlot slot, double tolerance = 0.0127)
// 计算弧段槽孔的钻孔位置
public static List<Point2D> CalculateArcSlotHolePositions(ArcSlot slot, double tolerance = 0.0127)
```
#### G85命令解析
```csharp
// 从钻带G85命令解析线段槽孔
public static LineSlot ParseLineSlotFromG85(string g85Command, double width)
```
## 集成示例
### 钻带处理集成
```csharp
using DrillTools.Integration;
// 处理钻带数据
string drillTapeContent = File.ReadAllText("drill_file.drl");
var result = DrillTapeProcessor.ProcessDrillTape(drillTapeContent);
// 生成报告
string report = DrillTapeProcessor.GenerateReport(result);
Console.WriteLine(report);
```
### 批量计算示例
```csharp
// 不同孔径的槽孔
var diameters = new[] { 1.601, 1.701, 1.801, 1.901, 2.001 };
var expectedCounts = new[] { 88, 85, 83, 81, 79 };
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);
Console.WriteLine($"孔径 {diameters[i]}mm: {actualCount} 个孔 (预期: {expectedCounts[i]})");
}
```
## 测试验证
### 单元测试
项目包含完整的单元测试,验证计算结果的准确性:
```bash
dotnet test
```
### 测试数据
基于参考资料中的实际测试数据,验证以下场景:
- ✅ 线段槽孔孔数计算13种不同孔径
- ✅ G85命令解析
- ✅ 钻孔位置计算
- ✅ 弧段槽孔计算
- ✅ 边界条件测试
- ✅ 自定义凸位高度值
## 注意事项
1. **坐标单位**所有坐标单位为毫米mm钻带坐标需要除以1000转换
2. **凸位高度值**默认使用CAM350标准的0.0127mm,可通过参数自定义
3. **计算精度**计算结果与CAM350软件保持一致精度误差小于1%
4. **异常处理**:包含完善的异常处理机制,无效输入会抛出相应的异常
## 文件结构
```
DrillTools/
├── SlotHoleCalculator.cs # 主要的槽孔计算类
├── SlotHoleCalculatorTests.cs # 单元测试
├── SlotHoleCalculatorExamples.cs # 使用示例
├── DrillTapeProcessor.cs # 集成示例
└── README_SlotHoleCalculator.md # 本文档
```
## 版本历史
- **v1.0.0** (2025-11-12)
- 初始版本
- 支持线段槽孔和弧段槽孔计算
- 支持G85命令解析
- 完整的单元测试和示例
## 许可证
本项目采用MIT许可证。
## 贡献
欢迎提交Issue和Pull Request来改进这个工具。
## 参考资料
1. PCB SLOT槽孔数量计算方法,同CAM350孔数一致 实现方法
2. PCB genesis Slot槽转钻孔(不用G85命令)实现方法
3. CAM350 NC Tool Table Report数据