在NX二次开发中,组件的平移可以通过录制的方式获得.cs代码,然后只需要修改三个地方:
///
/// 组件按照vector方向平移distance
///
/// 组件
/// 移动方向
/// 移动距离
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();
}
这个没有OPEN C方便
用OPEN C时,是用uFAssem.RepositionInstance(instanceTag, devicePointDouble, newCsys);实现的,其中:
instanceTag是由Tag instanceTag = uFAssem.AskInstOfPartOcc(deviceComponent.Tag);获得的,
devicePointDouble是重定位后设备的定位点;
newCsys是重定位后的组件坐标系的X轴和Y轴。
以下是实现组件旋转的代码:
///获取组件的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);