部件一旦装配之后,UG会对部件进行封装,并生成新的tag,那我们如何去查找装配后的部件呢?
这里提供2种思路:
1、通过ufun函数获取;
2、通过NXOPEN遍历部件;
上代码:
/// <summary>
/// 通过ufun获取组件里的部件信息
/// </summary>
public static void GetBodyListFromComponet(ref List<Component> compList, ref List<Body> bodyList)
{
theSession = Session.GetSession();
theUFSession = UFSession.GetUFSession();
workPart = theSession.Parts.Work;
compList = new List<Component>();
bodyList = new List<Body>();
ComponentTool.GetComponentList(workPart, compList);
ComponentAssembly compAssembly = workPart.ComponentAssembly;
Component rootComponent = compAssembly.RootComponent;
foreach (Component c in compList)
{
SetWorkPart(c);
workPart = theSession.Parts.Work;
Tag objTag = Tag.Null;
theUFSession.Obj.CycleObjsInPart(workPart.Tag, UFConstants.UF_solid_type, ref objTag);
while (objTag != Tag.Null)
{
int type, subtype;
theUFSession.Obj.AskTypeAndSubtype(objTag, out type, out subtype);
if (type == 70 && subtype == 0)
{
Body b = (Body)NXOpen.Utilities.NXObjectManager.Get(objTag);
bodyList.Add(b);
}
theUFSession.Obj.CycleObjsInPart(workPart.Tag, UFConstants.UF_solid_type, ref objTag);
}
}
SetWorkPart(rootComponent);
}
/// <summary>
/// 通过NXOPEN直接遍历
/// </summary>
public static void GetBodyListFromComponet1(ref List<Component> compList, ref List<Body> bodyList)
{
theSession = Session.GetSession();
theUFSession = UFSession.GetUFSession();
workPart = theSession.Parts.Work;
compList = new List<Component>();
bodyList = new List<Body>();
ComponentTool.GetComponentList(workPart, compList);
ComponentAssembly compAssembly = workPart.ComponentAssembly;
Component rootComponent = compAssembly.RootComponent;
foreach (Component c in compList)
{
SetWorkPart(c);
workPart = theSession.Parts.Work;
foreach (Body bodyObj in workPart.Bodies)
{
bodyList.Add(bodyObj);
}
}
SetWorkPart(rootComponent);
}
调用方法:
List<Component> compList = new List<Component>();
List<Body> bodyList = new List<Body>();
ComponentTool.GetBodyListFromComponet(ref compList, ref bodyList);
List<Component> compList1 = new List<Component>();
List<Body> bodyList1 = new List<Body>();
ComponentTool.GetBodyListFromComponet1(ref compList1, ref bodyList1);