UG NX二次開發(C#)-裝配-添加組件AddComponent

GimiGimmy 2022-01-07 09:03:15 阅读数:987

ug nx 二次 c# 添加

在UG NX的使用過程中,裝配是很重要的一環,本文是為了實現組件的批量裝配,采用UG NX二次開發,采用的主要函數為AddComponent。

AddComponent()的幫助說明如下:

其是屬於NXOpen.Assemblies空間的ComponentAssembly類,

采用:

using NXOpen.Assemblies;

其實現代碼如下:

 /// <summary>
/// 添加組件
/// </summary>
/// <param name="sourcePart">需要添加的組件全路徑</param>
/// <param name="destinationPart">組件名稱</param>
/// <param name="referenceSetName"></param>
/// <param name="component"></param>
public static void AddComponent(string sourcePart,string destinationPart,string referenceSetName,out Component component)
{
Point3d basePoint = new Point3d(0,0,0);
double[] mtx = new double[9] { 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0 };
UFMtx3 uFMtx3 = theUFSession.Mtx3;
double[] vecProduct = new double[3];
double[] pipeLineVector = new double[3] { 0, 0, 1 };
uFMtx3.VecMultiply(pipeLineVector, mtx, vecProduct);
double[] pipeLineMtx = new double[9];
uFMtx3.InitializeZ(vecProduct, pipeLineMtx);
NXOpen.Matrix3x3 orientation = new NXOpen.Matrix3x3();
orientation.Xx = pipeLineMtx[0];
orientation.Xy = pipeLineMtx[1];
orientation.Xz = pipeLineMtx[2];
orientation.Yx = pipeLineMtx[3];
orientation.Yy = pipeLineMtx[4];
orientation.Yz = pipeLineMtx[5];
orientation.Zx = pipeLineMtx[6];
orientation.Zy = pipeLineMtx[7];
orientation.Zz = pipeLineMtx[8];
int layer = workPart.Layers.WorkLayer;
PartLoadStatus partLoadStatus;
component = workPart.ComponentAssembly.AddComponent(sourcePart, referenceSetName, destinationPart, basePoint, orientation, layer, out partLoadStatus, true);
partLoadStatus.Dispose();
}

在代碼中,需要注意的是這個組件是添加在workPart下面的,workPart是顯示部件,即是:

 PartLoadStatus partLoadStatus;
theSession.Parts.SetDisplay((BasePart)workPart,true,true,out partLoadStatus);

效果如下圖所示:

版权声明:本文为[GimiGimmy]所创,转载请带上原文链接,感谢。 https://gsmany.com/2022/01/202201070903154449.html