solidworks二次开发C#----遍历

一.为何要遍历

当然是为了获得我们需要的信息。

二.如何遍历

1.1获得根零件

Configuration       swConf; 
Component2        swRootComponent; 
swConf = (Configuration)swModel.GetActiveConfiguration(); swRootComponent = (Component2)swConf.GetRootComponent(); 

1.2遍历零件

private void TraverseComponent(Component2 component)
 {     
  object[] children = (object[])component.GetChildren(); 
    if (children.Length > 0)    
     {         
        foreach (Component2 comp in children)         
        {             
            TraverseComponent(comp, tn);        
         }     
       } 
  }

1.3遍历零件特征

private void TraverseFeatures(Feature firstfeature, TreeNode tn)
 {     
    Feature feat = firstfeature;  
    while (feat != null)    
    {         
    MessageBox.Show(feat.Name);         
    feat = (Feature)feat.GetNextFeature();     
    } 
} 

1.4遍历子特征

private void TraverseSubFeatures(Feature feature, TreeNode node)
{     
    Feature subfeat = (Feature)feature.GetFirstSubFeature();     
    while (subfeat != null)     
    {         
      TraverseSubFeatures(subfeat, subtn);         
      subfeat = (Feature)subfeat.GetNextSubFeature(); 
    }
}

1.5遍历零部件特征

            if (swFeat != null)
            {
                Feature swFFeat = swFeat.GetFirstSubFeature();
                while (swFFeat != null)
                {
                    if (swFFeat.GetTypeName2() == "Cut")
                    {
                        swCompFeat = swFFeat;
                    }
                    Debug.Print(swFFeat.Name + swFFeat.GetTypeName2());
                    swFFeat = (Feature)swFFeat.GetNextSubFeature();
                }
                //Debug.Print(swCompFeat.Name + swCompFeat.GetTypeName2());
                if (swCompFeat != null)
                {
                    Feature subfeat = (Feature)swCompFeat.GetFirstSubFeature();
                    while (subfeat != null)
                    {
                        if (subfeat.GetTypeName2() == "Cut")
                        {
                            swCutFeature = subfeat;
                        }
                        Debug.Print(subfeat.Name + subfeat.GetTypeName2());
                        subfeat = (Feature)subfeat.GetNextSubFeature();
                    }
                }
            }
            if (swCutFeature != null)
            {
                swFaceObj = swCutFeature.GetFaces();
            }
            return swFaceObj;

你可能感兴趣的:(solidworks二次开发C#----遍历)