前言
这个例子介绍了利用 idling 事件,在事件处理中利用 Analysis Visualisation Framework (AVF) 分析可视化框架来更新墙的一个面的颜色。
内容
实现的效果:
这个例子有两个个关键点:
- idling event + thread
- Analysis Visualisation Framework (AVF)
核心逻辑1 - idling handler
通过 uiapp.Idling
注册一个事件处理函数:
m_hIdling = new EventHandler<IdlingEventArgs>(IdlingHandler);
uiapp.Idling += m_hIdling;
处理函数的核心是m_analyzer.UpdateResults
:
public void IdlingHandler(object sender, IdlingEventArgs args)
{
bool processing = false;
if (m_analyzer != null)
{
UIApplication uiapp = sender as UIApplication;
if (uiapp.ActiveUIDocument != null)
{
using (Transaction trans = new Transaction(uiapp.ActiveUIDocument.Document))
{
trans.Start("bogus transaction");
processing = m_analyzer.UpdateResults();
trans.Commit();
}
// 通知 Revit 去进行更新
args.SetRaiseWithoutDelay();
}
}
// 如果分析结束,就取消注册消息处理函数
if (!processing)
{
UnsubscribeFromIdling(sender as UIApplication);
m_analyzer = null;
}
}
Analysis Visualisation Framework (AVF)
Dynamo For Revit Node: Analysis Visualisation Framework (AVF) 分析可视化框架
FaceAnalyzer 是通过启动另外一个线程,调用 AVF 框架让选中的墙面逐渐改变颜色。
启动另外一个线程的逻辑,这个和多线程编程没有任何区别,它是通过m_analyzer.UpdateResults
层层调用过来的:
// Revit 2021 SDK\Samples\MultiThreading\WorkThread\CS\ThreadAgent.cs
m_thread = new Thread(new ParameterizedThreadStart(this.Run));
m_thread.Start(m_results);
在 Run 的方法里面做了AVF 需要的数据的更新。
其它
这个例子介绍的内容表明,在 Revit 的 idling 事件里面可以使用线程。但线程的使用肯定是有限制的,显然,我们不能在线程里面去改变 Revit 模型的内容。