在项目中遇到的一个问题,想把管道中的中心点投影到一个剖切视图上面,然后研究了一番,写了一个通用的方法,现在来记录一下。先上代码,后讲解~~~
1、点投影到平面的方法(如下)
/// <summary>
/// 点投影到一个平面
/// </summary>
/// <param name=""></param>
/// <param name="xyz"></param>
/// <returns></returns>
public static XYZ PointProjectPlane(this Plane plane, XYZ xyz)
{
Transform tf = Transform.Identity;
tf.BasisX = plane.XVec;
tf.BasisY = plane.YVec;
tf.BasisZ = plane.Normal;
tf.Origin = plane.Origin;
XYZ p = tf.Inverse.OfPoint(xyz);
p = new XYZ(p.X, p.Y, 0);
return tf.OfPoint(p);
}
2、使用(注意:当前视图是在剖面视图下)
UIDocument uidoc = commandData.Application.ActiveUIDocument;
Document doc = uidoc.Document;
Reference selRef = uidoc.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element);
Element elem = doc.GetElement(selRef);
Line locationLine = elem.GetLocationLine();
View v = doc.ActiveView;
Plane p = Plane.CreateByOriginAndBasis(v.Origin, v.RightDirection, v.UpDirection);
XYZ proPoint = p.PointProjectPlane(locationLine.GetEndPoint(0));
这里主要讲一下方法中理解的重点:
1、我们需要构建一个Plane,其中X坐标是视图的右方向,Y坐标是视图的上方向。
2、然后我们构造Transform的时候,就是要按照我们面对的这个视图去构建(如下图)
3、我们构造的Transform是从剖面视图转到当前项目中的,所以需要先逆矩阵(tf.Inverse)方法,把点投影到剖面视图的坐标系下。
4、然后我们在剖面视图的坐标系下,把Z值,即ViewDirection方向上的数值设置为0。然后得到在剖面视图下投影到这个平面上的点。最后在通过Transform.OfPoint()方法,把剖面视图下的点,转到项目坐标系中。
5、里面比较抽象的点是,把剖面视图的平面看成是一个坐标系,然后去计算,再把点转回到项目坐标系下。
今天的分享就到这里了,希望能帮到你~~~