420 lines
14 KiB
C#
420 lines
14 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel;
|
||
using System.Runtime.CompilerServices;
|
||
|
||
namespace DrillTools
|
||
{
|
||
/// <summary>
|
||
/// 刀具类型枚举
|
||
/// </summary>
|
||
public enum ToolType
|
||
{
|
||
Regular, // 圆孔刀具
|
||
Slot, // 槽孔刀具
|
||
MachineCode // 机台码刀具
|
||
}
|
||
|
||
/// <summary>
|
||
/// 刀具尾号类型枚举
|
||
/// </summary>
|
||
public enum ToolSuffixType
|
||
{
|
||
Drill, // 0 - 钻针
|
||
Slot, // 1 - 槽刀
|
||
EASlot, // 2 - EA型槽刀
|
||
DustSlot, // 3 - 粉尘刀(槽刀)
|
||
DeburrSlot, // 4 - 去毛刺刀(槽刀)
|
||
NonStandard, // 5 - 非标刀
|
||
EASlot2, // 6 - EA型槽刀
|
||
Special // 7 - 特殊刀具
|
||
}
|
||
|
||
/// <summary>
|
||
/// 最小刀径参数分组(按尾号使用标准)
|
||
/// </summary>
|
||
public enum MinDiameterParameterGroup
|
||
{
|
||
None,
|
||
Drill,
|
||
Slot,
|
||
EA,
|
||
Dust,
|
||
Deburr
|
||
}
|
||
|
||
/// <summary>
|
||
/// 刀具大类枚举
|
||
/// </summary>
|
||
public enum ToolCategory
|
||
{
|
||
Drill, // 钻针
|
||
Slot, // 槽刀(包含槽刀、粉尘刀、去毛刺刀)
|
||
EA, // EA刀(EA型槽刀)
|
||
NonStandard,// 非标刀
|
||
Special // 特殊刀具
|
||
}
|
||
|
||
/// <summary>
|
||
/// 刀具信息数据模型
|
||
/// </summary>
|
||
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<string> _holeLocations = new();
|
||
|
||
/// <summary>
|
||
/// 刀具编号
|
||
/// </summary>
|
||
public int ToolNumber
|
||
{
|
||
get => _toolNumber;
|
||
set => SetProperty(ref _toolNumber, value);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 孔径(mm)
|
||
/// </summary>
|
||
public double Diameter
|
||
{
|
||
get => _diameter;
|
||
set => SetProperty(ref _diameter, value);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 普通孔数
|
||
/// </summary>
|
||
public int RegularHoles
|
||
{
|
||
get => _regularHoles;
|
||
set => SetProperty(ref _regularHoles, value);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 槽孔实际钻孔数
|
||
/// </summary>
|
||
public int SlotHoles
|
||
{
|
||
get => _slotHoles;
|
||
set => SetProperty(ref _slotHoles, value);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 总孔数
|
||
/// </summary>
|
||
public int TotalHoles
|
||
{
|
||
get => _totalHoles;
|
||
set => SetProperty(ref _totalHoles, value);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 槽条数(G85行数)
|
||
/// </summary>
|
||
public int SlotCount
|
||
{
|
||
get => _slotCount;
|
||
set => SetProperty(ref _slotCount, value);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 钻机参数
|
||
/// </summary>
|
||
public string Parameters
|
||
{
|
||
get => _parameters;
|
||
set => SetProperty(ref _parameters, value);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 刀具类型
|
||
/// </summary>
|
||
public ToolType ToolType
|
||
{
|
||
get => _toolType;
|
||
set => SetProperty(ref _toolType, value);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 机台码类型 (A或B)
|
||
/// </summary>
|
||
public string MachineCodeType
|
||
{
|
||
get => _machineCodeType;
|
||
set => SetProperty(ref _machineCodeType, value);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 机台码命令 (M97或M98)
|
||
/// </summary>
|
||
public string MachineCodeCommand
|
||
{
|
||
get => _machineCodeCommand;
|
||
set => SetProperty(ref _machineCodeCommand, value);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 刀具尾号类型
|
||
/// </summary>
|
||
public ToolSuffixType ToolSuffixType
|
||
{
|
||
get => _toolSuffixType;
|
||
set => SetProperty(ref _toolSuffixType, value);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 刀具大类
|
||
/// </summary>
|
||
public ToolCategory ToolCategory
|
||
{
|
||
get => _toolCategory;
|
||
set => SetProperty(ref _toolCategory, value);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 孔位数据列表
|
||
/// </summary>
|
||
public List<string> HoleLocations
|
||
{
|
||
get => _holeLocations;
|
||
set => SetProperty(ref _holeLocations, value);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 刀具类型显示文本
|
||
/// </summary>
|
||
public string ToolTypeDisplay => ToolType switch
|
||
{
|
||
ToolType.Regular => "圆孔",
|
||
ToolType.Slot => "槽孔",
|
||
ToolType.MachineCode => "机台码",
|
||
_ => "未知"
|
||
};
|
||
|
||
/// <summary>
|
||
/// 刀具尾号类型显示文本
|
||
/// </summary>
|
||
public string ToolSuffixTypeDisplay => GetToolSuffixTypeDisplay(ToolSuffixType);
|
||
|
||
/// <summary>
|
||
/// 刀具大类显示文本
|
||
/// </summary>
|
||
public string ToolCategoryDisplay => GetToolCategoryDisplay(ToolCategory);
|
||
|
||
public event PropertyChangedEventHandler? PropertyChanged;
|
||
|
||
protected bool SetProperty<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
|
||
{
|
||
if (EqualityComparer<T>.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));
|
||
}
|
||
|
||
/// <summary>
|
||
/// PP定位孔(3.203)、外围参考孔(3.175)、Mark孔(1.049)、机台码(0.499)等特殊孔径。
|
||
/// 不按尾号归类,且不参与叠板块数计算。
|
||
/// </summary>
|
||
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;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据孔径尾号判断刀具尾号类型
|
||
/// </summary>
|
||
/// <param name="diameter">孔径</param>
|
||
/// <returns>刀具尾号类型</returns>
|
||
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; // 默认为非标刀
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据刀具尾号类型获取刀具大类
|
||
/// </summary>
|
||
/// <param name="suffixType">刀具尾号类型</param>
|
||
/// <returns>刀具大类</returns>
|
||
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
|
||
};
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取刀具尾号类型的显示文本
|
||
/// </summary>
|
||
/// <param name="suffixType">刀具尾号类型</param>
|
||
/// <returns>显示文本</returns>
|
||
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 => "特殊刀具",
|
||
_ => "未知"
|
||
};
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取刀具大类的显示文本
|
||
/// </summary>
|
||
/// <param name="category">刀具大类</param>
|
||
/// <returns>显示文本</returns>
|
||
public static string GetToolCategoryDisplay(ToolCategory category)
|
||
{
|
||
return category switch
|
||
{
|
||
ToolCategory.Drill => "钻针",
|
||
ToolCategory.Slot => "槽刀",
|
||
ToolCategory.EA => "EA刀",
|
||
ToolCategory.NonStandard => "非标刀",
|
||
ToolCategory.Special => "特殊刀",
|
||
_ => "未知"
|
||
};
|
||
}
|
||
|
||
/// <summary>
|
||
/// 0.749mm 不参与界面最小孔径显示。
|
||
/// </summary>
|
||
public const double MinDiameterExcludedDiameter = 0.749;
|
||
|
||
/// <summary>
|
||
/// 按尾号使用标准映射到最小刀径参数分组。
|
||
/// 0/5/8/9→钻针,1→槽刀,2/6→EA,3→粉尘刀,4→去毛刺刀(BSH),7→不设定参数。
|
||
/// </summary>
|
||
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
|
||
};
|
||
}
|
||
|
||
/// <summary>
|
||
/// 按尾号规则计算最小钻针、槽刀、EA 刀直径。
|
||
/// 粉尘刀与去毛刺刀使用独立参数表,不参与三者最小值;特殊刀与固定孔径不参与。
|
||
/// </summary>
|
||
/// <param name="tools">刀具集合</param>
|
||
/// <param name="exclude749ForDisplay">为 true 时排除 0.749mm(界面显示用)</param>
|
||
public static (double MinDrill, double MinSlot, double MinEA) CalculateMinDiameters(
|
||
IEnumerable<ToolItem> 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);
|
||
}
|
||
}
|
||
}
|