RevitRegisterManager.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*-------------------------------------------------------------------------
  2. * 功能描述:RevitEventsBinding
  3. * 作者:xulisong
  4. * 创建时间: 2019/1/4 9:17:42
  5. * 版本号:v1.0
  6. * -------------------------------------------------------------------------*/
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Reflection;
  12. using System.Xml;
  13. using Autodesk.Revit.UI;
  14. using FWindSoft.Common;
  15. namespace FWindSoft.Revit
  16. {
  17. /*
  18. * 该策略未实用维护;
  19. * 2016后,事件不能随意的在程序中绑定,所以后续使用通过代理来完成
  20. */
  21. /// <summary>
  22. /// 事件绑定
  23. /// </summary>
  24. public class RevitRegisterManager
  25. {
  26. private const string DefaultConfig = "RevitRegister";
  27. public RevitRegisterManager()
  28. {
  29. Registers = new List<IRevitRegister>();
  30. }
  31. public RevitRegisterManager(bool loadDefault):this()
  32. {
  33. if (loadDefault)
  34. {
  35. LoadDefaultConfig();
  36. }
  37. }
  38. /// <summary>
  39. /// 存放注册配置文件的位置
  40. /// </summary>
  41. public static List<string> ConfigFiles { get; private set; } = new List<string>();
  42. #region 属性相关
  43. /// <summary>
  44. /// 关联引用程序
  45. /// </summary>
  46. public UIControlledApplication CurrentApp { get; protected set; }
  47. public List<IRevitRegister> Registers { get;private set; }
  48. #endregion
  49. public virtual void AddEvents(ExternalApplication application)
  50. {
  51. //必须解绑,才能释放实例
  52. CurrentApp = application.UIControlApp;
  53. if (Registers != null)
  54. {
  55. Registers.ForEach(r => r.Register(application));
  56. }
  57. }
  58. /// <summary>
  59. /// 移除绑定事件
  60. /// </summary>
  61. /// <param name="application"></param>
  62. public virtual void RemoveEvents(ExternalApplication application)
  63. {
  64. if (Registers != null)
  65. {
  66. Registers.ForEach(r => r.Unregister(application));
  67. }
  68. //必须解绑,才能释放实例
  69. CurrentApp = application.UIControlApp;
  70. }
  71. /// <summary>
  72. /// 加载指定目录下的接口信息
  73. /// </summary>
  74. /// <param name="filePath"></param>
  75. public void LoadConfig(string filePath)
  76. {
  77. if (!File.Exists(filePath)||Path.GetExtension(filePath)!=".xml")
  78. {
  79. return;
  80. }
  81. List<object> objects = new List<object>();
  82. var document = new XmlDocument();
  83. document.Load(filePath);
  84. var nodes = document.SelectNodes(string.Format($"//RevitRegister"));
  85. if (nodes == null)
  86. {
  87. return;
  88. }
  89. foreach (XmlNode node in nodes)
  90. {
  91. try
  92. {
  93. string dllName = node.SelectSingleNode("DllName")?.InnerText.Trim();
  94. string className = node.SelectSingleNode("ClassName")?.InnerText.Trim();
  95. if (string.IsNullOrWhiteSpace(dllName) || string.IsNullOrWhiteSpace(className))
  96. {
  97. continue;
  98. }
  99. string path = Path.Combine(App.AppRunPath, dllName);
  100. if (File.Exists(path))
  101. {
  102. Assembly tempAsembly = Assembly.LoadFrom(path);
  103. objects.Add(tempAsembly.CreateInstance(className));
  104. }
  105. }
  106. catch (Exception e)
  107. {
  108. }
  109. }
  110. Registers.AddRange(objects.OfType<IRevitRegister>());
  111. }
  112. public void LoadDefaultConfig()
  113. {
  114. List<string> files = new List<string>();
  115. var fileName = Path.Combine(App.DllRunPath, DefaultConfig + ".xml");
  116. #region 路径获取
  117. if (!File.Exists(fileName))
  118. {
  119. var directory = Path.Combine(App.DllRunPath, DefaultConfig);
  120. var tempDirectors = new List<string>() {directory};
  121. for (int i = 0; i < tempDirectors.Count; i++)
  122. {
  123. var currentPath = tempDirectors[i];
  124. if (!Directory.Exists(currentPath))
  125. {
  126. continue;
  127. }
  128. files.AddRange(Directory.GetFiles(currentPath));
  129. tempDirectors.AddRange(Directory.GetDirectories(currentPath));
  130. }
  131. }
  132. else
  133. {
  134. files.Add(fileName);
  135. }
  136. #endregion
  137. files.AddRange(ConfigFiles);
  138. files.ForEach(s => LoadConfig(s));
  139. }
  140. }
  141. }