diff --git a/UnitTests/Core/PathHelperTests.cs b/UnitTests/Core/PathHelperTests.cs index 36bf856..9949ea5 100644 --- a/UnitTests/Core/PathHelperTests.cs +++ b/UnitTests/Core/PathHelperTests.cs @@ -24,5 +24,15 @@ namespace NavisworksTransport.UnitTests.Core Assert.AreEqual("副本_我的路径", duplicatedName); } + + [TestMethod] + public void GenerateCollisionReportFileName_ShouldUseUnifiedChinesePrefixAndTimestamp() + { + var now = new DateTime(2026, 4, 9, 23, 55, 1); + + var fileName = PathHelper.GenerateCollisionReportFileName("人工_0409_235000", "html", now); + + Assert.AreEqual("碰撞检测报告_人工_0409_235000_20260409_235501.html", fileName); + } } } diff --git a/src/Commands/GenerateCollisionReportCommand.cs b/src/Commands/GenerateCollisionReportCommand.cs index 1eb3f4e..181520f 100644 --- a/src/Commands/GenerateCollisionReportCommand.cs +++ b/src/Commands/GenerateCollisionReportCommand.cs @@ -602,19 +602,21 @@ namespace NavisworksTransport.Commands ShowReport(result); } - // 🔥 自动导出HTML报告 - try + if (!_parameters.ShowDialog) { - string htmlPath = Utils.CollisionReportHtmlGenerator.AutoSaveHtmlReport(result); - if (!string.IsNullOrEmpty(htmlPath)) + try { - LogManager.Info($"HTML报告已自动导出: {htmlPath}"); + string htmlPath = Utils.CollisionReportHtmlGenerator.AutoSaveHtmlReport(result); + if (!string.IsNullOrEmpty(htmlPath)) + { + LogManager.Info($"HTML报告已自动导出: {htmlPath}"); + } + } + catch (Exception ex) + { + LogManager.Error($"自动导出HTML报告失败: {ex.Message}"); + // HTML导出失败不影响报告生成 } - } - catch (Exception ex) - { - LogManager.Error($"自动导出HTML报告失败: {ex.Message}"); - // HTML导出失败不影响报告生成 } } catch (OperationCanceledException) diff --git a/src/UI/WPF/ViewModels/CollisionReportViewModel.cs b/src/UI/WPF/ViewModels/CollisionReportViewModel.cs index 32e95f5..5349f7b 100644 --- a/src/UI/WPF/ViewModels/CollisionReportViewModel.cs +++ b/src/UI/WPF/ViewModels/CollisionReportViewModel.cs @@ -917,7 +917,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels Title = "导出碰撞报告", Filter = "HTML报告 (*.html)|*.html|碰撞报告 (*.txt)|*.txt|所有文件 (*.*)|*.*", DefaultExt = "html", - FileName = $"NavisworksTransport_CollisionReport_{DateTime.Now:yyyyMMdd_HHmmss}" + FileName = PathHelper.GenerateCollisionReportFileName(CurrentReport?.PathName ?? PathName, "html") }; if (saveFileDialog.ShowDialog() == true) @@ -946,11 +946,10 @@ namespace NavisworksTransport.UI.WPF.ViewModels { try { - if (CurrentReport == null || !HasCollisions) return; + if (CurrentReport == null) return; // 生成默认文件名和路径 - var sanitizedName = PathHelper.SanitizeFileName(CurrentReport.PathName ?? "未知路径"); - var fileName = $"NavisworksTransport_CollisionReport_{sanitizedName}_{DateTime.Now:yyyyMMdd_HHmmss}.html"; + var fileName = PathHelper.GenerateCollisionReportFileName(CurrentReport.PathName, "html"); var reportDir = PathHelper.GetReportDirectory(); var filePath = Path.Combine(reportDir, fileName); diff --git a/src/UI/WPF/Views/CollisionReportDialog.xaml.cs b/src/UI/WPF/Views/CollisionReportDialog.xaml.cs index 7515ab0..21c60be 100644 --- a/src/UI/WPF/Views/CollisionReportDialog.xaml.cs +++ b/src/UI/WPF/Views/CollisionReportDialog.xaml.cs @@ -94,11 +94,8 @@ namespace NavisworksTransport.UI.WPF.Views { try { - LogManager.Info("用户关闭碰撞报告对话框,正在自动导出报告..."); - - // 关闭前自动导出报告 + LogManager.Info("用户关闭碰撞报告对话框,正在自动导出最终报告..."); await AutoExportReportAsync(); - this.Close(); } catch (Exception ex) @@ -107,24 +104,6 @@ namespace NavisworksTransport.UI.WPF.Views } } - /// - /// 自动导出报告 - /// - private async System.Threading.Tasks.Task AutoExportReportAsync() - { - try - { - if (_viewModel != null) - { - await _viewModel.AutoExportReportAsync(); - } - } - catch (Exception ex) - { - LogManager.Error($"自动导出报告失败: {ex.Message}"); - } - } - /// /// 窗口关闭事件 /// @@ -169,6 +148,14 @@ namespace NavisworksTransport.UI.WPF.Views } } + private async System.Threading.Tasks.Task AutoExportReportAsync() + { + if (_viewModel != null) + { + await _viewModel.AutoExportReportAsync(); + } + } + /// /// 窗口加载事件 /// @@ -470,4 +457,4 @@ namespace NavisworksTransport.UI.WPF.Views #endregion } -} \ No newline at end of file +} diff --git a/src/Utils/CollisionReportHtmlGenerator.cs b/src/Utils/CollisionReportHtmlGenerator.cs index 6d5d50e..e8e5ffa 100644 --- a/src/Utils/CollisionReportHtmlGenerator.cs +++ b/src/Utils/CollisionReportHtmlGenerator.cs @@ -422,8 +422,7 @@ namespace NavisworksTransport.Utils try { // 生成文件名 - string sanitizedName = PathHelper.SanitizeFileName(report.PathName ?? "未知路径"); - string fileName = PathHelper.GenerateTimestampedFileName($"collision_{sanitizedName}", "html"); + string fileName = PathHelper.GenerateCollisionReportFileName(report.PathName, "html"); // 获取报告目录 string reportDir = PathHelper.GetReportDirectory(); diff --git a/src/Utils/PathHelper.cs b/src/Utils/PathHelper.cs index c77a908..655d35a 100644 --- a/src/Utils/PathHelper.cs +++ b/src/Utils/PathHelper.cs @@ -84,6 +84,20 @@ namespace NavisworksTransport.Utils return $"{prefix}_{timestamp}.{extension}"; } + /// + /// 生成碰撞检测报告文件名。 + /// 格式:碰撞检测报告_{路径名}_{时间戳}.{extension} + /// + public static string GenerateCollisionReportFileName(string pathName, string extension, DateTime? now = null) + { + string sanitizedName = SanitizeFileName(pathName ?? "未知路径"); + string normalizedExtension = string.IsNullOrWhiteSpace(extension) + ? "html" + : extension.Trim().TrimStart('.'); + string timestamp = (now ?? DateTime.Now).ToString("yyyyMMdd_HHmmss"); + return $"碰撞检测报告_{sanitizedName}_{timestamp}.{normalizedExtension}"; + } + /// /// 生成复制路径名称。 /// 如果原名称以 MMdd_HHmmss 结尾,则重建时间戳;否则仅添加“副本_”前缀。