C# SolidWorks 二次开发 API---获取所有特征名称思路2

125 篇文章 71 订阅
54 篇文章 15 订阅

之前那篇各种遍历的博文中有写过怎么遍历零件中的特征 。
这两天翻了翻api,看看有没有更简单的办法获取所有特征的名称。
发现发一个新的办法,分享一下。

在这里插入图片描述
看到了吗,之前这里有个FeatureNames,是属于这个不熟悉的单词里的。
在这里插入图片描述
突然秒懂呀,想起来Solidworks里面有个功能是可以统计特征的复杂程度以及重建时间等信息的。
在这里插入图片描述
再深入一点点,看看这个怎么描述的。
在这里插入图片描述
哇,太直接了,返回的直接是数组。是不是比遍历更直接?
而且里面还有例子:
在这里插入图片描述
我就直接贴了,大小写下注释:

This example shows how to get the statistics of all of the features in a part document.

//-------------------------------------------
// Preconditions:
// 1. Open a part that has multiple features.  打一个带多个特征的零件
// 2. Open the Immediate window.
//
// Postconditions:
// 1. Gets the statistics for the features in  得到零件的特征统计信息
//    the part.
// 2. Examine the Immediate window.
//-------------------------------------------
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System;
using System.Diagnostics;
namespace GetFeatureStatisticsForPart_CSharp.csproj
{
    partial 
 class SolidWorksMacro
    {
        public  void Main()
        {
            FeatureStatistics swFeatStat = default(FeatureStatistics);
            FeatureManager swFeatMgr = default(FeatureManager);
            ModelDoc2 swModel = default(ModelDoc2);
            String[] featnames = null;
            Int32[] feattypes = null;
            Object[]  features = null;
            Double[] featureUpdateTimes = null;
            Double[] featureUpdatePercentTimes = null;
            int iter = 0;

            swModel = (ModelDoc2)swApp.ActiveDoc;
            swFeatMgr = swModel.FeatureManager;
            swFeatStat = swFeatMgr.FeatureStatistics;

            swFeatStat.Refresh();

            Debug.Print("Model name: " + swFeatStat.PartName); //名称
            Debug.Print("  Number of features: " + swFeatStat.FeatureCount);//特征数量
            Debug.Print("  Number of solid bodies: " + swFeatStat.SolidBodiesCount);//实体数量 
            Debug.Print("  Number of surface bodies: " + swFeatStat.SurfaceBodiesCount);//面体数量 
            Debug.Print("  Total rebuild time: " + swFeatStat.TotalRebuildTime);//重建时长
            Debug.Print("");
            features  = (Object[])swFeatStat.Features;
            featnames = (String[])swFeatStat.FeatureNames;
            feattypes = (Int32[])swFeatStat.FeatureTypes;
            featureUpdateTimes = (Double[])swFeatStat.FeatureUpdateTimes;
            featureUpdatePercentTimes = (Double[])swFeatStat.FeatureUpdatePercentageTimes;
            if  ((featnames != null))
            {
                for  (iter = 0; iter <= featnames.GetUpperBound(0); iter++)
                {
                    Debug.Print("  Feature name: " + featnames[iter]); //特征名称
                    Debug.Print("  Feature created: " + ((Feature)features[iter]).DateCreated);//创建时间
                    Debug.Print("  Feature type as defined in sw_SelectType_e: " + feattypes[iter]);//类型
                    Debug.Print("  Update time: " + featureUpdateTimes[iter]);//更新时间
                    Debug.Print("  Update % time: " + featureUpdatePercentTimes[iter]);//特征更新时长
                    Debug.Print("");
                }
            }
        }
        public SldWorks swApp; //这个是要自己处理得到实例对象的。
    }
}

好了,我就拿VBA的跑个结果:
在这里插入图片描述
然后可以自己把这个封装一下,变成一个ModelDoc2的扩展方法,后面用起来就非常方便了。
当前 这个适合不需要知道特征之前 的层级关系的时候。

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Paine Zeng

如果对有帮助,请我喝咖啡吧

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

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

打赏作者

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

抵扣说明:

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

余额充值