Files
AohDrllTools/StartupSelectionViewModel.cs

250 lines
7.4 KiB
C#
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.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
namespace DrillTools
{
/// <summary>
/// 启动选择窗口的 ViewModel承载基础信息显示和叠板计算
/// </summary>
public class StartupSelectionViewModel : INotifyPropertyChanged
{
#region
private string _fileName = string.Empty;
/// <summary>
/// 文件名(不含扩展名)
/// </summary>
public string FileName
{
get => _fileName;
set => SetProperty(ref _fileName, value);
}
private double _minDrillDiameter;
/// <summary>
/// 最小钻咀直径(显示用,已排除 0.749mm
/// </summary>
public double MinDrillDiameter
{
get => _minDrillDiameter;
set => SetProperty(ref _minDrillDiameter, value);
}
private double _minSlotDiameter;
/// <summary>
/// 最小槽刀直径(显示用)
/// </summary>
public double MinSlotDiameter
{
get => _minSlotDiameter;
set => SetProperty(ref _minSlotDiameter, value);
}
private double _minEADiameter;
/// <summary>
/// 最小EA刀直径显示用
/// </summary>
public double MinEADiameter
{
get => _minEADiameter;
set => SetProperty(ref _minEADiameter, value);
}
private bool _isPpDrillTape;
/// <summary>
/// 是否为 PP 钻带
/// </summary>
public bool IsPpDrillTape
{
get => _isPpDrillTape;
set => SetProperty(ref _isPpDrillTape, value);
}
private double _ppXSpacing;
/// <summary>
/// PP X 间距
/// </summary>
public double PpXSpacing
{
get => _ppXSpacing;
set => SetProperty(ref _ppXSpacing, value);
}
private double _ppYSpacing;
/// <summary>
/// PP Y 间距
/// </summary>
public double PpYSpacing
{
get => _ppYSpacing;
set => SetProperty(ref _ppYSpacing, value);
}
private bool _hasOuter3175Spacing;
/// <summary>
/// 是否有 3.175 外围孔间距
/// </summary>
public bool HasOuter3175Spacing
{
get => _hasOuter3175Spacing;
set => SetProperty(ref _hasOuter3175Spacing, value);
}
private double _outer3175XSpacing;
/// <summary>
/// 3.175 外围孔 X 间距
/// </summary>
public double Outer3175XSpacing
{
get => _outer3175XSpacing;
set => SetProperty(ref _outer3175XSpacing, value);
}
private double _outer3175YSpacing;
/// <summary>
/// 3.175 外围孔 Y 间距
/// </summary>
public double Outer3175YSpacing
{
get => _outer3175YSpacing;
set => SetProperty(ref _outer3175YSpacing, value);
}
#endregion
#region
private string _boardThicknessInput = string.Empty;
/// <summary>
/// 板厚输入(原始文本)
/// </summary>
public string BoardThicknessInput
{
get => _boardThicknessInput;
set
{
if (SetProperty(ref _boardThicknessInput, value))
TryCalculateStackCount();
}
}
private string _copperThicknessInput = string.Empty;
/// <summary>
/// 铜厚输入(原始文本,单位 oz
/// </summary>
public string CopperThicknessInput
{
get => _copperThicknessInput;
set
{
if (SetProperty(ref _copperThicknessInput, value))
TryCalculateStackCount();
}
}
private string _stackCountResult = string.Empty;
/// <summary>
/// 叠板计算结果文本(如"建议叠板4 张"或错误提示)
/// </summary>
public string StackCountResult
{
get => _stackCountResult;
set => SetProperty(ref _stackCountResult, value);
}
private string _stackCountDetail = string.Empty;
/// <summary>
/// 叠板计算明细文本
/// </summary>
public string StackCountDetail
{
get => _stackCountDetail;
set => SetProperty(ref _stackCountDetail, value);
}
#endregion
#region 0.749mm
/// <summary>
/// 叠板计算用的最小钻针直径(包含 0.749mm,由调用方遍历 Tools 独立计算)
/// </summary>
public double StackCalcMinDrill { get; set; }
/// <summary>
/// 叠板计算用的最小槽刀直径(尾号 1 槽刀,由调用方按尾号标准独立计算)
/// </summary>
public double StackCalcMinSlot { get; set; }
/// <summary>
/// 叠板计算用的最小 EA 刀直径(尾号 2/6 EA 型槽刀,由调用方按尾号标准独立计算)
/// </summary>
public double StackCalcMinEA { get; set; }
#endregion
#region
/// <summary>
/// 输入变化时自动尝试计算叠板数
/// 两个输入均有效时计算,否则静默清空结果
/// </summary>
private void TryCalculateStackCount()
{
// 两个输入都不为空且可解析为正数时才执行计算
if (double.TryParse(BoardThicknessInput, out double boardThickness) && boardThickness > 0 &&
double.TryParse(CopperThicknessInput, out double copperThickness) && copperThickness > 0)
{
var result = StackCountCalculator.Calculate(
boardThickness, copperThickness,
StackCalcMinDrill, StackCalcMinSlot, StackCalcMinEA);
if (result.Success)
{
StackCountResult = $"建议叠板:{result.RecommendedCount} 张";
StackCountDetail = result.Detail;
}
else
{
StackCountResult = "无法计算";
StackCountDetail = string.IsNullOrEmpty(result.Detail)
? result.Message
: $"{result.Message}\n{result.Detail}";
}
}
else
{
// 输入不完整,静默清空
StackCountResult = string.Empty;
StackCountDetail = string.Empty;
}
}
#endregion
#region INotifyPropertyChanged
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));
}
#endregion
}
}