通过递归来实现树列表的节点查找:
private List<Component> mAddComps = new List<Component>();//装配树结构列表
/// <summary>
/// 查找装配树结构
/// </summary>
private void CycleChildrensOfParent(Component PartI)
{
if (mAddComps.Find(p => p.DisplayName == PartI.DisplayName) == null)
mAddComps.Add(PartI);
Component[] childComps = PartI.GetChildren();
for (int i = 0; i < childComps.Length; i++)
{
Component tempComponent = mAddComps.Find(p => p.DisplayName == childComps[i].DisplayName);
if (tempComponent == null)
mAddComps.Add(childComps[i]);
if (childComps[i].GetChildren().Length != 0)
CycleChildrensOfParent(childComps[i]);
}
}
然后在使用时只要先找出根节点rootComponent即可。
根节点定义:
private Component RootComponent;//装配根节点组件
给rootComponent赋值,
Part workPart = theSession.Parts.Work;
rootComponent = workPart.ComponentAssembly.RootComponent;
使用:
CycleChildrensOfParent(rootComponent);