RevitHandler.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using Autodesk.Revit.UI;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace FWindSoft.Revit
  8. {
  9. /// <summary>
  10. /// 处理者,就是接收者
  11. /// </summary>
  12. public class RevitHandler : IExternalEventHandler
  13. {
  14. public RevitHandler() : this(Guid.NewGuid().ToString("N"))
  15. {
  16. }
  17. public RevitHandler(string name)
  18. {
  19. this.Name = name;
  20. }
  21. public RevitHandler(string name, Action<RevitEventArgs> action) : this(name)
  22. {
  23. if (action != null)
  24. {
  25. Executing += (s, e) => action(e);
  26. }
  27. }
  28. public RevitHandler(Action<RevitEventArgs> action) : this(Guid.NewGuid().ToString("N"), action)
  29. {
  30. }
  31. #region 接口逻辑实现
  32. public void Execute(UIApplication app)
  33. {
  34. this.UIApp = app;
  35. var args = new RevitEventArgs();
  36. args.UIApp = this.UIApp;
  37. args.Handler = this;
  38. OnPreExecute(args);
  39. if (args.Handled)
  40. return;
  41. OnExecute(args);
  42. if (args.Handled)
  43. return;
  44. OnPostExecute(args);
  45. }
  46. /// <summary>
  47. /// 命令名称
  48. /// </summary>
  49. public string Name { get; protected set; }
  50. public string GetName()
  51. {
  52. return Name;
  53. }
  54. #endregion
  55. /// <summary>
  56. /// 关联UIApp
  57. /// </summary>
  58. public UIApplication UIApp { get; private set; }
  59. #region 事件处理相关
  60. public event EventHandler<RevitEventArgs> PreExecute;
  61. public event EventHandler<RevitEventArgs> Executing;
  62. public event EventHandler<RevitEventArgs> PostExecute;
  63. public virtual void OnExecute(RevitEventArgs args)
  64. {
  65. if (Executing != null)
  66. {
  67. Executing(this, args);
  68. }
  69. }
  70. protected virtual void OnPreExecute(RevitEventArgs args)
  71. {
  72. if (PreExecute != null)
  73. {
  74. PreExecute(this, args);
  75. }
  76. }
  77. protected virtual void OnPostExecute(RevitEventArgs args)
  78. {
  79. if (PostExecute != null)
  80. {
  81. PostExecute(this, args);
  82. }
  83. }
  84. #endregion
  85. /// <summary>
  86. /// 关联参数
  87. /// </summary>
  88. public object Parameter { get; set; }
  89. }
  90. }