123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- /*-------------------------------------------------------------------------
- * 功能描述: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后,事件不能随意的在程序中绑定,所以后续使用通过代理来完成
- */
- /// <summary>
- /// 事件绑定
- /// </summary>
- public class RevitRegisterManager
- {
- private const string DefaultConfig = "RevitRegister";
- public RevitRegisterManager()
- {
- Registers = new List<IRevitRegister>();
-
- }
- public RevitRegisterManager(bool loadDefault):this()
- {
- if (loadDefault)
- {
- LoadDefaultConfig();
- }
- }
- /// <summary>
- /// 存放注册配置文件的位置
- /// </summary>
- public static List<string> ConfigFiles { get; private set; } = new List<string>();
- #region 属性相关
- /// <summary>
- /// 关联引用程序
- /// </summary>
- public UIControlledApplication CurrentApp { get; protected set; }
- public List<IRevitRegister> Registers { get;private set; }
- #endregion
- public virtual void AddEvents(ExternalApplication application)
- {
- //必须解绑,才能释放实例
- CurrentApp = application.UIControlApp;
- if (Registers != null)
- {
- Registers.ForEach(r => r.Register(application));
- }
- }
- /// <summary>
- /// 移除绑定事件
- /// </summary>
- /// <param name="application"></param>
- public virtual void RemoveEvents(ExternalApplication application)
- {
- if (Registers != null)
- {
- Registers.ForEach(r => r.Unregister(application));
- }
- //必须解绑,才能释放实例
- CurrentApp = application.UIControlApp;
- }
- /// <summary>
- /// 加载指定目录下的接口信息
- /// </summary>
- /// <param name="filePath"></param>
- public void LoadConfig(string filePath)
- {
- if (!File.Exists(filePath)||Path.GetExtension(filePath)!=".xml")
- {
- return;
- }
- List<object> objects = new List<object>();
- 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<IRevitRegister>());
- }
- public void LoadDefaultConfig()
- {
- List<string> files = new List<string>();
- var fileName = Path.Combine(App.DllRunPath, DefaultConfig + ".xml");
- #region 路径获取
- if (!File.Exists(fileName))
- {
- var directory = Path.Combine(App.DllRunPath, DefaultConfig);
- var tempDirectors = new List<string>() {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));
- }
- }
- }
|