C# SolidWorks 二次开发 API—获取所有特征名称思路2
之前那篇各种遍历的博文中有写过怎么遍历零件中的特征 。
这两天翻了翻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的扩展方法,后面用起来就非常方便了。
当前 这个适合不需要知道特征之前 的层级关系的时候。
posted @
2022-01-20 09:22
painezeng 阅读(
0) 评论(
0)
编辑
收藏
举报
来源