/*------------------------------------------------------------------------- * 功能描述:RevitEventsBinding * 作者:xulisong * 创建时间: 2019/1/4 9:17:42 * 版本号:v1.0 * -------------------------------------------------------------------------*/ using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Xml; using Autodesk.Revit.UI; using FWindSoft.Common; namespace FWindSoft.Revit { /* * 该策略未实用维护; * 2016后,事件不能随意的在程序中绑定,所以后续使用通过代理来完成 */ /// /// 事件绑定 /// public class RevitRegisterManager { private const string DefaultConfig = "RevitRegister"; public RevitRegisterManager() { Registers = new List(); } public RevitRegisterManager(bool loadDefault):this() { if (loadDefault) { LoadDefaultConfig(); } } /// /// 存放注册配置文件的位置 /// public static List ConfigFiles { get; private set; } = new List(); #region 属性相关 /// /// 关联引用程序 /// public UIControlledApplication CurrentApp { get; protected set; } public List Registers { get;private set; } #endregion public virtual void AddEvents(ExternalApplication application) { //必须解绑,才能释放实例 CurrentApp = application.UIControlApp; if (Registers != null) { Registers.ForEach(r => r.Register(application)); } } /// /// 移除绑定事件 /// /// public virtual void RemoveEvents(ExternalApplication application) { if (Registers != null) { Registers.ForEach(r => r.Unregister(application)); } //必须解绑,才能释放实例 CurrentApp = application.UIControlApp; } /// /// 加载指定目录下的接口信息 /// /// public void LoadConfig(string filePath) { if (!File.Exists(filePath)||Path.GetExtension(filePath)!=".xml") { return; } List objects = new List(); var document = new XmlDocument(); document.Load(filePath); var nodes = document.SelectNodes(string.Format($"//RevitRegister")); if (nodes == null) { return; } foreach (XmlNode node in nodes) { try { string dllName = node.SelectSingleNode("DllName")?.InnerText.Trim(); string className = node.SelectSingleNode("ClassName")?.InnerText.Trim(); if (string.IsNullOrWhiteSpace(dllName) || string.IsNullOrWhiteSpace(className)) { continue; } string path = Path.Combine(App.AppRunPath, dllName); if (File.Exists(path)) { Assembly tempAsembly = Assembly.LoadFrom(path); objects.Add(tempAsembly.CreateInstance(className)); } } catch (Exception e) { } } Registers.AddRange(objects.OfType()); } public void LoadDefaultConfig() { List files = new List(); var fileName = Path.Combine(App.DllRunPath, DefaultConfig + ".xml"); #region 路径获取 if (!File.Exists(fileName)) { var directory = Path.Combine(App.DllRunPath, DefaultConfig); var tempDirectors = new List() {directory}; for (int i = 0; i < tempDirectors.Count; i++) { var currentPath = tempDirectors[i]; if (!Directory.Exists(currentPath)) { continue; } files.AddRange(Directory.GetFiles(currentPath)); tempDirectors.AddRange(Directory.GetDirectories(currentPath)); } } else { files.Add(fileName); } #endregion files.AddRange(ConfigFiles); files.ForEach(s => LoadConfig(s)); } } }