Add startup PP drill tape generation
This commit is contained in:
54
App.xaml.cs
54
App.xaml.cs
@@ -35,7 +35,8 @@ namespace DrillTools
|
|||||||
ShutdownMode = ShutdownMode.OnExplicitShutdown;
|
ShutdownMode = ShutdownMode.OnExplicitShutdown;
|
||||||
|
|
||||||
bool canClearParameters = CanClearDrillTapeParameters(filePath);
|
bool canClearParameters = CanClearDrillTapeParameters(filePath);
|
||||||
var selectionWindow = new StartupSelectionWindow(filePath, canClearParameters);
|
bool canGeneratePpDrillTape = CanGeneratePpDrillTape(filePath);
|
||||||
|
var selectionWindow = new StartupSelectionWindow(filePath, canClearParameters, canGeneratePpDrillTape);
|
||||||
selectionWindow.ShowDialog();
|
selectionWindow.ShowDialog();
|
||||||
|
|
||||||
switch (selectionWindow.SelectedAction)
|
switch (selectionWindow.SelectedAction)
|
||||||
@@ -52,6 +53,10 @@ namespace DrillTools
|
|||||||
PerformParameterCleanup(filePath);
|
PerformParameterCleanup(filePath);
|
||||||
Shutdown();
|
Shutdown();
|
||||||
break;
|
break;
|
||||||
|
case StartupAction.GeneratePpDrillTape:
|
||||||
|
PerformPpDrillTapeGeneration(filePath);
|
||||||
|
Shutdown();
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
Shutdown();
|
Shutdown();
|
||||||
break;
|
break;
|
||||||
@@ -115,6 +120,53 @@ namespace DrillTools
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static bool CanGeneratePpDrillTape(string filePath)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var viewModel = new MainWindowViewModel
|
||||||
|
{
|
||||||
|
IsStartupDrillTapeFile = true,
|
||||||
|
OriginalFilePath = filePath
|
||||||
|
};
|
||||||
|
|
||||||
|
string content = CommandTypeFileReader.ReadAllText(filePath);
|
||||||
|
viewModel.LoadToolsFromDrillTape(content);
|
||||||
|
return viewModel.CanGeneratePpDrillTape;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void PerformPpDrillTapeGeneration(string filePath)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var viewModel = new MainWindowViewModel
|
||||||
|
{
|
||||||
|
IsStartupDrillTapeFile = true,
|
||||||
|
OriginalFilePath = filePath
|
||||||
|
};
|
||||||
|
|
||||||
|
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)
|
private static void PerformParameterCleanup(string filePath)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
|||||||
@@ -64,6 +64,12 @@
|
|||||||
Click="ApplyOrderButton_Click"
|
Click="ApplyOrderButton_Click"
|
||||||
Content="应用并保存"
|
Content="应用并保存"
|
||||||
IsEnabled="{Binding HasOriginalFile}" />
|
IsEnabled="{Binding HasOriginalFile}" />
|
||||||
|
<Button
|
||||||
|
Name="GeneratePpDrillTapeButton"
|
||||||
|
Click="GeneratePpDrillTapeButton_Click"
|
||||||
|
Content="生成PP钻带"
|
||||||
|
IsEnabled="{Binding CanGeneratePpDrillTape}"
|
||||||
|
Visibility="{Binding CanGeneratePpDrillTape, Converter={StaticResource BooleanToVisibilityConverter}}" />
|
||||||
<Button
|
<Button
|
||||||
Name="ToggleTopmostButton"
|
Name="ToggleTopmostButton"
|
||||||
Click="ToggleTopmostButton_Click"
|
Click="ToggleTopmostButton_Click"
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ namespace DrillTools
|
|||||||
//System.Diagnostics.Debug.WriteLine("创建 MainWindowViewModel...");
|
//System.Diagnostics.Debug.WriteLine("创建 MainWindowViewModel...");
|
||||||
var viewModel = new MainWindowViewModel();
|
var viewModel = new MainWindowViewModel();
|
||||||
//System.Diagnostics.Debug.WriteLine("MainWindowViewModel 创建完成");
|
//System.Diagnostics.Debug.WriteLine("MainWindowViewModel 创建完成");
|
||||||
|
viewModel.IsStartupDrillTapeFile = !string.IsNullOrEmpty(initialFilePath);
|
||||||
|
|
||||||
//System.Diagnostics.Debug.WriteLine("设置 DataContext...");
|
//System.Diagnostics.Debug.WriteLine("设置 DataContext...");
|
||||||
DataContext = viewModel;
|
DataContext = viewModel;
|
||||||
@@ -76,6 +77,7 @@ namespace DrillTools
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
// 保存原始文件路径
|
// 保存原始文件路径
|
||||||
|
viewModel.IsStartupDrillTapeFile = true;
|
||||||
viewModel.OriginalFilePath = filePath;
|
viewModel.OriginalFilePath = filePath;
|
||||||
|
|
||||||
// 加载钻带内容
|
// 加载钻带内容
|
||||||
@@ -141,6 +143,7 @@ namespace DrillTools
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
// 保存原始文件路径
|
// 保存原始文件路径
|
||||||
|
ViewModel.IsStartupDrillTapeFile = false;
|
||||||
ViewModel.OriginalFilePath = openFileDialog.FileName;
|
ViewModel.OriginalFilePath = openFileDialog.FileName;
|
||||||
|
|
||||||
string drillTapeContent = CommandTypeFileReader.ReadAllText(openFileDialog.FileName);
|
string drillTapeContent = CommandTypeFileReader.ReadAllText(openFileDialog.FileName);
|
||||||
@@ -217,6 +220,22 @@ namespace DrillTools
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 生成 PP 钻带按钮点击事件
|
||||||
|
/// </summary>
|
||||||
|
private void GeneratePpDrillTapeButton_Click(object sender, RoutedEventArgs e)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string outputFilePath = ViewModel.GeneratePpDrillTape();
|
||||||
|
System.Windows.MessageBox.Show($"PP钻带已生成:\n{outputFilePath}", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
System.Windows.MessageBox.Show($"生成PP钻带失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 生成排序种子按钮点击事件
|
/// 生成排序种子按钮点击事件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -327,6 +346,7 @@ namespace DrillTools
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
// 保存原始文件路径
|
// 保存原始文件路径
|
||||||
|
ViewModel.IsStartupDrillTapeFile = false;
|
||||||
ViewModel.OriginalFilePath = files[0];
|
ViewModel.OriginalFilePath = files[0];
|
||||||
|
|
||||||
string drillTapeContent = CommandTypeFileReader.ReadAllText(files[0]);
|
string drillTapeContent = CommandTypeFileReader.ReadAllText(files[0]);
|
||||||
|
|||||||
@@ -30,6 +30,8 @@ namespace DrillTools
|
|||||||
private double _minDrillDiameter;
|
private double _minDrillDiameter;
|
||||||
private double _minSlotDiameter;
|
private double _minSlotDiameter;
|
||||||
private double _minEADiameter;
|
private double _minEADiameter;
|
||||||
|
private bool _isStartupDrillTapeFile;
|
||||||
|
private bool _canGeneratePpDrillTape;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 刀具列表
|
/// 刀具列表
|
||||||
@@ -104,6 +106,8 @@ namespace DrillTools
|
|||||||
{
|
{
|
||||||
FileNameWithoutExtension = string.Empty;
|
FileNameWithoutExtension = string.Empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
UpdatePpDrillTapeState();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -122,6 +126,30 @@ namespace DrillTools
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public bool HasOriginalFile => !string.IsNullOrEmpty(OriginalFilePath);
|
public bool HasOriginalFile => !string.IsNullOrEmpty(OriginalFilePath);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 当前文件是否来自启动参数
|
||||||
|
/// </summary>
|
||||||
|
public bool IsStartupDrillTapeFile
|
||||||
|
{
|
||||||
|
get => _isStartupDrillTapeFile;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (SetProperty(ref _isStartupDrillTapeFile, value))
|
||||||
|
{
|
||||||
|
UpdatePpDrillTapeState();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否可以生成 PP 钻带
|
||||||
|
/// </summary>
|
||||||
|
public bool CanGeneratePpDrillTape
|
||||||
|
{
|
||||||
|
get => _canGeneratePpDrillTape;
|
||||||
|
private set => SetProperty(ref _canGeneratePpDrillTape, value);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 窗口是否置顶
|
/// 窗口是否置顶
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -291,15 +319,19 @@ namespace DrillTools
|
|||||||
{
|
{
|
||||||
CheckAndApplySortFile(OriginalFilePath, Tools);
|
CheckAndApplySortFile(OriginalFilePath, Tools);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
UpdatePpDrillTapeState();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
System.Windows.MessageBox.Show($"解析钻带失败: {result.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
System.Windows.MessageBox.Show($"解析钻带失败: {result.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||||
|
UpdatePpDrillTapeState();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
System.Windows.MessageBox.Show($"解析钻带时发生异常: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
System.Windows.MessageBox.Show($"解析钻带时发生异常: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||||
|
UpdatePpDrillTapeState();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1081,6 +1113,82 @@ M30";
|
|||||||
OpenFileExplorerAndSelectFile(outputFilePath);
|
OpenFileExplorerAndSelectFile(outputFilePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 生成 PP 钻带文件
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>生成的文件路径</returns>
|
||||||
|
public string GeneratePpDrillTape()
|
||||||
|
{
|
||||||
|
var sourceTool = GetPpSourceTool();
|
||||||
|
if (sourceTool == null)
|
||||||
|
throw new InvalidOperationException("当前钻带不满足生成 PP 钻带条件");
|
||||||
|
|
||||||
|
var locations = sourceTool.HoleLocations
|
||||||
|
.Where(location => !string.IsNullOrWhiteSpace(location))
|
||||||
|
.Take(2)
|
||||||
|
.Select(location => location.Trim())
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
if (locations.Count < 2)
|
||||||
|
throw new InvalidOperationException("3.203 刀具坐标不足,无法生成 PP 钻带");
|
||||||
|
|
||||||
|
string outputFilePath = GetPpDrillTapeOutputPath();
|
||||||
|
var ppDrillTape = new StringBuilder();
|
||||||
|
ppDrillTape.AppendLine("M48");
|
||||||
|
ppDrillTape.AppendLine("T01C4.");
|
||||||
|
ppDrillTape.AppendLine("%");
|
||||||
|
ppDrillTape.AppendLine("T01");
|
||||||
|
ppDrillTape.AppendLine(locations[0]);
|
||||||
|
ppDrillTape.AppendLine(locations[1]);
|
||||||
|
ppDrillTape.AppendLine("M30");
|
||||||
|
|
||||||
|
File.WriteAllText(outputFilePath, ppDrillTape.ToString(), CreateGb2312Encoding());
|
||||||
|
OpenFileExplorerAndSelectFile(outputFilePath);
|
||||||
|
|
||||||
|
return outputFilePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void UpdatePpDrillTapeState()
|
||||||
|
{
|
||||||
|
CanGeneratePpDrillTape = GetPpSourceTool() != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ToolItem? GetPpSourceTool()
|
||||||
|
{
|
||||||
|
if (!IsStartupDrillTapeFile || !HasOriginalFile || OriginalTools.Count < 2)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
var sourceTool = OriginalTools[OriginalTools.Count - 2];
|
||||||
|
if (Math.Abs(sourceTool.Diameter - 3.203) >= 0.001)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
if (sourceTool.TotalHoles != 3)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
if (sourceTool.HoleLocations == null || sourceTool.HoleLocations.Count < 2)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
return sourceTool;
|
||||||
|
}
|
||||||
|
|
||||||
|
private string GetPpDrillTapeOutputPath()
|
||||||
|
{
|
||||||
|
string directory = Path.GetDirectoryName(OriginalFilePath) ?? string.Empty;
|
||||||
|
string partNumber = ExtractPpPartNumber(FileNameWithoutExtension);
|
||||||
|
return Path.Combine(directory, $"{partNumber}-pp.drl");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string ExtractPpPartNumber(string fileNameWithoutExtension)
|
||||||
|
{
|
||||||
|
var parts = fileNameWithoutExtension
|
||||||
|
.Split(new[] { '-' }, StringSplitOptions.RemoveEmptyEntries)
|
||||||
|
.Where(part => !part.Equals("cs", StringComparison.OrdinalIgnoreCase)
|
||||||
|
&& !part.Equals("jp", StringComparison.OrdinalIgnoreCase))
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
return parts.Count > 0 ? string.Join("-", parts) : fileNameWithoutExtension;
|
||||||
|
}
|
||||||
|
|
||||||
private static void ConfirmOverwriteReport(string outputFilePath)
|
private static void ConfirmOverwriteReport(string outputFilePath)
|
||||||
{
|
{
|
||||||
if (!File.Exists(outputFilePath))
|
if (!File.Exists(outputFilePath))
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
Title="选择功能"
|
Title="选择功能"
|
||||||
Width="430"
|
Width="570"
|
||||||
SizeToContent="Height"
|
SizeToContent="Height"
|
||||||
ResizeMode="NoResize"
|
ResizeMode="NoResize"
|
||||||
WindowStartupLocation="CenterScreen">
|
WindowStartupLocation="CenterScreen">
|
||||||
@@ -19,7 +19,8 @@
|
|||||||
<StackPanel Grid.Row="2" HorizontalAlignment="Center" Orientation="Horizontal">
|
<StackPanel Grid.Row="2" HorizontalAlignment="Center" Orientation="Horizontal">
|
||||||
<Button Width="120" Height="30" Content="调整刀序" Margin="0,0,15,0" Click="AdjustToolOrder_Click"/>
|
<Button Width="120" Height="30" Content="调整刀序" Margin="0,0,15,0" Click="AdjustToolOrder_Click"/>
|
||||||
<Button Width="120" Height="30" Content="导出孔数" Margin="0,0,15,0" Click="ExportHoleCount_Click"/>
|
<Button Width="120" Height="30" Content="导出孔数" Margin="0,0,15,0" Click="ExportHoleCount_Click"/>
|
||||||
<Button Name="ClearParametersButton" Width="120" Height="30" Content="清空参数" Click="ClearParameters_Click"/>
|
<Button Name="ClearParametersButton" Width="120" Height="30" Content="清空参数" Margin="0,0,15,0" Click="ClearParameters_Click"/>
|
||||||
|
<Button Name="GeneratePpDrillTapeButton" Width="120" Height="30" Content="生成PP钻带" Click="GeneratePpDrillTape_Click"/>
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
</Grid>
|
</Grid>
|
||||||
</Window>
|
</Window>
|
||||||
|
|||||||
@@ -8,18 +8,20 @@ namespace DrillTools
|
|||||||
None,
|
None,
|
||||||
AdjustToolOrder,
|
AdjustToolOrder,
|
||||||
ExportHoleCount,
|
ExportHoleCount,
|
||||||
ClearParameters
|
ClearParameters,
|
||||||
|
GeneratePpDrillTape
|
||||||
}
|
}
|
||||||
|
|
||||||
public partial class StartupSelectionWindow : Window
|
public partial class StartupSelectionWindow : Window
|
||||||
{
|
{
|
||||||
public StartupAction SelectedAction { get; private set; } = StartupAction.None;
|
public StartupAction SelectedAction { get; private set; } = StartupAction.None;
|
||||||
|
|
||||||
public StartupSelectionWindow(string filePath, bool canClearParameters = false)
|
public StartupSelectionWindow(string filePath, bool canClearParameters = false, bool canGeneratePpDrillTape = false)
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
DataContext = new { FileName = Path.GetFileName(filePath) };
|
DataContext = new { FileName = Path.GetFileName(filePath) };
|
||||||
ClearParametersButton.Visibility = canClearParameters ? Visibility.Visible : Visibility.Collapsed;
|
ClearParametersButton.Visibility = canClearParameters ? Visibility.Visible : Visibility.Collapsed;
|
||||||
|
GeneratePpDrillTapeButton.Visibility = canGeneratePpDrillTape ? Visibility.Visible : Visibility.Collapsed;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void AdjustToolOrder_Click(object sender, RoutedEventArgs e)
|
private void AdjustToolOrder_Click(object sender, RoutedEventArgs e)
|
||||||
@@ -39,5 +41,11 @@ namespace DrillTools
|
|||||||
SelectedAction = StartupAction.ClearParameters;
|
SelectedAction = StartupAction.ClearParameters;
|
||||||
Close();
|
Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void GeneratePpDrillTape_Click(object sender, RoutedEventArgs e)
|
||||||
|
{
|
||||||
|
SelectedAction = StartupAction.GeneratePpDrillTape;
|
||||||
|
Close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user