首先说明,在UG二次开发时,读取的点坐标是工作部件绝对坐标系点坐标,不是WCS的坐标。
为了实现绝对坐标系到选择的坐标系变换,设计了一个验证示例。
其UI界面如下:
其变换代码如下:
#region 坐标系变换
Point3d origionPoint3D = new Point3d(); //定义工作部件绝对坐标系的位置点坐标
Point point = (Point)pointTaggedObjects[0]; //选择的点对象
origionPoint3D = point.Coordinates; //位置点坐标赋值
CoordinateSystem coordinateSystem = null; //定义坐标系对象
coordinateSystem = (CoordinateSystem)taggedObjects[0]; //坐标系设置的对象
NXUtilityTools nXUtilityTools = new NXUtilityTools(); //调用通用工具类
Point3d csysPoint3D = new Point3d(); //转换后的坐标点
WCSPointTransposeCsysPoint(origionPoint3D, coordinateSystem, out csysPoint3D);
#endregion
/// <summary>
/// 工作部件绝对坐标值转化为新坐标下点坐标值
/// </summary>
/// <param name="wcsPoint3D"></param>
/// <param name="csysTag"></param>
/// <param name="csysPoint3D"></param>
public static void WCSPointTransposeCsysPoint(Point3d wcsPoint3D,CoordinateSystem coordinateSystem, out Point3d csysPoint3D)
{
Point3d csysOrigionPoint3D;
csysOrigionPoint3D = coordinateSystem.Origin;
NXMatrix nXMatrix = coordinateSystem.Orientation;
Matrix3x3 csysMatrix3x3 = nXMatrix.Element;
Point3d tmpPoint3D = new Point3d();
tmpPoint3D.X = wcsPoint3D.X - csysOrigionPoint3D.X;
tmpPoint3D.Y = wcsPoint3D.Y - csysOrigionPoint3D.Y;
tmpPoint3D.Z = wcsPoint3D.Z - csysOrigionPoint3D.Z;
PointMatrixToNewPoint(tmpPoint3D,csysMatrix3x3,out csysPoint3D);
}
/// <summary>
/// 点的转换
/// </summary>
/// <param name="origionPoint3D">初始点</param>
/// <param name="matrix3X3">转换矩阵</param>
/// <param name="newPoint3D">转换后的点</param>
public static void PointMatrixToNewPoint(Point3d origionPoint3D,Matrix3x3 matrix3X3,out Point3d newPoint3D)
{
newPoint3D = new Point3d();
newPoint3D.X = matrix3X3.Xx * origionPoint3D.X + matrix3X3.Xy * origionPoint3D.Y + matrix3X3.Xz * origionPoint3D.Z;
newPoint3D.Y = matrix3X3.Yx * origionPoint3D.X + matrix3X3.Yy * origionPoint3D.Y + matrix3X3.Yz * origionPoint3D.Z;
newPoint3D.Z = matrix3X3.Zx * origionPoint3D.X + matrix3X3.Zy * origionPoint3D.Y + matrix3X3.Zz * origionPoint3D.Z;
}
对于矢量变换:
其代码如下:
TaggedObject[] taggedObjects = _VectorSelect.GetSelectedObjects();
if(taggedObjects.Length>0)
{
Vector3d origionVector3D = _VectorSelect.Vector;
if (_CoordSystemSelect.GetSelectedObjects().Length > 0)
{
CoordinateSystem coordinateSystem = (CoordinateSystem)_CoordSystemSelect.GetSelectedObjects()[0];
NXMatrix nXMatrix = coordinateSystem.Orientation;
Matrix3x3 matrix3X3 = nXMatrix.Element;
Vector3d newVector3D = new Vector3d();
VectorToNewVector(origionVector3D, coordinateSystem, out newVector3D);
}
}
/// <summary>
/// 矢量由工作部件绝对坐标系转换到新的坐标下的矢量值
/// </summary>
/// <param name="origionVec"></param>
/// <param name="coordinateSystem"></param>
/// <param name="newVector"></param>
public static void VectorToNewVector(Vector3d origionVec,CoordinateSystem coordinateSystem,out Vector3d newVector)
{
newVector = new Vector3d();
NXMatrix nXMatrix = coordinateSystem.Orientation;
Matrix3x3 csysMatrix3x3 = nXMatrix.Element;
newVector.X = origionVec.X * csysMatrix3x3.Xx + origionVec.Y * csysMatrix3x3.Xy + origionVec.Z * csysMatrix3x3.Xz;
newVector.Y = origionVec.X * csysMatrix3x3.Yx + origionVec.Y * csysMatrix3x3.Yy + origionVec.Z * csysMatrix3x3.Yz;
newVector.Z = origionVec.X * csysMatrix3x3.Zx + origionVec.Y * csysMatrix3x3.Zy + origionVec.Z * csysMatrix3x3.Zz;
}