V1.0
This commit is contained in:
@@ -5,6 +5,7 @@ using System.ComponentModel;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Windows;
|
||||
using DrillTools.Integration;
|
||||
@@ -115,12 +116,12 @@ namespace DrillTools
|
||||
DrillTapeContent = drillTapeContent;
|
||||
Tools.Clear();
|
||||
OriginalTools.Clear(); // 清空原始刀具顺序
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
// 使用现有的 DrillTapeProcessor 处理钻带数据
|
||||
var result = DrillTapeProcessor.ProcessDrillTape(drillTapeContent);
|
||||
|
||||
|
||||
if (result.Success)
|
||||
{
|
||||
foreach (var toolResult in result.ToolResults)
|
||||
@@ -138,16 +139,16 @@ namespace DrillTools
|
||||
ToolCategory = toolResult.ToolCategory,
|
||||
HoleLocations = toolResult.Locations ?? new List<string>()
|
||||
};
|
||||
|
||||
|
||||
// 如果是机台码,使用DrillTapeProcessor已经解析出的机台码信息
|
||||
if (toolResult.ToolType == ToolType.MachineCode)
|
||||
{
|
||||
toolItem.MachineCodeCommand = toolResult.MachineCodeCommand;
|
||||
toolItem.MachineCodeType = toolResult.MachineCodeType;
|
||||
|
||||
|
||||
// 添加日志验证机台码信息传递
|
||||
System.Diagnostics.Debug.WriteLine($"[机台码传递] T{toolResult.ToolNumber:D2}: 命令={toolItem.MachineCodeCommand}, 类型={toolItem.MachineCodeType}");
|
||||
|
||||
|
||||
// 如果 toolResult.Locations 已经包含坐标数据,则使用它
|
||||
// 否则从原始钻带内容中提取坐标行
|
||||
if (toolItem.HoleLocations == null || toolItem.HoleLocations.Count == 0)
|
||||
@@ -155,28 +156,28 @@ namespace DrillTools
|
||||
// 从钻带内容中提取机台码坐标行
|
||||
var toolPattern = $@"%.+?T{toolResult.ToolNumber:D2}(.*?)(?=T\d{{2}}|M30)";
|
||||
var match = System.Text.RegularExpressions.Regex.Match(DrillTapeContent, toolPattern, System.Text.RegularExpressions.RegexOptions.Singleline);
|
||||
|
||||
|
||||
if (match.Success)
|
||||
{
|
||||
string holeSection = match.Groups[1].Value;
|
||||
|
||||
|
||||
// 查找机台码坐标行
|
||||
var coordinatePattern = @"X([+-]?\d+\.?\d*)Y([+-]?\d+\.?\d*)";
|
||||
var coordinateMatches = System.Text.RegularExpressions.Regex.Matches(holeSection, coordinatePattern);
|
||||
|
||||
|
||||
toolItem.HoleLocations = new List<string>();
|
||||
foreach (Match coordMatch in coordinateMatches)
|
||||
{
|
||||
toolItem.HoleLocations.Add(coordMatch.Value);
|
||||
}
|
||||
|
||||
|
||||
System.Diagnostics.Debug.WriteLine($"[机台码坐标] T{toolResult.ToolNumber:D2}: 找到{toolItem.HoleLocations.Count}个坐标");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Tools.Add(toolItem);
|
||||
|
||||
|
||||
// 同时添加到原始刀具顺序集合中
|
||||
var originalToolItem = new ToolItem
|
||||
{
|
||||
@@ -191,9 +192,15 @@ namespace DrillTools
|
||||
};
|
||||
OriginalTools.Add(originalToolItem);
|
||||
}
|
||||
|
||||
|
||||
// 更新按钮状态
|
||||
UpdateMoveButtonsState();
|
||||
|
||||
// 检查并应用-sort.txt文件中的刀具排序
|
||||
if (!string.IsNullOrEmpty(OriginalFilePath))
|
||||
{
|
||||
CheckAndApplySortFile(OriginalFilePath, Tools);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -418,13 +425,31 @@ namespace DrillTools
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 将重新排序后的钻带内容写入原始文件
|
||||
File.WriteAllText(OriginalFilePath, reorderedDrillTape);
|
||||
// 3. 将重新排序后的钻带内容写入原始文件,使用ANSI编码
|
||||
Encoding ansiEncoding;
|
||||
try
|
||||
{
|
||||
ansiEncoding = Encoding.GetEncoding(936); // 936是GB2312的代码页
|
||||
}
|
||||
catch
|
||||
{
|
||||
ansiEncoding = Encoding.Default; // 如果获取失败,使用系统默认编码
|
||||
}
|
||||
|
||||
// 使用StreamWriter写入文件,确保编码正确应用
|
||||
using (var writer = new StreamWriter(OriginalFilePath, false, ansiEncoding))
|
||||
{
|
||||
writer.Write(reorderedDrillTape);
|
||||
writer.Flush(); // 确保所有数据都被写入
|
||||
}
|
||||
|
||||
// 4. 更新当前钻带内容
|
||||
DrillTapeContent = reorderedDrillTape;
|
||||
|
||||
// 5. 打开文件资源管理器并选中文件
|
||||
// 5. 生成刀具直径列表文件
|
||||
GenerateToolDiameterListFile(OriginalFilePath, Tools);
|
||||
|
||||
// 6. 打开文件资源管理器并选中文件
|
||||
OpenFileExplorerAndSelectFile(OriginalFilePath);
|
||||
|
||||
return reorderedDrillTape;
|
||||
@@ -481,6 +506,11 @@ namespace DrillTools
|
||||
MachineCodeType = t.MachineCodeType
|
||||
}).ToList();
|
||||
|
||||
//重新排一下 reorderedTools 的刀序Txx
|
||||
int number = 1;
|
||||
foreach (var item in reorderedTools)
|
||||
item.ToolNumber = number++;
|
||||
|
||||
//检查是否所有刀序没有变化
|
||||
int checkindex = 1;
|
||||
for (int i = 0; i < originalTools.Count; i++)
|
||||
@@ -1071,5 +1101,197 @@ M30";
|
||||
System.Windows.MessageBox.Show($"打开文件资源管理器失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成刀具直径列表文件
|
||||
/// </summary>
|
||||
/// <param name="originalFilePath">原始文件路径</param>
|
||||
/// <param name="tools">刀具列表</param>
|
||||
private void GenerateToolDiameterListFile(string originalFilePath, ObservableCollection<ToolItem> tools)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 获取原文件的目录和文件名(不含扩展名)
|
||||
string directory = Path.GetDirectoryName(originalFilePath) ?? "";
|
||||
string fileNameWithoutExt = Path.GetFileNameWithoutExtension(originalFilePath);
|
||||
string outputFilePath = Path.Combine(directory, $"{fileNameWithoutExt}-sort.txt");
|
||||
|
||||
// 按刀具编号排序获取最终顺序
|
||||
var sortedTools = tools.OrderBy(t => t.ToolNumber).ToList();
|
||||
|
||||
// 生成刀具直径列表内容
|
||||
var diameterList = new StringBuilder();
|
||||
foreach (var tool in sortedTools)
|
||||
{
|
||||
diameterList.AppendLine($"{tool.Diameter:F3}");
|
||||
}
|
||||
|
||||
// 使用ANSI编码写入文件
|
||||
Encoding ansiEncoding;
|
||||
try
|
||||
{
|
||||
ansiEncoding = Encoding.GetEncoding(936); // 936是GB2312的代码页
|
||||
}
|
||||
catch
|
||||
{
|
||||
ansiEncoding = Encoding.Default; // 如果获取失败,使用系统默认编码
|
||||
}
|
||||
|
||||
File.WriteAllText(outputFilePath, diameterList.ToString(), ansiEncoding);
|
||||
|
||||
// 记录日志
|
||||
System.Diagnostics.Debug.WriteLine($"刀具直径列表文件已生成: {outputFilePath}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 记录错误但不影响主要功能
|
||||
System.Diagnostics.Debug.WriteLine($"生成刀具直径列表文件失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查并应用-sort.txt文件中的刀具排序
|
||||
/// </summary>
|
||||
/// <param name="originalFilePath">原始文件路径</param>
|
||||
/// <param name="tools">当前刀具列表</param>
|
||||
private void CheckAndApplySortFile(string originalFilePath, ObservableCollection<ToolItem> tools)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 获取-sort.txt文件路径
|
||||
string directory = Path.GetDirectoryName(originalFilePath) ?? "";
|
||||
string fileNameWithoutExt = Path.GetFileNameWithoutExtension(originalFilePath);
|
||||
string sortFilePath = Path.Combine(directory, $"{fileNameWithoutExt}-sort.txt");
|
||||
|
||||
// 检查-sort.txt文件是否存在
|
||||
if (!File.Exists(sortFilePath))
|
||||
return;
|
||||
|
||||
// 读取-sort.txt文件内容
|
||||
List<double> sortDiameters = ReadSortFile(sortFilePath);
|
||||
if (sortDiameters.Count == 0)
|
||||
return;
|
||||
|
||||
// 检查直径数量是否一致
|
||||
if (sortDiameters.Count != tools.Count)
|
||||
return;
|
||||
|
||||
// 提示用户是否应用排序
|
||||
var result = System.Windows.MessageBox.Show(
|
||||
$"检测到之前刀序重排后的记录文件:{Path.GetFileName(sortFilePath)}\n\n" +
|
||||
$"是否直接按该记录文件进行排序?\n\n" +
|
||||
"是(Y):按记录文件排序\n" +
|
||||
"否(N):保持当前顺序",
|
||||
"刀具排序提示",
|
||||
MessageBoxButton.YesNo,
|
||||
MessageBoxImage.Question);
|
||||
|
||||
if (result == MessageBoxResult.Yes)
|
||||
{
|
||||
// 按照-sort.txt文件中的直径顺序重新排列刀具
|
||||
SortToolsByDiameterList(tools, sortDiameters);
|
||||
|
||||
// 执行完整的重排刀序流程,包括重新编号和更新钻带内容
|
||||
try
|
||||
{
|
||||
string reorderedDrillTape = ReorderAndRenumberTools();
|
||||
System.Diagnostics.Debug.WriteLine($"已按-sort.txt文件重新排序刀具并更新钻带内容");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"重排刀序失败: {ex.Message}");
|
||||
// 即使重排失败,也保持列表的排序
|
||||
OnPropertyChanged(nameof(Tools));
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 记录错误但不影响主要功能
|
||||
System.Diagnostics.Debug.WriteLine($"检查-sort.txt文件失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取-sort.txt文件内容并解析直径列表
|
||||
/// </summary>
|
||||
/// <param name="sortFilePath">-sort.txt文件路径</param>
|
||||
/// <returns>直径列表</returns>
|
||||
private List<double> ReadSortFile(string sortFilePath)
|
||||
{
|
||||
var diameters = new List<double>();
|
||||
|
||||
try
|
||||
{
|
||||
// 使用ANSI编码读取文件
|
||||
Encoding ansiEncoding;
|
||||
try
|
||||
{
|
||||
ansiEncoding = Encoding.GetEncoding(936); // 936是GB2312的代码页
|
||||
}
|
||||
catch
|
||||
{
|
||||
ansiEncoding = Encoding.Default; // 如果获取失败,使用系统默认编码
|
||||
}
|
||||
|
||||
string[] lines = File.ReadAllLines(sortFilePath, ansiEncoding);
|
||||
|
||||
foreach (string line in lines)
|
||||
{
|
||||
string trimmedLine = line.Trim();
|
||||
if (!string.IsNullOrEmpty(trimmedLine))
|
||||
{
|
||||
if (double.TryParse(trimmedLine, out double diameter))
|
||||
{
|
||||
diameters.Add(diameter);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"读取-sort.txt文件失败: {ex.Message}");
|
||||
}
|
||||
|
||||
return diameters;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据直径列表排序刀具
|
||||
/// </summary>
|
||||
/// <param name="tools">刀具列表</param>
|
||||
/// <param name="diameterList">直径列表(目标顺序)</param>
|
||||
private void SortToolsByDiameterList(ObservableCollection<ToolItem> tools, List<double> diameterList)
|
||||
{
|
||||
// 创建直径到刀具的映射
|
||||
var diameterToTools = new Dictionary<double, List<ToolItem>>();
|
||||
foreach (var tool in tools)
|
||||
{
|
||||
double roundedDiameter = Math.Round(tool.Diameter, 3);
|
||||
if (!diameterToTools.ContainsKey(roundedDiameter))
|
||||
{
|
||||
diameterToTools[roundedDiameter] = new List<ToolItem>();
|
||||
}
|
||||
diameterToTools[roundedDiameter].Add(tool);
|
||||
}
|
||||
|
||||
// 按照直径列表的顺序重新排列刀具
|
||||
var sortedTools = new List<ToolItem>();
|
||||
foreach (double diameter in diameterList)
|
||||
{
|
||||
double roundedDiameter = Math.Round(diameter, 3);
|
||||
if (diameterToTools.ContainsKey(roundedDiameter))
|
||||
{
|
||||
sortedTools.AddRange(diameterToTools[roundedDiameter]);
|
||||
}
|
||||
}
|
||||
|
||||
// 清空原列表并添加排序后的刀具
|
||||
tools.Clear();
|
||||
foreach (var tool in sortedTools)
|
||||
{
|
||||
tools.Add(tool);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user