使用以下代码,利用给定的参数创建1个具有旋转和拉伸特征的零件。
public bool CreatNewPart()
{
//在 StandardAddInServer.cs文件中修改如下定义(加public static)
//原定义: private Inventor.Application m_inventorApplication;
//修改为: public static Inventor.Application m_inventorApplication;
//在以下场景中试验成功
//开发工具: Microsoft Visual Studio Professional 2017
//使用环境: Autodesk Inventor Professional 2020 中文版
bool IsOk = true;
try
{
//取得当前项目的路径
DesignProject oDesignProject;
oDesignProject = StandardAddInServer.m_inventorApplication.DesignProjectManager.ActiveDesignProject;
//MessageBox.Show("oProject.FullFileName = " + oProject.FullFileName);
string PrjPath = oDesignProject.FullFileName.Substring(0, oDesignProject.FullFileName.LastIndexOf("\\")); //文件路径,不含名称, 如: "D:\\part1"
//MessageBox.Show("PrjPath = " + PrjPath);
//生成临时文件名
string tmpPartName;
string strDate; //定义一个字符串
strDate = DateTime.Now.ToString("yyMMddHHmmss");
tmpPartName = "NewPart" + strDate + ".ipt";
// Create a new FileDialog object.
Inventor.FileDialog oFileDialog;
StandardAddInServer.m_inventorApplication.CreateFileDialog(out (oFileDialog));
// Define the filter to select part and assembly files or any file.
//oFileDlg.Filter = "Inventor Files (*.iam;*.ipt)|*.iam;*.ipt|All Files (*.*)|*.*";
oFileDialog.Filter = "Inventor Files (*.ipt)|*.ipt|All Files (*.*)|*.*";
// Define the part and assembly files filter to be the default filter.
oFileDialog.FilterIndex = 1;
// Set the title for the dialog.
oFileDialog.DialogTitle = "请选择要保存的文件";
// Set the initial directory that will be displayed in the dialog.
oFileDialog.InitialDirectory = PrjPath;
oFileDialog.FileName = tmpPartName;
// Set the flag so an error will not be raised if the user clicks the Cancel button.
oFileDialog.CancelError = false;
// Show the open dialog. The same procedure is also used for the Save dialog.
// The commented code can be used for the Save dialog.
//oFileDlg.ShowOpen();
oFileDialog.ShowSave();
// oFileDlg.ShowSave();
//System.Windows.Forms.MessageBox.Show("File " + oFileDlg.FileName + " was selected.", "Selected file");
//MessageBox.Show("File " + oFileDlg.FileName + " was selected.", "Selected file");
//分离路径和名称
FileName = oFileDialog.FileName.Substring(oFileDialog.FileName.LastIndexOf("\\") + 1); //文件名称,不含路径,如: "Head.ipt"
FilePath = oFileDialog.FileName.Substring(0, oFileDialog.FileName.LastIndexOf("\\")); //文件路径,不含名称, 如: "D:\\part1"
//使用默认的模板创建一个新的零件文档
//PartDocument partDoc;
//partDoc = (PartDocument)StandardAddInServer.m_inventorApplication.Documents.Add(DocumentTypeEnum.kPartDocumentObject);
//使用米制单位、国标模板创建一个新的零件文档
PartDocument oPartDocument;
oPartDocument = (PartDocument)StandardAddInServer.m_inventorApplication.Documents.Add(
DocumentTypeEnum.kPartDocumentObject,
StandardAddInServer.m_inventorApplication.FileManager.GetTemplateFile(DocumentTypeEnum.kPartDocumentObject,
SystemOfMeasureEnum.kMetricSystemOfMeasure, DraftingStandardEnum.kGB_DraftingStandard, null), true);
//设置单位
oPartDocument.UnitsOfMeasure.LengthUnits = UnitsTypeEnum.kMillimeterLengthUnits; //mm
oPartDocument.UnitsOfMeasure.MassUnits = UnitsTypeEnum.kKilogramMassUnits; //kg
oPartDocument.UnitsOfMeasure.AngleUnits = UnitsTypeEnum.kDegreeAngleUnits; //度
//设置材料
Asset oAsset;
AssetLibrary oAssetLibrary;
oAssetLibrary = StandardAddInServer.m_inventorApplication.AssetLibraries["Autodesk 材料库"];
//英文版的 AssetLibraries["Autodesk Material Library"];
//非金属材料
Asset libAsset;
libAsset = oAssetLibrary.MaterialAssets["MaterialInv_058"]; //橡胶
//Copy the asset locally.
oAsset = libAsset.CopyTo(oPartDocument);
//不锈钢材料
libAsset = oAssetLibrary.MaterialAssets["MaterialInv_026"]; //不锈钢
//Copy the asset locally.
oAsset = libAsset.CopyTo(oPartDocument);
//碳钢材料
libAsset = oAssetLibrary.MaterialAssets["MaterialInv_028"]; //低碳钢、高强度
//Copy the asset locally.
oAsset = libAsset.CopyTo(oPartDocument);
//set material to the part
//设置当前材料
//oPartDoc.ActiveMaterial= oPartDoc.Assets["MaterialInv_028"]; //低碳钢、高强度;
oPartDocument.ActiveMaterial = oAsset;
oPartDocument.ActiveMaterial.DisplayName = MaterialCode;
//设置为当前活动文档
oPartDocument.Activate();
//Create a user parameter
//添加用户参数
UserParameter oUserParamD_A,
oUserParamD_D,
oUserParamD_K,
oUserParamD_L,
oUserParamD_Th,
oUserParamN_nTH,
oUserParamL_C,
oUserParamD_N,
oUserParamD_B,
oUserParamL_H,
oUserParamD_RF_d,
oUserParamL_RF_f1,
oUserParamL_RF_f2,
oUserParamD_MFMTG_d,
oUserParamD_MFMTG_W,
oUserParamD_MFMTG_X,
oUserParamD_MFMTG_Y,
oUserParamD_MFMTG_Z,
oUserParamL_MFMTG_f2,
oUserParamL_MFMTG_f3,
oUserParamL_ZL;
oUserParamD_A = oPartDocument.ComponentDefinition.Parameters.UserParameters.AddByExpression("D_A", "508" + " mm", UnitsTypeEnum.kMillimeterLengthUnits);
oUserParamD_D = oPartDocument.ComponentDefinition.Parameters.UserParameters.AddByExpression("D_D", "700" + " mm", UnitsTypeEnum.kMillimeterLengthUnits);
oUserParamD_K = oPartDocument.ComponentDefinition.Parameters.UserParameters.AddByExpression("D_K", "635" + " mm", UnitsTypeEnum.kMillimeterLengthUnits);
oUserParamD_L = oPartDocument.ComponentDefinition.Parameters.UserParameters.AddByExpression("D_L", "33" + " mm", UnitsTypeEnum.kMillimeterLengthUnits);
oUserParamD_Th = oPartDocument.ComponentDefinition.Parameters.UserParameters.AddByExpression("D_Th", "30" + " mm", UnitsTypeEnum.kMillimeterLengthUnits);
oUserParamN_nTH = oPartDocument.ComponentDefinition.Parameters.UserParameters.AddByExpression("N_nTH", "20" + " ul", UnitsTypeEnum.kUnitlessUnits);
oUserParamL_C = oPartDocument.ComponentDefinition.Parameters.UserParameters.AddByExpression("L_C", "41.3" + " mm", UnitsTypeEnum.kMillimeterLengthUnits);
oUserParamD_N = oPartDocument.ComponentDefinition.Parameters.UserParameters.AddByExpression("D_N", "559" + " mm", UnitsTypeEnum.kMillimeterLengthUnits);
oUserParamD_B = oPartDocument.ComponentDefinition.Parameters.UserParameters.AddByExpression("D_B", "476" + " mm", UnitsTypeEnum.kMillimeterLengthUnits);
oUserParamL_H = oPartDocument.ComponentDefinition.Parameters.UserParameters.AddByExpression("L_H", "143" + " mm", UnitsTypeEnum.kMillimeterLengthUnits);
oUserParamD_RF_d = oPartDocument.ComponentDefinition.Parameters.UserParameters.AddByExpression("D_RF_d", "584.2" + " mm", UnitsTypeEnum.kMillimeterLengthUnits);
oUserParamL_RF_f1 = oPartDocument.ComponentDefinition.Parameters.UserParameters.AddByExpression("L_RF_f1", "2" + " mm", UnitsTypeEnum.kMillimeterLengthUnits);
oUserParamL_RF_f2 = oPartDocument.ComponentDefinition.Parameters.UserParameters.AddByExpression("L_RF_f2", "7" + " mm", UnitsTypeEnum.kMillimeterLengthUnits);
oUserParamD_MFMTG_d = oPartDocument.ComponentDefinition.Parameters.UserParameters.AddByExpression("D_MFMTG_d", "595" + " mm", UnitsTypeEnum.kMillimeterLengthUnits);
oUserParamD_MFMTG_W = oPartDocument.ComponentDefinition.Parameters.UserParameters.AddByExpression("D_MFMTG_W", "533.4" + " mm", UnitsTypeEnum.kMillimeterLengthUnits);
oUserParamD_MFMTG_X = oPartDocument.ComponentDefinition.Parameters.UserParameters.AddByExpression("D_MFMTG_X", "584.2" + " mm", UnitsTypeEnum.kMillimeterLengthUnits);
oUserParamD_MFMTG_Y = oPartDocument.ComponentDefinition.Parameters.UserParameters.AddByExpression("D_MFMTG_Y", "585.8" + " mm", UnitsTypeEnum.kMillimeterLengthUnits);
oUserParamD_MFMTG_Z = oPartDocument.ComponentDefinition.Parameters.UserParameters.AddByExpression("D_MFMTG_Z", "531.8" + " mm", UnitsTypeEnum.kMillimeterLengthUnits);
oUserParamL_MFMTG_f2 = oPartDocument.ComponentDefinition.Parameters.UserParameters.AddByExpression("L_MFMTG_f2", "7" + " mm", UnitsTypeEnum.kMillimeterLengthUnits);
oUserParamL_MFMTG_f3 = oPartDocument.ComponentDefinition.Parameters.UserParameters.AddByExpression("L_MFMTG_f3", "5" + " mm", UnitsTypeEnum.kMillimeterLengthUnits);
oUserParamL_ZL = oPartDocument.ComponentDefinition.Parameters.UserParameters.AddByExpression("L_ZL", "7" + " mm", UnitsTypeEnum.kMillimeterLengthUnits);
//定义工作点:原点
WorkPoint oCenterWorkPoint0;
//原点是:oPartDocument.ComponentDefinition.WorkPoints[1]
//也可写为:oPartDocument.ComponentDefinition.WorkPoints["Center Point"]
oCenterWorkPoint0 = oPartDocument.ComponentDefinition.WorkPoints["Center Point"];
//开始创建旋转实体
//创建二维草图,目的:旋转
//定义工作平面
//compDef.WorkPlanes[1]是YZ平面,compDef.WorkPlanes[2]是XZ平面,compDef.WorkPlanes[3]是XY平面
//也可以更直观的标示如下:
//WorkPlanes[1] = WorkPlanes["YZ Plane"]
//WorkPlanes[2] = WorkPlanes["XZ Plane"]
//WorkPlanes[3] = WorkPlanes["XY Plane"]
//也可以通过AddByLineAndTangent等方法自定义工作平面
//Dim oWorkPlane As WorkPlane
//Set oWorkPlane = oPartCompDef.WorkPlanes.AddByLinePlaneAndAngle (oWorkAxis, oPartCompDef.WorkPlanes.Item("YZ Plane"), 45, False)
//oWorkPlane.Name = "MyFirstWorkPlane"
WorkPlane oWorkPlane101 = oPartDocument.ComponentDefinition.WorkPlanes["XY Plane"];
//本零件的第一个草图
PlanarSketch oPlanarSketch100;
oPlanarSketch100 = oPartDocument.ComponentDefinition.Sketches.Add(oWorkPlane101);
oPlanarSketch100.Name = "WN旋转草图";
//投影原点到草图
SketchPoint oSketchPointOrigin101;
oSketchPointOrigin101 = (SketchPoint)oPlanarSketch100.AddByProjectingEntity(oCenterWorkPoint0);
//Set the origin point to use the center point work point.
oPlanarSketch100.OriginPoint = oCenterWorkPoint0;
//中心线
Point2d oPoint2d0101, oPoint2d0102;
SketchLine oSketchLine0101;
oPoint2d0101 = StandardAddInServer.m_inventorApplication.TransientGeometry.CreatePoint2d(0, 0);
oPoint2d0102 = StandardAddInServer.m_inventorApplication.TransientGeometry.CreatePoint2d(10, 0);
oSketchLine0101 = oPlanarSketch100.SketchLines.AddByTwoPoints(oPoint2d0101, oPoint2d0102);
oSketchLine0101.LineType = LineTypeEnum.kDashDottedLineType;
oSketchLine0101.StartSketchPoint.Merge(oSketchPointOrigin101);
oPlanarSketch100.GeometricConstraints.AddHorizontal((SketchEntity)oSketchLine0101);
oSketchLine0101.Centerline = true;
oSketchLine0101.Construction = true;
//添加实体
Point2d oPoint2d101, oPoint2d102, oPoint2d103, oPoint2d104, oPoint2d105, oPoint2d106, oPoint2d107, oPoint2d108, oPoint2d109;
oPoint2d101 = StandardAddInServer.m_inventorApplication.TransientGeometry.CreatePoint2d(0, 7);
oPoint2d102 = StandardAddInServer.m_inventorApplication.TransientGeometry.CreatePoint2d(1, 7);
oPoint2d103 = StandardAddInServer.m_inventorApplication.TransientGeometry.CreatePoint2d(5, 8);
oPoint2d104 = StandardAddInServer.m_inventorApplication.TransientGeometry.CreatePoint2d(5, 13);
oPoint2d105 = StandardAddInServer.m_inventorApplication.TransientGeometry.CreatePoint2d(8, 13);
oPoint2d106 = StandardAddInServer.m_inventorApplication.TransientGeometry.CreatePoint2d(8, 9);
oPoint2d107 = StandardAddInServer.m_inventorApplication.TransientGeometry.CreatePoint2d(9, 9);
oPoint2d108 = StandardAddInServer.m_inventorApplication.TransientGeometry.CreatePoint2d(9, 5);
oPoint2d109 = StandardAddInServer.m_inventorApplication.TransientGeometry.CreatePoint2d(0, 5);
SketchLine oSketchLine101, oSketchLine102, oSketchLine103, oSketchLine104, oSketchLine105,
oSketchLine106, oSketchLine107, oSketchLine108, oSketchLine109;
oSketchLine101 = oPlanarSketch100.SketchLines.AddByTwoPoints(oPoint2d101, oPoint2d102);
oSketchLine102 = oPlanarSketch100.SketchLines.AddByTwoPoints(oPoint2d102, oPoint2d103);
oSketchLine103 = oPlanarSketch100.SketchLines.AddByTwoPoints(oPoint2d103, oPoint2d104);
oSketchLine104 = oPlanarSketch100.SketchLines.AddByTwoPoints(oPoint2d104, oPoint2d105);
oSketchLine105 = oPlanarSketch100.SketchLines.AddByTwoPoints(oPoint2d105, oPoint2d106);
oSketchLine106 = oPlanarSketch100.SketchLines.AddByTwoPoints(oPoint2d106, oPoint2d107);
oSketchLine107 = oPlanarSketch100.SketchLines.AddByTwoPoints(oPoint2d107, oPoint2d108);
oSketchLine108 = oPlanarSketch100.SketchLines.AddByTwoPoints(oPoint2d108, oPoint2d109);
oSketchLine109 = oPlanarSketch100.SketchLines.AddByTwoPoints(oPoint2d109, oPoint2d101);
//添加约束
//oSketch.GeometricConstraints.AddGround((SketchEntity)oSketchLine2.EndSketchPoint);
//错误 oSketch.GeometricConstraints.AddCoincident((SketchEntity)oSketchLine2.EndSketchPoint, (SketchEntity)oSketchLine3.StartSketchPoint);
//如果你想通过它的端点连接两条曲线,不要使用addincident()方法,而是将两个点合并为一个。
//这段代码适合我。
//If you want to joint two curves by its end points, don't use AddCoincident() method but Merge two points to one.
oSketchLine101.EndSketchPoint.Merge(oSketchLine102.StartSketchPoint);
oSketchLine102.EndSketchPoint.Merge(oSketchLine103.StartSketchPoint);
oSketchLine103.EndSketchPoint.Merge(oSketchLine104.StartSketchPoint);
oSketchLine104.EndSketchPoint.Merge(oSketchLine105.StartSketchPoint);
oSketchLine105.EndSketchPoint.Merge(oSketchLine106.StartSketchPoint);
oSketchLine106.EndSketchPoint.Merge(oSketchLine107.StartSketchPoint);
oSketchLine107.EndSketchPoint.Merge(oSketchLine108.StartSketchPoint);
oSketchLine108.EndSketchPoint.Merge(oSketchLine109.StartSketchPoint);
oSketchLine109.EndSketchPoint.Merge(oSketchLine101.StartSketchPoint);
oPlanarSketch100.GeometricConstraints.AddHorizontal((SketchEntity)oSketchLine108);
oPlanarSketch100.GeometricConstraints.AddVerticalAlign(oSketchLine101.StartSketchPoint, oSketchLine0101.StartSketchPoint);
oPlanarSketch100.GeometricConstraints.AddParallel((SketchEntity)oSketchLine108, (SketchEntity)oSketchLine101);
oPlanarSketch100.GeometricConstraints.AddParallel((SketchEntity)oSketchLine108, (SketchEntity)oSketchLine104);
oPlanarSketch100.GeometricConstraints.AddParallel((SketchEntity)oSketchLine108, (SketchEntity)oSketchLine106);
oPlanarSketch100.GeometricConstraints.AddPerpendicular((SketchEntity)oSketchLine108, (SketchEntity)oSketchLine109);
oPlanarSketch100.GeometricConstraints.AddParallel((SketchEntity)oSketchLine109, (SketchEntity)oSketchLine103);
oPlanarSketch100.GeometricConstraints.AddParallel((SketchEntity)oSketchLine109, (SketchEntity)oSketchLine105);
oPlanarSketch100.GeometricConstraints.AddParallel((SketchEntity)oSketchLine109, (SketchEntity)oSketchLine107);
//添加尺寸约束
//两点约束
//oSketch.DimensionConstraints.AddTwoPointDistance(oProjectionOrigin,oSketchLine8.StartSketchPoint, DimensionOrientationEnum.kVerticalDim, oSketchLine8.Geometry.MidPoint);
//两条线的约束,true = 直径形式 ,false = 距离
//繁琐形式3行
//OffsetDimConstraint oOffsetDimD_A;
//oOffsetDimD_A=oSketch.DimensionConstraints.AddOffset(oSketchLine4, (SketchEntity)oSketchLine0, oSketchLine8.Geometry.MidPoint,true);
//oOffsetDimD_A.Parameter.Expression = oUserParamD_A.Name;
//简化形式1行
oPlanarSketch100.DimensionConstraints.AddOffset(oSketchLine104, (SketchEntity)oSketchLine0101, oSketchLine104.Geometry.MidPoint, true).Parameter.Expression = oUserParamD_D.Name;
oPlanarSketch100.DimensionConstraints.AddOffset(oSketchLine106, (SketchEntity)oSketchLine0101, oSketchLine106.Geometry.MidPoint, true).Parameter.Expression = oUserParamD_RF_d.Name;
oPlanarSketch100.DimensionConstraints.AddOffset(oSketchLine0101, (SketchEntity)oSketchLine102.EndSketchPoint, oSketchLine0101.Geometry.MidPoint, true).Parameter.Expression = oUserParamD_N.Name;
oPlanarSketch100.DimensionConstraints.AddOffset(oSketchLine101, (SketchEntity)oSketchLine0101, oSketchLine101.Geometry.MidPoint, true).Parameter.Expression = oUserParamD_A.Name;
oPlanarSketch100.DimensionConstraints.AddOffset(oSketchLine108, (SketchEntity)oSketchLine0101, oSketchLine108.Geometry.MidPoint, true).Parameter.Expression = oUserParamD_B.Name;
oPlanarSketch100.DimensionConstraints.AddTwoPointDistance(oSketchLine101.StartSketchPoint, oSketchLine101.EndSketchPoint, DimensionOrientationEnum.kHorizontalDim,
oSketchLine101.Geometry.MidPoint).Parameter.Expression = oUserParamL_ZL.Name;
oPlanarSketch100.DimensionConstraints.AddTwoPointDistance(oSketchLine104.StartSketchPoint, oSketchLine104.EndSketchPoint, DimensionOrientationEnum.kHorizontalDim,
oSketchLine104.Geometry.MidPoint).Parameter.Expression = oUserParamL_C.Name;
oPlanarSketch100.DimensionConstraints.AddTwoPointDistance(oSketchLine106.StartSketchPoint, oSketchLine106.EndSketchPoint, DimensionOrientationEnum.kHorizontalDim,
oSketchLine106.Geometry.MidPoint).Parameter.Expression = oUserParamL_RF_f1.Name;
oPlanarSketch100.DimensionConstraints.AddOffset(oSketchLine105, (SketchEntity)oSketchLine109, oSketchLine0101.Geometry.MidPoint, false).Parameter.Expression = oUserParamL_H.Name;
//通过草图定义轮廓
Profile oProfile101;
oProfile101 = oPlanarSketch100.Profiles.AddForSolid();
//创建旋转特征
RevolveFeature oRevFeature101;
oRevFeature101 = oPartDocument.ComponentDefinition.Features.RevolveFeatures.AddFull(oProfile101, oSketchLine0101, PartFeatureOperationEnum.kJoinOperation);
oRevFeature101.Name = "主体旋转";
//旋转特征创建完毕
//开始创建拉伸实体
//创建二维草图,目的:拉伸螺栓孔
//定义螺栓孔草图工作平面
//定义工作平面
//compDef.WorkPlanes[1]是YZ平面,compDef.WorkPlanes[2]是XZ平面,compDef.WorkPlanes[3]是XY平面
//也可以更直观的标示如下:
//WorkPlanes[1] = WorkPlanes["YZ Plane"]
//WorkPlanes[2] = WorkPlanes["XZ Plane"]
//WorkPlanes[3] = WorkPlanes["XY Plane"]
//也可以通过AddByLineAndTangent等方法自定义工作平面
//Dim oWorkPlane As WorkPlane
//Set oWorkPlane = oPartCompDef.WorkPlanes.AddByLinePlaneAndAngle (oWorkAxis, oPartCompDef.WorkPlanes.Item("YZ Plane"), 45, False)
//oWorkPlane.Name = "MyFirstWorkPlane"
WorkPlane oWorkPlane201;
oWorkPlane201 = oPartDocument.ComponentDefinition.WorkPlanes.AddByPlaneAndPoint(oPartDocument.ComponentDefinition.WorkPlanes["YZ Plane"], oSketchLine103.StartSketchPoint); ;
oWorkPlane201.Name = "螺孔草图工作平面";
//工作平面不可见
oWorkPlane201.Visible = false;
PlanarSketch oPlanarSketch200;
oPlanarSketch200 = oPartDocument.ComponentDefinition.Sketches.Add(oWorkPlane201);
oPlanarSketch200.Name = "螺栓孔草图";
//投影原点到草图
SketchPoint oSketchPointOrigin201; ;
oSketchPointOrigin201 = (SketchPoint)oPlanarSketch200.AddByProjectingEntity(oCenterWorkPoint0);
//Set the origin point to use the center point work point.
oPlanarSketch200.OriginPoint = oCenterWorkPoint0;
//辅助线:基线
Point2d oPoint2d201, oPoint2d202;
oPoint2d201 = StandardAddInServer.m_inventorApplication.TransientGeometry.CreatePoint2d(0, 0);
oPoint2d202 = StandardAddInServer.m_inventorApplication.TransientGeometry.CreatePoint2d(100, 0);
SketchLine oSketchLine201;
oSketchLine201 = oPlanarSketch200.SketchLines.AddByTwoPoints(oPoint2d201, oPoint2d202);
oSketchLine201.StartSketchPoint.Merge(oSketchPointOrigin201);
oPlanarSketch200.GeometricConstraints.AddHorizontal((SketchEntity)oSketchLine201);
oSketchLine201.Construction = true;
//辅助线:螺栓孔定为半径线
Point2d oPoint2d203, oPoint2d204;
oPoint2d203 = StandardAddInServer.m_inventorApplication.TransientGeometry.CreatePoint2d(0, 0);
oPoint2d204 = StandardAddInServer.m_inventorApplication.TransientGeometry.CreatePoint2d(100, 50);
SketchLine oSketchLine202;
oSketchLine202 = oPlanarSketch200.SketchLines.AddByTwoPoints(oPoint2d203, oPoint2d204);
oSketchLine202.StartSketchPoint.Merge(oSketchPointOrigin201);
oSketchLine202.Construction = true;
//添加等长约束
oPlanarSketch200.GeometricConstraints.AddEqualLength(oSketchLine201, oSketchLine202);
//添加尺寸约束
TwoPointDistanceDimConstraint oTwoPointDistanceDimConstraint201;
oTwoPointDistanceDimConstraint201 = oPlanarSketch200.DimensionConstraints.AddTwoPointDistance(oSketchLine202.StartSketchPoint, oSketchLine202.EndSketchPoint,
DimensionOrientationEnum.kAlignedDim,
oSketchLine202.EndSketchPoint.Geometry);
oTwoPointDistanceDimConstraint201.Parameter.Expression = oUserParamD_K.Name + "/2 ul";
TwoLineAngleDimConstraint oTwoLineAngleDimConstraint201;
oTwoLineAngleDimConstraint201 = oPlanarSketch200.DimensionConstraints.AddTwoLineAngle(oSketchLine201, oSketchLine202, oSketchLine202.EndSketchPoint.Geometry);
oTwoLineAngleDimConstraint201.Parameter.Expression = "180 deg/" + oUserParamN_nTH.Name;
//Create a sketch circle
//平面草图添加圆
SketchCircle oSketchCircle201;
oSketchCircle201 = oPlanarSketch200.SketchCircles.AddByCenterRadius(oSketchLine202.EndSketchPoint, 10);
oSketchCircle201.CenterSketchPoint.Merge(oSketchLine202.EndSketchPoint);
//Add dimensional constraints to the sketch entities
DiameterDimConstraint oDiameter201;
//DiameterDimConstraint oDiameter = null;
oDiameter201 = oPlanarSketch200.DimensionConstraints.AddDiameter((SketchEntity)oSketchCircle201, oSketchCircle201.CenterSketchPoint.Geometry);
oDiameter201.Parameter.Expression = oUserParamD_L.Name;
//准备旋转
//定义集合,用来保存对象
ObjectCollection oObjectCollection201;
oObjectCollection201 = StandardAddInServer.m_inventorApplication.TransientObjects.CreateObjectCollection();
//加入要旋转的对象
oObjectCollection201.Clear();
oObjectCollection201.Add(oSketchCircle201);
//通过草图定义轮廓
Profile oProfile201;
oProfile201 = oPlanarSketch200.Profiles.AddForSolid(false,oObjectCollection201);
//创建拉伸定义
//Create a base extrusion 4 cm thick.
ExtrudeDefinition oExtrudeDefinition201;
oExtrudeDefinition201 = oPartDocument.ComponentDefinition.Features.ExtrudeFeatures.CreateExtrudeDefinition(oProfile201, PartFeatureOperationEnum.kCutOperation);
//设置拉伸距离和方向
oExtrudeDefinition201.SetThroughAllExtent(PartFeatureExtentDirectionEnum.kPositiveExtentDirection);
//创建拉伸特征
ExtrudeFeature oExtrudeFeature201;
oExtrudeFeature201 = oPartDocument.ComponentDefinition.Features.ExtrudeFeatures.Add(oExtrudeDefinition201);
oExtrudeFeature201.Name = "螺栓孔拉伸";
//阵列CircularPatternFeature
//pPartCompDef.Features.CircularPatternFeatures.AddByDefinition();
//定义集合,用来保存Feature对象
ObjectCollection oObjectCollection202;
oObjectCollection202 = StandardAddInServer.m_inventorApplication.TransientObjects.CreateObjectCollection();
//加入要阵列的Feature对象
oObjectCollection202.Clear();
oObjectCollection202.Add(oExtrudeFeature201);
CircularPatternFeatureDefinition oCircularPatternFeatureDefinition201;
oCircularPatternFeatureDefinition201 = oPartDocument.ComponentDefinition.Features.CircularPatternFeatures.CreateDefinition(oObjectCollection202,
oPartDocument.ComponentDefinition.WorkAxes[1], false, 6, 2 * 3.1415926, true);
//CircularPatternFeatures.CreateDefinition( ParentFeatures As ObjectCollection, AxisEntity As Object, NaturalAxisDirection As Boolean, Count As Variant, Angle As Variant, [FitWithinAngle] As Boolean ) As CircularPatternFeatureDefinition
CircularPatternFeature oCircularPatternFeature201;
oCircularPatternFeature201 = oPartDocument.ComponentDefinition.Features.CircularPatternFeatures.AddByDefinition(oCircularPatternFeatureDefinition201);
oCircularPatternFeature201.Name = "螺栓孔阵列";
oCircularPatternFeature201.Parameters[1].Name = "阵列包角";
oCircularPatternFeature201.Parameters[1].Expression = "360 deg";
oCircularPatternFeature201.Parameters[2].Name = "阵列数量";
oCircularPatternFeature201.Parameters[2].Expression = oUserParamN_nTH.Name;
oPartDocument.Rebuild();
oPartDocument.Views[1].Fit();
//更新质量
//解释说明:只要属性MassProperties被调用,那么就会自动更新
//要放在 oPartDoc.Rebuild() 之后
double m1 = oPartDocument.ComponentDefinition.MassProperties.Mass;
string strFileName = FilePath + "\\" + FileName;
oPartDocument.FullFileName = strFileName;
oPartDocument.Save();
//oPartDoc.SaveAs(strFileName, false);
}
catch (Exception e)
{
MessageBox.Show("CreatNewPart() 出现错误!");
MessageBox.Show(e.ToString());
IsOk = false;
}
return IsOk;
}