109 lines
3.4 KiB
C#
109 lines
3.4 KiB
C#
using Autodesk.Revit.DB;
|
||
using Autodesk.Revit.UI;
|
||
|
||
namespace ShrlAlgoToolkit.RevitCore.Assists;
|
||
|
||
public class Updater : IUpdater
|
||
{
|
||
/// <summary>
|
||
/// 应用程序范围的动态模型更新
|
||
/// </summary>
|
||
/// <param name="UiApplication"></param>
|
||
/// <param name="guid"></param>
|
||
/// <param name="execute"></param>
|
||
/// <param name="isOptional">true:只在本次生效,下次需要重新注册(不会发出警告)</param>
|
||
/// <param name="updateInfo"></param>
|
||
public Updater(UIApplication UiApplication, Guid guid, Action<UpdaterData> 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<UpdaterData> action;
|
||
private readonly string updateInfo;
|
||
private readonly UpdaterId updaterId;
|
||
/// <summary>
|
||
/// 当注册的元素发生ChangeType触发时,回调的函数
|
||
/// </summary>
|
||
/// <param name="data"></param>
|
||
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);
|
||
}
|
||
/// <summary>
|
||
/// 添加动态更新的对象和修改的类型的触发器
|
||
/// </summary>
|
||
/// <typeparam name="T">元素类型</typeparam>
|
||
/// <param name="change">修改的类型</param>
|
||
public void AddClassTrigger<T>(ChangeType change)
|
||
{
|
||
var filter = new ElementClassFilter(typeof(T));
|
||
UpdaterRegistry.AddTrigger(updaterId, filter, change);
|
||
}
|
||
public void AddFilterTrigger<T>(ElementFilter filter, ChangeType change)
|
||
{
|
||
UpdaterRegistry.AddTrigger(updaterId, filter, change);
|
||
}
|
||
|
||
public void UnRegister()
|
||
{
|
||
UpdaterRegistry.UnregisterUpdater(updaterId);
|
||
}
|
||
} |