Files
AohDrllTools/App.xaml.cs
Mr.Xia ceb88c216a feat: 在启动功能选择窗口中显示基础信息
在 StartupSelectionWindow 顶部添加基础信息展示模块,
显示文件名、最小钻咀、最小槽刀、最小EA刀直径,
与 MainWindow 中的基础信息面板布局一致。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-23 12:23:28 +08:00

243 lines
8.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
namespace DrillTools
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : System.Windows.Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
string? filePath = null;
if (e.Args.Length > 0)
{
filePath = e.Args[0];
if (!IsValidDrillTapeFile(filePath))
filePath = null;
}
if (filePath == null)
{
ShowMainWindow(null);
return;
}
// 防止选择窗口关闭后触发 OnLastWindowClose 导致应用退出
ShutdownMode = ShutdownMode.OnExplicitShutdown;
bool canClearParameters = CanClearDrillTapeParameters(filePath);
bool canGeneratePpDrillTape = CanGeneratePpDrillTape(filePath);
GetMinDiameters(filePath, out double minDrill, out double minSlot, out double minEA);
var selectionWindow = new StartupSelectionWindow(filePath, canClearParameters, canGeneratePpDrillTape, minDrill, minSlot, minEA);
selectionWindow.ShowDialog();
switch (selectionWindow.SelectedAction)
{
case StartupAction.AdjustToolOrder:
ShutdownMode = ShutdownMode.OnLastWindowClose;
ShowMainWindow(filePath);
break;
case StartupAction.ExportHoleCount:
PerformHeadlessExport(filePath);
Shutdown();
break;
case StartupAction.ClearParameters:
PerformParameterCleanup(filePath);
Shutdown();
break;
case StartupAction.GeneratePpDrillTape:
PerformPpDrillTapeGeneration(filePath);
Shutdown();
break;
default:
Shutdown();
break;
}
}
private static void ShowMainWindow(string? filePath)
{
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}",
"启动失败", MessageBoxButton.OK, MessageBoxImage.Error);
throw;
}
}
private static void PerformHeadlessExport(string filePath)
{
try
{
var viewModel = new MainWindowViewModel
{
ShouldCheckSortFileOnLoad = false
};
string content = CommandTypeFileReader.ReadAllText(filePath);
viewModel.OriginalFilePath = filePath;
viewModel.LoadToolsFromDrillTape(content);
if (viewModel.Tools.Count == 0)
{
System.Windows.MessageBox.Show("钻带文件中未找到有效的刀具数据,无法导出孔数报表。",
"提示", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
viewModel.ExportDrillUsageReport();
}
catch (OperationCanceledException)
{
}
catch (Exception ex)
{
System.Windows.MessageBox.Show($"导出孔数报表失败:\n{ex.Message}",
"错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private static bool CanClearDrillTapeParameters(string filePath)
{
try
{
string content = CommandTypeFileReader.ReadAllText(filePath);
return DrillTapeParameterCleaner.CanClearParameters(content);
}
catch
{
return false;
}
}
private static bool CanGeneratePpDrillTape(string filePath)
{
try
{
var viewModel = new MainWindowViewModel
{
IsStartupDrillTapeFile = true,
OriginalFilePath = filePath,
ShouldCheckSortFileOnLoad = false
};
string content = CommandTypeFileReader.ReadAllText(filePath);
viewModel.LoadToolsFromDrillTape(content);
return viewModel.CanGeneratePpDrillTape;
}
catch
{
return false;
}
}
private static void GetMinDiameters(string filePath, out double minDrillDiameter, out double minSlotDiameter, out double minEADiameter)
{
minDrillDiameter = 0;
minSlotDiameter = 0;
minEADiameter = 0;
try
{
var viewModel = new MainWindowViewModel
{
ShouldCheckSortFileOnLoad = false
};
string content = CommandTypeFileReader.ReadAllText(filePath);
viewModel.LoadToolsFromDrillTape(content);
if (viewModel.Tools.Count > 0)
{
minDrillDiameter = viewModel.MinDrillDiameter;
minSlotDiameter = viewModel.MinSlotDiameter;
minEADiameter = viewModel.MinEADiameter;
}
}
catch
{
}
}
private static void PerformPpDrillTapeGeneration(string filePath)
{
try
{
var viewModel = new MainWindowViewModel
{
IsStartupDrillTapeFile = true,
OriginalFilePath = filePath,
ShouldCheckSortFileOnLoad = false
};
string content = CommandTypeFileReader.ReadAllText(filePath);
viewModel.LoadToolsFromDrillTape(content);
string outputFilePath = viewModel.GeneratePpDrillTape();
System.Windows.MessageBox.Show(
$"PP钻带已生成\n{outputFilePath}",
"生成PP钻带完成",
MessageBoxButton.OK,
MessageBoxImage.Information);
}
catch (Exception ex)
{
System.Windows.MessageBox.Show($"生成PP钻带失败\n{ex.Message}",
"错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private static void PerformParameterCleanup(string filePath)
{
try
{
DrillTapeParameterCleaner.ClearParametersAndSave(filePath);
System.Windows.MessageBox.Show(
$"参数已清空,原文件已备份为:\n{Path.GetFileName(filePath)}.bak",
"清空参数完成",
MessageBoxButton.OK,
MessageBoxImage.Information);
}
catch (Exception ex)
{
System.Windows.MessageBox.Show($"清空参数失败:\n{ex.Message}",
"错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
/// <summary>
/// 验证是否为有效的钻带文件
/// </summary>
/// <param name="filePath">文件路径</param>
/// <returns>是否为有效钻带文件</returns>
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);
}
}
}