Files
ShrlAlgoToolkit/Sai.Toolkit.Revit/Helpers/DMUAssist.cs

149 lines
4.8 KiB
C#
Raw Normal View History

2024-09-22 11:05:41 +08:00
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Sai.Toolkit.Revit.Assist;
namespace Sai.Toolkit.Revit.Helpers
{
/// <summary>
/// 动态模型更新
/// </summary>
/// <typeparam name="T"></typeparam>
public static class DMUAssist
{
public static void AddTrigger(this IUpdater updater, Document document, ElementFilter filter, ChangeType change)
{
UpdaterRegistry.AddTrigger(updater.GetUpdaterId(), document, filter, change);
}
}
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元素的增加和删除监听
*
* ElementElement附上扩展数据
* 使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);
}
}
//internal class test
//{
// private readonly ChangeType change = null;
// private readonly ICollection<ElementId> elements = null;
// private readonly uiApplication uiApplication = null;
// private Guid guid = new();
// public void MyMethod()
// {
// var guid = new Guid();
// Updater updater = new Updater
// {
// addinID = null,
// Guid = guid
// };
// DMUAssist<Updater> dynamicModel =
// new DMUAssist<Updater>(uiApplication, elements, change);
// dynamicModel.UseUpdater();
// }
//}
}