Refactor drill tape reading and reorder logic
This commit is contained in:
@@ -594,19 +594,22 @@ namespace DrillTools
|
||||
MachineCodeType = t.MachineCodeType
|
||||
}).ToList();
|
||||
|
||||
//重新排一下 reorderedTools 的刀序Txx
|
||||
//检查是否所有刀序没有变化
|
||||
bool hasOrderChanged = originalTools.Count != reorderedTools.Count;
|
||||
for (int i = 0; i < originalTools.Count; i++)
|
||||
{
|
||||
if (hasOrderChanged)
|
||||
break;
|
||||
|
||||
hasOrderChanged = reorderedTools[i].ToolNumber != originalTools[i].ToolNumber;
|
||||
}
|
||||
|
||||
//重新排一下 reorderedTools 的刀序Txx,用于确认窗口展示
|
||||
int number = 1;
|
||||
foreach (var item in reorderedTools)
|
||||
item.ToolNumber = number++;
|
||||
|
||||
//检查是否所有刀序没有变化
|
||||
int checkindex = 1;
|
||||
for (int i = 0; i < originalTools.Count; i++)
|
||||
{
|
||||
if (reorderedTools[i].ToolNumber == originalTools[i].ToolNumber)
|
||||
checkindex++;
|
||||
}
|
||||
if (checkindex == originalTools.Count)
|
||||
if (!hasOrderChanged)
|
||||
{
|
||||
if (isApply)
|
||||
{
|
||||
@@ -648,7 +651,8 @@ namespace DrillTools
|
||||
}
|
||||
|
||||
// 3. 更新钻带内容中的刀具编号和孔位数据
|
||||
string updatedDrillTape = UpdateDrillTapeWithNewToolNumbers(DrillTapeContent, toolNumberMapping);
|
||||
string updatedDrillTape = DrillTapeProcessorExtensions.UpdateDrillTapeWithNewToolNumbers(
|
||||
DrillTapeContent, toolNumberMapping, Tools.ToList());
|
||||
|
||||
// 4. 更新钻带内容
|
||||
DrillTapeContent = updatedDrillTape;
|
||||
@@ -662,132 +666,6 @@ namespace DrillTools
|
||||
return updatedDrillTape;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 使用新的刀具编号更新钻带内容
|
||||
/// </summary>
|
||||
/// <param name="originalDrillTape">原始钻带内容</param>
|
||||
/// <param name="toolNumberMapping">刀具编号映射</param>
|
||||
/// <returns>更新后的钻带内容</returns>
|
||||
private string UpdateDrillTapeWithNewToolNumbers(string originalDrillTape, Dictionary<int, int> toolNumberMapping)
|
||||
{
|
||||
var lines = originalDrillTape.Split(new[] { '\r', '\n' }, StringSplitOptions.None);
|
||||
var result = new List<string>();
|
||||
|
||||
// 创建新编号到刀具对象的映射,以便获取对应的坐标数据
|
||||
var newNumberToToolMap = new Dictionary<int, ToolItem>();
|
||||
foreach (var tool in Tools)
|
||||
{
|
||||
if (toolNumberMapping.ContainsValue(tool.ToolNumber))
|
||||
{
|
||||
newNumberToToolMap[tool.ToolNumber] = tool;
|
||||
}
|
||||
}
|
||||
|
||||
// 首先解析原始钻带中的刀具定义行,保存参数信息
|
||||
var originalToolDefinitions = new Dictionary<int, string>();
|
||||
var headerLines = new List<string>();
|
||||
|
||||
foreach (var line in lines)
|
||||
{
|
||||
string trimmedLine = line.Trim();
|
||||
|
||||
if (trimmedLine == "%")
|
||||
{
|
||||
//headerLines.Add(line);
|
||||
break;
|
||||
}
|
||||
|
||||
// 处理刀具定义行(如 T01C1.049H05000Z+0.000S060.00F105.0U0700.0)
|
||||
if (Regex.IsMatch(trimmedLine, @"^T(\d+)C"))
|
||||
{
|
||||
var match = Regex.Match(trimmedLine, @"^T(\d+)C(.*)$");
|
||||
if (match.Success)
|
||||
{
|
||||
int originalToolNumber = int.Parse(match.Groups[1].Value);
|
||||
originalToolDefinitions[originalToolNumber] = match.Groups[2].Value;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (line != "")
|
||||
headerLines.Add(line);
|
||||
}
|
||||
}
|
||||
|
||||
// 添加头部信息
|
||||
result.AddRange(headerLines);
|
||||
|
||||
// 按照新的刀具编号顺序输出刀具定义部分
|
||||
var sortedTools = Tools.OrderBy(t => t.ToolNumber).ToList();
|
||||
foreach (var tool in sortedTools)
|
||||
{
|
||||
// 查找原始刀具编号
|
||||
int originalToolNumber = -1;
|
||||
foreach (var kvp in toolNumberMapping)
|
||||
{
|
||||
if (kvp.Value == tool.ToolNumber)
|
||||
{
|
||||
originalToolNumber = kvp.Key;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (originalToolNumber != -1 && originalToolDefinitions.ContainsKey(originalToolNumber))
|
||||
{
|
||||
string toolDef = $"T{tool.ToolNumber:D2}C{originalToolDefinitions[originalToolNumber]}";
|
||||
result.Add(toolDef);
|
||||
}
|
||||
}
|
||||
|
||||
// 添加 % 符号
|
||||
result.Add("%");
|
||||
|
||||
// 处理刀具切换行和坐标数据部分
|
||||
// 按新刀具编号顺序输出刀具切换行和对应的坐标数据
|
||||
var sortedToolsForData = Tools.OrderBy(t => t.ToolNumber).ToList();
|
||||
|
||||
foreach (var tool in sortedToolsForData)
|
||||
{
|
||||
// 添加刀具切换行
|
||||
result.Add($"T{tool.ToolNumber:D2}");
|
||||
|
||||
// 添加该刀具对应的所有坐标数据
|
||||
// HoleLocations现在应该已经保持了原始顺序
|
||||
if (tool.ToolType != ToolType.MachineCode && tool.HoleLocations != null && tool.HoleLocations.Count > 0)
|
||||
{
|
||||
foreach (var location in tool.HoleLocations)
|
||||
{
|
||||
result.Add(location);
|
||||
}
|
||||
}
|
||||
|
||||
// 如果是机台码刀具,添加机台码命令和坐标
|
||||
if (tool.ToolType == ToolType.MachineCode)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(tool.MachineCodeCommand) && !string.IsNullOrEmpty(tool.MachineCodeType))
|
||||
{
|
||||
result.Add($"{tool.MachineCodeCommand},{tool.MachineCodeType},$S $N");
|
||||
|
||||
// 添加机台码的坐标数据
|
||||
// HoleLocations现在应该已经保持了原始顺序
|
||||
if (tool.HoleLocations != null && tool.HoleLocations.Count > 0)
|
||||
{
|
||||
foreach (var location in tool.HoleLocations)
|
||||
{
|
||||
result.Add(location);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 添加结束标记
|
||||
result.Add("M30");
|
||||
|
||||
// 在M30后添加空行,符合标准钻带文件格式
|
||||
return string.Join("\r\n", result) + "\r\n";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载示例数据
|
||||
/// </summary>
|
||||
@@ -1428,34 +1306,7 @@ M30";
|
||||
|
||||
try
|
||||
{
|
||||
// 使用ANSI编码
|
||||
Encoding ansiEncoding;
|
||||
try
|
||||
{
|
||||
ansiEncoding = Encoding.GetEncoding(936); // 936是GB2312的代码页
|
||||
}
|
||||
catch
|
||||
{
|
||||
ansiEncoding = Encoding.Default; // 如果获取失败,使用系统默认编码
|
||||
}
|
||||
|
||||
// 使用 cmd 命令读取-sort.txt文件,参考钻带文件读取方法
|
||||
var process = new Process
|
||||
{
|
||||
StartInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = "cmd.exe",
|
||||
Arguments = $"/c type \"{sortFilePath}\"",
|
||||
RedirectStandardOutput = true,
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true,
|
||||
StandardOutputEncoding = ansiEncoding
|
||||
}
|
||||
};
|
||||
|
||||
process.Start();
|
||||
string fileContent = process.StandardOutput.ReadToEnd();
|
||||
process.WaitForExit();
|
||||
string fileContent = CommandTypeFileReader.ReadAllText(sortFilePath);
|
||||
|
||||
// 按行分割内容
|
||||
string[] lines = fileContent.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
@@ -1953,27 +1804,7 @@ M30";
|
||||
throw new System.IO.FileNotFoundException($"参考钻带文件不存在:{referenceFilePath}");
|
||||
}
|
||||
|
||||
// 使用与加载钻带文件相同的方式读取文件内容
|
||||
var process = new Process
|
||||
{
|
||||
StartInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = "cmd.exe",
|
||||
Arguments = $"/c type \"{referenceFilePath}\"",
|
||||
RedirectStandardOutput = true,
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true
|
||||
}
|
||||
};
|
||||
|
||||
process.Start();
|
||||
string drillTapeContent = process.StandardOutput.ReadToEnd();
|
||||
process.WaitForExit();
|
||||
|
||||
if (process.ExitCode != 0)
|
||||
{
|
||||
throw new InvalidOperationException($"读取参考钻带文件失败,退出代码:{process.ExitCode}");
|
||||
}
|
||||
string drillTapeContent = CommandTypeFileReader.ReadAllText(referenceFilePath);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(drillTapeContent))
|
||||
{
|
||||
@@ -2401,4 +2232,4 @@ M30";
|
||||
return (true, "刀具匹配验证通过", warnings);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user