前言
这个例子介绍了如何用 Revit 的置换图元功能做动画。
内容
什么是置换图元?
置换图元仅仅是用来显示的,不会修改原始构件。
通过它可以为各个构件创建一个置换图元,可以通过修改图元的位置,达到下面的效果。
选中通过 RevitLookup 可以看到,置换图元有自己的类型,DisplacementElement:
SDK 的例子
效果
例子中只针对五种类型的结构构件做了处理:
BuiltInCategory.OST_StructuralFoundation
BuiltInCategory.OST_StructuralFraming
BuiltInCategory.OST_Floors
BuiltInCategory.OST_Walls
BuiltInCategory.OST_StructuralColumns
主要逻辑
步骤:
- 分别过滤出五类结构构件
- 为每类构件创建置换图元
- 注册 Revit 的 Idling 事件以及 timer 事件
- 通过上述两个事件对置换图元的位置进行更新
创建 DisplacementElement
接口:DisplacementElement.Create
创建置换图元,注意可以设置父子关系:
private void BuildDisplacementElementGroup(Document doc, ICollection<ElementId> ids, View view)
{
// The last element will be in the "parent" displacement element. (At least one element
// must remain assigned to the parent, so all child displacements will be relative to the
// parent's displacement.)
Element lastElement = doc.GetElement(ids.Last<ElementId>());
XYZ parentDisplacedLocation = GetDisplacementXYFor(lastElement, XYZ.Zero);
parentDisplacedLocation = MoveToElevation(parentDisplacedLocation, initialHeight);
// All elements are added to the parent displacement element.
DisplacementElement parent = DisplacementElement.Create(doc, ids, parentDisplacedLocation, view, null);
m_displacementElements.Add(parent);
int count = ids.Count;
List<ElementId> childIds = new List<ElementId>();
List<ElementId> idsList = ids.ToList<ElementId>();
// Add all elements except the last one to child displacement elements
for (int index = 0; index < count - 1; index++)
{
ElementId childId = idsList[index];
Element e = doc.GetElement(childId);
XYZ displacedLocation = GetDisplacementXYFor(e, parentDisplacedLocation);
// Setup id container for child DisplacementElement creation
childIds.Clear();
childIds.Add(childId);
DisplacementElement child = DisplacementElement.Create(doc, childIds, displacedLocation, view, parent);
}
view.HideElements(ids);
}
其他逻辑可以参考源码:Revit 2021 SDK\Samples\DisplacementElementAnimation\CS\DisplacementStructureModelAnimator.cs