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;
}
}
}