NavisworksTransport/src/Utils/VisibilityHelper.cs

145 lines
4.9 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;
using System.Linq;
using Autodesk.Navisworks.Api;
using NavisApplication = Autodesk.Navisworks.Api.Application;
namespace NavisworksTransport
{
/// <summary>
/// 可见性管理器
/// 负责ModelItem可见性控制的核心业务逻辑
/// </summary>
public static class VisibilityHelper
{
/// <summary>
/// 显示所有项目
/// </summary>
/// <returns>操作是否成功</returns>
public static bool ShowAllItems()
{
try
{
NavisApplication.ActiveDocument?.Models?.ResetAllHidden();
return true;
}
catch (Exception ex)
{
LogManager.Error($"[VisibilityManager] 显示所有项目失败: {ex.Message}");
return false;
}
}
/// <summary>
/// 仅显示指定的模型项集合,隐藏其他所有项
/// </summary>
/// <param name="itemsToShow">要显示的模型项集合</param>
/// <returns>操作是否成功</returns>
public static bool IsolateSpecificItems(ModelItemCollection itemsToShow)
{
try
{
var stopwatch = System.Diagnostics.Stopwatch.StartNew();
var document = NavisApplication.ActiveDocument;
if (document == null || document.Models.Count == 0)
{
LogManager.Warning("[VisibilityManager] 没有加载的模型");
return false;
}
if (itemsToShow == null || !itemsToShow.Any())
{
LogManager.Warning("[VisibilityManager] 没有指定要显示的项目");
return false;
}
// 1. 重置所有隐藏状态
document.Models.ResetAllHidden();
// 2. 创建可见项集合(要保证从根节点开始的路径完整)
ModelItemCollection visible = new ModelItemCollection();
foreach (ModelItem item in itemsToShow)
{
// 添加祖先路径
if (item.AncestorsAndSelf != null)
visible.AddRange(item.AncestorsAndSelf);
}
// 3. 创建候选隐藏集合 - 收集所有可见项的兄弟节点
ModelItemCollection hidden = new ModelItemCollection();
foreach (ModelItem toShow in visible)
{
if (toShow.Parent != null)
{
// 添加父节点的所有子节点
hidden.AddRange(toShow.Parent.Children);
}
}
// 4. 从隐藏集合中移除可见项
foreach (ModelItem toShow in visible)
{
hidden.Remove(toShow);
}
// 5. 执行隐藏操作
if (hidden.Count > 0)
{
document.Models.SetHidden(hidden, true);
}
stopwatch.Stop();
LogManager.Info($"[VisibilityManager] 隔离显示完成,耗时 {stopwatch.ElapsedMilliseconds}ms");
return true;
}
catch (Exception ex)
{
LogManager.Error($"[VisibilityManager] 隔离显示失败: {ex.Message}");
return false;
}
}
/// <summary>
/// 设置指定模型项集合的可见性
/// </summary>
/// <param name="items">要设置的模型项集合</param>
/// <param name="isHidden">是否隐藏true=隐藏false=显示)</param>
/// <returns>操作是否成功</returns>
public static bool SetItemsVisibility(ModelItemCollection items, bool isHidden)
{
try
{
var document = NavisApplication.ActiveDocument;
if (document == null || document.Models.Count == 0)
{
LogManager.Warning("[VisibilityManager] 没有加载的模型");
return false;
}
if (items == null || !items.Any())
{
LogManager.Warning("[VisibilityManager] 没有指定要操作的项目");
return false;
}
// 设置可见性状态
document.Models.SetHidden(items, isHidden);
var action = isHidden ? "隐藏" : "显示";
LogManager.Info($"[VisibilityManager] 已{action} {items.Count} 个模型项");
return true;
}
catch (Exception ex)
{
var action = isHidden ? "隐藏" : "显示";
LogManager.Error($"[VisibilityManager] {action}指定项目失败: {ex.Message}");
return false;
}
}
}
}