NavisworksTransport/CategoryAttributeManager.cs

288 lines
11 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.Collections.Generic;
using System.Linq;
using Autodesk.Navisworks.Api;
using ComApi = Autodesk.Navisworks.Api.Interop.ComApi;
using ComApiBridge = Autodesk.Navisworks.Api.ComApi.ComApiBridge;
namespace NavisworksTransport
{
/// <summary>
/// 类别属性管理器 - 负责为模型元素添加和管理物流相关的自定义属性
/// </summary>
public class CategoryAttributeManager
{
/// <summary>
/// 物流类别定义
/// </summary>
public static class LogisticsCategories
{
public const string LOGISTICS = "物流属性";
public const string CATEGORY_INTERNAL_NAME = "物流属性_Internal";
}
/// <summary>
/// 物流属性定义
/// </summary>
public static class LogisticsProperties
{
public const string TYPE = "类型";
public const string TRAVERSABLE = "可通行";
public const string PRIORITY = "优先级";
public const string VEHICLE_SIZE = "适用车辆尺寸";
public const string SPEED_LIMIT = "速度限制";
}
/// <summary>
/// 物流元素类型
/// </summary>
public enum LogisticsElementType
{
,
,
,
,
,
,
,
}
/// <summary>
/// 为选定的模型项添加物流属性
/// </summary>
/// <param name="items">要处理的模型项集合</param>
/// <param name="elementType">物流元素类型</param>
/// <param name="isTraversable">是否可通行</param>
/// <param name="priority">优先级1-10</param>
/// <param name="vehicleSize">适用车辆尺寸</param>
/// <param name="speedLimit">速度限制km/h</param>
/// <returns>成功处理的项目数量</returns>
public static int AddLogisticsAttributes(
ModelItemCollection items,
LogisticsElementType elementType,
bool isTraversable = true,
int priority = 5,
string vehicleSize = "标准",
double speedLimit = 10.0)
{
if (items == null || items.Count == 0)
{
return 0;
}
int successCount = 0;
try
{
// 获取COM API状态对象
ComApi.InwOpState10 state = ComApiBridge.State;
// 转换选择集合为COM对象
ComApi.InwOpSelection comSelection = ComApiBridge.ToInwOpSelection(items);
// 遍历每个路径对象
foreach (ComApi.InwOaPath3 path in comSelection.Paths())
{
try
{
// 获取属性节点
ComApi.InwGUIPropertyNode2 propertyNode =
(ComApi.InwGUIPropertyNode2)state.GetGUIPropertyNode(path, true);
// 创建新的属性类别
ComApi.InwOaPropertyVec propertyCategory =
(ComApi.InwOaPropertyVec)state.ObjectFactory(
ComApi.nwEObjectType.eObjectType_nwOaPropertyVec, null, null);
// 创建并添加类型属性
AddProperty(state, propertyCategory, LogisticsProperties.TYPE,
elementType.ToString(), elementType.ToString() + "_Internal");
// 创建并添加可通行属性
AddProperty(state, propertyCategory, LogisticsProperties.TRAVERSABLE,
isTraversable ? "是" : "否", "Traversable_Internal");
// 创建并添加优先级属性
AddProperty(state, propertyCategory, LogisticsProperties.PRIORITY,
priority.ToString(), "Priority_Internal");
// 创建并添加车辆尺寸属性
AddProperty(state, propertyCategory, LogisticsProperties.VEHICLE_SIZE,
vehicleSize, "VehicleSize_Internal");
// 创建并添加速度限制属性
AddProperty(state, propertyCategory, LogisticsProperties.SPEED_LIMIT,
speedLimit.ToString("F1") + " km/h", "SpeedLimit_Internal");
// 将属性类别设置到模型项
propertyNode.SetUserDefined(0, LogisticsCategories.LOGISTICS,
LogisticsCategories.CATEGORY_INTERNAL_NAME, propertyCategory);
successCount++;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"处理单个模型项时发生错误: {ex.Message}");
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"添加物流属性时发生错误: {ex.Message}");
}
return successCount;
}
/// <summary>
/// 创建并添加单个属性到属性类别
/// </summary>
/// <param name="state">COM API状态对象</param>
/// <param name="propertyCategory">属性类别对象</param>
/// <param name="displayName">显示名称</param>
/// <param name="value">属性值</param>
/// <param name="internalName">内部名称</param>
private static void AddProperty(ComApi.InwOpState10 state,
ComApi.InwOaPropertyVec propertyCategory,
string displayName,
string value,
string internalName)
{
// 创建新属性
ComApi.InwOaProperty property = (ComApi.InwOaProperty)state.ObjectFactory(
ComApi.nwEObjectType.eObjectType_nwOaProperty, null, null);
// 设置属性信息
property.name = internalName; // 内部名称
property.UserName = displayName; // 显示名称
property.value = value; // 属性值
// 添加到属性类别
propertyCategory.Properties().Add(property);
}
/// <summary>
/// 检查模型项是否已有物流属性
/// </summary>
/// <param name="item">要检查的模型项</param>
/// <returns>如果已有物流属性返回true</returns>
public static bool HasLogisticsAttributes(ModelItem item)
{
try
{
// 检查自定义属性
foreach (PropertyCategory category in item.PropertyCategories)
{
if (category.DisplayName == LogisticsCategories.LOGISTICS)
{
return true;
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"检查物流属性时发生错误: {ex.Message}");
}
return false;
}
/// <summary>
/// 获取模型项的物流属性值
/// </summary>
/// <param name="item">模型项</param>
/// <param name="propertyName">属性名称</param>
/// <returns>属性值,如果不存在返回空字符串</returns>
public static string GetLogisticsPropertyValue(ModelItem item, string propertyName)
{
try
{
foreach (PropertyCategory category in item.PropertyCategories)
{
if (category.DisplayName == LogisticsCategories.LOGISTICS)
{
foreach (DataProperty property in category.Properties)
{
if (property.DisplayName == propertyName)
{
return property.Value.ToDisplayString();
}
}
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"获取物流属性值时发生错误: {ex.Message}");
}
return string.Empty;
}
/// <summary>
/// 根据物流属性筛选模型项
/// </summary>
/// <param name="items">要筛选的模型项集合</param>
/// <param name="elementType">要筛选的元素类型</param>
/// <returns>匹配条件的模型项集合</returns>
public static ModelItemCollection FilterByLogisticsType(ModelItemCollection items, LogisticsElementType elementType)
{
ModelItemCollection filteredItems = new ModelItemCollection();
foreach (ModelItem item in items)
{
string typeValue = GetLogisticsPropertyValue(item, LogisticsProperties.TYPE);
if (typeValue == elementType.ToString())
{
filteredItems.Add(item);
}
}
return filteredItems;
}
/// <summary>
/// 筛选可通行的模型项
/// </summary>
/// <param name="items">要筛选的模型项集合</param>
/// <returns>可通行的模型项集合</returns>
public static ModelItemCollection FilterTraversableItems(ModelItemCollection items)
{
ModelItemCollection filteredItems = new ModelItemCollection();
foreach (ModelItem item in items)
{
string traversableValue = GetLogisticsPropertyValue(item, LogisticsProperties.TRAVERSABLE);
if (traversableValue == "是")
{
filteredItems.Add(item);
}
}
return filteredItems;
}
/// <summary>
/// 根据车辆尺寸筛选适用的通道
/// </summary>
/// <param name="items">要筛选的模型项集合</param>
/// <param name="vehicleSize">车辆尺寸</param>
/// <returns>适用的通道集合</returns>
public static ModelItemCollection FilterByVehicleSize(ModelItemCollection items, string vehicleSize)
{
ModelItemCollection filteredItems = new ModelItemCollection();
foreach (ModelItem item in items)
{
string sizeValue = GetLogisticsPropertyValue(item, LogisticsProperties.VEHICLE_SIZE);
if (sizeValue == vehicleSize || sizeValue == "标准" || string.IsNullOrEmpty(sizeValue))
{
filteredItems.Add(item);
}
}
return filteredItems;
}
}
}