ExternalEventCommand.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using Autodesk.Revit.UI;
  2. namespace FWindSoft.Revit
  3. {
  4. /*
  5. * 验证是否是同步,不是同步的话,则需要提供回调函数
  6. */
  7. /// <summary>
  8. /// 扩展事件
  9. /// </summary>
  10. public class ExternalEventCommand
  11. {
  12. public ExternalEventCommand(RevitHandler handler) : this(handler, true)
  13. {
  14. }
  15. public ExternalEventCommand(RevitHandler handler, bool createRefCommand)
  16. {
  17. Handler = handler;
  18. if (createRefCommand)
  19. {
  20. CreateEvent();
  21. }
  22. }
  23. /// <summary>
  24. /// 命令实际处理者
  25. /// </summary>
  26. public RevitHandler Handler { get; private set; }
  27. /// <summary>
  28. /// 生成Event
  29. /// </summary>
  30. /// <returns></returns>
  31. public ExternalEvent CreateEvent()
  32. {
  33. if (Handler != null && Event == null)
  34. {
  35. Event = ExternalEvent.Create(Handler);
  36. }
  37. return Event;
  38. }
  39. #region 关联扩展事件
  40. /// <summary>
  41. /// 扩展事件的命令
  42. /// </summary>
  43. public ExternalEvent Event { get; private set; }
  44. /// <summary>
  45. /// 触发命令
  46. /// </summary>
  47. /// <returns></returns>
  48. public ExternalEventRequest Raise()
  49. {
  50. #region 如果关联命令为空,创建一次
  51. if (Event == null)
  52. {
  53. CreateEvent();
  54. }
  55. #endregion
  56. if (Event == null)
  57. {
  58. return ExternalEventRequest.Pending;
  59. }
  60. return Event.Raise();
  61. }
  62. /// <summary>
  63. /// 根据指定参数触发命令
  64. /// </summary>
  65. /// <param name="parameter">传入参数,在回调方法参数中可以找打使用</param>
  66. /// <returns></returns>
  67. public ExternalEventRequest Raise(object parameter)
  68. {
  69. if (Handler != null)
  70. {
  71. Handler.Parameter = parameter;
  72. }
  73. #region 如果关联命令为空,创建一次
  74. if (Event == null)
  75. {
  76. CreateEvent();
  77. }
  78. #endregion
  79. if (Event == null)
  80. {
  81. return ExternalEventRequest.Pending;
  82. }
  83. return Event.Raise();
  84. }
  85. #endregion
  86. #region 析构处理
  87. ~ExternalEventCommand()
  88. {
  89. if (Event != null)
  90. Event.Dispose();
  91. }
  92. #endregion
  93. }
  94. }