C# Solidworks二次开发:三种获取SW设计结构树的方法-第三讲

今天要讲的文章接着上一篇讲,第三种获取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)循环一起使用,才可以获取到所有的节点。

今天这篇文章就介绍这么多,我们下篇文章再见。

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-09 09:22:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-09 09:22:02       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-09 09:22:02       82 阅读
  4. Python语言-面向对象

    2023-12-09 09:22:02       91 阅读

热门阅读

  1. 软件工程 考试重点

    2023-12-09 09:22:02       55 阅读
  2. C++学习笔记(十一)

    2023-12-09 09:22:02       55 阅读
  3. 《C++新经典设计模式》之第19章 职责链模式

    2023-12-09 09:22:02       39 阅读
  4. 什么是Docker

    2023-12-09 09:22:02       57 阅读
  5. 做题笔记:SQL Sever 方式做牛客SQL的题目--SQL157

    2023-12-09 09:22:02       52 阅读
  6. forceUpdate

    2023-12-09 09:22:02       48 阅读
  7. HASH以及leetcode刷题

    2023-12-09 09:22:02       48 阅读
  8. 区分工业设计软件中CAD、CAE、CAM、PDM等概念

    2023-12-09 09:22:02       76 阅读
  9. Elasticsearch的Snapshot and Restore(快照备份与恢复)

    2023-12-09 09:22:02       53 阅读
  10. 2021 OWASP Top 10 Web Application Security Risks (2)

    2023-12-09 09:22:02       42 阅读
  11. MySQL六 | 索引

    2023-12-09 09:22:02       60 阅读