今天要讲的文章接着上一篇讲,第三种获取SW设计结构树的方法。
这个方法的逻辑是通过先获取第一个特征,然后通过循环不断的寻找下一个特征来完成获取所有节点。
1、获取第一个特征的API如下所示:FirstFeature Method (IModelDoc2)
这个API的方法解释为:获取文档中的第一个特性。
返回值的类型为Feature。
使用的例子如下所示:
ModelDoc2 swModel = default(ModelDoc2);ModelView swModelView = default(ModelView);PartDoc swPart = default(PartDoc);AssemblyDoc swAssy = default(AssemblyDoc);FeatureManager swFeatMgr = default(FeatureManager);Feature swFeat = default(Feature);object[] featFace = null;Face2 swFace = default(Face2);object rect = null;string featureName = null;ArrayList featName = new ArrayList();string nameString;int docType = 0;int i = 0;int j = 0;bool status = false;swModel = (ModelDoc2)swApp.ActiveDoc;swModelView = (ModelView)swModel.ActiveView;rect = null;swFeatMgr = (FeatureManager)swModel.FeatureManager;swFeat = (Feature)swModel.FirstFeature();docType = swModel.GetType();switch (docType){case (int)swDocumentTypes_e.swDocPART:swPart = (PartDoc)swModel;break;case (int)swDocumentTypes_e.swDocASSEMBLY:swAssy = (AssemblyDoc)swModel;break;default:Debug.Print("Open a part or assembly document, then rerun this macro.");break;}while ((swFeat != null)){featureName = swFeat.Name;featName.Add(featureName);swFeat = (Feature)swFeat.GetNextFeature();}
2、获取下一个特征的API为:GetNextFeature Method (IFeature)
这个API的解释为:获取部件中的下一个特性。
使用的例子如下所示:
public void TraverseFeatureFeatures(Feature swFeat, long nLevel){Feature swSubFeat;Feature swSubSubFeat;Feature swSubSubSubFeat;string sPadStr = " ";long i = 0;for (i = 0; i <= nLevel; i++){sPadStr = sPadStr + " ";}while ((swFeat != null)){Debug.Print(sPadStr + swFeat.Name + " [" + swFeat.GetTypeName2() + "]");swSubFeat = (Feature)swFeat.GetFirstSubFeature();while ((swSubFeat != null)){Debug.Print(sPadStr + " " + swSubFeat.Name + " [" + swSubFeat.GetTypeName() + "]");swSubSubFeat = (Feature)swSubFeat.GetFirstSubFeature();while ((swSubSubFeat != null)){Debug.Print(sPadStr + " " + swSubSubFeat.Name + " [" + swSubSubFeat.GetTypeName() + "]");swSubSubSubFeat = (Feature)swSubSubFeat.GetFirstSubFeature();while ((swSubSubSubFeat != null)){Debug.Print(sPadStr + " " + swSubSubSubFeat.Name + " [" + swSubSubSubFeat.GetTypeName() + "]");swSubSubSubFeat = (Feature)swSubSubSubFeat.GetNextSubFeature();}swSubSubFeat = (Feature)swSubSubFeat.GetNextSubFeature();}swSubFeat = (Feature)swSubFeat.GetNextSubFeature();}swFeat = (Feature)swFeat.GetNextFeature();}}
上面的这两个例子只是说怎么用,具体这两个API的配合使用逻辑是要结合 while (feature != null)循环一起使用,才可以获取到所有的节点。
今天这篇文章就介绍这么多,我们下篇文章再见。