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