using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FWindSoft.Revit
{
///
/// 守护命令【该命令队列,可控制是否移除】
///
public class GuardianIdlingCommand : IRevitRegister
{
public List Commands { get; private set; }
public GuardianIdlingCommand()
{
Commands = new List();
}
#region 关联命令
///
/// 对接事件绑定
///
///
///
public void ApplicaionIdling(object sender, IdlingEventArgs e)
{
var application = sender as UIApplication;
foreach (var command in Commands)
{
command.Raise(application);
}
e.SetRaiseWithoutDelay();
}
#endregion
#region 操作命令
//添加,移除
public IdlingCommand AddExecute(Func handler)
{
IdlingCommand command = new IdlingCommand(RevitHandlerFactory.CreateOnceHandler(handler));
Commands.Add(command);
return command;
}
public IdlingCommand AddExecute(IdlingCommand command)
{
if (command == null)
return null;
Commands.Add(command);
return command;
}
public void RemoveExecute(IdlingCommand command)
{
if (command == null)
return;
Commands.Remove(command);
}
public void RemoveExecute(string commandName)
{
if (string.IsNullOrWhiteSpace(commandName))
return;
Commands.RemoveAll(command=>command.Handler?.Name==commandName);
}
#endregion
#region 事件注册相关
public void Register(ExternalApplication application)
{
application.Idling += ApplicaionIdling;
}
public void Unregister(ExternalApplication application)
{
application.Idling -= ApplicaionIdling;
}
#endregion
}
}