C# Solidworks二次开发:访问BOM表特性相关API详解

本文详细介绍了BOM表中的两个关键API:GetConfigurationCount用于获取显示或可用配置数,GetFeature则获取BOM表特征。文章通过示例展示了如何在SolidWorks中使用这些API处理组件和配置信息。
摘要由CSDN通过智能技术生成

大家好,今天要讲的文章是和BOM表特性相关的API。

下面为要介绍的API:

(1)第一个为GetConfigurationCount,这个API的含义为获取此BOM表可用或在此BOM表中使用的配置数,下面是官方的具体解释:

其输入的参数值为bool值,当为true的时候,表示获取该表中当前显示的配置数,当为false的时候,表示获取该表中可用配置的总数。

返回值为此BOM表中或可用于此BOM表的配置个数。

使用API的时候可以先看一下备注:

The view associated with this BOM can contain a model with multiple configurations.

For a top-level only style BOM table, there can be several Quantity columns, each showing the results for a different configuration.  For the other styles of BOM tables, only a particular configuration can be shown in the table, and that configuration can be changed. To determine the style of the BOM table, use IBomFeature::TableType.

If OnlyVisible is...

Then the value returned is...

True

Number of configurations currently shown in the BOM table.

For a top-level only style BOM table, this could be any number from 0 to the total number of configurations available. For the other styles of BOM tables, this value is always 1.

false

Total number of configurations available.

To get the configuration names, call IBomFeature::GetConfigurations or IBomFeature::IGetConfigurations.

Call this method before calling IBomFeature::IGetConfigurations to get the number of configurations.

(2)第二个为GetFeature,这个API的含义为获取BOM表的特征,下面是官方的具体解释:

其没有输入值,只有返回值为指向BOM表特征的指针对象。

下面是官方使用的例子:

This example shows how to get the components in each row of a BOM table annotation.

//-----------------------------------------------------------------------------
// Preconditions:
// 1. Open public_documents\samples\tutorial\assemblyvisualize\food_processor.sldasm.
// 2. Make a drawing from the assembly.
// 3. Click Insert > Tables > Bill of Materials.
// 4. Ensure that Parts only in Bom Type is selected.
// 5. Ensure that Display configurations of the same part separate items
//    in Part Configuration Grouping is selected.
// 6. Click OK.
// 7. Click anywhere in the drawing to insert the BOM table.
//
// Postconditions:
// 1. Gets the Bill of Materials1 feature.
// 2. Gets the Default configuration.
// 3. Processes the BOM table for the Default configuration.
// 4. Examine the Immediate window.
//
// NOTE: Because the assembly is used elsewhere, do not save changes.
//-----------------------------------------------------------------------------
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
namespace Macro1CSharp.csproj
{
    partial class SolidWorksMacro
    {
 
        public void ProcessTableAnn(SldWorks swApp, ModelDoc2 swModel, TableAnnotation swTableAnn, string ConfigName)
        {
            int nNumRow = 0;
            int J = 0;
            int I = 0;
            string ItemNumber = null;
            string PartNumber = null;
            bool RowLocked;
            double RowHeight;
 
            Debug.Print("   Table Title: " + swTableAnn.Title);
 
            nNumRow = swTableAnn.RowCount;
 
            BomTableAnnotation swBOMTableAnn = default(BomTableAnnotation);
            swBOMTableAnn = (BomTableAnnotation)swTableAnn;
 
            for (J = 0; J <= nNumRow - 1; J++)
            {
                RowLocked = swTableAnn.GetLockRowHeight(J);
                RowHeight = swTableAnn.GetRowHeight(J);
                Debug.Print("   Row Number " + J + " (height = " + RowHeight + "; height locked = " + RowLocked + ")");
                Debug.Print("     Component Count: " + swBOMTableAnn.GetComponentsCount2(J, ConfigName, out ItemNumber, out PartNumber));
                Debug.Print("       Item Number: " + ItemNumber);
                Debug.Print("       Part Number: " + PartNumber);
 
                object[] vPtArr = null;
                Component2 swComp = null;
                object pt = null;
 
                vPtArr = (object[])swBOMTableAnn.GetComponents2(J, ConfigName);
 
                if (((vPtArr != null)))
                {
                    for (I = 0; I <= vPtArr.GetUpperBound(0); I++)
                    {
                        pt = vPtArr[I];
                        swComp = (Component2)pt;
                        if ((swComp != null))
                        {
                            Debug.Print("           Component Name: " + swComp.Name2);
                            Debug.Print("           Configuration Name: " + swComp.ReferencedConfiguration);
                            Debug.Print("           Component Path: " + swComp.GetPathName());
                        }
                        else
                        {
                            Debug.Print("  Could not get component.");
                        }
                    }
                }
            }
        }
 
 
 
        public void ProcessBomFeature(SldWorks swApp, ModelDoc2 swModel, BomFeature swBomFeat)
        {
            Feature swFeat = default(Feature);
            object[] vTableArr = null;
            object vTable = null;
            string[] vConfigArray = null;
            object vConfig = null;
            string ConfigName = null;
            TableAnnotation swTable = default(TableAnnotation);
            object visibility = null;
 
            swFeat = swBomFeat.GetFeature();
            vTableArr = (object[])swBomFeat.GetTableAnnotations();
 
            foreach (TableAnnotation vTable_loopVariable in vTableArr)
            {
                vTable = vTable_loopVariable;
                swTable = (TableAnnotation)vTable;
                vConfigArray = (string[])swBomFeat.GetConfigurations(true, ref visibility);
                foreach (object vConfig_loopVariable in vConfigArray)
                {
                    vConfig = vConfig_loopVariable;
                    ConfigName = (string)vConfig;
                    Debug.Print(" Component for Configuration: " + ConfigName);
                    ProcessTableAnn(swApp, swModel, swTable, ConfigName);
                }
            }
 
        }
 
        public void Main()
        {
            ModelDoc2 swModel = default(ModelDoc2);
            DrawingDoc swDraw = default(DrawingDoc);
            Feature swFeat = default(Feature);
            BomFeature swBomFeat = default(BomFeature);
 
            swModel = (ModelDoc2)swApp.ActiveDoc;
            swDraw = (DrawingDoc)swModel;
            swFeat = (Feature)swModel.FirstFeature();
 
            while ((swFeat != null))
            {
                if ("BomFeat" == swFeat.GetTypeName())
                {
                    Debug.Print("Feature Name: " + swFeat.Name);
                    swBomFeat = (BomFeature)swFeat.GetSpecificFeature2();
                    ProcessBomFeature(swApp, swModel, swBomFeat);
                }
                swFeat = (Feature)swFeat.GetNextFeature();
            }
        }
 
 
        public SldWorks swApp;
 
    }
}

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

  • 28
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 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、付费专栏及课程。

余额充值