using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
namespace ShrlAlgoToolkit.RevitCore.Assists;
public class Updater : IUpdater
{
///
/// 应用程序范围的动态模型更新
///
///
///
///
/// true:只在本次生效,下次需要重新注册(不会发出警告)
///
public Updater(UIApplication UiApplication, Guid guid, Action execute, bool isOptional = true, string updateInfo = "DynamicModelUpdate")
{
updaterId = new UpdaterId(UiApplication.ActiveAddInId, guid);
this.action = execute;
this.updateInfo = updateInfo;
if (!UpdaterRegistry.IsUpdaterRegistered(updaterId))
{
UpdaterRegistry.RegisterUpdater(this, isOptional);
/*
* 对FamilyInstance元素的增加和删除监听
*
* 如果需要关注某些有自己程序创建出来的Element,可以把每个Element附上扩展数据
* 然后使用ExtensibleStorageFilter过滤器注册DMU即可
*
* DUM对用户的Ctrl + Z 无效, 可以在DocumentChanged事件中完善该机制
*/
}
}
private readonly Action action;
private readonly string updateInfo;
private readonly UpdaterId updaterId;
///
/// 当注册的元素发生ChangeType触发时,回调的函数
///
///
public void Execute(UpdaterData data)
{
//var rvtDoc = data.GetDocument();
//var idsModified = data.GetModifiedElementIds();
//var idsDeleted = data.GetDeletedElementIds();
//var idsAdded = data.GetAddedElementIds();
//可以根据类型、元素Id、过滤器等等,分情况执行更新的操作
action(data);
}
public void Enable()
{
UpdaterRegistry.EnableUpdater(updaterId);
}
public void Disable()
{
if (UpdaterRegistry.IsUpdaterEnabled(updaterId))
{
UpdaterRegistry.DisableUpdater(updaterId);
}
}
#region 接口实现
public string GetAdditionalInformation()
{
return updateInfo;
}
public ChangePriority GetChangePriority()
{
return ChangePriority.FreeStandingComponents;
}
public UpdaterId GetUpdaterId()
{
return updaterId;
}
public string GetUpdaterName()
{
return updateInfo;
}
#endregion
public void RemoveAllTriggers()
{
UpdaterRegistry.RemoveAllTriggers(updaterId);
}
///
/// 添加动态更新的对象和修改的类型的触发器
///
/// 元素类型
/// 修改的类型
public void AddClassTrigger(ChangeType change)
{
var filter = new ElementClassFilter(typeof(T));
UpdaterRegistry.AddTrigger(updaterId, filter, change);
}
public void AddFilterTrigger(ElementFilter filter, ChangeType change)
{
UpdaterRegistry.AddTrigger(updaterId, filter, change);
}
public void UnRegister()
{
UpdaterRegistry.UnregisterUpdater(updaterId);
}
}