ExternalApplication.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using System.Text;
  3. using Autodesk.Revit.DB.Events;
  4. using Autodesk.Revit.UI;
  5. using Autodesk.Revit.UI.Events;
  6. using FWindSoft.Events;
  7. namespace FWindSoft.Revit
  8. {
  9. [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
  10. [Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
  11. [Autodesk.Revit.Attributes.Journaling(Autodesk.Revit.Attributes.JournalingMode.NoCommandData)]
  12. public class ExternalApplication : IExternalApplication
  13. {
  14. #region 构造函数
  15. public ExternalApplication()
  16. {
  17. Events = new EventsWrapper();
  18. Registers = new RevitRegisterManager(true);
  19. }
  20. #endregion
  21. #region 私有相关
  22. /// <summary>
  23. /// 事件包装器(不是每一个事件都进行了包装)
  24. /// </summary>
  25. public EventsWrapper Events { get;private set; }
  26. private RevitRegisterManager Registers { get; set; }
  27. #endregion
  28. public static ExternalApplication CurrentApp{ get;private set; }
  29. public virtual Result OnStartup(UIControlledApplication application)
  30. {
  31. RevitVersion.AddInManager.CheckCancelAddin();
  32. CurrentApp = this;
  33. UIControlApp = application;
  34. #region 核心区,与ShutDown顺序相反相反
  35. #region 事件绑定
  36. Events.AddBindings(application);
  37. Events.AddBindings(application.ControlledApplication);
  38. #endregion
  39. Registers.AddEvents(this);
  40. AttachExecute(application);
  41. #endregion
  42. //加载application
  43. Idling += ExternalApplication_Idling;
  44. return Result.Succeeded;
  45. }
  46. /*此类由revit初始化
  47. */
  48. public virtual Result OnShutdown(UIControlledApplication application)
  49. {
  50. CurrentApp = null;
  51. UIControlApp = application;
  52. ShutExecute(application);
  53. Registers.RemoveEvents(this);
  54. #region 事件绑定
  55. Events.RemoveBindings(application);
  56. Events.RemoveBindings(application.ControlledApplication);
  57. #endregion
  58. return Result.Succeeded;
  59. }
  60. private static void ExternalApplication_Idling(object sender, IdlingEventArgs e)
  61. {
  62. RevitCore.InitCore((UIApplication) sender);
  63. if (CurrentApp != null)
  64. {
  65. CurrentApp.Idling -= ExternalApplication_Idling;
  66. }
  67. }
  68. #region 属性相关
  69. /// <summary>
  70. /// 关联UIControlApp
  71. /// </summary>
  72. public UIControlledApplication UIControlApp { get; private set; }
  73. #endregion
  74. #region 事件相关扩展
  75. public event EventHandler<IdlingEventArgs> Idling
  76. {
  77. add { Events["Idling"]?.AddEventHandler(value); }
  78. remove { Events["Idling"]?.RemoveEventHandler(value); }
  79. }
  80. public event EventHandler<DocumentOpenedEventArgs> DocumentOpened
  81. {
  82. add { Events["DocumentOpened"]?.AddEventHandler(value); }
  83. remove { Events["DocumentOpened"]?.RemoveEventHandler(value); }
  84. }
  85. #endregion
  86. #region 自定义扩展
  87. /// <summary>
  88. /// 附加启动时的初始化设置
  89. /// </summary>
  90. protected virtual void AttachExecute(UIControlledApplication application)
  91. {
  92. //读一个初始化的配置文件,然后顺序执行配置文件中的各项操作
  93. //1、定义一个命令度配置文件
  94. //2、根据配置文件中的相关设置,再进行执行
  95. DockableWindow win = new DockableWindow();
  96. win.RegisterDockableWindow(CurrentApp.UIControlApp, RevitCustomDockablePanels.Default);
  97. }
  98. /// <summary>
  99. /// 附加启动时的初始化设置
  100. /// </summary>
  101. protected virtual void ShutExecute(UIControlledApplication application)
  102. {
  103. }
  104. #endregion
  105. #region 未处理异常捕获
  106. /// <summary>
  107. /// 不会未处理异常
  108. /// </summary>
  109. protected void CatchUnhandledException()
  110. {
  111. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
  112. }
  113. /// <summary>
  114. /// 去掉捕获未处理异常
  115. /// </summary>
  116. protected void NoCatchUnhandledException()
  117. {
  118. AppDomain.CurrentDomain.UnhandledException -= CurrentDomain_UnhandledException;
  119. }
  120. private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  121. {
  122. StringBuilder builder = new StringBuilder();
  123. var exception = e.ExceptionObject as Exception;
  124. if (exception != null)
  125. {
  126. builder.AppendLine("Messsage:" + exception.Message);
  127. builder.AppendLine("Trace:" + exception.StackTrace);
  128. }
  129. else
  130. {
  131. builder.Append("未处理异常");
  132. }
  133. TaskDialog.Show("捕获异常", builder.ToString());
  134. }
  135. #endregion
  136. }
  137. }