Xyz坐标系任意两个面之间投影转换方法
using Autodesk.AutoCAD.Geometry; // 使用 AutoCAD 的几何库
public static class CoordinateProjection
{
/// <summary>
/// 在 AutoCAD 中实现 XY→XZ 平面坐标转换
/// </summary>
public static Point3d ProjectBetweenStandardPlanes(Point3d point, string fromPlane, string toPlane)
{
switch (fromPlane.ToUpper() + "_" + toPlane.ToUpper())
{
case "XY_XZ": return new Point3d(point.X, point.Z, point.Y); // (1,1,0) → (1,0,1)
case "XY_YZ": return new Point3d(point.Z, point.Y, -point.X);
case "XZ_XY": return new Point3d(point.X, point.Z, point.Y);
case "XZ_YZ": return new Point3d(point.Z, point.X, point.Y);
case "YZ_XY": return new Point3d(-point.Z, point.Y, point.X);
case "YZ_XZ": return new Point3d(point.Y, point.X, point.Z);
default: return point;
}
}
}
Point3d pointInXY = new Point3d(1, 1, 0);
Point3d pointInXZ = CoordinateProjection.ProjectBetweenStandardPlanes(pointInXY, "XY", "XZ");
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage($"转换结果: {pointInXZ}\n"); // 输出 (1, 0, 1)
public static Point3d TransformPoint(Point3d point, Vector3d newX, Vector3d newY, Vector3d newZ, Point3d origin)
{
// 构建变换矩阵
Matrix3d mat = Matrix3d.AlignCoordinateSystem(
Point3d.Origin, Vector3d.XAxis, Vector3d.YAxis, Vector3d.ZAxis, // 原坐标系
origin, newX, newY, newZ // 新坐标系
);
return point.TransformBy(mat);
}
// 示例:XY → XZ 转换
Point3d pointInXY = new Point3d(1, 1, 0);
Point3d pointInXZ = TransformPoint(
pointInXY,
Vector3d.XAxis, // 新 X 轴 = 原 X 轴
Vector3d.ZAxis, // 新 Y 轴 = 原 Z 轴
-Vector3d.YAxis, // 新 Z 轴 = -原 Y 轴
Point3d.Origin
);
// 结果: (1, 0, 1)
## **方案 2:在非 Revit 环境(如控制台应用)**
如果 **没有 Revit**,可以用 `System.Numerics.Vector3`(需要安装 NuGet 包):
### **步骤 1:安装 NuGet 包**
```bash
Install-Package System.Numerics
XYZ pointInXY = new XYZ(1, 1, 0);
XYZ pointInXZ = CoordinateProjection.ProjectBetweenStandardPlanes(pointInXY, "XY", "XZ");
// 结果将是 (1, 0, 1)
public static XYZ ProjectBetweenStandardPlanes(XYZ point, string fromPlane, string toPlane)