From 5c9a70c9f0dc3e43e7887289d61610c6a50b7439 Mon Sep 17 00:00:00 2001 From: "Mr.Xia" <1424473282@qq.com> Date: Mon, 3 Aug 2026 18:19:33 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20ROU=E6=88=90=E5=9E=8B=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E5=8A=9F=E8=83=BD=20=E2=80=94=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81=20.rou/.rou1/.rou2/.roux=20=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E4=BB=8E=E5=8F=82=E6=95=B0=20txt=20=E8=87=AA=E5=8A=A8=E8=A1=A5?= =?UTF-8?q?=E5=85=85=20S/F/R/Z/H?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.xaml.cs | 72 +++++++++-- DrillTapeLineParser.cs | 57 +++++++++ DrillTapeParameterCleaner.cs | 42 +------ ParameterFileSelectionWindow.xaml | 32 +++++ ParameterFileSelectionWindow.xaml.cs | 66 ++++++++++ RouParameterReplacer.cs | 178 +++++++++++++++++++++++++++ StartupSelectionWindow.xaml | 1 + StartupSelectionWindow.xaml.cs | 12 +- 8 files changed, 409 insertions(+), 51 deletions(-) create mode 100644 DrillTapeLineParser.cs create mode 100644 ParameterFileSelectionWindow.xaml create mode 100644 ParameterFileSelectionWindow.xaml.cs create mode 100644 RouParameterReplacer.cs diff --git a/App.xaml.cs b/App.xaml.cs index a91554b..499da10 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -51,6 +51,7 @@ namespace DrillTools bool canGeneratePpDrillTape = viewModel.CanGeneratePpDrillTape; string extension = Path.GetExtension(filePath).ToLowerInvariant(); bool canScaleDrillTape = extension == ".dr2" || extension == ".dpin"; + bool isRouFile = extension == ".rou" || extension == ".rou1" || extension == ".rou2" || extension == ".roux"; double minDrill = viewModel.MinDrillDiameter; double minSlot = viewModel.MinSlotDiameter; double minEA = viewModel.MinEADiameter; @@ -82,7 +83,8 @@ namespace DrillTools selectionViewModel, canClearParameters, canGeneratePpDrillTape, - canScaleDrillTape); + canScaleDrillTape, + canReplaceRouParameters: isRouFile); selectionWindow.ShowDialog(); switch (selectionWindow.SelectedAction) @@ -107,6 +109,10 @@ namespace DrillTools PerformDrillScaling(filePath); Shutdown(); break; + case StartupAction.ReplaceRouParameters: + PerformRouParameterReplace(filePath); + Shutdown(); + break; default: Shutdown(); break; @@ -222,12 +228,14 @@ namespace DrillTools string content = CommandTypeFileReader.ReadAllText(filePath); string scaledContent = Integration.ExcellonScaler.Scale(content, result.Kx, result.Ky); - // 备份原文件 - string backupFilePath = filePath + ".bak"; - File.Copy(filePath, backupFilePath, true); + // 在源文件所在目录下新建子文件夹 dr2 x{InputX} y{InputY} + string sourceDir = Path.GetDirectoryName(filePath)!; + string outputDir = Path.Combine(sourceDir, $"dr2 x{result.InputX} y{result.InputY}"); + Directory.CreateDirectory(outputDir); - // 写入缩放后的内容 - using var writer = new StreamWriter(filePath, false, Encoding.GetEncoding(936)); + // 将涨缩后的钻带写入子文件夹,文件名不变 + string outputFilePath = Path.Combine(outputDir, Path.GetFileName(filePath)); + using var writer = new StreamWriter(outputFilePath, false, Encoding.GetEncoding(936)); writer.Write(scaledContent); // 系数记忆:紧跟钻带写回立即落盘,不滞后到完成提示之后。 @@ -242,7 +250,7 @@ namespace DrillTools memoryWarning = $"\n(系数记忆保存失败:{ex.Message})"; } - string completionMessage = $"涨缩完成,原文件已备份为:\n{Path.GetFileName(filePath)}.bak"; + string completionMessage = $"涨缩完成,输出至:\n{outputFilePath}"; if (memoryWarning != null) completionMessage += memoryWarning; @@ -264,7 +272,7 @@ namespace DrillTools try { string exePath = Process.GetCurrentProcess().MainModule!.FileName!; - string[] extensions = { ".drl", ".dr2", ".dr3", ".trg", ".dpin", ".txt" }; + string[] extensions = { ".drl", ".dr2", ".dr3", ".trg", ".dpin", ".txt", ".rou", ".rou1", ".rou2", ".roux" }; string menuText = "用DrillTools打开"; foreach (string ext in extensions) @@ -295,11 +303,57 @@ namespace DrillTools // 检查文件扩展名是否为支持的钻带文件格式 string extension = Path.GetExtension(filePath).ToLowerInvariant(); - string[] supportedExtensions = { ".txt", ".drl", ".dr2", ".dr3", ".trg", ".dpin" }; + string[] supportedExtensions = { ".txt", ".drl", ".dr2", ".dr3", ".trg", ".dpin", ".rou", ".rou1", ".rou2", ".roux" }; return supportedExtensions.Contains(extension); } + private static void PerformRouParameterReplace(string filePath) + { + try + { +#if DEBUG + string paramDir = @"C:\Users\14244\source\repos\Aoh\DrillTools\ROU-canshu\"; +#else + string paramDir = @"\\aht-file-1\AHT-workdatatemp\泰国澳弘的相关说明资料及作业指引\钻锣带\成型\"; +#endif + if (!Directory.Exists(paramDir)) + { + System.Windows.MessageBox.Show( + $"参数文件目录不存在:\n{paramDir}", + "错误", + MessageBoxButton.OK, + MessageBoxImage.Error); + return; + } + + var selectionWindow = new ParameterFileSelectionWindow(paramDir); + selectionWindow.ShowDialog(); + + if (selectionWindow.SelectedFilePath == null) + return; + + RouParameterReplacer.ReplaceParameters(filePath, selectionWindow.SelectedFilePath); + + System.Windows.MessageBox.Show( + $"参数替换完成:\n{filePath}\n原文件已备份为 .bak", + "替换完成", + MessageBoxButton.OK, + MessageBoxImage.Information); + } + catch (OperationCanceledException) + { + } + catch (Exception ex) + { + System.Windows.MessageBox.Show( + $"参数替换失败:\n{ex.Message}", + "错误", + MessageBoxButton.OK, + MessageBoxImage.Error); + } + } + /// /// 从 MainWindowViewModel.Tools 独立遍历计算叠板用最小刀径。 /// 分类逻辑与 UpdateMinDiameterInfo 一致(按尾号使用标准),不排除 0.749mm; diff --git a/DrillTapeLineParser.cs b/DrillTapeLineParser.cs new file mode 100644 index 0000000..1037eee --- /dev/null +++ b/DrillTapeLineParser.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; + +namespace DrillTools +{ + /// + /// 钻带文本行结构,保留原始文本和行尾换行符。 + /// + internal readonly record struct DrillTapeLine(string Text, string LineEnding); + + /// + /// 钻带行解析器,将钻带文本按行拆分,保留原始换行符。 + /// + internal static class DrillTapeLineParser + { + private static readonly char[] LineEndChars = { '\r', '\n' }; + + /// + /// 将文本内容按行拆分,保留每行的换行符。 + /// + public static List SplitLines(string content) + { + var lines = new List(); + int index = 0; + + while (index < content.Length) + { + int lineEndIndex = content.IndexOfAny(LineEndChars, index); + if (lineEndIndex < 0) + { + lines.Add(new DrillTapeLine(content[index..], string.Empty)); + return lines; + } + + string text = content[index..lineEndIndex]; + string lineEnding; + if (content[lineEndIndex] == '\r' && lineEndIndex + 1 < content.Length && content[lineEndIndex + 1] == '\n') + { + lineEnding = "\r\n"; + index = lineEndIndex + 2; + } + else + { + lineEnding = content[lineEndIndex].ToString(); + index = lineEndIndex + 1; + } + + lines.Add(new DrillTapeLine(text, lineEnding)); + } + + if (content.Length == 0) + lines.Add(new DrillTapeLine(string.Empty, string.Empty)); + + return lines; + } + } +} diff --git a/DrillTapeParameterCleaner.cs b/DrillTapeParameterCleaner.cs index b8c1762..e624696 100644 --- a/DrillTapeParameterCleaner.cs +++ b/DrillTapeParameterCleaner.cs @@ -9,7 +9,6 @@ namespace DrillTools internal static class DrillTapeParameterCleaner { private const int Gb2312CodePage = 936; - private static readonly char[] LineEndChars = { '\r', '\n' }; private static readonly Regex HeaderToolDefinitionStartPattern = new( @"^T\d+C", RegexOptions.Compiled | RegexOptions.IgnoreCase); @@ -19,7 +18,7 @@ namespace DrillTools public static bool CanClearParameters(string drillTapeContent) { - var lines = SplitLines(drillTapeContent); + var lines = DrillTapeLineParser.SplitLines(drillTapeContent); if (lines.Count < 2) return false; @@ -45,7 +44,7 @@ namespace DrillTools if (!CanClearParameters(drillTapeContent)) throw new InvalidOperationException("钻带文件不符合清空参数条件"); - var lines = SplitLines(drillTapeContent); + var lines = DrillTapeLineParser.SplitLines(drillTapeContent); var builder = new StringBuilder(drillTapeContent.Length); for (int i = 0; i < lines.Count; i++) @@ -81,42 +80,6 @@ namespace DrillTools writer.Write(cleanedContent); } - private static List SplitLines(string content) - { - var lines = new List(); - int index = 0; - - while (index < content.Length) - { - int lineEndIndex = content.IndexOfAny(LineEndChars, index); - if (lineEndIndex < 0) - { - lines.Add(new DrillTapeLine(content[index..], string.Empty)); - return lines; - } - - string text = content[index..lineEndIndex]; - string lineEnding; - if (content[lineEndIndex] == '\r' && lineEndIndex + 1 < content.Length && content[lineEndIndex + 1] == '\n') - { - lineEnding = "\r\n"; - index = lineEndIndex + 2; - } - else - { - lineEnding = content[lineEndIndex].ToString(); - index = lineEndIndex + 1; - } - - lines.Add(new DrillTapeLine(text, lineEnding)); - } - - if (content.Length == 0) - lines.Add(new DrillTapeLine(string.Empty, string.Empty)); - - return lines; - } - private static List GetHeaderToolDefinitionLines(IReadOnlyList lines) { var toolDefinitionLines = new List(); @@ -150,6 +113,5 @@ namespace DrillTools return Encoding.GetEncoding(Gb2312CodePage); } - private readonly record struct DrillTapeLine(string Text, string LineEnding); } } diff --git a/ParameterFileSelectionWindow.xaml b/ParameterFileSelectionWindow.xaml new file mode 100644 index 0000000..00f5641 --- /dev/null +++ b/ParameterFileSelectionWindow.xaml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + +