NX二次开发如何使导入的标准件跟随CSYS自由运动(C#)

文章详细描述了如何在编程中通过创建特定方位的控件,定义全局数组变量,以及在update_cb函数中更新组件坐标的过程,包括获取和处理当前位置、目标位置的CSYS坐标,以及使用TransformComponent和CreateTempCsys方法进行坐标变换。
摘要由CSDN通过智能技术生成

第一步:制作dlx的时候添加一个指定方位的控件,将这个控件放到一个单独的控件组 中,然后隐藏这个组

第二步:定义四个数组类型的全局变量

    //动态坐标移动时的目标点位和目标点位的3x3矩阵
    double[] dest_origin = new double[3];
    double[] dest_csys_matrix = new double[9];
    //动态坐标移动时的现有点位和现有点位的3x3矩阵
    double[] init_origin = new double[3];
    double[] init_csys_matrix = new double[9];

第三步:在update_cb中的block == manip0中写入

                //获取到当前点的CSYS坐标的TAG值
                Tag old_csys = CreateTempCsys(init_origin, init_csys_matrix);
                //获取到目标点的位置信息
                dest_origin[0] = manip0.Origin.X;
                dest_origin[1] = manip0.Origin.Y;
                dest_origin[2] = manip0.Origin.Z;
                double[] vcx = { manip0.XAxis.X, manip0.XAxis.Y, manip0.XAxis.Z };
                double[] vcy = { manip0.YAxis.X, manip0.YAxis.Y, manip0.YAxis.Z };
                UFSession.GetUFSession().Mtx3.Initialize(vcx, vcy, dest_csys_matrix);


                //获取到目标点的CSYS坐标的TAG值
                Tag new_csys = CreateTempCsys(dest_origin, dest_csys_matrix);

                //移动组件
                TransformComponent(importComponent.Tag, old_csys, new_csys);

                 //将目标点信息赋值给现有点
                 init_csys_matrix = dest_csys_matrix;
                 init_origin = dest_origin;

下面是上面这段代码涉及的自定义方法

    /// <summary>
    /// 获取当前动态坐标的TAG信息
    /// </summary>
    public Tag CreateTempCsys(double[] origin, double[] mtx9)
    {
        UFSession.GetUFSession().Csys.CreateMatrix(mtx9, out Tag mx);
        UFSession.GetUFSession().Csys.CreateTempCsys(origin, mx, out Tag result);
        return result;
    }
    /// <summary>
    /// 让组件从一个坐标移动到另一个坐标上
    /// </summary>
    public void TransformComponent(Tag comp, Tag ref_csys, Tag dest_csys)
    {
        UFSession theufSession = UFSession.GetUFSession();

        if (comp == Tag.Null||ref_csys == Tag.Null||dest_csys == Tag.Null)
        {
            return;
        }        
        else
        {
            if (!theufSession.Assem.IsPartOccurrence(comp))
            {
                return;
            }
            //获取组件信息(目前所处的点、矩阵等)
            double[] origin = new double[3];
            double[] csys_matrix = new double[9];
            double[,] transform = new double[4, 4];
            theufSession.Assem.AskComponentData(comp, out string part_name, out string refset_name,  out string instance_name, origin, csys_matrix, transform);

            int zero = 0, one = 1, two = 2;
            Tag mx;
            double[] mxd = new double[12];

            //获取到组件现有状态的位置的csystag
            Tag csys = CreateTempCsys(origin, csys_matrix);

            TransformCsys2Csys(ref_csys, dest_csys, mxd);
            Tag[] objs = new Tag[1];
            Tag[] objs1 = new Tag[1];
            Tag objs2 = Tag.Null;
            objs[0] = csys = csys;

            int resp = 0;
            theufSession.Trns.TransformObjects(mxd, objs, ref one, ref one, ref zero, ref two, objs1, out objs2, out resp);

            theufSession.Csys.AskCsysInfo(csys, out mx, origin);
            theufSession.Csys.AskMatrixValues(mx, csys_matrix);

            NXOpen.Tag instance = theufSession.Assem.AskInstOfPartOcc(comp);
            theufSession.Assem.RepositionInstance(instance, origin, csys_matrix);
        }
    }
    /// <summary>
    /// 将CSYS从一个位置移动到另外一个位置
    /// </summary>
    public void TransformCsys2Csys(Tag ref_csys, Tag dest_csys, double[] mx)
    {
        UFSession theufSession = UFSession.GetUFSession();
        if (ref_csys == Tag.Null ||dest_csys == Tag.Null)
        {
            return;
        }

        double[] orig = new double[3];
        double[] tx1 = new double[12];
        double[] tx2 = new double[12];
        double[] tx3 = new double[12];
        double[] tx4 = new double[12];
        double[] v = new double[3];
        Tag csys_mx = Tag.Null;

        //根据已知初始位置的tag值获取3x3矩阵
        double[] csys = new double[9];
        theufSession.Csys.AskCsysInfo(ref_csys, out csys_mx, orig);
        theufSession.Csys.AskMatrixValues(csys_mx, csys);

        tx1[0] = csys[0];
        tx1[1] = csys[1];
        tx1[2] = csys[2];
        tx1[3] = 0;
        tx1[4] = csys[3];
        tx1[5] = csys[4];
        tx1[6] = csys[5];
        tx1[7] = 0;
        tx1[8] = csys[6];
        tx1[9] = csys[7];
        tx1[10] = csys[8];
        tx1[11] = 0;

        /* set up to translate from reference csys back to absolute */
        for (int i  = 0; i < 3; i++) 
        { 
            v[i] = -orig[i];
        } 

        theufSession.Trns.CreateTranslationMatrix(v, tx2);

        /* combine this with the rotation matrix from the reference csys */
        theufSession.Trns.MultiplyMatrices(tx2, tx1, tx3);

        //根据已知目标位置的tag值获取3x3矩阵
        double[] csys2 = new double[9];
        theufSession.Csys.AskCsysInfo(dest_csys, out csys_mx, orig);
        theufSession.Csys.AskMatrixValues(csys_mx, csys2);
        
        /* Invert the rotation from the destination matrix */
        tx2[0] = csys2[0];
        tx2[1] = csys2[3];
        tx2[2] = csys2[6];
        tx2[3] = 0;
        tx2[4] = csys2[1];
        tx2[5] = csys2[4];
        tx2[6] = csys2[7];
        tx2[7] = 0;
        tx2[8] = csys2[2];
        tx2[9] = csys2[5];
        tx2[10] = csys2[8];
        tx2[11] = 0;

        /* set up to translate from abs to the destination csys */
         theufSession.Trns.CreateTranslationMatrix(orig, tx1);

        /* combine this with the inverted rotation csys above */
        theufSession.Trns.MultiplyMatrices(tx2, tx1, tx4);

        /* and the one from the reference csys */
        theufSession.Trns.MultiplyMatrices(tx3, tx4, mx);
    }

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值