IdlingEventCommand.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using Autodesk.Revit.UI;
  2. using Autodesk.Revit.UI.Events;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace FWindSoft.Revit
  9. {
  10. /// <summary>
  11. /// 业务轮询命令
  12. /// </summary>
  13. public class IdlingEventCommand: IRevitRegister
  14. {
  15. #region 初始化
  16. public IdlingEventCommand()
  17. {
  18. Commands = new IdlingCommands();
  19. }
  20. /// <summary>
  21. /// 命令队列
  22. /// </summary>
  23. private readonly IdlingCommands Commands;
  24. #endregion
  25. #region 消费队列
  26. /// <summary>
  27. /// 对接事件绑定
  28. /// </summary>
  29. /// <param name="sender"></param>
  30. /// <param name="e"></param>
  31. protected void ApplicaionIdling(object sender, IdlingEventArgs e)
  32. {
  33. IdlingCommand currentCommand = Commands.Dequeue();
  34. if (currentCommand != null)
  35. {
  36. currentCommand.Raise(sender as UIApplication);
  37. e.SetRaiseWithoutDelay();
  38. }
  39. }
  40. #endregion
  41. #region 生产队列
  42. public IdlingCommand OnceExecute(Func<RevitEventArgs, HandlerResult> handler, Action<RevitEventArgs> callBack)
  43. {
  44. IdlingCommand command = new IdlingCommand(RevitHandlerFactory.CreateOnceHandler(handler, callBack));
  45. Commands.Enqueue(command);
  46. return command;
  47. }
  48. public IdlingCommand WhileExecute(Func<RevitEventArgs, HandlerResult> handler, Action<RevitEventArgs> callBack)
  49. {
  50. IdlingCommand command = new IdlingCommand(RevitHandlerFactory.CreateWhileHandler(handler, callBack));
  51. Commands.Enqueue(command);
  52. return command;
  53. }
  54. public IdlingCommand Execute(IdlingCommand command)
  55. {
  56. if (command == null)
  57. return null;
  58. Commands.Enqueue(command);
  59. return command;
  60. }
  61. #endregion
  62. #region 事件注册相关
  63. public void Register(ExternalApplication application)
  64. {
  65. application.Idling += ApplicaionIdling;
  66. }
  67. public void Unregister(ExternalApplication application)
  68. {
  69. application.Idling -= ApplicaionIdling;
  70. }
  71. #endregion
  72. }
  73. }