来自天空* 发表于 2018-2-11 13:09:24

UG变换实体功能

// NX 10.0.0.24
// Journal created by Administrator on Sat Feb 10 16:04:57 2018 中国标准时间
//
using System;
using NXOpen;

public class NXJournal
{
    private static TaggedObject taggedObject; //存储选择的对象
    private static Point3d point3d;//存储选择的点


    public static void Main(string[] args)
    {
      Session theSession = Session.GetSession();
      Part theWork = theSession.Parts.Work;
      Part displayPart = theSession.Parts.Display;

      //选择要变换的对象
      UI.GetUI().SelectionManager.SelectTaggedObject("WCS变换到ABS", "选择变换对象", Selection.SelectionScope.AnyInAssembly, false, false,
            out taggedObject, out point3d);

      //UI.GetUI().NXMessageBox.Show("",NXMessageBox.DialogType.Information, (taggedObject as Body).Name);

      Body body = taggedObject as Body;

      NXOpen.Features.MoveObject nullNXOpen_Features_MoveObject = null;

      NXOpen.Features.MoveObjectBuilder moveObjectBuilder1 = theWork.BaseFeatures.CreateMoveObjectBuilder
            (nullNXOpen_Features_MoveObject); //构建移动构造器

      moveObjectBuilder1.ObjectToMoveObject.Add(body); //添加到列表

      moveObjectBuilder1.TransformMotion.Option = NXOpen.GeometricUtilities.ModlMotion.Options.CsysToCsys;//变换的方式

      //绝对坐标
      Point3d absoluteOrigin = new Point3d(0.0, 0.0, 0.0); //绝对原点
      Matrix3x3 absoluteMatrix = new Matrix3x3//绝对方向
      {
            Xx = 1.0,
            Xy = 0.0,
            Xz = 0.0,
            Yx = 0.0,
            Yy = 1.0,
            Yz = 0.0,
            Zx = 0.0,
            Zy = 0.0,
            Zz = 1.0
      };
      CoordinateSystem coordinateSystem = theWork.CoordinateSystems.CreateCoordinateSystem(absoluteOrigin, absoluteMatrix, true);

      //设置移动坐标系
      moveObjectBuilder1.TransformMotion.FromCsys = theWork.WCS.CoordinateSystem;//起始CSYS
      moveObjectBuilder1.TransformMotion.ToCsys = coordinateSystem;//目标CSYS

      try
      {
            //提交移动
            moveObjectBuilder1.Commit();
            moveObjectBuilder1.GetCommittedObjects();
            moveObjectBuilder1.Destroy();

            //wcs回绝对零点,
            theWork.WCS.SetOriginAndMatrix(absoluteOrigin, absoluteMatrix);
      }
      catch (Exception)
      {
            UI.GetUI().NXMessageBox.Show("变换错误", NXMessageBox.DialogType.Error, "命令出现错误,请核查");
            // throw;
      }
    }
    public static int GetUnloadOption(string dummy) { return (int)NXOpen.Session.LibraryUnloadOption.Immediately; }
}







bingbwang 发表于 2018-2-11 13:51:36

谢谢分享,学习了!
页: [1]
查看完整版本: UG变换实体功能