using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace DrillTools
{
///
/// 刀具类型枚举
///
public enum ToolType
{
Regular, // 圆孔刀具
Slot, // 槽孔刀具
MachineCode // 机台码刀具
}
///
/// 刀具尾号类型枚举
///
public enum ToolSuffixType
{
Drill, // 0 - 钻针
Slot, // 1 - 槽刀
EASlot, // 2 - EA型槽刀
DustSlot, // 3 - 粉尘刀(槽刀)
DeburrSlot, // 4 - 去毛刺刀(槽刀)
NonStandard, // 5 - 非标刀
EASlot2, // 6 - EA型槽刀
Special // 7 - 特殊刀具
}
///
/// 最小刀径参数分组(按尾号使用标准)
///
public enum MinDiameterParameterGroup
{
None,
Drill,
Slot,
EA,
Dust,
Deburr
}
///
/// 刀具大类枚举
///
public enum ToolCategory
{
Drill, // 钻针
Slot, // 槽刀(包含槽刀、粉尘刀、去毛刺刀)
EA, // EA刀(EA型槽刀)
NonStandard,// 非标刀
Special // 特殊刀具
}
///
/// 刀具信息数据模型
///
public class ToolItem : INotifyPropertyChanged
{
private int _toolNumber;
private double _diameter;
private int _regularHoles;
private int _slotHoles;
private int _totalHoles;
private int _slotCount;
private string _parameters = string.Empty;
private ToolType _toolType;
private string _machineCodeType = string.Empty;
private string _machineCodeCommand = string.Empty;
private ToolSuffixType _toolSuffixType;
private ToolCategory _toolCategory;
private List _holeLocations = new();
///
/// 刀具编号
///
public int ToolNumber
{
get => _toolNumber;
set => SetProperty(ref _toolNumber, value);
}
///
/// 孔径(mm)
///
public double Diameter
{
get => _diameter;
set => SetProperty(ref _diameter, value);
}
///
/// 普通孔数
///
public int RegularHoles
{
get => _regularHoles;
set => SetProperty(ref _regularHoles, value);
}
///
/// 槽孔实际钻孔数
///
public int SlotHoles
{
get => _slotHoles;
set => SetProperty(ref _slotHoles, value);
}
///
/// 总孔数
///
public int TotalHoles
{
get => _totalHoles;
set => SetProperty(ref _totalHoles, value);
}
///
/// 槽条数(G85行数)
///
public int SlotCount
{
get => _slotCount;
set => SetProperty(ref _slotCount, value);
}
///
/// 钻机参数
///
public string Parameters
{
get => _parameters;
set => SetProperty(ref _parameters, value);
}
///
/// 刀具类型
///
public ToolType ToolType
{
get => _toolType;
set => SetProperty(ref _toolType, value);
}
///
/// 机台码类型 (A或B)
///
public string MachineCodeType
{
get => _machineCodeType;
set => SetProperty(ref _machineCodeType, value);
}
///
/// 机台码命令 (M97或M98)
///
public string MachineCodeCommand
{
get => _machineCodeCommand;
set => SetProperty(ref _machineCodeCommand, value);
}
///
/// 刀具尾号类型
///
public ToolSuffixType ToolSuffixType
{
get => _toolSuffixType;
set => SetProperty(ref _toolSuffixType, value);
}
///
/// 刀具大类
///
public ToolCategory ToolCategory
{
get => _toolCategory;
set => SetProperty(ref _toolCategory, value);
}
///
/// 孔位数据列表
///
public List HoleLocations
{
get => _holeLocations;
set => SetProperty(ref _holeLocations, value);
}
///
/// 刀具类型显示文本
///
public string ToolTypeDisplay => ToolType switch
{
ToolType.Regular => "圆孔",
ToolType.Slot => "槽孔",
ToolType.MachineCode => "机台码",
_ => "未知"
};
///
/// 刀具尾号类型显示文本
///
public string ToolSuffixTypeDisplay => GetToolSuffixTypeDisplay(ToolSuffixType);
///
/// 刀具大类显示文本
///
public string ToolCategoryDisplay => GetToolCategoryDisplay(ToolCategory);
public event PropertyChangedEventHandler? PropertyChanged;
protected bool SetProperty(ref T field, T value, [CallerMemberName] string? propertyName = null)
{
if (EqualityComparer.Default.Equals(field, value))
return false;
field = value;
OnPropertyChanged(propertyName);
return true;
}
protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
///
/// PP定位孔(3.203)、外围参考孔(3.175)、Mark孔(1.049)、机台码(0.499)等特殊孔径。
/// 不按尾号归类,且不参与叠板块数计算。
///
public static bool IsSpecialFixedDiameter(double diameter)
{
return Math.Abs(diameter - 1.049) < 0.001
|| Math.Abs(diameter - 3.175) < 0.001
|| Math.Abs(diameter - 3.203) < 0.001
|| Math.Abs(diameter - 0.499) < 0.001;
}
///
/// 根据孔径尾号判断刀具尾号类型
///
/// 孔径
/// 刀具尾号类型
public static ToolSuffixType GetToolSuffixType(double diameter)
{
// 特殊孔径优先判断
if (IsSpecialFixedDiameter(diameter))
{
System.Diagnostics.Debug.WriteLine($"[尾号类型判断] 直径={diameter:F3}: 特殊孔径,尾号类型=钻针");
return ToolSuffixType.Drill; // 固定为圆孔
}
// 获取孔径的小数部分最后一位
string diameterStr = diameter.ToString("F3");
if (diameterStr.Length >= 4 && diameterStr.Contains('.'))
{
char lastChar = diameterStr[diameterStr.Length - 1];
int suffix = int.Parse(lastChar.ToString());
var suffixType = suffix switch
{
0 => ToolSuffixType.Drill,
1 => ToolSuffixType.Slot,
2 => ToolSuffixType.EASlot,
3 => ToolSuffixType.DustSlot,
4 => ToolSuffixType.DeburrSlot,
5 => ToolSuffixType.NonStandard,
6 => ToolSuffixType.EASlot2,
7 => ToolSuffixType.Special,
8 => ToolSuffixType.Drill,
9 => ToolSuffixType.Drill,
_ => ToolSuffixType.NonStandard
};
System.Diagnostics.Debug.WriteLine($"[尾号类型判断] 直径={diameter:F3}, 字符串={diameterStr}, 尾号={suffix}, 尾号类型={suffixType}");
return suffixType;
}
System.Diagnostics.Debug.WriteLine($"[尾号类型判断] 直径={diameter:F3}: 格式异常,尾号类型=非标刀");
return ToolSuffixType.NonStandard; // 默认为非标刀
}
///
/// 根据刀具尾号类型获取刀具大类
///
/// 刀具尾号类型
/// 刀具大类
public static ToolCategory GetToolCategory(ToolSuffixType suffixType)
{
return suffixType switch
{
ToolSuffixType.Drill => ToolCategory.Drill,
ToolSuffixType.Slot or ToolSuffixType.DustSlot or ToolSuffixType.DeburrSlot => ToolCategory.Slot,
ToolSuffixType.EASlot or ToolSuffixType.EASlot2 => ToolCategory.EA,
ToolSuffixType.NonStandard => ToolCategory.NonStandard,
ToolSuffixType.Special => ToolCategory.Special,
_ => ToolCategory.NonStandard
};
}
///
/// 获取刀具尾号类型的显示文本
///
/// 刀具尾号类型
/// 显示文本
public static string GetToolSuffixTypeDisplay(ToolSuffixType suffixType)
{
return suffixType switch
{
ToolSuffixType.Drill => "钻针",
ToolSuffixType.Slot => "槽刀",
ToolSuffixType.EASlot => "EA型槽刀",
ToolSuffixType.DustSlot => "粉尘刀",
ToolSuffixType.DeburrSlot => "去毛刺刀",
ToolSuffixType.NonStandard => "非标刀",
ToolSuffixType.EASlot2 => "EA型槽刀",
ToolSuffixType.Special => "特殊刀具",
_ => "未知"
};
}
///
/// 获取刀具大类的显示文本
///
/// 刀具大类
/// 显示文本
public static string GetToolCategoryDisplay(ToolCategory category)
{
return category switch
{
ToolCategory.Drill => "钻针",
ToolCategory.Slot => "槽刀",
ToolCategory.EA => "EA刀",
ToolCategory.NonStandard => "非标刀",
ToolCategory.Special => "特殊刀",
_ => "未知"
};
}
///
/// 0.749mm 不参与界面最小孔径显示。
///
public const double MinDiameterExcludedDiameter = 0.749;
///
/// 按尾号使用标准映射到最小刀径参数分组。
/// 0/5/8/9→钻针,1→槽刀,2/6→EA,3→粉尘刀,4→去毛刺刀(BSH),7→不设定参数。
///
public static MinDiameterParameterGroup GetMinDiameterParameterGroup(ToolSuffixType suffixType)
{
return suffixType switch
{
ToolSuffixType.Drill => MinDiameterParameterGroup.Drill,
ToolSuffixType.NonStandard => MinDiameterParameterGroup.Drill,
ToolSuffixType.Slot => MinDiameterParameterGroup.Slot,
ToolSuffixType.EASlot or ToolSuffixType.EASlot2 => MinDiameterParameterGroup.EA,
ToolSuffixType.DustSlot => MinDiameterParameterGroup.Dust,
ToolSuffixType.DeburrSlot => MinDiameterParameterGroup.Deburr,
ToolSuffixType.Special => MinDiameterParameterGroup.None,
_ => MinDiameterParameterGroup.None
};
}
///
/// 按尾号规则计算最小钻针、槽刀、EA 刀直径。
/// 粉尘刀与去毛刺刀使用独立参数表,不参与三者最小值;特殊刀与固定孔径不参与。
///
/// 刀具集合
/// 为 true 时排除 0.749mm(界面显示用)
public static (double MinDrill, double MinSlot, double MinEA) CalculateMinDiameters(
IEnumerable tools,
bool exclude749ForDisplay = false)
{
double minDrill = 0;
double minSlot = 0;
double minEA = 0;
foreach (var tool in tools)
{
if (tool.ToolType == ToolType.MachineCode)
continue;
if (IsSpecialFixedDiameter(tool.Diameter))
continue;
if (exclude749ForDisplay
&& Math.Abs(tool.Diameter - MinDiameterExcludedDiameter) < 0.001)
{
continue;
}
switch (GetMinDiameterParameterGroup(tool.ToolSuffixType))
{
case MinDiameterParameterGroup.Drill:
if (minDrill == 0 || tool.Diameter < minDrill)
minDrill = tool.Diameter;
break;
case MinDiameterParameterGroup.Slot:
if (minSlot == 0 || tool.Diameter < minSlot)
minSlot = tool.Diameter;
break;
case MinDiameterParameterGroup.EA:
if (minEA == 0 || tool.Diameter < minEA)
minEA = tool.Diameter;
break;
}
}
return (minDrill, minSlot, minEA);
}
}
}