123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- using System;
- using System.Text;
- using Autodesk.Revit.DB.Events;
- using Autodesk.Revit.UI;
- using Autodesk.Revit.UI.Events;
- using FWindSoft.Events;
- namespace FWindSoft.Revit
- {
- [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
- [Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
- [Autodesk.Revit.Attributes.Journaling(Autodesk.Revit.Attributes.JournalingMode.NoCommandData)]
- public class ExternalApplication : IExternalApplication
- {
- #region 构造函数
- public ExternalApplication()
- {
- Events = new EventsWrapper();
- Registers = new RevitRegisterManager(true);
- }
- #endregion
- #region 私有相关
- /// <summary>
- /// 事件包装器(不是每一个事件都进行了包装)
- /// </summary>
- public EventsWrapper Events { get;private set; }
- private RevitRegisterManager Registers { get; set; }
- #endregion
- public static ExternalApplication CurrentApp{ get;private set; }
- public virtual Result OnStartup(UIControlledApplication application)
- {
- RevitVersion.AddInManager.CheckCancelAddin();
- CurrentApp = this;
- UIControlApp = application;
- #region 核心区,与ShutDown顺序相反相反
- #region 事件绑定
- Events.AddBindings(application);
- Events.AddBindings(application.ControlledApplication);
- #endregion
- Registers.AddEvents(this);
- AttachExecute(application);
- #endregion
- //加载application
- Idling += ExternalApplication_Idling;
- return Result.Succeeded;
- }
- /*此类由revit初始化
- */
- public virtual Result OnShutdown(UIControlledApplication application)
- {
- CurrentApp = null;
- UIControlApp = application;
- ShutExecute(application);
- Registers.RemoveEvents(this);
- #region 事件绑定
- Events.RemoveBindings(application);
- Events.RemoveBindings(application.ControlledApplication);
- #endregion
-
- return Result.Succeeded;
- }
- private static void ExternalApplication_Idling(object sender, IdlingEventArgs e)
- {
- RevitCore.InitCore((UIApplication) sender);
- if (CurrentApp != null)
- {
- CurrentApp.Idling -= ExternalApplication_Idling;
- }
- }
- #region 属性相关
- /// <summary>
- /// 关联UIControlApp
- /// </summary>
- public UIControlledApplication UIControlApp { get; private set; }
- #endregion
- #region 事件相关扩展
- public event EventHandler<IdlingEventArgs> Idling
- {
- add { Events["Idling"]?.AddEventHandler(value); }
- remove { Events["Idling"]?.RemoveEventHandler(value); }
- }
- public event EventHandler<DocumentOpenedEventArgs> DocumentOpened
- {
- add { Events["DocumentOpened"]?.AddEventHandler(value); }
- remove { Events["DocumentOpened"]?.RemoveEventHandler(value); }
- }
- #endregion
- #region 自定义扩展
- /// <summary>
- /// 附加启动时的初始化设置
- /// </summary>
- protected virtual void AttachExecute(UIControlledApplication application)
- {
- //读一个初始化的配置文件,然后顺序执行配置文件中的各项操作
- //1、定义一个命令度配置文件
- //2、根据配置文件中的相关设置,再进行执行
- DockableWindow win = new DockableWindow();
- win.RegisterDockableWindow(CurrentApp.UIControlApp, RevitCustomDockablePanels.Default);
- }
- /// <summary>
- /// 附加启动时的初始化设置
- /// </summary>
- protected virtual void ShutExecute(UIControlledApplication application)
- {
-
- }
- #endregion
- #region 未处理异常捕获
- /// <summary>
- /// 不会未处理异常
- /// </summary>
- protected void CatchUnhandledException()
- {
- AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
- }
- /// <summary>
- /// 去掉捕获未处理异常
- /// </summary>
- protected void NoCatchUnhandledException()
- {
- AppDomain.CurrentDomain.UnhandledException -= CurrentDomain_UnhandledException;
- }
- private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
- {
- StringBuilder builder = new StringBuilder();
- var exception = e.ExceptionObject as Exception;
- if (exception != null)
- {
- builder.AppendLine("Messsage:" + exception.Message);
- builder.AppendLine("Trace:" + exception.StackTrace);
- }
- else
- {
- builder.Append("未处理异常");
- }
- TaskDialog.Show("捕获异常", builder.ToString());
- }
- #endregion
- }
- }
|