添加项目文件。
This commit is contained in:
51
RevitTools.Utils/Revit/ExEventHandle.cs
Normal file
51
RevitTools.Utils/Revit/ExEventHandle.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
using Autodesk.Revit.DB;
|
||||
using Autodesk.Revit.UI;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
|
||||
namespace RevitTools.Utils.Revit
|
||||
{
|
||||
/// <summary>
|
||||
/// 外部事件处理程序
|
||||
/// </summary>
|
||||
public class ExEventHandle : IExternalEventHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// 创建具有指定名字的外部事件处理程序
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
public ExEventHandle(string name)
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
private readonly string name;
|
||||
|
||||
/// <summary>
|
||||
/// 工作项队列
|
||||
/// </summary>
|
||||
internal readonly ConcurrentQueue<ExEventWorkItem> ExEventQueue = new ConcurrentQueue<ExEventWorkItem>();
|
||||
|
||||
/// <summary>
|
||||
/// 执行外部事件函数
|
||||
/// </summary>
|
||||
/// <param name="app"></param>
|
||||
public void Execute(UIApplication app)
|
||||
{
|
||||
while (ExEventQueue.TryDequeue(out ExEventWorkItem workItem))
|
||||
{
|
||||
workItem.InvokeAction(app);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取字符串标识
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string GetName()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
}
|
||||
}
|
||||
78
RevitTools.Utils/Revit/ExEventUtils.cs
Normal file
78
RevitTools.Utils/Revit/ExEventUtils.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
using Autodesk.Revit.UI;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RevitTools.Utils.Revit
|
||||
{
|
||||
/// <summary>
|
||||
/// 外部事件封装类
|
||||
/// </summary>
|
||||
public class ExEventUtils
|
||||
{
|
||||
/// <summary>
|
||||
/// 创建外部事件
|
||||
/// </summary>
|
||||
/// <param name="handleName"></param>
|
||||
private ExEventUtils(string handleName)
|
||||
{
|
||||
this.Handle = new ExEventHandle(handleName);
|
||||
this.ExEvent = ExternalEvent.Create(this.Handle);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 外部事件实例
|
||||
/// </summary>
|
||||
private readonly ExternalEvent ExEvent;
|
||||
|
||||
/// <summary>
|
||||
/// 外部事件处理程序
|
||||
/// </summary>
|
||||
private readonly ExEventHandle Handle;
|
||||
|
||||
/// <summary>
|
||||
/// 在外部事件中执行函数, 可以等待函数执行完成
|
||||
/// </summary>
|
||||
/// <param name="action">要执行的函数</param>
|
||||
/// <param name="transactionGroupName">事务组名</param>
|
||||
/// <returns></returns>
|
||||
public async Task InvokeAsync(Action<UIApplication> action)
|
||||
{
|
||||
var workItem = new ExEventWorkItem(action);
|
||||
Handle.ExEventQueue.Enqueue(workItem);
|
||||
ExEvent.Raise();
|
||||
await workItem.CompletionSource.Task;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 在外部事件中执行函数, 并且可以等待函数执行完成
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="func">要执行的函数</param>
|
||||
/// <returns></returns>
|
||||
public async Task<T> InvokeAsync<T>(Func<UIApplication, T> func)
|
||||
{
|
||||
object func1(UIApplication app)
|
||||
{
|
||||
return func(app);
|
||||
}
|
||||
var workItem = new ExEventWorkItem(func1);
|
||||
Handle.ExEventQueue.Enqueue(workItem);
|
||||
ExEvent.Raise();
|
||||
var result = await workItem.CompletionSource.Task;
|
||||
return (T)result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取或创建外部事件处理实例
|
||||
/// </summary>
|
||||
/// <param name="name">外部事件处理程序名称</param>
|
||||
/// <returns></returns>
|
||||
public static ExEventUtils GetOrCreate(string name = "ScriptPadExEventHandle")
|
||||
{
|
||||
return EventUtils.GetOrAdd(name, new ExEventUtils(name));
|
||||
}
|
||||
|
||||
private static readonly ConcurrentDictionary<string, ExEventUtils> EventUtils = new ConcurrentDictionary<string, ExEventUtils>();
|
||||
}
|
||||
}
|
||||
64
RevitTools.Utils/Revit/ExEventWorkItem.cs
Normal file
64
RevitTools.Utils/Revit/ExEventWorkItem.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using Autodesk.Revit.UI;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RevitTools.Utils.Revit
|
||||
{
|
||||
/// <summary>
|
||||
/// 外部事件工作项
|
||||
/// </summary>
|
||||
internal class ExEventWorkItem
|
||||
{
|
||||
/// <summary>
|
||||
/// 异步阻塞信号
|
||||
/// </summary>
|
||||
public TaskCompletionSource<object> CompletionSource { get; protected set; }
|
||||
|
||||
/// <summary>
|
||||
/// 在外部事件中执行的, 带有一个返回值的函数
|
||||
/// </summary>
|
||||
private Action<UIApplication> Action { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 在外部事件中执行的, 带有一个返回值的函数
|
||||
/// </summary>
|
||||
private Func<UIApplication, object> Func { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 执行外部事件函数
|
||||
/// </summary>
|
||||
/// <param name="application"></param>
|
||||
public void InvokeAction(UIApplication application)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (Action != null)
|
||||
{
|
||||
Action.Invoke(application);
|
||||
this.CompletionSource.TrySetResult(null);
|
||||
}
|
||||
else
|
||||
{
|
||||
var result = Func.Invoke(application);
|
||||
this.CompletionSource.TrySetResult(result);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this.CompletionSource.TrySetException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public ExEventWorkItem(Action<UIApplication> action)
|
||||
{
|
||||
this.CompletionSource = new TaskCompletionSource<object>();
|
||||
this.Action = action;
|
||||
}
|
||||
|
||||
public ExEventWorkItem(Func<UIApplication, object> action)
|
||||
{
|
||||
this.CompletionSource = new TaskCompletionSource<object>();
|
||||
this.Func = action;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user