如上图,有时候,我们需要对图元批量旋转指定角度,并且从图元的中心点旋转。
他的原理是使用api:ElementTransformUtils,可以实现您的意图,api详情如下:
using System.Collections.Generic;
namespace Autodesk.Revit.DB
{
public static class ElementTransformUtils
{
public static bool CanMirrorElement(Document ADoc, ElementId elemId);
public static bool CanMirrorElements(Document ADoc, ICollection<ElementId> elemIds);
public static ICollection<ElementId> CopyElement(Document document, ElementId elementToCopy, XYZ translation);
public static ICollection<ElementId> CopyElements(View sourceView, ICollection<ElementId> elementsToCopy, View destinationView, Transform additionalTransform, CopyPasteOptions options);
public static ICollection<ElementId> CopyElements(Document sourceDocument, ICollection<ElementId> elementsToCopy, Document destinationDocument, Transform transform, CopyPasteOptions options);
public static ICollection<ElementId> CopyElements(Document document, ICollection<ElementId> elementsToCopy, XYZ translation);
public static Transform GetTransformFromViewToView(View sourceView, View destinationView);
public static void MirrorElement(Document document, ElementId elementToMirror, Plane plane);
public static IList<ElementId> MirrorElements(Document document, ICollection<ElementId> elementsToMirror, Plane plane, bool mirrorCopies);
public static void MoveElement(Document document, ElementId elementToMove, XYZ translation);
public static void MoveElements(Document document, ICollection<ElementId> elementsToMove, XYZ translation);
public static void RotateElement(Document document, ElementId elementToRotate, Line axis, double angle);
public static void RotateElements(Document document, ICollection<ElementId> elementsToRotate, Line axis, double angle);
}
}
本文用到的是这个方法:
具体实现代码如下:
/// <summary>
/// 旋转
/// </summary>
/// <param name="references"></param>
private void Rotate(IList<Reference> references)
{
foreach (var reference in references)
{
var e = this._document.GetElement(reference);
var locationPoint = e.Location as LocationPoint;
if (locationPoint == null)
{
continue;
}
var point = locationPoint.Point;
var axis = Autodesk.Revit.DB.Line.CreateBound(point, new XYZ(point.X, point.Y, point.Z + 1));
var alpha = this._angle.ToRadian();
ElementTransformUtils.RotateElement(this._document, reference.ElementId, axis, alpha);
}
}
界面如下图:
当您点击确定后,需要您选择图元,图元数量不限,多选。然后如下图,点击完成即可实现:
源码下载地址:https://download.csdn.net/download/mazhiyuan1981/12901583