From 69f32020faeb555fa9fd31991c0c122fbdf9abfe Mon Sep 17 00:00:00 2001 From: sladro Date: Sun, 21 Sep 2025 11:00:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E6=A8=A1=E5=9E=8B=E7=BB=9F?= =?UTF-8?q?=E8=AE=A1=E4=BF=A1=E6=81=AF=E5=87=86=E7=A1=AE=E6=80=A7=EF=BC=8C?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E7=9C=9F=E5=AE=9E=E5=85=83=E7=B4=A0=E5=88=86?= =?UTF-8?q?=E7=B1=BB=E7=BB=9F=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加递归元素统计方法CountElementsByType(),使用GetActualType()获取准确元素类型 - 实现Zone专门统计方法CountZones(),统计SITE下的ZONE元素及其名称 - 替换硬编码统计逻辑,支持SITE、ZONE、PIPE、EQUI、STRU、VALVE、FITT、NOZZ等类型 - 修复GetValidString API调用,使用正确的两参数方式 - 移除所有硬编码的0值,返回真实的模型统计数据 现在/api/status/model接口将返回准确的模型元素统计信息。 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- Core/PdmsManager.cs | 138 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 116 insertions(+), 22 deletions(-) diff --git a/Core/PdmsManager.cs b/Core/PdmsManager.cs index e26fafc..530251a 100644 --- a/Core/PdmsManager.cs +++ b/Core/PdmsManager.cs @@ -131,41 +131,33 @@ namespace TellmePdmsPluging.Core var designDb = currentMdb.GetFirstDB(DbType.Design); if (designDb?.World != null) { - // 获取真实的元素统计信息 - var worldMembers = designDb.WorldMembers(); + // 初始化统计计数器 var elementCounts = new Dictionary { - {"PIPE", 0}, {"EQUI", 0}, {"STRU", 0}, - {"VALVE", 0}, {"FITT", 0}, {"NOZZ", 0} + {"SITE", 0}, {"ZONE", 0}, {"PIPE", 0}, {"EQUI", 0}, + {"STRU", 0}, {"VALVE", 0}, {"FITT", 0}, {"NOZZ", 0} }; - + int totalElements = 0; var activeZones = new List(); - - // 遍历World的直接子元素进行统计 - if (worldMembers != null) - { - foreach (var element in worldMembers) - { - if (element != null && !element.IsNull && element.IsValid) - { - totalElements++; - // 这里需要获取元素类型,可能需要进一步的API调用来判断具体类型 - // 暂时先统计总数 - } - } - } - + int zoneCount = 0; + + // 递归统计所有元素 + CountElementsByType(designDb.World, elementCounts, ref totalElements); + + // 专门统计Zone信息 + CountZones(designDb.World, ref zoneCount, activeZones); + return new ModelStatistics { TotalElements = totalElements, ElementCounts = elementCounts, - ZoneCount = 0, // Zone统计需要额外的API + ZoneCount = zoneCount, ActiveZones = activeZones }; } } - + return new ModelStatistics { TotalElements = 0, @@ -187,6 +179,108 @@ namespace TellmePdmsPluging.Core } } + private void CountElementsByType(DbElement parentElement, Dictionary elementCounts, ref int totalElements) + { + try + { + if (parentElement == null || parentElement.IsNull || !parentElement.IsValid) + return; + + // 获取所有子元素 + var members = parentElement.Members(); + if (members != null) + { + foreach (var element in members) + { + if (element != null && !element.IsNull && element.IsValid) + { + totalElements++; + + // 获取元素的实际类型 + var elementType = element.GetActualType(); + if (elementType != null) + { + string typeName = elementType.Name; + if (elementCounts.ContainsKey(typeName)) + { + elementCounts[typeName]++; + } + } + + // 递归统计子元素 + CountElementsByType(element, elementCounts, ref totalElements); + } + } + } + } + catch (Exception ex) + { + System.Diagnostics.Debug.WriteLine($"统计元素类型时出错: {ex.Message}"); + } + } + + private void CountZones(DbElement worldElement, ref int zoneCount, List activeZones) + { + try + { + if (worldElement == null || worldElement.IsNull || !worldElement.IsValid) + return; + + // 查找SITE元素 + var sites = worldElement.Members(); + if (sites != null) + { + foreach (var site in sites) + { + if (site != null && !site.IsNull && site.IsValid) + { + var siteType = site.GetActualType(); + if (siteType != null && siteType.Name == "SITE") + { + // 在SITE下查找ZONE元素 + var zones = site.Members(); + if (zones != null) + { + foreach (var zone in zones) + { + if (zone != null && !zone.IsNull && zone.IsValid) + { + var zoneType = zone.GetActualType(); + if (zoneType != null && zoneType.Name == "ZONE") + { + zoneCount++; + + // 获取Zone名称 + try + { + string zoneName = ""; + if (zone.GetValidString(DbAttributeInstance.NAME, ref zoneName)) + { + if (!string.IsNullOrEmpty(zoneName)) + { + activeZones.Add(zoneName); + } + } + } + catch + { + // 如果无法获取名称,跳过 + } + } + } + } + } + } + } + } + } + } + catch (Exception ex) + { + System.Diagnostics.Debug.WriteLine($"统计Zone信息时出错: {ex.Message}"); + } + } + private SessionInfo GetSessionInfo() { try