Files
AohDrllTools/MainWindow.xaml.cs
Mr.Xia 0eab0f42ee 钻带解析顺序与格式优化,界面信息增强
本次更新聚焦于钻带文件解析顺序与格式的准确还原,提升了界面基础信息展示,并优化了相关数据结构和辅助方法。主要包括:
- 钻带孔位逐行顺序解析,完整保留原始坐标字符串格式,避免顺序错乱和格式丢失。
- 界面新增基础信息分组,自动统计并展示最小钻咀、槽刀、EA刀直径。
- 数据结构如Point2D等增加运算符重载和构造函数,便于几何计算。
- 机台码(0.499刀具)坐标行顺序提取及正则健壮性提升。
- 移除强制编码指定,提升跨平台兼容性。
- 清理冗余测试代码,更新示例/测试钻带文件内容。
- 新增《必读.md》,明确AI开发不需编写测试单元。
本次无其他功能或逻辑变动的占位diff。
2025-12-22 16:50:42 +08:00

225 lines
7.9 KiB
C#

using System;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
namespace DrillTools
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private MainWindowViewModel ViewModel => (MainWindowViewModel)DataContext;
public MainWindow()
{
InitializeComponent();
var viewModel = new MainWindowViewModel();
DataContext = viewModel;
InitializeDragDrop();
// 设置默认置顶状态
this.Topmost = viewModel.IsTopmost;
}
/// <summary>
/// 初始化拖放功能
/// </summary>
private void InitializeDragDrop()
{
DragDropHelper.EnableDragDrop<ToolItem>(ToolsListView, InsertionIndicator);
}
/// <summary>
/// 加载示例数据按钮点击事件
/// </summary>
private void LoadSampleDataButton_Click(object sender, RoutedEventArgs e)
{
ViewModel.LoadSampleData();
}
/// <summary>
/// 加载钻带文件按钮点击事件
/// </summary>
private void LoadDrillTapeButton_Click(object sender, RoutedEventArgs e)
{
var openFileDialog = new Microsoft.Win32.OpenFileDialog
{
Filter = "钻带文件 (*.txt;*.drl;*.dr2;*.dpin)|*.txt;*.drl;*.dr2;*.dpin|所有文件 (*.*)|*.*",
Title = "选择钻带文件"
};
if (openFileDialog.ShowDialog() == true)
{
try
{
// 保存原始文件路径
ViewModel.OriginalFilePath = openFileDialog.FileName;
// 使用 cmd 命令读取加密钻带文件
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = $"/c type \"{openFileDialog.FileName}\"",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
}
};
process.Start();
string drillTapeContent = process.StandardOutput.ReadToEnd();
process.WaitForExit();
ViewModel.LoadToolsFromDrillTape(drillTapeContent);
}
catch (Exception ex)
{
System.Windows.MessageBox.Show($"加载钻带文件失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
/// <summary>
/// 重排刀序按钮点击事件
/// </summary>
private void ReorderToolsButton_Click(object sender, RoutedEventArgs e)
{
try
{
string reorderedDrillTape = ViewModel.ReorderAndRenumberTools();
ViewModel.DrillTapeContent = reorderedDrillTape;
System.Windows.MessageBox.Show("刀序重排完成", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
}
catch (OperationCanceledException)
{
// 用户取消操作,不显示错误消息
System.Diagnostics.Debug.WriteLine("用户取消了刀序重排操作");
}
catch (Exception ex)
{
System.Windows.MessageBox.Show($"重排刀序失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
/// <summary>
/// 应用顺序到钻带按钮点击事件
/// </summary>
private void ApplyOrderButton_Click(object sender, RoutedEventArgs e)
{
try
{
string reorderedDrillTape = ViewModel.ReorderAndRenumberTools(true);
ViewModel.DrillTapeContent = reorderedDrillTape;
System.Windows.MessageBox.Show("刀具顺序已应用到钻带", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
}
catch (OperationCanceledException)
{
// 用户取消操作,不显示错误消息
System.Diagnostics.Debug.WriteLine("用户取消了保存操作");
}
catch (Exception ex)
{
System.Windows.MessageBox.Show($"应用刀具顺序失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
/// <summary>
/// 上移按钮点击事件
/// </summary>
private void MoveUpButton_Click(object sender, RoutedEventArgs e)
{
ViewModel.MoveSelectedToolUp();
}
/// <summary>
/// 下移按钮点击事件
/// </summary>
private void MoveDownButton_Click(object sender, RoutedEventArgs e)
{
ViewModel.MoveSelectedToolDown();
}
/// <summary>
/// 置顶按钮点击事件
/// </summary>
private void ToggleTopmostButton_Click(object sender, RoutedEventArgs e)
{
ViewModel.ToggleTopmost();
this.Topmost = ViewModel.IsTopmost;
}
/// <summary>
/// 刀具列表双击事件
/// </summary>
private void ToolsListView_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (sender is System.Windows.Controls.ListView listView && listView.SelectedItem is ToolItem selectedTool)
{
ViewModel.ShowToolDetail(selectedTool);
}
}
/// <summary>
/// 拖放文件进入窗口事件
/// </summary>
protected override void OnDragEnter(System.Windows.DragEventArgs e)
{
if (e.Data.GetDataPresent(System.Windows.DataFormats.FileDrop))
{
e.Effects = System.Windows.DragDropEffects.Copy;
}
else
{
e.Effects = System.Windows.DragDropEffects.None;
}
base.OnDragEnter(e);
}
/// <summary>
/// 拖放文件到窗口事件
/// </summary>
protected override void OnDrop(System.Windows.DragEventArgs e)
{
if (e.Data.GetDataPresent(System.Windows.DataFormats.FileDrop))
{
string[] files = (string[])e.Data.GetData(System.Windows.DataFormats.FileDrop);
if (files.Length > 0)
{
try
{
// 保存原始文件路径
ViewModel.OriginalFilePath = files[0];
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = $"/c type \"{files[0]}\"",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
}
};
process.Start();
string drillTapeContent = process.StandardOutput.ReadToEnd();
process.WaitForExit();
ViewModel.LoadToolsFromDrillTape(drillTapeContent);
}
catch (Exception ex)
{
System.Windows.MessageBox.Show($"加载钻带文件失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
base.OnDrop(e);
}
}
}