From a832e91b7b44d4730e9789e28f1bffd878825981 Mon Sep 17 00:00:00 2001 From: tian <11429339@qq.com> Date: Fri, 5 Dec 2025 17:48:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E4=BF=9D=E5=AD=98?= =?UTF-8?q?=E5=BD=93=E5=89=8D=E9=80=89=E6=8B=A9=E9=9B=86=E6=97=A0=E5=93=8D?= =?UTF-8?q?=E5=BA=94=E7=9A=84bug=EF=BC=8C=E5=8E=BB=E6=8E=89=E4=BA=86?= =?UTF-8?q?=E5=A4=9A=E4=BD=99=E7=9A=84=E5=AD=90=E8=8A=82=E7=82=B9=E5=B1=95?= =?UTF-8?q?=E5=BC=80=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ViewModels/LayerManagementViewModel.cs | 596 +++++++----------- src/Utils/VisibilityHelper.cs | 2 +- 2 files changed, 236 insertions(+), 362 deletions(-) diff --git a/src/UI/WPF/ViewModels/LayerManagementViewModel.cs b/src/UI/WPF/ViewModels/LayerManagementViewModel.cs index 36acf08..a86bfc2 100644 --- a/src/UI/WPF/ViewModels/LayerManagementViewModel.cs +++ b/src/UI/WPF/ViewModels/LayerManagementViewModel.cs @@ -44,10 +44,10 @@ namespace NavisworksTransport.UI.WPF.ViewModels private readonly AttributeGrouper _attributeGrouper; private readonly UIStateManager _uiStateManager; private CancellationTokenSource _cancellationTokenSource; - + // 选择事件订阅管理器 private SelectionEventSubscription _selectionEventSubscription; - + // 资源释放状态标志 private bool _disposed; @@ -152,7 +152,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels /// /// 可用属性列表 - 使用线程安全集合 /// - public ThreadSafeObservableCollection AvailableAttributes { get; } = + public ThreadSafeObservableCollection AvailableAttributes { get; } = new ThreadSafeObservableCollection(); /// @@ -249,7 +249,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels /// /// 分层策略列表 - 使用线程安全集合 /// - public ThreadSafeObservableCollection SplitStrategies { get; } = + public ThreadSafeObservableCollection SplitStrategies { get; } = new ThreadSafeObservableCollection { "智能检测楼层", @@ -259,16 +259,16 @@ namespace NavisworksTransport.UI.WPF.ViewModels /// /// 选中的分层策略 /// - public string SelectedSplitStrategy - { + public string SelectedSplitStrategy + { get => _selectedSplitStrategy; - set + set { if (SetPropertyThreadSafe(ref _selectedSplitStrategy, value)) { // 当选择"按自定义分层"时显示二级选项 ShowCustomLayerOptions = value == "按自定义分层"; - + OnPropertyChanged(nameof(CanPreviewSplit)); OnPropertyChanged(nameof(CanExecuteSplit)); OnPropertyChanged(nameof(CanIsolateSelectedLayer)); @@ -279,21 +279,21 @@ namespace NavisworksTransport.UI.WPF.ViewModels /// /// 自定义分层选项集合 /// - public ThreadSafeObservableCollection CustomLayerOptions { get; } = + public ThreadSafeObservableCollection CustomLayerOptions { get; } = new ThreadSafeObservableCollection { "按楼层", - "按区域", + "按区域", "按子系统" }; /// /// 选中的自定义分层选项 /// - public string SelectedCustomLayerOption - { + public string SelectedCustomLayerOption + { get => _selectedCustomLayerOption; - set + set { if (SetPropertyThreadSafe(ref _selectedCustomLayerOption, value)) { @@ -307,8 +307,8 @@ namespace NavisworksTransport.UI.WPF.ViewModels /// /// 是否显示自定义分层选项 /// - public bool ShowCustomLayerOptions - { + public bool ShowCustomLayerOptions + { get => _showCustomLayerOptions; set => SetPropertyThreadSafe(ref _showCustomLayerOptions, value); } @@ -362,10 +362,10 @@ namespace NavisworksTransport.UI.WPF.ViewModels /// 选中的预览结果 /// private SplitPreviewItem _selectedPreviewResult; - public SplitPreviewItem SelectedPreviewResult - { - get => _selectedPreviewResult; - set + public SplitPreviewItem SelectedPreviewResult + { + get => _selectedPreviewResult; + set { if (SetPropertyThreadSafe(ref _selectedPreviewResult, value)) { @@ -490,8 +490,8 @@ namespace NavisworksTransport.UI.WPF.ViewModels /// /// 选中的楼层标识 /// - public string SelectedFloorLevel - { + public string SelectedFloorLevel + { get => _selectedFloorLevel; set => SetPropertyThreadSafe(ref _selectedFloorLevel, value); } @@ -499,8 +499,8 @@ namespace NavisworksTransport.UI.WPF.ViewModels /// /// 选中的区域标识 /// - public string SelectedZone - { + public string SelectedZone + { get => _selectedZone; set => SetPropertyThreadSafe(ref _selectedZone, value); } @@ -508,18 +508,18 @@ namespace NavisworksTransport.UI.WPF.ViewModels /// /// 选中的子系统标识 /// - public string SelectedSubSystem - { + public string SelectedSubSystem + { get => _selectedSubSystem; set => SetPropertyThreadSafe(ref _selectedSubSystem, value); } /// /// 选中的分层参数类型(楼层、区域、子系统) /// - public string SelectedLayerParameter - { + public string SelectedLayerParameter + { get => _selectedLayerParameter; - set + set { if (SetPropertyThreadSafe(ref _selectedLayerParameter, value)) { @@ -539,7 +539,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels SelectedParameterValue = "F1"; break; } - + // 通知相关属性更新 OnPropertyChanged(nameof(CanSetLayerAttribute)); } @@ -549,10 +549,10 @@ namespace NavisworksTransport.UI.WPF.ViewModels /// /// 分层参数值 /// - public string SelectedParameterValue - { + public string SelectedParameterValue + { get => _selectedParameterValue; - set + set { if (SetPropertyThreadSafe(ref _selectedParameterValue, value)) { @@ -565,8 +565,8 @@ namespace NavisworksTransport.UI.WPF.ViewModels /// /// 是否显示分层属性信息 /// - public bool ShowLayerAttributeInfo - { + public bool ShowLayerAttributeInfo + { get => _showLayerAttributeInfo; set => SetPropertyThreadSafe(ref _showLayerAttributeInfo, value); } @@ -574,8 +574,8 @@ namespace NavisworksTransport.UI.WPF.ViewModels /// /// 当前分层属性信息 /// - public string CurrentLayerAttributeInfo - { + public string CurrentLayerAttributeInfo + { get => _currentLayerAttributeInfo; set => SetPropertyThreadSafe(ref _currentLayerAttributeInfo, value); } @@ -585,15 +585,15 @@ namespace NavisworksTransport.UI.WPF.ViewModels public ObservableCollection LayerParameterOptions { get; } = new ObservableCollection { "楼层", - "区域", + "区域", "子系统" }; /// /// 是否显示楼层属性信息 /// - public bool ShowFloorAttributeInfo - { + public bool ShowFloorAttributeInfo + { get => _showFloorAttributeInfo; set => SetPropertyThreadSafe(ref _showFloorAttributeInfo, value); } @@ -601,8 +601,8 @@ namespace NavisworksTransport.UI.WPF.ViewModels /// /// 当前楼层属性信息 /// - public string CurrentFloorAttributeInfo - { + public string CurrentFloorAttributeInfo + { get => _currentFloorAttributeInfo; set => SetPropertyThreadSafe(ref _currentFloorAttributeInfo, value); } @@ -633,7 +633,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels { get => HasSelectedModels && !string.IsNullOrEmpty(SelectedFloorLevel) && IsNotProcessing; } - + /// /// 是否可以设置分层属性 /// @@ -656,7 +656,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels if (document?.CurrentSelection?.SelectedItems?.Count > 0) { var selection = document.CurrentSelection.SelectedItems; - + foreach (var item in selection) { // 使用新的快速检查方法 @@ -693,7 +693,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels if (document?.CurrentSelection?.SelectedItems?.Count > 0) { var selection = document.CurrentSelection.SelectedItems; - + foreach (var item in selection) { // 使用新的 Native API 方法 @@ -738,18 +738,18 @@ namespace NavisworksTransport.UI.WPF.ViewModels public ICommand CancelOperationCommand { get; private set; } public ICommand DiagnosticCommand { get; private set; } public ICommand TestExportToNwdCommand { get; private set; } - + // 楼层属性相关命令 public ICommand RefreshSelectionCommand { get; private set; } public ICommand SetFloorAttributeCommand { get; private set; } public ICommand ClearFloorAttributeCommand { get; private set; } public ICommand ViewFloorAttributeCommand { get; private set; } - + // 新的分层属性命令 public ICommand SetLayerAttributeCommand { get; private set; } public ICommand ClearLayerAttributeCommand { get; private set; } public ICommand ViewLayerAttributeCommand { get; private set; } - + // 单独显示相关命令 public ICommand IsolateSelectedLayerCommand { get; private set; } public ICommand ShowAllLayersCommand { get; private set; } @@ -770,7 +770,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels LogManager.Error("UIStateManager初始化失败"); throw new InvalidOperationException("UIStateManager初始化失败"); } - + // 初始化业务逻辑组件 _floorDetector = new FloorDetector(); _modelSplitterManager = new ModelSplitterManager(); @@ -782,19 +782,19 @@ namespace NavisworksTransport.UI.WPF.ViewModels // 订阅事件 _modelSplitterManager.ProgressChanged += OnProgressChanged; _modelSplitterManager.StatusChanged += OnStatusChanged; - + // 订阅Navisworks选择变化事件 - 使用新的选择管理服务 SubscribeToSelectionEvents(); // 异步初始化 InitializeAsync(); - + LogManager.Info("LayerManagementViewModel构造函数执行完成 - 使用统一UI架构"); } catch (Exception ex) { LogManager.Error($"LayerManagementViewModel构造函数异常: {ex.Message}", ex); - + // 在构造函数中尽量保证对象处于可用状态 CurrentOperationText = "初始化失败,请检查日志"; throw; @@ -811,7 +811,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels { // 设置主ViewModel引用到基类 SetMainViewModel(mainViewModel); - + // 获取UI状态管理器实例 _uiStateManager = UIStateManager.Instance; // 验证关键组件是否正常初始化 @@ -820,7 +820,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels LogManager.Error("UIStateManager初始化失败"); throw new InvalidOperationException("UIStateManager初始化失败"); } - + // 初始化业务逻辑组件 _floorDetector = new FloorDetector(); _modelSplitterManager = new ModelSplitterManager(); @@ -832,19 +832,19 @@ namespace NavisworksTransport.UI.WPF.ViewModels // 订阅事件 _modelSplitterManager.ProgressChanged += OnProgressChanged; _modelSplitterManager.StatusChanged += OnStatusChanged; - + // 订阅Navisworks选择变化事件 - 使用新的选择管理服务 SubscribeToSelectionEvents(); // 异步初始化 InitializeAsync(); - + LogManager.Info("LayerManagementViewModel构造函数执行完成 - 支持统一状态栏"); } catch (Exception ex) { LogManager.Error($"LayerManagementViewModel构造函数异常: {ex.Message}", ex); - + // 在构造函数中尽量保证对象处于可用状态 CurrentOperationText = "初始化失败,请检查日志"; throw; @@ -864,84 +864,84 @@ namespace NavisworksTransport.UI.WPF.ViewModels { // 使用异步RelayCommand,采用正确的业务逻辑与UI分离模式 AnalyzeFloorsCommand = new RelayCommand( - async () => await AnalyzeFloorsAsync(), + async () => await AnalyzeFloorsAsync(), () => IsNotProcessing); - + RefreshAttributesCommand = new RelayCommand( - async () => await RefreshAttributesAsync(), + async () => await RefreshAttributesAsync(), () => IsNotProcessing); - + SelectNodesCommand = new RelayCommand( - async () => await SelectNodesAsync(), + async () => await SelectNodesAsync(), () => IsNotProcessing); - + ApplyFloorAttributesCommand = new RelayCommand( - async () => await ApplyFloorAttributesAsync(), + async () => await ApplyFloorAttributesAsync(), () => CanApplyFloorAttributes); - + PreviewSplitCommand = new RelayCommand( - async () => await PreviewSplitAsync(), + async () => await PreviewSplitAsync(), () => CanPreviewSplit); - + ExecuteSplitCommand = new RelayCommand( - async () => await ExecuteSplitAsync(), + async () => await ExecuteSplitAsync(), () => CanExecuteSplit); - + BrowseOutputDirectoryCommand = new RelayCommand( - async () => await BrowseOutputDirectoryAsync(), + async () => await BrowseOutputDirectoryAsync(), () => IsNotProcessing); - + SaveSelectedItemsCommand = new RelayCommand( - async () => await SaveSelectedItemsAsync(), + async () => await SaveSelectedItemsAsync(), () => HasSelectedItems); - + CancelOperationCommand = new RelayCommand(CancelOperation, () => IsProcessing); - + DiagnosticCommand = new RelayCommand( - async () => await RunDiagnosticAsync(), + async () => await RunDiagnosticAsync(), () => IsNotProcessing); TestExportToNwdCommand = new RelayCommand( - async () => await TestExportToNwdAsync(), + async () => await TestExportToNwdAsync(), () => IsNotProcessing); - + // 楼层属性相关命令初始化 RefreshSelectionCommand = new RelayCommand( - async () => await RefreshSelectionAsync(), + async () => await RefreshSelectionAsync(), () => IsNotProcessing); - + SetFloorAttributeCommand = new RelayCommand( - async () => await SetFloorAttributeAsync(), + async () => await SetFloorAttributeAsync(), () => CanSetFloorAttribute); - + ClearFloorAttributeCommand = new RelayCommand( - async () => await ClearFloorAttributeAsync(), + async () => await ClearFloorAttributeAsync(), () => CanClearFloorAttribute); - + ViewFloorAttributeCommand = new RelayCommand( - async () => await ViewFloorAttributeAsync(), + async () => await ViewFloorAttributeAsync(), () => HasSelectedItems); // 新的分层属性命令初始化 SetLayerAttributeCommand = new RelayCommand( - async () => await SetLayerAttributeAsync(), + async () => await SetLayerAttributeAsync(), () => CanSetLayerAttribute); - + ClearLayerAttributeCommand = new RelayCommand( - async () => await ClearLayerAttributeAsync(), + async () => await ClearLayerAttributeAsync(), () => CanClearLayerAttribute); - + ViewLayerAttributeCommand = new RelayCommand( - async () => await ViewLayerAttributeAsync(), + async () => await ViewLayerAttributeAsync(), () => HasSelectedItems); - + // 单独显示相关命令 IsolateSelectedLayerCommand = new RelayCommand( - async () => await IsolateSelectedLayerAsync(), + async () => await IsolateSelectedLayerAsync(), () => CanIsolateSelectedLayer); - + ShowAllLayersCommand = new RelayCommand( - async () => await ShowAllLayersAsync(), + async () => await ShowAllLayersAsync(), () => IsNotProcessing); LogManager.Info("分层管理命令初始化完成"); @@ -994,7 +994,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels catch (Exception ex) { LogManager.Error($"[LayerManagementViewModel] 分析楼层异常: {ex.Message}", ex); - + // 异常UI更新 await _uiStateManager.ExecuteUIUpdateAsync(() => { @@ -1055,7 +1055,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels catch (Exception ex) { LogManager.Error($"[LayerManagementViewModel] 刷新属性异常: {ex.Message}", ex); - + // 异常UI更新 await _uiStateManager.ExecuteUIUpdateAsync(() => { @@ -1099,7 +1099,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels if (result.IsSuccess) { SelectedNodesText = NavisworksSelectionHelper.FormatSelectionText(result.Count, result.SelectedItems, "个节点"); - UpdateMainStatus(result.Count > 0 + UpdateMainStatus(result.Count > 0 ? $"获取到 {result.Count} 个选中节点" : "当前没有选中的节点"); } @@ -1113,7 +1113,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels catch (Exception ex) { LogManager.Error($"[LayerManagementViewModel] 选择节点异常: {ex.Message}", ex); - + // 异常UI更新 await _uiStateManager.ExecuteUIUpdateAsync(() => { @@ -1170,7 +1170,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels catch (Exception ex) { LogManager.Error($"[LayerManagementViewModel] 应用楼层属性异常: {ex.Message}", ex); - + // 异常UI更新 await _uiStateManager.ExecuteUIUpdateAsync(() => { @@ -1202,14 +1202,14 @@ namespace NavisworksTransport.UI.WPF.ViewModels CurrentOperationText = "正在生成分层预览..."; ProgressPercentage = 0; UpdateMainStatus("准备分层配置"); - + // 清空旧的预览结果 SplitPreviewResults.Clear(); - + // 显示加载状态 ShowPreviewResults = false; ShowPreviewPrompt = false; - + // 添加加载提示项 var loadingItem = new SplitPreviewItem { @@ -1249,7 +1249,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels { // 清空加载提示 SplitPreviewResults.Clear(); - + if (result.IsSuccess && result.PreviewItems?.Count > 0) { // 添加实际预览结果 @@ -1274,13 +1274,13 @@ namespace NavisworksTransport.UI.WPF.ViewModels catch (Exception ex) { LogManager.Error($"[LayerManagementViewModel] 预览分层异常: {ex.Message}", ex); - + // 异常UI更新 await _uiStateManager.ExecuteUIUpdateAsync(() => { // 清空加载提示,隐藏结果列表并显示异常提示 SplitPreviewResults.Clear(); - + ShowPreviewResults = false; ShowPreviewPrompt = true; // 显示提示信息 UpdateMainStatus($"预览异常: {ex.Message}"); @@ -1396,7 +1396,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels catch (Exception ex) { LogManager.Error($"[LayerManagementViewModel] 执行分层异常: {ex.Message}", ex); - + // 异常UI更新 await _uiStateManager.ExecuteUIUpdateAsync(() => { @@ -1571,12 +1571,12 @@ namespace NavisworksTransport.UI.WPF.ViewModels if (preview.Items != null && preview.Items.Count > 100) { LogManager.Warning($"[LayerManagementViewModel] 大量模型项检测:{preview.Items.Count}个,可能需要更多内存和时间"); - + // 强制垃圾回收,释放内存 GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); - + LogManager.Info($"[LayerManagementViewModel] 垃圾回收完成,当前内存: {GC.GetTotalMemory(false) / 1024 / 1024} MB"); } @@ -1610,7 +1610,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels try { var document = NavisApplication.ActiveDocument; - + // 优先使用文档文件名(去除扩展名) if (document != null && !string.IsNullOrEmpty(document.FileName)) { @@ -1620,7 +1620,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels return SanitizeFileName(fileName); } } - + // 其次尝试使用第一个模型根项的DisplayName if (document?.Models != null && document.Models.Count > 0) { @@ -1632,7 +1632,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels } } } - + // 最后使用默认名称 return "NavisworksModel"; } @@ -1678,21 +1678,21 @@ namespace NavisworksTransport.UI.WPF.ViewModels // 移除文件名中的非法字符,保留中文字符 var invalidChars = Path.GetInvalidFileNameChars(); string sanitized = fileName; - + foreach (char c in invalidChars) { sanitized = sanitized.Replace(c, '_'); } - + // 移除其他特殊字符,保留字母、数字、下划线和中文字符 sanitized = System.Text.RegularExpressions.Regex.Replace(sanitized, @"[^\w\u4e00-\u9fa5\-\.]", "_"); - + // 移除多余的下划线 sanitized = System.Text.RegularExpressions.Regex.Replace(sanitized, @"_{2,}", "_"); - + // 移除首尾下划线 sanitized = sanitized.Trim('_', ' '); - + // 限制长度 if (sanitized.Length > 50) { @@ -1715,22 +1715,22 @@ namespace NavisworksTransport.UI.WPF.ViewModels { // 获取根节点名称 string rootNodeName = GetRootNodeName(); - + // 获取分层属性类型名称 string attributeTypeName = GetAttributeTypeName(strategy); - + // 清理属性值(原分层名称) string sanitizedAttributeValue = SanitizeFileName(attributeValue); - + // 生成时间戳 string timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss"); - + // 新的统一格式:根节点名称_分层属性_属性值_时间戳.nwd string fileName = $"{rootNodeName}_{attributeTypeName}_{sanitizedAttributeValue}_{timestamp}.nwd"; - + LogManager.Info($"[LayerManagementViewModel] 生成智能文件名: {fileName}"); LogManager.Info($"[LayerManagementViewModel] 文件名组成: 根节点='{rootNodeName}', 属性类型='{attributeTypeName}', 属性值='{sanitizedAttributeValue}', 时间戳='{timestamp}'"); - + return fileName; } catch (Exception ex) @@ -1778,7 +1778,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels { OutputDirectory = result.SelectedPath; }); - + LogManager.Info($"[分层管理] 用户选择目录: {result.SelectedPath}"); } else if (!result.IsSuccess && !string.IsNullOrEmpty(result.ErrorMessage)) @@ -1793,7 +1793,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels } /// - /// 保存选中项目 - 使用复杂导出测试验证的成功逻辑 + /// 保存选中项目 /// private async Task SaveSelectedItemsAsync() { @@ -1801,42 +1801,25 @@ namespace NavisworksTransport.UI.WPF.ViewModels { LogManager.Info("[LayerManagementViewModel] 开始保存当前选择项"); - // 1. 简单状态更新 IsProcessing = true; - // 获取当前文档 var document = Autodesk.Navisworks.Api.Application.ActiveDocument; - if (document?.Models?.Count == 0) + if (document?.Models?.Count == 0 || document.CurrentSelection.SelectedItems.Count == 0) { - LogManager.Error("[LayerManagementViewModel] 没有活动文档或模型"); - MessageBox.Show("没有活动文档或模型", "保存错误", MessageBoxButton.OK, MessageBoxImage.Error); + LogManager.Warning("[LayerManagementViewModel] 没有活动文档或未选择节点"); + MessageBox.Show("请先选择要保存的项目", "保存提示", MessageBoxButton.OK, MessageBoxImage.Warning); return; } // 保存当前选择状态 - var originalSelection = new List(); - foreach (ModelItem item in document.CurrentSelection.SelectedItems) - { - originalSelection.Add(item); - } - - if (originalSelection.Count == 0) - { - LogManager.Warning("[LayerManagementViewModel] 当前没有选中的节点"); - MessageBox.Show("当前没有选中的节点,请先选择要保存的项目", "保存提示", MessageBoxButton.OK, MessageBoxImage.Warning); - return; - } - - LogManager.Info($"[LayerManagementViewModel] 用户选中了 {originalSelection.Count} 个节点"); - foreach (var item in originalSelection) - { - LogManager.Info($"[LayerManagementViewModel] 选中节点: {item.DisplayName}"); - } - - // 2. 生成默认文件名:根节点名_选中节点名_时间戳(多选时显示节点数量) - string defaultFileName = GenerateDefaultFileNameForMultipleItems(document, originalSelection); + var originalSelection = new List(document.CurrentSelection.SelectedItems); - // 3. 获取保存路径 + LogManager.Info($"[LayerManagementViewModel] 用户选中了 {originalSelection.Count} 个节点"); + + // 生成默认文件名 + string defaultFileName = GenerateDefaultFileNameForMultipleItems(document, originalSelection); + + // 获取保存路径 string saveFilePath = null; System.Windows.Application.Current.Dispatcher.Invoke(() => { @@ -1856,199 +1839,90 @@ namespace NavisworksTransport.UI.WPF.ViewModels if (string.IsNullOrEmpty(saveFilePath)) { - LogManager.Info("[LayerManagementViewModel] 用户取消了保存操作"); return; } - LogManager.Info($"[LayerManagementViewModel] 开始保存选中项目到: {saveFilePath}"); - LogManager.Info($"[LayerManagementViewModel] 保存 {originalSelection.Count} 个选中节点及其所有子项"); - - // 4. 使用借鉴自 ModelSplitterManager.ExportLayerToNwd 的精确隔离逻辑 bool exportResult = false; string errorMessage = ""; - - try + + // 在主线程执行导出 + await Task.Run(() => { - // 收集要导出的项目(包含子节点) - var itemsToExport = new List(); - - foreach (var selectedItem in originalSelection) - { - // 添加选中节点本身 - itemsToExport.Add(selectedItem); - LogManager.Info($"[LayerManagementViewModel] 添加选中节点到导出列表: {selectedItem.DisplayName}"); - - // 添加所有子节点(如果用户开启了包含子节点选项) - if (IncludeChildNodes) - { - try - { - // 使用DescendantsAndSelf来获取所有后代节点 - var childItems = selectedItem.DescendantsAndSelf.Where(x => x != selectedItem); - int childCount = 0; - - foreach (ModelItem child in childItems) - { - itemsToExport.Add(child); - childCount++; - } - - LogManager.Info($"[LayerManagementViewModel] 为选中节点 {selectedItem.DisplayName} 添加了 {childCount} 个子节点"); - } - catch (Exception ex) - { - LogManager.Warning($"[LayerManagementViewModel] 遍历子节点失败: {ex.Message}"); - } - } - } - - LogManager.Info($"[LayerManagementViewModel] 总共需要导出的节点数量: {itemsToExport.Count}"); - - // 核心修复:使用与 ModelSplitterManager.ExportLayerToNwd 相同的精确隔离逻辑 - // 在主线程中执行 Navisworks API 调用 System.Windows.Application.Current.Dispatcher.Invoke(() => { try { - LogManager.Info($"[LayerManagementViewModel] 在主线程中执行导出操作"); - + // 收集要导出的项目 (仅根节点) + var itemsToExport = new ModelItemCollection(); + itemsToExport.AddRange(originalSelection); + + LogManager.Info($"[LayerManagementViewModel] 准备导出 {itemsToExport.Count} 个节点"); + // 保存当前可见性状态 - LogManager.Info($"[LayerManagementViewModel] 开始保存当前可见性状态"); var originalVisibilityState = SaveCurrentVisibilityState(document); - LogManager.Info($"[LayerManagementViewModel] 已保存可见性状态"); try { - // 使用 VisibilityHelper.IsolateSpecificItems 精确隔离显示 - // 这个方法已经在分层导出中验证有效,不会包含额外的祖先节点 - LogManager.Info($"[LayerManagementViewModel] 准备隔离显示 {itemsToExport.Count} 个节点"); + // 隔离显示 + bool isolateSuccess = VisibilityHelper.IsolateSpecificItems(itemsToExport); + if (!isolateSuccess) throw new InvalidOperationException("隔离显示失败"); - // 将 List 转换为 ModelItemCollection - var itemsToIsolate = new ModelItemCollection(); - foreach (var item in itemsToExport) - { - itemsToIsolate.Add(item); - } - - bool isolateSuccess = VisibilityHelper.IsolateSpecificItems(itemsToIsolate); - - if (!isolateSuccess) - { - throw new InvalidOperationException("隔离显示失败"); - } - - LogManager.Info($"[LayerManagementViewModel] 隔离显示成功"); - - // 创建导出选项 - 使用用户配置的参数 + // 导出选项 var exportOptions = new Autodesk.Navisworks.Api.NwdExportOptions { - ExcludeHiddenItems = true, // 只导出可见项目(选中节点及其子项) + ExcludeHiddenItems = true, EmbedXrefs = EmbedXrefs, PreventObjectPropertyExport = PreventObjectPropertyExport }; - LogManager.Info($"[LayerManagementViewModel] SaveSelectedItems导出选项: EmbedXrefs={exportOptions.EmbedXrefs}, PreventObjectPropertyExport={exportOptions.PreventObjectPropertyExport}"); - LogManager.Info("[LayerManagementViewModel] 开始调用ExportToNwd API"); - - // 使用ExportToNwd API document.ExportToNwd(saveFilePath, exportOptions); - exportResult = true; LogManager.Info("[LayerManagementViewModel] ExportToNwd API调用完成"); } finally { - // 恢复原始可见性状态 - try - { - RestoreVisibilityState(document, originalVisibilityState); - LogManager.Info("[LayerManagementViewModel] 已恢复原始可见性状态"); - } - catch (Exception restoreEx) - { - LogManager.Error($"[LayerManagementViewModel] 恢复可见性失败: {restoreEx.Message}"); - } + // 恢复可见性 + RestoreVisibilityState(document, originalVisibilityState); } } catch (Exception ex) { - LogManager.Error($"[LayerManagementViewModel] 主线程导出异常: {ex.Message}", ex); + LogManager.Error($"[LayerManagementViewModel] 导出异常: {ex.Message}", ex); errorMessage = ex.Message; exportResult = false; } }); + }); - // 恢复原始选择 + // 恢复原始选择 (需要在主线程) + System.Windows.Application.Current.Dispatcher.Invoke(() => + { try { document.CurrentSelection.Clear(); - foreach (ModelItem item in originalSelection) - { - try - { - document.CurrentSelection.Add(item); - } - catch (Exception ex) - { - LogManager.Warning($"[LayerManagementViewModel] 恢复选择项时出错: {ex.Message}"); - } - } - LogManager.Info("[LayerManagementViewModel] 已恢复原始选择"); + document.CurrentSelection.AddRange(originalSelection); } catch (Exception ex) { LogManager.Warning($"[LayerManagementViewModel] 恢复选择失败: {ex.Message}"); } - } - catch (Exception ex) - { - LogManager.Error($"[LayerManagementViewModel] 保存选中项目异常: {ex.Message}", ex); - errorMessage = ex.Message; - exportResult = false; - } + }); - // 5. 显示结果 + // 显示结果 if (exportResult) { if (File.Exists(saveFilePath)) { var fileInfo = new FileInfo(saveFilePath); - - // 构建选中节点的描述 - string nodeDescription; - if (originalSelection.Count == 1) - { - nodeDescription = $"选中节点: {originalSelection[0].DisplayName}"; - } - else - { - nodeDescription = $"选中 {originalSelection.Count} 个节点:\n"; - for (int i = 0; i < Math.Min(originalSelection.Count, 5); i++) - { - nodeDescription += $" • {originalSelection[i].DisplayName}\n"; - } - if (originalSelection.Count > 5) - { - nodeDescription += $" ... 以及其他 {originalSelection.Count - 5} 个节点"; - } - } - MessageBox.Show( - $"保存选中项目成功!\n\n文件路径: {saveFilePath}\n文件大小: {fileInfo.Length / 1024} KB\n创建时间: {fileInfo.CreationTime}\n\n{nodeDescription}", + $"保存成功!\n\n文件: {saveFilePath}\n大小: {fileInfo.Length / 1024} KB", "保存结果", MessageBoxButton.OK, MessageBoxImage.Information); } - else - { - MessageBox.Show("保存操作完成,但文件不存在。", "保存结果", MessageBoxButton.OK, MessageBoxImage.Warning); - } } else { - MessageBox.Show($"保存选中项目失败!\n\n错误信息: {errorMessage}\n\n请检查日志了解详细信息。", - "保存结果", MessageBoxButton.OK, MessageBoxImage.Error); + MessageBox.Show($"保存失败: {errorMessage}", "保存错误", MessageBoxButton.OK, MessageBoxImage.Error); } - - LogManager.Info("[LayerManagementViewModel] 保存当前选择项完成"); } catch (Exception ex) { @@ -2057,7 +1931,6 @@ namespace NavisworksTransport.UI.WPF.ViewModels } finally { - // 6. 简单的状态清理 IsProcessing = false; } } @@ -2068,7 +1941,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels private Dictionary SaveCurrentVisibilityState(Document document) { var visibilityState = new Dictionary(); - + try { // 获取所有顶级项目并记录它们的可见性状态 @@ -2088,7 +1961,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels } } } - + LogManager.Info($"[LayerManagementViewModel] 保存可见性状态完成,记录了 {visibilityState.Count} 个顶级项目"); } catch (Exception ex) @@ -2206,14 +2079,14 @@ namespace NavisworksTransport.UI.WPF.ViewModels catch (Exception ex) { LogManager.Error($"[LayerManagementViewModel] 环境诊断异常: {ex.Message}", ex); - + // 异常UI更新 await _uiStateManager.ExecuteUIUpdateAsync(() => { CurrentOperationText = "环境诊断异常"; UpdateMainStatus($"诊断失败: {ex.Message}"); }); - + ShowDiagnosticResult($"诊断过程中发生异常: {ex.Message}"); } finally @@ -2234,13 +2107,13 @@ namespace NavisworksTransport.UI.WPF.ViewModels { var report = new System.Text.StringBuilder(); report.AppendLine("=== Navisworks API环境诊断报告 ==="); - + try { // 1. 线程状态 var apartmentState = System.Threading.Thread.CurrentThread.GetApartmentState(); report.AppendLine($"线程状态: {apartmentState} {(apartmentState == System.Threading.ApartmentState.STA ? "✓" : "✗")}"); - + // 2. API可用性 try { @@ -2251,7 +2124,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels { report.AppendLine($"API可用性: 异常 - {apiEx.Message} ✗"); } - + // 3. 文档状态 try { @@ -2270,13 +2143,13 @@ namespace NavisworksTransport.UI.WPF.ViewModels { report.AppendLine($"活动文档: 异常 - {docEx.Message} ✗"); } - + // 4. 内存状态 long memoryMB = GC.GetTotalMemory(false) / 1024 / 1024; report.AppendLine($"当前内存: {memoryMB} MB"); - + report.AppendLine("=== 诊断完成 ==="); - + string result = report.ToString(); LogManager.Info($"[LayerManagement] 环境诊断报告:\n{result}"); return result; @@ -2321,7 +2194,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels }; var panel = new System.Windows.Controls.DockPanel(); - + var buttonPanel = new System.Windows.Controls.StackPanel { Orientation = System.Windows.Controls.Orientation.Horizontal, @@ -2415,11 +2288,11 @@ namespace NavisworksTransport.UI.WPF.ViewModels // 3. 使用ExportToNwd API测试 bool exportResult = false; string errorMessage = ""; - + try { LogManager.Info($"[LayerManagementViewModel] 开始ExportToNwd测试到: {saveFilePath}"); - + // 获取当前文档 var document = Autodesk.Navisworks.Api.Application.ActiveDocument; if (document?.Models?.Count == 0) @@ -2441,7 +2314,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels // 复杂测试:基于用户当前选择的节点进行导出 ModelItem targetItem = null; var allVisibilityItems = new List(); - + // 第一步:检查用户当前是否有选中的节点 if (document.CurrentSelection.SelectedItems.Count > 0) { @@ -2453,19 +2326,19 @@ namespace NavisworksTransport.UI.WPF.ViewModels { LogManager.Info("[LayerManagementViewModel] 用户没有选中节点,将自动选择一个二级节点"); } - + // 第二步:收集所有可见的模型项(用于可见性控制) foreach (Model model in document.Models) { var rootItem = model.RootItem; LogManager.Info($"[LayerManagementViewModel] 处理模型根项: {rootItem.DisplayName},子项数量: {rootItem.Children.Count()}"); - + // 遍历一级节点 foreach (ModelItem firstLevel in rootItem.Children) { allVisibilityItems.Add(firstLevel); // 收集用于可见性控制 LogManager.Info($"[LayerManagementViewModel] 一级节点: {firstLevel.DisplayName},子项数量: {firstLevel.Children.Count()}"); - + // 如果用户没有选中节点,则选择第一个二级节点作为fallback if (targetItem == null && firstLevel.Children.Count() > 0) { @@ -2476,7 +2349,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels break; // 只要第一个二级节点 } } - + // 收集其他二级节点用于可见性控制 foreach (ModelItem secondLevel in firstLevel.Children) { @@ -2509,11 +2382,11 @@ namespace NavisworksTransport.UI.WPF.ViewModels // 第三步:隐藏其他节点,只保留目标节点可见 LogManager.Info($"[LayerManagementViewModel] 准备导出选中节点及其所有子项: {targetItem.DisplayName}"); LogManager.Info($"[LayerManagementViewModel] 目标节点子项数量: {targetItem.Children.Count()}"); - + // 创建要隐藏的项目集合 - 隐藏除了目标节点之外的所有一级节点 var itemsToHide = new ModelItemCollection(); int hiddenCount = 0; - + foreach (ModelItem item in allVisibilityItems) { // 隐藏除了目标节点之外的所有一级节点 @@ -2544,25 +2417,25 @@ namespace NavisworksTransport.UI.WPF.ViewModels LogManager.Warning($"[LayerManagementViewModel] 隐藏操作失败: {ex.Message}"); } } - + // 确保选中目标节点 document.CurrentSelection.Clear(); document.CurrentSelection.Add(targetItem); LogManager.Info($"[LayerManagementViewModel] 已选择目标节点: {targetItem.DisplayName}"); - + // 创建导出选项 - 使用用户配置的参数 var exportOptions = new Autodesk.Navisworks.Api.NwdExportOptions(); exportOptions.ExcludeHiddenItems = true; // 只导出可见项目(目标节点及其子项) exportOptions.EmbedXrefs = EmbedXrefs; exportOptions.PreventObjectPropertyExport = PreventObjectPropertyExport; - + LogManager.Info($"[LayerManagementViewModel] TestExportToNwd导出选项: EmbedXrefs={exportOptions.EmbedXrefs}, PreventObjectPropertyExport={exportOptions.PreventObjectPropertyExport}"); LogManager.Info("[LayerManagementViewModel] 开始调用ExportToNwd API"); - + // 使用ExportToNwd API document.ExportToNwd(saveFilePath, exportOptions); - + exportResult = true; LogManager.Info("[LayerManagementViewModel] ExportToNwd API调用完成"); } @@ -2582,7 +2455,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels allItems.Add(item); } } - + if (allItems.Count > 0) { document.Models.SetHidden(allItems, false); @@ -2700,7 +2573,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels catch (Exception ex) { LogManager.Error($"[LayerManagementViewModel] 更新选择状态异常: {ex.Message}", ex); - + // 异常UI更新 await _uiStateManager.ExecuteUIUpdateAsync(() => { @@ -2790,7 +2663,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels return SelectedSplitStrategy; } - + /// /// 生成针对多个选中项目的默认文件名 /// @@ -2965,7 +2838,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels _modelSplitterManager.ProgressChanged -= OnProgressChanged; _modelSplitterManager.StatusChanged -= OnStatusChanged; } - + // 取消Navisworks选择变化事件订阅 UnsubscribeFromSelectionEvents(); @@ -2976,7 +2849,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels LogManager.Error($"[LayerManagementViewModel] 资源清理失败: {ex.Message}"); } } - + _disposed = true; } } @@ -2995,9 +2868,9 @@ namespace NavisworksTransport.UI.WPF.ViewModels /// public bool IsValidState() { - return _uiStateManager != null && - AvailableAttributes != null && - SplitPreviewResults != null && + return _uiStateManager != null && + AvailableAttributes != null && + SplitPreviewResults != null && SplitStrategies != null; } @@ -3025,7 +2898,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels // 使用新的选择管理服务订阅选择变化事件 _selectionEventSubscription = NavisworksSelectionHelper.SubscribeToSelectionChanges( OnSelectionChangedAsync, _uiStateManager); - + LogManager.Info("[LayerManagementViewModel] 已通过NavisworksSelectionHelper订阅选择变化事件"); } catch (Exception ex) @@ -3044,7 +2917,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels // 通过Dispose方法取消订阅 _selectionEventSubscription?.Dispose(); _selectionEventSubscription = null; - + LogManager.Info("[LayerManagementViewModel] 已通过NavisworksSelectionHelper取消订阅选择变化事件"); } catch (Exception ex) @@ -3060,12 +2933,12 @@ namespace NavisworksTransport.UI.WPF.ViewModels { // 如果已经释放,直接返回 if (_disposed) return; - + try { // 更新楼层属性相关的选择状态(使用新的选择结果) await UpdateFloorAttributeSelectionStateAsync(selectionResult); - + LogManager.Info($"[LayerManagementViewModel] 选择状态已更新: {selectionResult.Count}个项目"); } catch (Exception ex) @@ -3081,7 +2954,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels { // 如果已经释放,直接返回 if (_disposed) return; - + try { // 如果没有提供选择结果,则获取当前选择状态 @@ -3099,13 +2972,13 @@ namespace NavisworksTransport.UI.WPF.ViewModels { SelectedModelsText = selectionResult.ErrorMessage ?? "检查选择状态异常"; } - + // 刷新命令状态 OnPropertyChanged(nameof(HasSelectedItems)); OnPropertyChanged(nameof(HasSelectedModels)); OnPropertyChanged(nameof(CanSetFloorAttribute)); OnPropertyChanged(nameof(CanClearFloorAttribute)); - + LogManager.Info($"[LayerManagementViewModel] 楼层属性选择状态已更新: 成功={selectionResult.Success}, 数量={selectionResult.Count}"); } catch (Exception ex) @@ -3149,7 +3022,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels SelectedModelsText = selectionResult.ErrorMessage ?? "检查选择状态失败"; CurrentOperationText = "检查失败"; } - + // 刷新命令状态 OnPropertyChanged(nameof(HasSelectedItems)); OnPropertyChanged(nameof(HasSelectedModels)); @@ -3160,7 +3033,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels catch (Exception ex) { LogManager.Error($"[LayerManagementViewModel] 刷新选择异常: {ex.Message}", ex); - + // 异常UI更新 await _uiStateManager.ExecuteUIUpdateAsync(() => { @@ -3205,7 +3078,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels try { bool result = _floorAttributeManager.SetFloorAttribute( - item, + item, SelectedFloorLevel, string.IsNullOrWhiteSpace(SelectedZone) ? null : SelectedZone, string.IsNullOrWhiteSpace(SelectedSubSystem) ? null : SelectedSubSystem); @@ -3223,7 +3096,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels CurrentOperationText = $"楼层属性设置完成,成功设置 {successCount}/{selectedItems.Count} 个模型项"; LogManager.Info($"[LayerManagementViewModel] 楼层属性设置完成,成功 {successCount}/{selectedItems.Count} 个"); - + // 刷新选中模型信息 await RefreshSelectionAsync(); }, "设置楼层属性"); @@ -3276,13 +3149,14 @@ namespace NavisworksTransport.UI.WPF.ViewModels } } - return new { - Success = successCount > 0, - Count = successCount, + return new + { + Success = successCount > 0, + Count = successCount, TotalCount = selectedItems.Count, - Message = successCount > 0 ? - $"已清除 {successCount} 个模型项的楼层属性" : - "没有找到可清除的楼层属性" + Message = successCount > 0 ? + $"已清除 {successCount} 个模型项的楼层属性" : + "没有找到可清除的楼层属性" }; } catch (Exception ex) @@ -3295,10 +3169,10 @@ namespace NavisworksTransport.UI.WPF.ViewModels await _uiStateManager.ExecuteUIUpdateAsync(() => { CurrentOperationText = result.Message; - UpdateMainStatus(result.Success ? + UpdateMainStatus(result.Success ? $"楼层属性清除完成,成功清除 {result.Count}/{result.TotalCount} 个模型项" : "楼层属性清除失败"); - + if (result.Success) { // 清除成功,刷新选择状态 @@ -3319,7 +3193,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels catch (Exception ex) { LogManager.Error($"[LayerManagementViewModel] 清除楼层属性异常: {ex.Message}", ex); - + // 异常UI更新 await _uiStateManager.ExecuteUIUpdateAsync(() => { @@ -3371,7 +3245,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels { string floorLevel = _floorAttributeManager.GetFloorProperty(item, "楼层"); string itemName = item.DisplayName ?? "未命名"; - + if (!string.IsNullOrEmpty(floorLevel)) { floorAttributeInfo.Add($"✅ {itemName}: {floorLevel}"); @@ -3406,13 +3280,13 @@ namespace NavisworksTransport.UI.WPF.ViewModels CurrentFloorAttributeInfo = result.Info; ShowFloorAttributeInfo = true; }); - + LogManager.Info($"[LayerManagementViewModel] 楼层属性查看完成,共 {result.Count} 个模型项"); } catch (Exception ex) { LogManager.Error($"[LayerManagementViewModel] 查看楼层属性异常: {ex.Message}", ex); - + // 在UI线程显示错误信息 await _uiStateManager.ExecuteUIUpdateAsync(() => { @@ -3452,23 +3326,23 @@ namespace NavisworksTransport.UI.WPF.ViewModels { // 使用成员变量 _floorAttributeManager var selectedItems = document.CurrentSelection.SelectedItems.ToList(); - + foreach (var item in selectedItems) { // 使用新的SetSingleLayerAttribute方法,只设置指定的属性类型 bool result = _floorAttributeManager.SetSingleLayerAttribute(item, SelectedLayerParameter, SelectedParameterValue); - + if (result) { processedItems++; } - + ProgressPercentage = (double)processedItems / totalItems * 100; } CurrentOperationText = $"分层属性设置完成,成功设置 {processedItems}/{totalItems} 个对象的{SelectedLayerParameter}属性:{SelectedParameterValue}"; LogManager.Info($"[LayerManagementViewModel] 成功设置分层属性: {SelectedLayerParameter}={SelectedParameterValue},影响对象数:{processedItems}"); - + // 刷新选中模型信息 await RefreshSelectionAsync(); } @@ -3509,7 +3383,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels { // 使用成员变量 _floorAttributeManager var selectedItems = document.CurrentSelection.SelectedItems.ToList(); - + foreach (var item in selectedItems) { try @@ -3524,13 +3398,13 @@ namespace NavisworksTransport.UI.WPF.ViewModels { LogManager.Error($"清除模型 {item.DisplayName} 的分层属性失败:{ex.Message}"); } - + ProgressPercentage = (double)(processedItems + 1) / totalItems * 100; } CurrentOperationText = $"分层属性清除完成,成功清除 {processedItems}/{totalItems} 个对象的分层属性"; LogManager.Info($"[LayerManagementViewModel] 成功清除分层属性,影响对象数:{processedItems}"); - + // 刷新选中模型信息 await RefreshSelectionAsync(); ShowLayerAttributeInfo = false; @@ -3575,7 +3449,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels var selectedItems = document.CurrentSelection.SelectedItems.ToList(); var totalItems = selectedItems.Count; var attributeInfo = new System.Text.StringBuilder(); - + attributeInfo.AppendLine($"选中对象总数:{totalItems}"); attributeInfo.AppendLine(); @@ -3587,7 +3461,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels { string itemName = item.DisplayName ?? "未命名"; var itemAttributes = new List(); - + // 获取楼层属性 var floorLevel = _floorAttributeManager.GetFloorProperty(item, "楼层"); if (!string.IsNullOrEmpty(floorLevel)) @@ -3644,13 +3518,13 @@ namespace NavisworksTransport.UI.WPF.ViewModels CurrentLayerAttributeInfo = result.Info; ShowLayerAttributeInfo = true; }); - + LogManager.Info($"[LayerManagementViewModel] 分层属性查看完成,共 {result.Count} 个模型项,其中 {result.HasAttributeCount} 个有分层属性"); } catch (Exception ex) { LogManager.Error($"[LayerManagementViewModel] 查看分层属性异常: {ex.Message}", ex); - + // 在UI线程显示错误信息 await _uiStateManager.ExecuteUIUpdateAsync(() => { @@ -3668,7 +3542,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels try { LogManager.Info("[LayerManagementViewModel] 开始执行单独显示分层"); - + if (SelectedPreviewResult == null) { LogManager.Warning("[LayerManagementViewModel] 未选择任何预览结果"); @@ -3686,7 +3560,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels // 2. 在UI线程执行Navisworks API调用 bool isolateResult = false; string resultMessage = ""; - + await _uiStateManager.ExecuteUIUpdateAsync(() => { try @@ -3699,10 +3573,10 @@ namespace NavisworksTransport.UI.WPF.ViewModels resultMessage = "选中的分层没有模型项"; return; } - + // 调用可见性管理器的公共工具函数 bool success = VisibilityHelper.IsolateSpecificItems(SelectedPreviewResult.Items); - + LogManager.Info($"[LayerManagementViewModel] 单独显示执行结果: {success}"); isolateResult = success; resultMessage = success ? "已隔离显示项目" : "隔离显示失败"; @@ -3735,7 +3609,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels catch (Exception ex) { LogManager.Error($"[LayerManagementViewModel] 单独显示分层异常: {ex.Message}", ex); - + // 异常UI更新 await _uiStateManager.ExecuteUIUpdateAsync(() => { @@ -3761,7 +3635,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels try { LogManager.Info("[LayerManagementViewModel] 开始恢复所有分层显示"); - + // 1. 初始UI状态更新 await _uiStateManager.ExecuteUIUpdateAsync(() => { @@ -3773,14 +3647,14 @@ namespace NavisworksTransport.UI.WPF.ViewModels // 2. 在UI线程执行Navisworks API调用 bool restoreResult = false; string resultMessage = ""; - + await _uiStateManager.ExecuteUIUpdateAsync(() => { try { // 调用可见性管理器的公共工具函数 bool success = VisibilityHelper.ShowAllItems(); - + LogManager.Info($"[LayerManagementViewModel] 恢复显示执行结果: {success}"); restoreResult = success; resultMessage = success ? "所有项目已显示" : "显示失败"; @@ -3813,7 +3687,7 @@ namespace NavisworksTransport.UI.WPF.ViewModels catch (Exception ex) { LogManager.Error($"[LayerManagementViewModel] 恢复所有分层显示异常: {ex.Message}", ex); - + // 异常UI更新 await _uiStateManager.ExecuteUIUpdateAsync(() => { diff --git a/src/Utils/VisibilityHelper.cs b/src/Utils/VisibilityHelper.cs index 00278e1..c5b9cf8 100644 --- a/src/Utils/VisibilityHelper.cs +++ b/src/Utils/VisibilityHelper.cs @@ -142,4 +142,4 @@ namespace NavisworksTransport } } -} \ No newline at end of file +} \ No newline at end of file