摘要
通过 Revit API 来对模型中的构件进行移动和旋转等操作是一个非常基本的需求。Revit API 当然也提供了这个功能,并且把它打包在一个类中,方便开发者使用。这个类就是 ElementTransformUtils
。
细节
先从一个例子开始,然后介绍类 ElementTransformUtils
。
例子
可以直接打开 Revit 的“宏”这项功能,把下面这段代码贴上去,编译完成之后,就可以使用了。他们分别完成了移动和旋转的功能:ElementTransformUtils.MoveElement
和 ElementTransformUtils.RotateElement
。
public void MoveElement()
{
Reference refer = Application.ActiveUIDocument.Selection.PickObject(ObjectType.Element);
Transaction tran = new Transaction(Document, "Move Element");
tran.Start();
ElementTransformUtils.MoveElement(Document, refer.ElementId, new XYZ(10, 10, 10));
tran.Commit();
}
public void rotateElement()
{
Reference refer = Application.ActiveUIDocument.Selection.PickObject(ObjectType.Element);
Line zAxis = Line.CreateBound(new XYZ(0, 0, 0), new XYZ(0, 0, 1));
Transaction tran = new Transaction(Document, "Move Element");
tran.Start();
ElementTransformUtils.RotateElement(Document, refer.ElementId, zAxis, 90);
tran.Commit();
}
ElementTransformUtils
可以实现四种操作:
名称 | 接口 |
---|---|
复制 | ElementTransformUtils::CopyElement(s) |
镜像 | ElementTransformUtils::MirrorElement(s) |
移动 | ElementTransformUtils::MoveElement(s) |
旋转 | ElementTransformUtils::RotateElement(s) |
每种操作都是大同小异,并且都会对文档进行修改。