TellmePdmsPluging/Core/PdmsManager.cs
sladro 69f32020fa 改进模型统计信息准确性,实现真实元素分类统计
- 添加递归元素统计方法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 <noreply@anthropic.com>
2025-09-21 11:00:03 +08:00

328 lines
12 KiB
C#

using System;
using System.Collections.Generic;
using TellmePdmsPluging.Models;
using Aveva.ApplicationFramework;
using Aveva.Pdms.Database;
namespace TellmePdmsPluging.Core
{
public class PdmsManager
{
private static PdmsManager _instance;
private static readonly object _lock = new object();
public static PdmsManager Instance
{
get
{
if (_instance == null)
{
lock (_lock)
{
if (_instance == null)
_instance = new PdmsManager();
}
}
return _instance;
}
}
private PdmsManager()
{
}
public ModelStatusResponse GetModelStatus()
{
try
{
// 检查PDMS是否连接
if (!IsPdmsConnected())
{
return new ModelStatusResponse
{
ModelLoaded = false
};
}
return new ModelStatusResponse
{
ModelLoaded = true,
ProjectInfo = GetProjectInfo(),
ModelStatistics = GetModelStatistics(),
SessionInfo = GetSessionInfo()
};
}
catch (Exception ex)
{
// 记录错误日志
System.Diagnostics.Debug.WriteLine($"获取PDMS模型状态失败: {ex.Message}");
return new ModelStatusResponse
{
ModelLoaded = false
};
}
}
private bool IsPdmsConnected()
{
try
{
// 使用MDB.CurrentMDB检查PDMS连接状态
var currentMdb = MDB.CurrentMDB;
return currentMdb != null;
}
catch
{
return false;
}
}
private ProjectInfo GetProjectInfo()
{
try
{
// 使用MDB.CurrentMDB和Project.CurrentProject获取真实项目信息
var currentMdb = MDB.CurrentMDB;
var currentProject = Project.CurrentProject;
if (currentMdb != null)
{
var designDb = currentMdb.GetFirstDB(DbType.Design);
if (designDb != null)
{
return new ProjectInfo
{
ProjectName = designDb.Name ?? "Unknown",
MdsName = currentMdb.Name ?? "Unknown",
PdmsVersion = "12.1.SP4"
};
}
}
return new ProjectInfo
{
ProjectName = "Unknown",
MdsName = "Unknown",
PdmsVersion = "12.1.SP4"
};
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"获取项目信息失败: {ex.Message}");
return new ProjectInfo
{
ProjectName = "Unknown",
MdsName = "Unknown",
PdmsVersion = "12.1.SP4"
};
}
}
private ModelStatistics GetModelStatistics()
{
try
{
// 使用MDB.CurrentMDB获取真实模型统计信息
var currentMdb = MDB.CurrentMDB;
if (currentMdb != null)
{
var designDb = currentMdb.GetFirstDB(DbType.Design);
if (designDb?.World != null)
{
// 初始化统计计数器
var elementCounts = new Dictionary<string, int>
{
{"SITE", 0}, {"ZONE", 0}, {"PIPE", 0}, {"EQUI", 0},
{"STRU", 0}, {"VALVE", 0}, {"FITT", 0}, {"NOZZ", 0}
};
int totalElements = 0;
var activeZones = new List<string>();
int zoneCount = 0;
// 递归统计所有元素
CountElementsByType(designDb.World, elementCounts, ref totalElements);
// 专门统计Zone信息
CountZones(designDb.World, ref zoneCount, activeZones);
return new ModelStatistics
{
TotalElements = totalElements,
ElementCounts = elementCounts,
ZoneCount = zoneCount,
ActiveZones = activeZones
};
}
}
return new ModelStatistics
{
TotalElements = 0,
ElementCounts = new Dictionary<string, int>(),
ZoneCount = 0,
ActiveZones = new List<string>()
};
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"获取模型统计信息失败: {ex.Message}");
return new ModelStatistics
{
TotalElements = 0,
ElementCounts = new Dictionary<string, int>(),
ZoneCount = 0,
ActiveZones = new List<string>()
};
}
}
private void CountElementsByType(DbElement parentElement, Dictionary<string, int> 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<string> 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
{
// 使用MDB.CurrentMDB获取真实会话信息
var currentMdb = MDB.CurrentMDB;
if (currentMdb != null)
{
var designDb = currentMdb.GetFirstDB(DbType.Design);
if (designDb?.CurrentSession != null)
{
// 从真实的数据库会话获取信息
var session = designDb.CurrentSession;
return new SessionInfo
{
UserName = session.User ?? Environment.UserName, // 从DbSession获取用户名
StartTime = session.Date, // 从DbSession获取会话创建时间
DurationMinutes = (int)(DateTime.Now - session.Date).TotalMinutes // 计算会话持续时间
};
}
}
return new SessionInfo
{
UserName = Environment.UserName,
StartTime = DateTime.Now,
DurationMinutes = 0
};
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"获取会话信息失败: {ex.Message}");
return new SessionInfo
{
UserName = Environment.UserName,
StartTime = DateTime.Now,
DurationMinutes = 0
};
}
}
}
}