En el desarrollo secundario de NX, la traducción de componentes se puede obtener registrando el código .cs, y luego solo es necesario modificar tres lugares:
- movableObjects2 [0] = componente; /// Establecer el componente de traducción
- NXOpen.Point3d origin1 = nuevo NXOpen.Point3d (0.0, 0.0, 0.0);
NXOpen.Vector3d vector1 = nuevo NXOpen.Vector3d (vector [0], vector [1], vector [2]); /// 平移
矢量 NXOpen.Direction direction1;
direction1 = workPart.Directions.CreateDirection (origen1, vector1, NXOpen.SmartObject.UpdateOption.AfterModeling); - NXOpen.Vector3d translation1 = new NXOpen.Vector3d (distancia * vector [0], distancia * vector [1], distancia * vector [2]); /// Calcular el vector de conversión de traducción
componentNetwork2.DragByTranslation (translation1);
/// <summary>
/// 组件按照vector方向平移distance
/// </summary>
/// <param name="component">组件</param>
/// <param name="vector">移动方向</param>
/// <param name="distance">移动距离</param>
public static void TransformComponent(Component component,double[]vector,double distance)
{
NXOpen.Session theSession = NXOpen.Session.GetSession();
NXOpen.Part workPart = theSession.Parts.Work;
NXOpen.Part displayPart = theSession.Parts.Display;
NXOpen.Positioning.ComponentPositioner componentPositioner1;
componentPositioner1 = workPart.ComponentAssembly.Positioner;
componentPositioner1.ClearNetwork();
NXOpen.Assemblies.Arrangement arrangement1 = (NXOpen.Assemblies.Arrangement)workPart.ComponentAssembly.Arrangements.FindObject("Arrangement 1");
componentPositioner1.PrimaryArrangement = arrangement1;
componentPositioner1.BeginMoveComponent();
bool allowInterpartPositioning1;
allowInterpartPositioning1 = theSession.Preferences.Assemblies.InterpartPositioning;
NXOpen.Positioning.Network network1;
network1 = componentPositioner1.EstablishNetwork();
NXOpen.Positioning.ComponentNetwork componentNetwork1 = (NXOpen.Positioning.ComponentNetwork)network1;
componentNetwork1.MoveObjectsState = true;
NXOpen.Assemblies.Component nullNXOpen_Assemblies_Component = null;
componentNetwork1.DisplayComponent = nullNXOpen_Assemblies_Component;
componentNetwork1.NetworkArrangementsMode = NXOpen.Positioning.ComponentNetwork.ArrangementsMode.Existing;
componentNetwork1.NonMovingGroupGrounded = true;
componentNetwork1.Solve();
componentNetwork1.RemoveAllConstraints();
NXOpen.NXObject[] movableObjects1 = new NXOpen.NXObject[1];
movableObjects1[0] = component;
componentNetwork1.SetMovingGroup(movableObjects1);
componentNetwork1.Solve();
componentNetwork1.MoveObjectsState = true;
componentNetwork1.Solve();
componentPositioner1.ClearNetwork();
int nErrs1;
nErrs1 = theSession.UpdateManager.AddToDeleteList(componentNetwork1);
NXOpen.Positioning.Network network2;
network2 = componentPositioner1.EstablishNetwork();
NXOpen.Positioning.ComponentNetwork componentNetwork2 = (NXOpen.Positioning.ComponentNetwork)network2;
componentNetwork2.MoveObjectsState = true;
componentNetwork2.DisplayComponent = nullNXOpen_Assemblies_Component;
componentNetwork2.NetworkArrangementsMode = NXOpen.Positioning.ComponentNetwork.ArrangementsMode.Existing;
componentNetwork2.NonMovingGroupGrounded = true;
componentNetwork2.Solve();
componentNetwork2.RemoveAllConstraints();
NXOpen.NXObject[] movableObjects2 = new NXOpen.NXObject[1];
movableObjects2[0] = component; ///设置平移的组件
componentNetwork2.SetMovingGroup(movableObjects2);
componentNetwork2.Solve();
NXOpen.Unit unit1 = (NXOpen.Unit)workPart.UnitCollection.FindObject("MilliMeter");
NXOpen.Expression expression1;
expression1 = workPart.Expressions.CreateSystemExpressionWithUnits("0", unit1);
NXOpen.Point3d origin1 = new NXOpen.Point3d(0.0, 0.0, 0.0);
NXOpen.Vector3d vector1 = new NXOpen.Vector3d(vector[0], vector[1], vector[2]); ///平移矢量
NXOpen.Direction direction1;
direction1 = workPart.Directions.CreateDirection(origin1, vector1, NXOpen.SmartObject.UpdateOption.AfterModeling);
bool loaded1;
loaded1 = componentNetwork2.IsReferencedGeometryLoaded();
componentNetwork2.BeginDrag();
NXOpen.Vector3d translation1 = new NXOpen.Vector3d(distance*vector[0], distance * vector[1], distance * vector[2]); ///计算平移的距离
componentNetwork2.DragByTranslation(translation1);
componentNetwork2.EndDrag();
componentNetwork2.ResetDisplay();
componentNetwork2.ApplyToModel();
componentNetwork2.Solve();
componentPositioner1.ClearNetwork();
int nErrs3;
nErrs3 = theSession.UpdateManager.AddToDeleteList(componentNetwork2);
NXOpen.Positioning.Network network3;
network3 = componentPositioner1.EstablishNetwork();
NXOpen.Positioning.ComponentNetwork componentNetwork3 = (NXOpen.Positioning.ComponentNetwork)network3;
componentNetwork3.MoveObjectsState = true;
componentNetwork3.DisplayComponent = nullNXOpen_Assemblies_Component;
componentNetwork3.NetworkArrangementsMode = NXOpen.Positioning.ComponentNetwork.ArrangementsMode.Existing;
componentNetwork3.NonMovingGroupGrounded = true;
componentNetwork3.Solve();
componentNetwork3.RemoveAllConstraints();
NXOpen.NXObject[] movableObjects3 = new NXOpen.NXObject[1];
movableObjects3[0] = component;
componentNetwork3.SetMovingGroup(movableObjects3);
componentNetwork3.Solve();
}
Esto no es tan conveniente como OPEN C
Cuando se usa OPEN C, se implementa con uFAssem.RepositionInstance (instanceTag, devicePointDouble, newCsys) ;, donde:
instanceTag se obtiene mediante la etiqueta instanceTag = uFAssem.AskInstOfPartOcc (deviceComponent.Tag);
devicePointDouble es el punto de posicionamiento del dispositivo después de la reubicación;
newCsys es el eje X y el eje Y del sistema de coordenadas del componente reposicionado.
El siguiente es el código para implementar la rotación de componentes:
///获取组件的instance
Tag instanceTag = uFAssem.AskInstOfPartOcc(deviceComponent.Tag);
///计算旋转矩阵
double[] mtx = new double[9];
uFMtx3.RotateAboutAxis(deviceAxisVectorDouble, rotateAngle.Value * Math.PI / 180, mtx);
///获得设备旋转后的新坐标系的X轴
double[] XAxis = new double[3];
uFMtx3.VecMultiply(deviceAxisVectorDouble, mtx, XAxis);
///获取设备旋转后的新坐标系的Y轴
///其中,原Z轴是已知的,Y轴由Z×X得到的
UFVec3 uFVec3 = theUFSession.Vec3;
double[] cross = new double[3];
uFVec3.Cross(deviceZVectorDouble, deviceAxisVectorDouble, cross);
double[] YAxis = new double[3];
uFMtx3.VecMultiply(cross, mtx, YAxis);
///根据定位点和新的坐标系获得设备的定位信息
double[] newCsys = new double[6] { XAxis[0], XAxis[1], XAxis[2], YAxis[0], YAxis[1], YAxis[2] };
uFAssem.RepositionInstance(instanceTag, devicePointDouble, newCsys);
#region 修改组件的属性
NXOpen.PartLoadStatus partLoadStatus;
theSession.Parts.SetWorkComponent(deviceComponent, out partLoadStatus);
workPart = theSession.Parts.Work;
double[] ZAxis = new double[3];
uFVec3.Cross(XAxis, YAxis, ZAxis);