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 worldMembers = designDb.WorldMembers(); var elementCounts = new Dictionary { {"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调用来判断具体类型 // 暂时先统计总数 } } } return new ModelStatistics { TotalElements = totalElements, ElementCounts = elementCounts, ZoneCount = 0, // Zone统计需要额外的API ActiveZones = activeZones }; } } return new ModelStatistics { TotalElements = 0, ElementCounts = new Dictionary(), ZoneCount = 0, ActiveZones = new List() }; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"获取模型统计信息失败: {ex.Message}"); return new ModelStatistics { TotalElements = 0, ElementCounts = new Dictionary(), ZoneCount = 0, ActiveZones = new List() }; } } 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 }; } } } }