From cacbbe6c33e8e6cc3b6f5446787f597868c8a969 Mon Sep 17 00:00:00 2001 From: "Mr.Xia" <1424473282@qq.com> Date: Fri, 5 Jun 2026 22:17:15 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E9=92=BB=E5=B8=A6?= =?UTF-8?q?=E6=B6=A8=E7=BC=A9=E5=8A=9F=E8=83=BD=EF=BC=8C=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=80=8D=E7=8E=87/PPM=E6=A8=A1=E5=BC=8F=E7=BC=A9=E6=94=BE?= =?UTF-8?q?=E5=9D=90=E6=A0=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 ExcellonScaler 引擎:普通孔缩放坐标,G85槽孔保护槽长,刀具定义行原样保留 - 新增 DrillScalingWindow 参数设置窗口:倍率/PPM模式切换,X/Y独立输入 - 集成到启动功能菜单:.dr2/.dpin 文件显示"涨缩"按钮 - 输出覆盖原文件 + .bak 备份,GB2312 编码 --- App.xaml.cs | 43 +++++++++ DrillScalingViewModel.cs | 145 +++++++++++++++++++++++++++++++ DrillScalingWindow.xaml | 94 ++++++++++++++++++++ DrillScalingWindow.xaml.cs | 57 ++++++++++++ ExcellonScaler.cs | 154 +++++++++++++++++++++++++++++++++ StartupSelectionWindow.xaml | 3 +- StartupSelectionWindow.xaml.cs | 11 ++- 7 files changed, 505 insertions(+), 2 deletions(-) create mode 100644 DrillScalingViewModel.cs create mode 100644 DrillScalingWindow.xaml create mode 100644 DrillScalingWindow.xaml.cs create mode 100644 ExcellonScaler.cs diff --git a/App.xaml.cs b/App.xaml.cs index e41972d..138c7a7 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -48,6 +48,8 @@ namespace DrillTools bool canClearParameters = DrillTapeParameterCleaner.CanClearParameters(content); bool canGeneratePpDrillTape = viewModel.CanGeneratePpDrillTape; + string extension = Path.GetExtension(filePath).ToLowerInvariant(); + bool canScaleDrillTape = extension == ".dr2" || extension == ".dpin"; double minDrill = viewModel.MinDrillDiameter; double minSlot = viewModel.MinSlotDiameter; double minEA = viewModel.MinEADiameter; @@ -62,6 +64,7 @@ namespace DrillTools filePath, canClearParameters, canGeneratePpDrillTape, + canScaleDrillTape, minDrill, minSlot, minEA, @@ -91,6 +94,10 @@ namespace DrillTools PerformPpDrillTapeGeneration(filePath); Shutdown(); break; + case StartupAction.ScaleDrillTape: + PerformDrillScaling(filePath); + Shutdown(); + break; default: Shutdown(); break; @@ -191,6 +198,42 @@ namespace DrillTools } } + private static void PerformDrillScaling(string filePath) + { + try + { + var scalingWindow = new DrillScalingWindow(); + scalingWindow.ShowDialog(); + + if (scalingWindow.ScalingResult == null) + return; + + var (kx, ky) = scalingWindow.ScalingResult.Value; + + string content = CommandTypeFileReader.ReadAllText(filePath); + string scaledContent = Integration.ExcellonScaler.Scale(content, kx, ky); + + // 备份原文件 + string backupFilePath = filePath + ".bak"; + File.Copy(filePath, backupFilePath, true); + + // 写入缩放后的内容 + using var writer = new StreamWriter(filePath, false, Encoding.GetEncoding(936)); + writer.Write(scaledContent); + + System.Windows.MessageBox.Show( + $"涨缩完成,原文件已备份为:\n{Path.GetFileName(filePath)}.bak", + "涨缩完成", + MessageBoxButton.OK, + MessageBoxImage.Information); + } + catch (Exception ex) + { + System.Windows.MessageBox.Show($"涨缩失败:\n{ex.Message}", + "错误", MessageBoxButton.OK, MessageBoxImage.Error); + } + } + private static void RegisterShellContextMenu() { try diff --git a/DrillScalingViewModel.cs b/DrillScalingViewModel.cs new file mode 100644 index 0000000..534f6e6 --- /dev/null +++ b/DrillScalingViewModel.cs @@ -0,0 +1,145 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Runtime.CompilerServices; + +namespace DrillTools +{ + /// + /// 涨缩参数输入模式 + /// + public enum ScalingMode + { + /// 倍率模式 + Ratio, + /// PPM 模式 + Ppm + } + + /// + /// 钻带涨缩窗口视图模型 + /// + public class DrillScalingViewModel : INotifyPropertyChanged + { + private ScalingMode _mode = ScalingMode.Ratio; + private string _inputX = "1.0"; + private string _inputY = "1.0"; + + /// + /// 缩放模式(倍率 / PPM) + /// + public ScalingMode Mode + { + get => _mode; + set + { + if (SetProperty(ref _mode, value)) + { + OnPropertyChanged(nameof(ModeLabelX)); + OnPropertyChanged(nameof(ModeLabelY)); + OnPropertyChanged(nameof(ModeDescription)); + } + } + } + + /// + /// X 方向输入值(原始字符串) + /// + public string InputX + { + get => _inputX; + set => SetProperty(ref _inputX, value); + } + + /// + /// Y 方向输入值(原始字符串) + /// + public string InputY + { + get => _inputY; + set => SetProperty(ref _inputY, value); + } + + /// + /// X 方向标签文本(根据模式变化) + /// + public string ModeLabelX => Mode == ScalingMode.Ratio ? "倍率 X" : "PPM X"; + + /// + /// Y 方向标签文本(根据模式变化) + /// + public string ModeLabelY => Mode == ScalingMode.Ratio ? "倍率 Y" : "PPM Y"; + + /// + /// 模式说明文本 + /// + public string ModeDescription => Mode == ScalingMode.Ratio + ? "倍率模式:直接输入缩放倍率(如 1.0005)" + : "PPM 模式:输入百万分率(如 500 表示 +500ppm)"; + + /// + /// 计算后的 X 方向实际缩放倍率 + /// + public double Kx + { + get + { + if (!double.TryParse(_inputX, out double value)) + return 1.0; + return Mode == ScalingMode.Ratio ? value : 1.0 + value / 1e6; + } + } + + /// + /// 计算后的 Y 方向实际缩放倍率 + /// + public double Ky + { + get + { + if (!double.TryParse(_inputY, out double value)) + return 1.0; + return Mode == ScalingMode.Ratio ? value : 1.0 + value / 1e6; + } + } + + /// + /// 验证输入是否有效 + /// + /// 验证结果,null 表示有效,非 null 为错误消息 + 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(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)); + } + } +} diff --git a/DrillScalingWindow.xaml b/DrillScalingWindow.xaml new file mode 100644 index 0000000..e425789 --- /dev/null +++ b/DrillScalingWindow.xaml @@ -0,0 +1,94 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +