Files
AohDrllTools/MainWindow.xaml.cs
2025-12-07 20:25:27 +08:00

216 lines
7.6 KiB
C#

using System;
using System.Diagnostics;
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();
// 测试修正后的重排刀序功能
// TestReorderDemo.RunDemo();
// 测试机台码孔数计算功能
//viewModel.TestMachineCodeHoleCalculation();
// 测试机台码处理修复效果
viewModel.TestMachineCodeProcessingFix();
}
/// <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 (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.ApplyToolOrderToDrillTape();
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 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);
}
}
}