diff --git a/App.xaml b/App.xaml index 8c2d638..4736da5 100644 --- a/App.xaml +++ b/App.xaml @@ -2,8 +2,7 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:DrillTools" - xmlns:sys="clr-namespace:System;assembly=mscorlib" - StartupUri="MainWindow.xaml"> + xmlns:sys="clr-namespace:System;assembly=mscorlib"> diff --git a/App.xaml.cs b/App.xaml.cs index 09bd235..1e9e64e 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -1,5 +1,7 @@ using System.Configuration; using System.Data; +using System.IO; +using System.Linq; using System.Windows; namespace DrillTools @@ -16,9 +18,49 @@ namespace DrillTools // 运行孔位数据功能测试 //MainWindowViewModel.TestHoleLocationsFunctionality(); - // 创建并显示主窗口 - //MainWindow mainWindow = new MainWindow(); - //mainWindow.Show(); + // 检查是否有命令行参数(拖拽文件到exe上) + string? filePath = null; + if (e.Args.Length > 0) + { + // 获取第一个参数作为文件路径 + filePath = e.Args[0]; + // 验证文件是否存在且为有效的钻带文件 + if (!IsValidDrillTapeFile(filePath)) + filePath = null; // 重置为null,按正常启动流程 + } + + try + { + MainWindow mainWindow = new MainWindow(filePath); + mainWindow.Show(); + } + catch (Exception ex) + { + // 显示错误消息 + System.Windows.MessageBox.Show($"创建MainWindow时发生异常:\n{ex.GetType().Name} - {ex.Message}\n\n{ex.StackTrace}", + "启动失败", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error); + throw; + } + } + + /// + /// 验证是否为有效的钻带文件 + /// + /// 文件路径 + /// 是否为有效钻带文件 + private static bool IsValidDrillTapeFile(string filePath) + { + if (string.IsNullOrEmpty(filePath)) + return false; + + if (!File.Exists(filePath)) + return false; + + // 检查文件扩展名是否为支持的钻带文件格式 + string extension = Path.GetExtension(filePath).ToLowerInvariant(); + string[] supportedExtensions = { ".txt", ".drl", ".dr2", ".dpin" }; + + return supportedExtensions.Contains(extension); } } } \ No newline at end of file diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 5c56ac3..031845b 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -13,15 +13,117 @@ namespace DrillTools { private MainWindowViewModel ViewModel => (MainWindowViewModel)DataContext; - public MainWindow() + public MainWindow(string? initialFilePath = null) { - InitializeComponent(); - var viewModel = new MainWindowViewModel(); - DataContext = viewModel; - InitializeDragDrop(); + //System.Diagnostics.Debug.WriteLine("=== MainWindow构造函数开始执行 ==="); + //System.Diagnostics.Debug.WriteLine($"参数 initialFilePath: {initialFilePath ?? "null"}"); - // 设置默认置顶状态 - this.Topmost = viewModel.IsTopmost; + try + { + //System.Diagnostics.Debug.WriteLine("调用 InitializeComponent()..."); + InitializeComponent(); + //System.Diagnostics.Debug.WriteLine("InitializeComponent() 执行完成"); + + //System.Diagnostics.Debug.WriteLine("创建 MainWindowViewModel..."); + var viewModel = new MainWindowViewModel(); + //System.Diagnostics.Debug.WriteLine("MainWindowViewModel 创建完成"); + + //System.Diagnostics.Debug.WriteLine("设置 DataContext..."); + DataContext = viewModel; + //System.Diagnostics.Debug.WriteLine("DataContext 设置完成"); + + //System.Diagnostics.Debug.WriteLine("初始化拖放功能..."); + InitializeDragDrop(); + //System.Diagnostics.Debug.WriteLine("拖放功能初始化完成"); + + // 设置默认置顶状态 + this.Topmost = viewModel.IsTopmost; + //System.Diagnostics.Debug.WriteLine("置顶状态设置完成"); + + // 如果有初始文件路径,在窗口加载完成后自动加载 + if (!string.IsNullOrEmpty(initialFilePath)) + { + //System.Diagnostics.Debug.WriteLine("注册文件加载事件..."); + this.Loaded += (sender, e) => LoadInitialFile(initialFilePath, viewModel); + //System.Diagnostics.Debug.WriteLine("文件加载事件注册完成"); + } + + //System.Diagnostics.Debug.WriteLine("=== MainWindow构造函数执行完成 ==="); + } + catch (Exception ex) + { + //System.Diagnostics.Debug.WriteLine($"MainWindow构造函数中发生异常: {ex.GetType().Name} - {ex.Message}"); + //System.Diagnostics.Debug.WriteLine($"异常堆栈: {ex.StackTrace}"); + throw; + } + } + + /// + /// 加载初始文件 + /// + /// 文件路径 + /// 视图模型 + private async void LoadInitialFile(string filePath, MainWindowViewModel viewModel) + { + try + { + // 异步加载文件以避免阻塞UI + await System.Threading.Tasks.Task.Run(() => + { + // 使用与现有代码相同的方式读取文件 + var process = new System.Diagnostics.Process + { + StartInfo = new System.Diagnostics.ProcessStartInfo + { + FileName = "cmd.exe", + Arguments = $"/c type \"{filePath}\"", + RedirectStandardOutput = true, + UseShellExecute = false, + CreateNoWindow = true + } + }; + + process.Start(); + string drillTapeContent = process.StandardOutput.ReadToEnd(); + process.WaitForExit(); + + // 在UI线程中更新界面 + this.Dispatcher.Invoke(() => + { + try + { + // 保存原始文件路径 + viewModel.OriginalFilePath = filePath; + + // 加载钻带内容 + viewModel.LoadToolsFromDrillTape(drillTapeContent); + + // 显示成功提示 + //System.Windows.MessageBox.Show( + // $"已成功加载钻带文件:\n{System.IO.Path.GetFileName(filePath)}\n\n刀具数量:{viewModel.Tools.Count}把", + // "文件加载完成", + // System.Windows.MessageBoxButton.OK, + // System.Windows.MessageBoxImage.Information); + } + catch (Exception ex) + { + System.Windows.MessageBox.Show( + $"加载钻带文件失败:\n{ex.Message}", + "错误", + System.Windows.MessageBoxButton.OK, + System.Windows.MessageBoxImage.Error); + } + }); + }); + } + catch (Exception ex) + { + System.Windows.MessageBox.Show( + $"读取文件时发生错误:\n{ex.Message}", + "错误", + System.Windows.MessageBoxButton.OK, + System.Windows.MessageBoxImage.Error); + } } /// @@ -98,7 +200,7 @@ namespace DrillTools catch (OperationCanceledException) { // 用户取消操作,不显示错误消息 - System.Diagnostics.Debug.WriteLine("用户取消了刀序重排操作"); + //System.Diagnostics.Debug.WriteLine("用户取消了刀序重排操作"); } catch (Exception ex) { @@ -120,7 +222,7 @@ namespace DrillTools catch (OperationCanceledException) { // 用户取消操作,不显示错误消息 - System.Diagnostics.Debug.WriteLine("用户取消了保存操作"); + //System.Diagnostics.Debug.WriteLine("用户取消了保存操作"); } catch (Exception ex) { @@ -141,7 +243,7 @@ namespace DrillTools catch (OperationCanceledException) { // 用户取消操作,不显示错误消息 - System.Diagnostics.Debug.WriteLine("用户取消了生成排序种子操作"); + //System.Diagnostics.Debug.WriteLine("用户取消了生成排序种子操作"); } catch (Exception ex) {