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

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

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

下面是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。

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

其例子如下所示:

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已经完成。

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

  • 20
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C# SolidWorks二次开发是指使用C#编程语言来扩展和定制SolidWorks软件的功能。下面是进行C# SolidWorks二次开发的步骤: 1. 环境配置: - 首先,确保您已经安装了SolidWorks软件和Visual Studio开发环境。 - 然后,安装SolidWorks API SDK。您可以在SolidWorks安装包中找到"SolidWorks API SDK.msi"文件,并按照安装向导进行安装。 2. 创建插件Addin: - 打开Visual Studio,并创建一个新的C#项目。 - 在项目中,添加对SolidWorks API的引用。您可以在项目的引用中添加SolidWorks.Interop.sldworks和SolidWorks.Interop.swcommands等引用。 - 创建一个类,并实现SolidWorks的事件接口,例如ISwAddin或ISwAddin2。 - 实现所需的功能,例如创建自定义工具栏、菜单或按钮,并实现其相应的事件处理程序。 3. 运行和调试: - 在Visual Studio中编译和生成项目。 - 将生成的插件文件(.dll文件)复制到SolidWorks的插件目录中,一般为"C:\ProgramData\SolidWorks\SOLIDWORKS 20XX\addins"。 - 启动SolidWorks软件,您应该能够看到您创建的自定义工具栏、菜单或按钮。 - 单击按钮或执行其他操作来测试和调试您的插件功能。 通过以上步骤,您可以开始使用C#编程语言进行SolidWorks的二次开发,实现自定义的功能和工具,以满足您的需求。请确保在开发过程中参考官方文档和其他相关教程,以便更好地理解和掌握C# SolidWorks二次开发的技术。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喵桑さん

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值