移动组件

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using NXOpen; 
using NXOpen.UF; 

namespace DragComp 
{ 
    public class Drag 
    { 
        private  UFSession theUfSession = UFSession.GetUFSession(); 
        private  Session theSession = Session.GetSession(); 
        public double[] currentMtx44 = new double[16]; 
        private NXOpen.Positioning.ComponentNetwork netWork; 
        private NXOpen.Positioning.ComponentPositioner positioner; 
        public Drag(Part workPart,double[] current) 
        { 
            this.positioner = workPart.ComponentAssembly.Positioner; 
            this.netWork = (NXOpen.Positioning.ComponentNetwork)this.positioner.EstablishNetwork(); 
            currentMtx44 = current; 
        } 

        /// <summary> 
        /// Transes the comps. 
        /// </summary> 
        /// <param name="component">The component.</param> 
        /// <param name="trans">The trans.</param> 
        public void Move(NXOpen.Assemblies.Component component, double[] position) 
        { 
            Array.Resize(ref position, 16); 
            double[] trans = CalTrans(position); 

            Matrix3x3 matrix = new Matrix3x3(); 
            matrix.Xx = trans[0];matrix.Xy = trans[4];matrix.Xz = trans[8]; 
            matrix.Yx = trans[1]; matrix.Yy = trans[5]; matrix.Yz = trans[9]; 
            matrix.Zx = trans[2]; matrix.Zy = trans[6]; matrix.Zz = trans[10]; 
            Vector3d vector = new Vector3d(trans[3], trans[7], trans[11]); 

            if ( netWork == null) 
            { 
                 positioner = Session.GetSession().Parts.Work.ComponentAssembly.Positioner; 
                 netWork = (NXOpen.Positioning.ComponentNetwork) positioner.EstablishNetwork(); 
            } 

             netWork.SetMovingGroup(new NXOpen.Assemblies.Component[] { component }); 
             netWork.NetworkArrangementsMode = NXOpen.Positioning.ComponentNetwork.ArrangementsMode.InUsed; 
             netWork.RemoveAllConstraints(); 
             netWork.BeginDrag(); 
             positioner.BeginMoveComponent(); 
             netWork.MoveObjectsState = true; 
             netWork.DragByTransform(vector, matrix); 
             positioner.EndMoveComponent(); 
             netWork.EndDrag(); 
            //更新 
            currentMtx44 = position; 
        } 

        private double[] CalTrans(double[] position) 
        { 
            double[] trans = new double[16]; 
            double[] invertCurrentMtx44 = new double[16]; 
            theUfSession.Mtx4.Invert(currentMtx44, invertCurrentMtx44); 
            theUfSession.Mtx4.Multiply(invertCurrentMtx44, position, trans); 
            double[] translation = new double[3]; 
            theUfSession.Mtx4.AskTranslation(trans, translation); 
            double[] rotation = new double[9]; 
            theUfSession.Mtx4.AskRotation(trans, rotation); 
            theUfSession.Mtx3.Transpose(rotation, rotation); 
            theUfSession.Mtx4.Initialize(1, translation, rotation, trans); 
            return trans; 
        } 

    } 
}

 

转载于:https://my.oschina.net/hitbangmaker/blog/882614

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值