feat: 未匹配直径在完成弹窗中提示 — ReplaceParameters 返回 unmatchedDiameters 列表

This commit is contained in:
2026-08-03 18:28:36 +08:00
parent 5c9a70c9f0
commit 5ee4fb9c59
2 changed files with 24 additions and 7 deletions

View File

@@ -1,6 +1,7 @@
using System.Configuration; using System.Configuration;
using System.Data; using System.Data;
using System.Diagnostics; using System.Diagnostics;
using System.Globalization;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@@ -333,11 +334,19 @@ namespace DrillTools
if (selectionWindow.SelectedFilePath == null) if (selectionWindow.SelectedFilePath == null)
return; return;
RouParameterReplacer.ReplaceParameters(filePath, selectionWindow.SelectedFilePath); RouParameterReplacer.ReplaceParameters(filePath, selectionWindow.SelectedFilePath, out var unmatchedDiameters);
string message = $"参数替换完成:\n{filePath}\n原文件已备份为:\n{Path.GetFileName(filePath)}.bak";
if (unmatchedDiameters.Count > 0)
{
var diaStrings = unmatchedDiameters.Select(d => d.ToString("F3", CultureInfo.InvariantCulture)).ToList();
message += $"\n\n⚠ 以下直径未找到匹配参数,已保留原行:\n{string.Join(", ", diaStrings)}";
}
System.Windows.MessageBox.Show( System.Windows.MessageBox.Show(
$"参数替换完成:\n{filePath}\n原文件已备份为 .bak", message,
"替换完成", "参数替换完成",
MessageBoxButton.OK, MessageBoxButton.OK,
MessageBoxImage.Information); MessageBoxImage.Information);
} }

View File

@@ -36,7 +36,7 @@ namespace DrillTools
/// </summary> /// </summary>
/// <param name="rouFilePath">.rou 文件完整路径</param> /// <param name="rouFilePath">.rou 文件完整路径</param>
/// <param name="paramFilePath">参数 .txt 文件完整路径</param> /// <param name="paramFilePath">参数 .txt 文件完整路径</param>
public static void ReplaceParameters(string rouFilePath, string paramFilePath) public static void ReplaceParameters(string rouFilePath, string paramFilePath, out List<double> unmatchedDiameters)
{ {
// 1. 读取参数文件,构建直径→参数字符串映射 // 1. 读取参数文件,构建直径→参数字符串映射
var paramDict = LoadParameterDictionary(paramFilePath); var paramDict = LoadParameterDictionary(paramFilePath);
@@ -47,8 +47,9 @@ namespace DrillTools
// 2. 读取 .rou 文件 // 2. 读取 .rou 文件
string rouContent = CommandTypeFileReader.ReadAllText(rouFilePath); string rouContent = CommandTypeFileReader.ReadAllText(rouFilePath);
// 3. 执行替换 // 3. 执行替换(同时收集未匹配直径)
string replacedContent = ReplaceParametersInContent(rouContent, paramDict); var unmatchedSet = new HashSet<double>();
string replacedContent = ReplaceParametersInContent(rouContent, paramDict, unmatchedSet);
// 4. 备份原文件 // 4. 备份原文件
string backupFilePath = rouFilePath + ".bak"; string backupFilePath = rouFilePath + ".bak";
@@ -57,6 +58,9 @@ namespace DrillTools
// 5. 写回文件 // 5. 写回文件
using var writer = new StreamWriter(rouFilePath, false, Encoding.GetEncoding(Gb2312CodePage)); using var writer = new StreamWriter(rouFilePath, false, Encoding.GetEncoding(Gb2312CodePage));
writer.Write(replacedContent); writer.Write(replacedContent);
// 6. 输出未匹配直径
unmatchedDiameters = new List<double>(unmatchedSet);
} }
/// <summary> /// <summary>
@@ -92,7 +96,7 @@ namespace DrillTools
/// <summary> /// <summary>
/// 在 .rou 文件内容中,将 M48~% 区域内的刀径行末尾追加参数。 /// 在 .rou 文件内容中,将 M48~% 区域内的刀径行末尾追加参数。
/// </summary> /// </summary>
private static string ReplaceParametersInContent(string rouContent, Dictionary<double, string> paramDict) private static string ReplaceParametersInContent(string rouContent, Dictionary<double, string> paramDict, HashSet<double> unmatchedDiameters)
{ {
var lines = DrillTapeLineParser.SplitLines(rouContent); var lines = DrillTapeLineParser.SplitLines(rouContent);
var builder = new StringBuilder(rouContent.Length + 1024); var builder = new StringBuilder(rouContent.Length + 1024);
@@ -144,6 +148,10 @@ namespace DrillTools
builder.Append(line.LineEnding); builder.Append(line.LineEnding);
continue; continue;
} }
else
{
unmatchedDiameters.Add(diameter);
}
} }
} }