Files
AohDrllTools/DrillScalingViewModel.cs
Mr.Xia cacbbe6c33 feat: 增加钻带涨缩功能,支持倍率/PPM模式缩放坐标
- 新增 ExcellonScaler 引擎:普通孔缩放坐标,G85槽孔保护槽长,刀具定义行原样保留
- 新增 DrillScalingWindow 参数设置窗口:倍率/PPM模式切换,X/Y独立输入
- 集成到启动功能菜单:.dr2/.dpin 文件显示"涨缩"按钮
- 输出覆盖原文件 + .bak 备份,GB2312 编码
2026-06-05 22:17:15 +08:00

146 lines
4.2 KiB
C#
Raw 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.Runtime.CompilerServices;
namespace DrillTools
{
/// <summary>
/// 涨缩参数输入模式
/// </summary>
public enum ScalingMode
{
/// <summary>倍率模式</summary>
Ratio,
/// <summary>PPM 模式</summary>
Ppm
}
/// <summary>
/// 钻带涨缩窗口视图模型
/// </summary>
public class DrillScalingViewModel : INotifyPropertyChanged
{
private ScalingMode _mode = ScalingMode.Ratio;
private string _inputX = "1.0";
private string _inputY = "1.0";
/// <summary>
/// 缩放模式(倍率 / PPM
/// </summary>
public ScalingMode Mode
{
get => _mode;
set
{
if (SetProperty(ref _mode, value))
{
OnPropertyChanged(nameof(ModeLabelX));
OnPropertyChanged(nameof(ModeLabelY));
OnPropertyChanged(nameof(ModeDescription));
}
}
}
/// <summary>
/// X 方向输入值(原始字符串)
/// </summary>
public string InputX
{
get => _inputX;
set => SetProperty(ref _inputX, value);
}
/// <summary>
/// Y 方向输入值(原始字符串)
/// </summary>
public string InputY
{
get => _inputY;
set => SetProperty(ref _inputY, value);
}
/// <summary>
/// X 方向标签文本(根据模式变化)
/// </summary>
public string ModeLabelX => Mode == ScalingMode.Ratio ? "倍率 X" : "PPM X";
/// <summary>
/// Y 方向标签文本(根据模式变化)
/// </summary>
public string ModeLabelY => Mode == ScalingMode.Ratio ? "倍率 Y" : "PPM Y";
/// <summary>
/// 模式说明文本
/// </summary>
public string ModeDescription => Mode == ScalingMode.Ratio
? "倍率模式:直接输入缩放倍率(如 1.0005"
: "PPM 模式:输入百万分率(如 500 表示 +500ppm";
/// <summary>
/// 计算后的 X 方向实际缩放倍率
/// </summary>
public double Kx
{
get
{
if (!double.TryParse(_inputX, out double value))
return 1.0;
return Mode == ScalingMode.Ratio ? value : 1.0 + value / 1e6;
}
}
/// <summary>
/// 计算后的 Y 方向实际缩放倍率
/// </summary>
public double Ky
{
get
{
if (!double.TryParse(_inputY, out double value))
return 1.0;
return Mode == ScalingMode.Ratio ? value : 1.0 + value / 1e6;
}
}
/// <summary>
/// 验证输入是否有效
/// </summary>
/// <returns>验证结果null 表示有效,非 null 为错误消息</returns>
public string? Validate()
{
if (!double.TryParse(_inputX, out double xVal))
return "X 值不是有效的数字";
if (!double.TryParse(_inputY, out double yVal))
return "Y 值不是有效的数字";
if (Mode == ScalingMode.Ratio)
{
if (xVal <= 0)
return "倍率 X 必须大于 0";
if (yVal <= 0)
return "倍率 Y 必须大于 0";
}
return null;
}
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));
}
}
}