关于IFeatureManager接口下面的常用方法。
- 挤压凸台,挤压切除凹陷,倒直角,倒圆角
//SolidWorks 2020
using SolidWorks.Interop.sldworks;
namespace DemoSketchTest
{
class FeatureSw
{
private IModelDoc2 swDoc;
public void setDoc(IModelDoc2 doc)
{
swDoc = doc;
}
//选中草图,挤压凸台。
public bool Extrude(string SketchName, bool Direction,
double Thickness, string FeatureNameOut)
{
swDoc.ClearSelection2(true);//清除选中项
//选中一个草绘
bool b = swDoc.Extension.SelectByID2(SketchName, "SKETCH",
0, 0, 0, false, 0, null, 0);
if (!b) return false;
//生成凸台
//Direction:拉伸方向,正反两个方向
//Thickness:挤压深度(米)
IFeature swFeat = swDoc.FeatureManager.FeatureExtrusion2(
true, false, Direction, 0, 0, Thickness, Thickness,
false, false, false, false, 0.0, 0.0,
false, false, false, false, true, false, true, 0, 0.0, false);
//修改凸台名称
swDoc.SelectedFeatureProperties(0, 0, 0, 0, 0, 0, 0,
true, false, FeatureNameOut);
return true;
}
//挤压切除凹陷
public bool FeatureCut4(string SketchName, bool Direction,
double height, string FeatureNameOut)
{
swDoc.ClearSelection2(true);//清除选中项
bool b = swDoc.Extension.SelectByID2(SketchName, "SKETCH",
0, 0, 0, false, 0, null, 0);//选中一个草绘
if (!b) return false;
//挤压切除凹陷
swDoc.FeatureManager.FeatureCut4(
false, false, Direction, 1, 1, height, height,
false, false, false, false, 0, 0, false, false, false, false, false,
true, true, true, true, false, 0, 0, false, false);
swDoc.SelectedFeatureProperties(0, 0, 0, 0, 0, 0, 0,
true, false, FeatureNameOut);//重命名
return true;
}
/*
倒角的sample 文件位置如下:
C:\Users\Public\Documents\SOLIDWORKS\SOLIDWORKS 2020\samples\tutorial\api\block20.sldprt
*/
//倒直角
public void Chamfer()
{
FeatureManager swFeatMgr = swDoc.FeatureManager;
SelectionMgr swSelMgr = (SelectionMgr)swDoc.SelectionManager;
// Select two edges to partially chamfer
//SelectByRay函数,选择指定类型的第一个实体,该实体与从指定点开始并在指定半径内平行于指定方向矢量延伸的射线相交。
bool boolstatus1 = swDoc.Extension.SelectByRay(
-0.0627751435901871, 0.0395396450382464, 0.000603865270420556,
0.156569748366562, -0.551708952719479, -0.81920885333693,
0.0011220164860728, 1, false, 1, 0);//swSelEDGES=1,swSelectOptionDefault=0
//Gets the selected object.
Edge filletItemA = (Edge)swSelMgr.GetSelectedObject6(1, -1);//Mark:-1 = All selections regardless of marks
// Edge filletItemB = (Edge)swSelMgr.GetSelectedObject6(2, -1);
//将圆角特征数据对象创建为简单圆角类型
// Create the fillet feature data object to be a simple fillet type
object swFeatDataObj = swFeatMgr.CreateDefinition((int)swFeatureNameID_e.swFmFillet);//swFmFillet 圆角
SimpleFilletFeatureData2 swFilletData = (SimpleFilletFeatureData2)swFeatDataObj;
// Initilize the feature type to be a constant radius fillet
swFilletData.Initialize((int)swSimpleFilletType_e.swConstRadiusFillet);
// Specialize the feature type to be a chamfer
swFilletData.ConicTypeForCrossSectionProfile = //简单圆角的横截面轮廓
(int)swFeatureFilletProfileType_e.swFeatureFilletConicRhoZeroChamfer;
swFilletData.OverflowType = (int)swFilletOverFlowType_e.swFilletOverFlowType_Default;
swFilletData.DefaultRadius = 0.01;//半径
swFilletData.EnablePartialEdgeParameters = true;
PartialEdgeFilletData PartialEdgeAFilletData = (PartialEdgeFilletData)swFilletData.GetPartialEdgeFilletData(filletItemA);
int errorCode1 = PartialEdgeAFilletData.SetPartialFilletParameters(true,
(int)swSimpleFilletPartialEdgeCondition_e.swPartialEdgeDistanceOffset, 0.01, null,
(int)swSimpleFilletPartialEdgeCondition_e.swPartialEdgeDistanceOffset, 0.02, null);
Feature swFeat = swFeatMgr.CreateFeature(swFilletData);
bool warning;
int errorCode3 = swFeat.GetErrorCode2(out warning);
}
//倒圆角
public void Chamfer2()
{
FeatureManager swFeatMgr = swDoc.FeatureManager;
SelectionMgr swSelMgr = (SelectionMgr)swDoc.SelectionManager;
// Select two edges to partially chamfer
//SelectByRay函数,选择指定类型的第一个实体,该实体与从指定点开始并在指定半径内平行于指定方向矢量延伸的射线相交。
bool boolstatus1 = swDoc.Extension.SelectByRay(
-0.0627751435901871, 0.0395396450382464, 0.000603865270420556,
0.156569748366562, -0.551708952719479, -0.81920885333693,
0.0011220164860728, 1, false, 1, 0);//swSelEDGES=1,swSelectOptionDefault=0
bool boolstatus2 = swDoc.Extension.SelectByRay(
0.0644200432014657, 0.039572847309671, 0.0130368065144353,
0.156569748366562, -0.551708952719479, -0.81920885333693,
0.0011220164860728, 1, true, 1, 0);//Already selected -- Entity is removed from the current selection list
//将圆角特征数据对象创建为简单圆角类型
// Create the fillet feature data object to be a simple fillet type
object swFeatDataObj = swFeatMgr.CreateDefinition((int)swFeatureNameID_e.swFmFillet);//swFmFillet 圆角
SimpleFilletFeatureData2 swFilletData = (SimpleFilletFeatureData2)swFeatDataObj;
// Initilize the feature type to be a constant radius fillet
swFilletData.Initialize((int)swSimpleFilletType_e.swConstRadiusFillet);
// Specialize the feature type to be a chamfer
swFilletData.AsymmetricFillet = false; //AsymmetricFillet:是否不对称
swFilletData.DefaultRadius = 0.03; //默认半径
//横截面轮廓 sw中有几个选项都是从这里设置的。
swFilletData.ConicTypeForCrossSectionProfile = (int)swFeatureFilletProfileType_e.swFeatureFilletCircular;
swFilletData.CurvatureContinuous = false;//是否与相邻曲面平滑过渡。
swFilletData.ConstantWidth = true;//是否等宽
swFilletData.IsMultipleRadius = false;//是否为多半径圆角
swFilletData.OverflowType = (int)swFilletOverFlowType_e.swFilletOverFlowType_Default;//溢出类型,即与相邻面的过渡变换。
Feature swFeat = swFeatMgr.CreateFeature(swFilletData);
bool warning;
int errorCode3 = swFeat.GetErrorCode2(out warning);
}
}
}