FeatureManager对象的方法提供给我们创建各类特征,如FeatureManager:: FeatureCut4,FeatureManager:: FeatureExtrusion3,用于创建切除及拉伸特征。这种是采用FeatureManager的具体某个方法创建具体某类特征。
其实,FeatureManager还有一个通用方法可以创建各类特征:FeatureManager:: CreateFeature,此方法将传入某个特征的特征数据对象。
特征数据对象是什么?ExtrudeFeatureData2,CircularPatternFeatureData,RefAxisFeatureData都是特征数据对象,ExtrudeFeatureData2对应的是凸台拉伸及切除的特征数据,CircularPatternFeatureData对应的是特征圆周阵列的数据,RefAxisFeatureData对应的基准轴的特征数据。
简单来说,几乎每种特征都有其对应的特征数据对象,其含义可以理解成如下图的特征数据。故虽然我们可以直接通过尺寸修改,去修改某些特征,但是这种方法的修改范围是有限的,而通过特征数据对象,我们可以操作如下图中各特征的各类参数。
实例1
如下图,获取模型中相关特征的特征数据对象
代码实例
public static void GetFeatureData(ModelDoc2 SwDoc)
{
FeatureManager SwFeatMrg = SwDoc.FeatureManager;
object[] ObjFeats = SwFeatMrg.GetFeatures(true);
StringBuilder Sb = new StringBuilder("");
foreach (object ObjFeat in ObjFeats)
{
if (ObjFeat is Feature)
{
Feature SwFeat = (Feature)ObjFeat;
string FeatTypeName = SwFeat.GetTypeName2();//
if (FeatTypeName == "ICE")//
{
ExtrudeFeatureData2 rfd = SwFeat.GetDefinition();
if (rfd != null)
{
Sb.Append("【" + SwFeat.Name + "】特征数据对象: ExtrudeFeatureData2 \r\n");
}
}
else if (FeatTypeName == "Extrusion")//
{
ExtrudeFeatureData2 efd = SwFeat.GetDefinition();
if (efd != null)
{
Sb.Append("【" + SwFeat.Name + "】特征数据对象:ExtrudeFeatureData2\r\n");
}
}
else if (FeatTypeName == "RefAxis")//
{
RefAxisFeatureData rafd = SwFeat.GetDefinition();
if (rafd != null)
{
Sb.Append("【" + SwFeat.Name + "】特征数据对象:RefAxisFeatureData\r\n");
}
}
else if (FeatTypeName == "CirPattern")//
{
CircularPatternFeatureData cpfd = SwFeat.GetDefinition();
if (cpfd != null)
{
Sb.Append("【" + SwFeat.Name + "】特征数据对象:CircularPatternFeatureData\r\n");
}
}
SwFeat.GetDefinition();
}
}
System.Windows.MessageBox.Show(Sb.ToString().Trim());
}
运行效果
实例分析
如果需要获得特征对应的特征数据对象,我们先需要通过Feature:: GetTypeName2方法,获得特征对应的特征类型名称,然后有针对性地使用Feature:: GetDefinition方法,获得对应的特征数据对象,从而进一步获取与修改特征对应的参数。
实例2
对装配体中的圆柱与方台进行圆周阵列
代码示例
public static void CreateCircularPatternByFeatureData(ModelDoc2 SwDoc, string AxiName, List<string> CompontName)
{
FeatureManager SwFeatMrg = SwDoc.FeatureManager;
Feature SwAxiFeat = ((AssemblyDoc)SwDoc).FeatureByName(AxiName);
SwAxiFeat.Select2(false, 2);
foreach (string compname in CompontName)
{
Feature SwComp = ((AssemblyDoc)SwDoc).FeatureByName(compname);
SwComp.Select2(true, 1);
}
LocalCircularPatternFeatureData SwLocalCircularPatternFeatureData = SwFeatMrg.CreateDefinition((int)swFeatureNameID_e.swFmLocalCirPattern);
SwLocalCircularPatternFeatureData.TotalInstances = 3;
SwLocalCircularPatternFeatureData.EqualSpacing = true;
Feature SwCirPattern = SwFeatMrg.CreateFeature(SwLocalCircularPatternFeatureData);
SwDoc.ClearSelection2(true);
SwCirPattern.Name = "圆周阵列";
}
运行效果
实例3
对实例2的阵列结果进行修改,取消方台部件的阵列。
代码示例
public static void ReviseFeatureData(ModelDoc2 SwDoc, string FeatureName, List<string> CompontName)
{
FeatureManager SwFeatMrg = SwDoc.FeatureManager;
Feature CircleParFeat = ((AssemblyDoc)SwDoc).FeatureByName(FeatureName);
LocalCircularPatternFeatureData SwLocalCircularPatternFeatureData = CircleParFeat.GetDefinition();
foreach (string compname in CompontName)
{
Feature SwComp = ((AssemblyDoc)SwDoc).FeatureByName(compname);
SwComp.Select2(true, 1);
}
bool sc = SwLocalCircularPatternFeatureData.AccessSelections(SwDoc, null);
if (sc)//进入回退编辑状态
{
try
{
Feature[] CompFeats = new Feature[CompontName.Count];
for (int i = 0; i < CompontName.Count; i++)
{
Feature SwComp = ((AssemblyDoc)SwDoc).FeatureByName(CompontName[i]);
CompFeats[i] = SwComp;
}
SwLocalCircularPatternFeatureData.SeedComponentArray = CompFeats;
CircleParFeat.ModifyDefinition(SwLocalCircularPatternFeatureData, SwDoc, null);
}
catch//出错就退出修改
{
SwLocalCircularPatternFeatureData.ReleaseSelectionAccess();
}
}
}
运行效果
实例分析:
使用特征数据对象的AccessSelections方法让特征处于编辑状态,此时,即可对特征数据对象中的参数进行修改,所有参数修改完毕后,通过Feature:: ModifyDefinition完成对特征的修改。如果需要取消修改,则使用特征数据对象的ReleaseSelectionAccess方法即可。