From 1d28c71cbaf2ebc2df9d471a3eb429496805f253 Mon Sep 17 00:00:00 2001
From: tian <11429339@qq.com>
Date: Thu, 4 Sep 2025 13:54:24 +0800
Subject: [PATCH] =?UTF-8?q?=E7=94=A8SearchAPI=E6=9D=A5=E6=90=9C=E7=B4=A2Ca?=
=?UTF-8?q?tegoryAttributeManager=E4=B8=AD=E7=9A=84FilterByLogisticsType()?=
=?UTF-8?q?=E3=80=81FilterTraversableItems()=E7=AD=89=E6=96=B9=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.claude/settings.local.json | 6 +-
doc/requirement/todo_features.md | 2 +-
.../Properties/CategoryAttributeManager.cs | 143 +++++++++++++++---
3 files changed, 125 insertions(+), 26 deletions(-)
diff --git a/.claude/settings.local.json b/.claude/settings.local.json
index 27e57bd..a7f2a30 100644
--- a/.claude/settings.local.json
+++ b/.claude/settings.local.json
@@ -54,7 +54,11 @@
"Bash(git add:*)",
"Bash(git commit:*)",
"Read(/C:\\Users\\Tellme\\apps\\NavisworksTransport\\src\\Utils/**)",
- "Read(/C:\\Users\\Tellme\\apps\\NavisworksTransport\\src\\Core/**)"
+ "Read(/C:\\Users\\Tellme\\apps\\NavisworksTransport\\src\\Core/**)",
+ "Read(/C:\\Users\\Tellme\\apps\\NavisworksTransport\\doc\\design\\2026/**)",
+ "Read(/C:\\Users\\Tellme\\apps\\NavisworksTransport\\doc\\design\\2026/**)",
+ "Read(/C:\\Users\\Tellme\\apps\\NavisworksTransport\\doc\\navisworks_api\\NET\\examples\\PlugIns\\SearchComparisonPlugIn/**)",
+ "Read(/C:\\Users\\Tellme\\apps\\NavisworksTransport\\doc\\navisworks_api/**)"
],
"deny": [],
"additionalDirectories": [
diff --git a/doc/requirement/todo_features.md b/doc/requirement/todo_features.md
index 1aa459c..546bbb4 100644
--- a/doc/requirement/todo_features.md
+++ b/doc/requirement/todo_features.md
@@ -14,7 +14,7 @@
3. [x] (性能优化)增加直接采用包围盒的2.5D自动寻路算法,代替空间索引+高度扫描算法。
4. [x] (BUG)自动规划有时成功(过滤3个通道,找到26个障碍物),有时失败(过滤2个通道,只找到2个障碍物),连续自动规划,有时会崩溃。
5. [ ] (BUG)还有厚度为0的障碍物(不一定是bug)
-6. [ ] (性能优化) 用SearchAPI来搜索CategoryAttributeManager中的FilterByLogisticsType()、FilterTraversableItems()等方法
+6. [x] (性能优化) 用SearchAPI来搜索CategoryAttributeManager中的FilterByLogisticsType()、FilterTraversableItems()等方法
### [2025/08/31]
diff --git a/src/Core/Properties/CategoryAttributeManager.cs b/src/Core/Properties/CategoryAttributeManager.cs
index 68065f8..cebd761 100644
--- a/src/Core/Properties/CategoryAttributeManager.cs
+++ b/src/Core/Properties/CategoryAttributeManager.cs
@@ -289,18 +289,45 @@ namespace NavisworksTransport
/// 匹配条件的模型项集合
public static ModelItemCollection FilterByLogisticsType(ModelItemCollection items, LogisticsElementType elementType)
{
- ModelItemCollection filteredItems = new ModelItemCollection();
-
- foreach (ModelItem item in items)
+ try
{
- string typeValue = GetLogisticsPropertyValue(item, LogisticsProperties.TYPE);
- if (typeValue == elementType.ToString())
+ // 使用SearchAPI优化:直接搜索具有指定类型值的物流项目
+ using (var search = new Search())
{
- filteredItems.Add(item);
+ // 将输入的ModelItemCollection转换为搜索选择
+ search.Selection.CopyFrom(items);
+ search.Locations = SearchLocations.DescendantsAndSelf;
+
+ var searchConditions = search.SearchConditions;
+ searchConditions.Clear();
+
+ // 搜索条件:必须有物流分类
+ searchConditions.Add(SearchCondition.HasCategoryByDisplayName(LogisticsCategories.LOGISTICS));
+
+ // 搜索条件:类型属性等于指定值
+ searchConditions.Add(
+ SearchCondition.HasPropertyByDisplayName(LogisticsCategories.LOGISTICS, LogisticsProperties.TYPE)
+ .EqualValue(VariantData.FromDisplayString(elementType.ToString())));
+
+ return search.FindAll(Application.ActiveDocument, false);
}
}
-
- return filteredItems;
+ catch (Exception ex)
+ {
+ LogManager.Error($"按物流类型过滤失败: {ex.Message}");
+
+ // 如果SearchAPI失败,回退到原始实现
+ ModelItemCollection filteredItems = new ModelItemCollection();
+ foreach (ModelItem item in items)
+ {
+ string typeValue = GetLogisticsPropertyValue(item, LogisticsProperties.TYPE);
+ if (typeValue == elementType.ToString())
+ {
+ filteredItems.Add(item);
+ }
+ }
+ return filteredItems;
+ }
}
///
@@ -310,18 +337,45 @@ namespace NavisworksTransport
/// 可通行的模型项集合
public static ModelItemCollection FilterTraversableItems(ModelItemCollection items)
{
- ModelItemCollection filteredItems = new ModelItemCollection();
-
- foreach (ModelItem item in items)
+ try
{
- string traversableValue = GetLogisticsPropertyValue(item, LogisticsProperties.TRAVERSABLE);
- if (traversableValue == "是")
+ // 使用SearchAPI优化:直接搜索可通行的物流项目
+ using (var search = new Search())
{
- filteredItems.Add(item);
+ // 将输入的ModelItemCollection转换为搜索选择
+ search.Selection.CopyFrom(items);
+ search.Locations = SearchLocations.DescendantsAndSelf;
+
+ var searchConditions = search.SearchConditions;
+ searchConditions.Clear();
+
+ // 搜索条件:必须有物流分类
+ searchConditions.Add(SearchCondition.HasCategoryByDisplayName(LogisticsCategories.LOGISTICS));
+
+ // 搜索条件:可通行属性等于"是"
+ searchConditions.Add(
+ SearchCondition.HasPropertyByDisplayName(LogisticsCategories.LOGISTICS, LogisticsProperties.TRAVERSABLE)
+ .EqualValue(VariantData.FromDisplayString("是")));
+
+ return search.FindAll(Application.ActiveDocument, false);
}
}
-
- return filteredItems;
+ catch (Exception ex)
+ {
+ LogManager.Error($"过滤可通行物流项目失败: {ex.Message}");
+
+ // 如果SearchAPI失败,回退到原始实现
+ ModelItemCollection filteredItems = new ModelItemCollection();
+ foreach (ModelItem item in items)
+ {
+ string traversableValue = GetLogisticsPropertyValue(item, LogisticsProperties.TRAVERSABLE);
+ if (traversableValue == "是")
+ {
+ filteredItems.Add(item);
+ }
+ }
+ return filteredItems;
+ }
}
///
@@ -369,18 +423,59 @@ namespace NavisworksTransport
/// 适用的通道集合
public static ModelItemCollection FilterByVehicleSize(ModelItemCollection items, string vehicleSize)
{
- ModelItemCollection filteredItems = new ModelItemCollection();
-
- foreach (ModelItem item in items)
+ try
{
- string sizeValue = GetLogisticsPropertyValue(item, LogisticsProperties.VEHICLE_SIZE);
- if (sizeValue == vehicleSize || sizeValue == "标准" || string.IsNullOrEmpty(sizeValue))
+ // 对于车辆尺寸过滤,由于需要处理"标准"和空值的特殊逻辑,
+ // SearchAPI的复杂条件组合比较困难,所以采用混合方式:
+ // 1. 使用SearchAPI快速获取有物流属性的项目
+ // 2. 再进行车辆尺寸过滤
+
+ ModelItemCollection logisticsItems;
+
+ using (var search = new Search())
{
- filteredItems.Add(item);
+ // 将输入的ModelItemCollection转换为搜索选择
+ search.Selection.CopyFrom(items);
+ search.Locations = SearchLocations.DescendantsAndSelf;
+
+ var searchConditions = search.SearchConditions;
+ searchConditions.Clear();
+
+ // 搜索条件:必须有物流分类
+ searchConditions.Add(SearchCondition.HasCategoryByDisplayName(LogisticsCategories.LOGISTICS));
+
+ logisticsItems = search.FindAll(Application.ActiveDocument, false);
}
+
+ // 对获取到的物流项目进行车辆尺寸过滤
+ ModelItemCollection filteredItems = new ModelItemCollection();
+ foreach (ModelItem item in logisticsItems)
+ {
+ string sizeValue = GetLogisticsPropertyValue(item, LogisticsProperties.VEHICLE_SIZE);
+ if (sizeValue == vehicleSize || sizeValue == "标准" || string.IsNullOrEmpty(sizeValue))
+ {
+ filteredItems.Add(item);
+ }
+ }
+
+ return filteredItems;
+ }
+ catch (Exception ex)
+ {
+ LogManager.Error($"按车辆尺寸过滤失败: {ex.Message}");
+
+ // 如果SearchAPI失败,回退到完全原始实现
+ 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;
}
-
- return filteredItems;
}
///