一个会的不多的人

一个会的不多的人

大家好,今天要介绍的是关于solidworks中可以获取对象唯一ID的几种API,获取唯一ID的API有如下几种:

(1)第一种是GetID Method (IComponent2),其含义为获取每个组件的唯一ID。

C# Solidworks二次开发:获取唯一ID的API详解-LMLPHP

下面是API中的使用例子:

using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Runtime.InteropServices;
using System;
using System.Diagnostics;
 
namespace Macro1CSharp.csproj
{
    public partial class SolidWorksMacro
    {
 
        public void Main()
        {
            ModelDoc2 swModel = default(ModelDoc2);
            ConfigurationManager swConfMgr = default(ConfigurationManager);
            Configuration swConf = default(Configuration);
            Component2 swRootComp = default(Component2);
 
            swModel = (ModelDoc2)swApp.ActiveDoc;
            swConfMgr = (ConfigurationManager)swModel.ConfigurationManager;
            swConf = (Configuration)swConfMgr.ActiveConfiguration;
            swRootComp = (Component2)swConf.GetRootComponent3(true);
            Debug.Print("File = " + swModel.GetPathName());
            if (swModel.GetType() == (int)swDocumentTypes_e.swDocASSEMBLY)
            {
                TraverseComponent(swRootComp, 1);
            }
 
        }
 
        public void TraverseComponent(Component2 swComp, int nLevel)
        {
            object[] vChildComp = null;
            Component2 swChildComp = default(Component2);
            string sPadStr = "";
            int i = 0;
 
            for (i = 0; i <= nLevel - 1; i++)
            {
                sPadStr = sPadStr + "  ";
            }
            vChildComp = (object[])swComp.GetChildren();
            for (i = 0; i <= vChildComp.Length - 1; i++)
            {
                swChildComp = (Component2)vChildComp[i];
                Debug.Print(sPadStr + "Component name: " + swChildComp.Name2 + ", Component ID: " + swChildComp.GetID());
                TraverseComponent(swChildComp, nLevel + 1);
            }
        }
 
        /// <summary>
        ///  The SldWorks swApp variable is pre-assigned for you.
        /// </summary>
        public SldWorks swApp;
    }
}

(2)第二种为GetID Method (IEdge),其含义为获取实体中每条边的唯一ID。

C# Solidworks二次开发:获取唯一ID的API详解-LMLPHP

(3)第三种为GetID Method (IFeature),其含义为获取每个特征的唯一ID。

C# Solidworks二次开发:获取唯一ID的API详解-LMLPHP

其例子如下所示:

using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System;
using System.Diagnostics;

namespace Instant3DFeatureCSharp.csproj
{

    
partial class SolidWorksMacro
    {


        
public void Main()
        {
            ModelDoc2 swModel =
default(ModelDoc2);
            Feature swFeature =
default(Feature);
            
string fileName = null;
            
int errors = 0;
            
int warnings = 0;

            fileName =
"C:\\Users\\Public\\Documents\\SOLIDWORKS\\SOLIDWORKS 2018\\samples\\tutorial\\api\\block.sldprt";
            swModel = (ModelDoc2)swApp.OpenDoc6(fileName, (
int)swDocumentTypes_e.swDocPART, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings);

            swFeature = (Feature)swModel.FirstFeature();

            SelectFeat(swFeature);

        }

        
public bool SelectFeat(Feature featureTemp)
        {
            
while ((featureTemp != null))
            {
                
string featureName = null;
                featureName = featureTemp.GetTypeName2();

                
Debug.Print(featureName);

                
// Instant3D features are ICE features
                if (featureName == "ICE")
                {
                    
Debug.Print("   Type:     " + featureTemp.GetTypeName());
                    
Debug.Print("   ID:       " + featureTemp.GetID());
                }

                featureTemp = (Feature)featureTemp.GetNextFeature();
            }
            
return true;
        }


        
/// <summary>
        /// The SldWorks swApp variable is pre-assigned for you.
        /// </summary>

        public SldWorks swApp;

    }
}

今天要介绍的三种获取唯一ID的API已经完成。

本篇文章也到此结束了,我们下篇文章再见。

04-08 02:37