Balder中自动创建的Cylinder默认是处于垂直状态的,如果想要根据两个三维坐标创建一个圆柱,那么就需要对刚创建的Cylinder进行旋转操作。
private Cylinder CreatePipeCylinderFromGraphic(double x1,double y1,double z1,double x2,double y2,double z2,double Radius,Color color)
{
Cylinder cylinder = new Cylinder();
double x0 = (x1 + x2) / 2;
double y0 = (y1 + y2) / 2;
double z0 = (z1 + z2) / 2;
double length = Math.Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2));
cylinder.Size = length;
double VirtualX = x1 - x0;
double VirtualY = y1 - y0;
double VirtualZ = z1 - z0;
double A = VirtualX / (length / 2);
double B = VirtualY / (length / 2);
double C = VirtualZ / (length / 2);
double AngleX = Math.Asin(C) * 180 / Math.PI;
double AngleZ = Math.Atan(-A / B) * 180 / Math.PI;
if (y1 < y2) AngleZ += 180;
cylinder.Rotation = new Coordinate(AngleX, 0, AngleZ);
cylinder.Position = new Coordinate(x0, y0, z0);
cylinder.TopRadius = cylinder.BottomRadius = Radius;
cylinder.Color = color;
cylinder.Segments = 20; //圆滑程度
return cylinder;
}