GuardianIdlingCommand.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 GuardianIdlingCommand : IRevitRegister
  14. {
  15. public List<IdlingCommand> Commands { get; private set; }
  16. public GuardianIdlingCommand()
  17. {
  18. Commands = new List<IdlingCommand>();
  19. }
  20. #region 关联命令
  21. /// <summary>
  22. /// 对接事件绑定
  23. /// </summary>
  24. /// <param name="sender"></param>
  25. /// <param name="e"></param>
  26. public void ApplicaionIdling(object sender, IdlingEventArgs e)
  27. {
  28. var application = sender as UIApplication;
  29. foreach (var command in Commands)
  30. {
  31. command.Raise(application);
  32. }
  33. e.SetRaiseWithoutDelay();
  34. }
  35. #endregion
  36. #region 操作命令
  37. //添加,移除
  38. public IdlingCommand AddExecute(Func<RevitEventArgs, HandlerResult> handler)
  39. {
  40. IdlingCommand command = new IdlingCommand(RevitHandlerFactory.CreateOnceHandler(handler));
  41. Commands.Add(command);
  42. return command;
  43. }
  44. public IdlingCommand AddExecute(IdlingCommand command)
  45. {
  46. if (command == null)
  47. return null;
  48. Commands.Add(command);
  49. return command;
  50. }
  51. public void RemoveExecute(IdlingCommand command)
  52. {
  53. if (command == null)
  54. return;
  55. Commands.Remove(command);
  56. }
  57. public void RemoveExecute(string commandName)
  58. {
  59. if (string.IsNullOrWhiteSpace(commandName))
  60. return;
  61. Commands.RemoveAll(command=>command.Handler?.Name==commandName);
  62. }
  63. #endregion
  64. #region 事件注册相关
  65. public void Register(ExternalApplication application)
  66. {
  67. application.Idling += ApplicaionIdling;
  68. }
  69. public void Unregister(ExternalApplication application)
  70. {
  71. application.Idling -= ApplicaionIdling;
  72. }
  73. #endregion
  74. }
  75. }