NX9+VS2012 using System; using NXOpen; using NXOpen.UF; using NXOpenUI; using NXOpen.Utilities; public class Program { // class members private static Session theSession; private static Part workPart; private static UI theUI; private static UFSession theUfSession; public static Program theProgram; public static bool isDisposeCalled; //------------------------------------------------------------------------------ // Constructor //------------------------------------------------------------------------------ public Program() { try { theSession = Session.GetSession(); workPart = theSession.Parts.Work; theUI = UI.GetUI(); theUfSession = UFSession.GetUFSession(); isDisposeCalled = false; } catch (NXOpen.NXException ex) { // ---- Enter your exception handling code here ----- // UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, ex.Message); } } //------------------------------------------------------------------------------ // Explicit Activation // This entry point is used to activate the application explicitly //------------------------------------------------------------------------------ public static int Main(string[] args) { int retValue = 0; try { theProgram = new Program(); //TODO: Add your application code here //在任务环境中绘制草图,不加就是直接草图 theSession.BeginTaskEnvironment(); NXOpen.Sketch nullNXOpen_Sketch = null; //按平面方式创建草图 NXOpen.SketchInPlaceBuilder sketchInPlaceBuilder1; sketchInPlaceBuilder1 = workPart.Sketches.CreateNewSketchInPlaceBuilder(nullNXOpen_Sketch); //设置平面选项 sketchInPlaceBuilder1.PlaneOption = NXOpen.Sketch.PlaneOption.NewPlane; //创建平面(Z平面) sketchInPlaceBuilder1.Plane.SetMethod(NXOpen.PlaneTypes.MethodType.Fixed); //连续自动标注尺寸 theSession.Preferences.Sketch.ContinuousAutoDimensioning = false; //生成 NXOpen.NXObject nXObject1; nXObject1 = sketchInPlaceBuilder1.Commit(); //设置对象属性的名字 nXObject1.SetName("AAAA"); //转换成Feature NXOpen.Sketch sketch1 = (NXOpen.Sketch)nXObject1; NXOpen.Features.Feature feature1; feature1 = sketch1.Feature; //设置草图特征的名字 feature1.SetName("BBB"); //销毁 sketchInPlaceBuilder1.Destroy(); //退出任务环境草图,不加就是直接草图 theSession.EndTaskEnvironment(); //激活草图 sketch1.Activate(NXOpen.Sketch.ViewReorient.True);//参数是否将视图定向到草图 //创建四条直线(做矩形) Point3d startPoint1 = new Point3d(0, 0, 0); Point3d endPoint1 = new Point3d(100, 0, 0); Line line1 = workPart.Curves.CreateLine(startPoint1, endPoint1); Point3d startPoint2 = new Point3d(100, 0, 0); Point3d endPoint2 = new Point3d(100, -100, 0); Line line2 = workPart.Curves.CreateLine(startPoint2, endPoint2); Point3d startPoint3 = new Point3d(100, -100, 0); Point3d endPoint3 = new Point3d(0, -100, 0); Line line3 = workPart.Curves.CreateLine(startPoint3, endPoint3); Point3d startPoint4 = new Point3d(0, -100, 0); Point3d endPoint4 = new Point3d(0, 0, 0); Line line4 = workPart.Curves.CreateLine(startPoint4, endPoint4); //添加到草图里 sketch1.AddGeometry(line1, NXOpen.Sketch.InferConstraintsOption.InferCoincidentConstraints);//参数二,自动推断出约束 sketch1.AddGeometry(line2, NXOpen.Sketch.InferConstraintsOption.InferCoincidentConstraints); sketch1.AddGeometry(line3, NXOpen.Sketch.InferConstraintsOption.InferCoincidentConstraints); sketch1.AddGeometry(line4, NXOpen.Sketch.InferConstraintsOption.InferCoincidentConstraints); //1.由创建几何约束方法使用,以指示约束应该应用于什么几何 Sketch.ConstraintGeometry geom_line1; geom_line1.Geometry = line1;//几何对象 geom_line1.PointType = Sketch.ConstraintPointType.None;//点的类型 geom_line1.SplineDefiningPointIndex = 0;//忽略,除非点类型是SplineDefiningPoint //创建一个水平约束 sketch1.CreateHorizontalConstraint(geom_line1); //2. Sketch.ConstraintGeometry geom_line2; geom_line2.Geometry = line2; geom_line2.PointType = Sketch.ConstraintPointType.None; geom_line2.SplineDefiningPointIndex = 0; //创建一个垂直约束 sketch1.CreateVerticalConstraint(geom_line2); //3. Sketch.ConstraintGeometry geom_line3; geom_line3.Geometry = line3; geom_line3.PointType = Sketch.ConstraintPointType.None; geom_line3.SplineDefiningPointIndex = 0; //创建一个水平约束 sketch1.CreateHorizontalConstraint(geom_line3); //4. Sketch.ConstraintGeometry geom_line4; geom_line4.Geometry = line4; geom_line4.PointType = Sketch.ConstraintPointType.None; geom_line4.SplineDefiningPointIndex = 0; //创建一个垂直约束 sketch1.CreateVerticalConstraint(geom_line4); //1. Sketch.ConstraintGeometry geom_line1_startPoint; geom_line1_startPoint.Geometry = line1;//几何对象(直线) geom_line1_startPoint.PointType = Sketch.ConstraintPointType.StartVertex;//通过这条线找到它的起始端点 geom_line1_startPoint.SplineDefiningPointIndex = 0;//忽略,除非点类型是SplineDefiningPoint //得到草图原点坐标 Point3d SketchOri = sketch1.Origin; //创建一个点 Point OriPoint = workPart.Points.CreatePoint(SketchOri); //2. Sketch.ConstraintGeometry geom_OriPoint; geom_OriPoint.Geometry = OriPoint;//几何对象(点) geom_OriPoint.PointType = Sketch.ConstraintPointType.None;//点的类型为空 geom_OriPoint.SplineDefiningPointIndex = 0;//忽略,除非点类型是SplineDefiningPoint //创建点到点约束 sketch1.CreateCoincidentConstraint(geom_line1_startPoint, geom_OriPoint); //标注尺寸约束(直线1) NXOpen.NXObject nullNXOpen_NXObject = null; Sketch.DimensionGeometry dimLine1_startPoint = new NXOpen.Sketch.DimensionGeometry(); dimLine1_startPoint.Geometry = line1; dimLine1_startPoint.AssocType = Sketch.AssocType.StartPoint;//起点 dimLine1_startPoint.AssocValue = 0; dimLine1_startPoint.HelpPoint.X = 0.0; dimLine1_startPoint.HelpPoint.Y = 0.0; dimLine1_startPoint.HelpPoint.Z = 0.0; dimLine1_startPoint.View = nullNXOpen_NXObject; Sketch.DimensionGeometry dimLine1_endPoint = new NXOpen.Sketch.DimensionGeometry(); dimLine1_endPoint.Geometry = line1; dimLine1_endPoint.AssocType = Sketch.AssocType.EndPoint;//终点 dimLine1_endPoint.AssocValue = 0; dimLine1_endPoint.HelpPoint.X = 0.0; dimLine1_endPoint.HelpPoint.Y = 0.0; dimLine1_endPoint.HelpPoint.Z = 0.0; dimLine1_endPoint.View = nullNXOpen_NXObject; Point3d dimOri1 = new Point3d(100, 15, 0);//尺寸位置放置的点 Expression dimExp1 = workPart.Expressions.CreateSystemExpression("A1=200");//创建表达式 sketch1.CreateDimension(NXOpen.Sketch.ConstraintType.ParallelDim, dimLine1_startPoint, dimLine1_endPoint, dimOri1, dimExp1, NXOpen.Sketch.DimensionOption.CreateAsDriving); //标注尺寸约束(直线2) Sketch.DimensionGeometry dimLine2_startPoint = new NXOpen.Sketch.DimensionGeometry(); dimLine2_startPoint.Geometry = line2; dimLine2_startPoint.AssocType = Sketch.AssocType.StartPoint;//起点 dimLine2_startPoint.AssocValue = 0; dimLine2_startPoint.HelpPoint.X = 0.0; dimLine2_startPoint.HelpPoint.Y = 0.0; dimLine2_startPoint.HelpPoint.Z = 0.0; dimLine2_startPoint.View = nullNXOpen_NXObject; Sketch.DimensionGeometry dimLine2_endPoint = new NXOpen.Sketch.DimensionGeometry(); dimLine2_endPoint.Geometry = line2; dimLine2_endPoint.AssocType = Sketch.AssocType.EndPoint;//终点 dimLine2_endPoint.AssocValue = 0; dimLine2_endPoint.HelpPoint.X = 0.0; dimLine2_endPoint.HelpPoint.Y = 0.0; dimLine2_endPoint.HelpPoint.Z = 0.0; dimLine2_endPoint.View = nullNXOpen_NXObject; Point3d dimOri2 = new Point3d(210, -100, 0);//尺寸位置放置的点 Expression dimExp2 = workPart.Expressions.CreateSystemExpression("A2=200");//创建表达式 sketch1.CreateDimension(NXOpen.Sketch.ConstraintType.ParallelDim, dimLine2_startPoint, dimLine2_endPoint, dimOri2, dimExp2, NXOpen.Sketch.DimensionOption.CreateAsDriving); //完成草图 theSession.ActiveSketch.Deactivate(NXOpen.Sketch.ViewReorient.True, NXOpen.Sketch.UpdateLevel.Model); theProgram.Dispose(); } catch (NXOpen.NXException ex) { // ---- Enter your exception handling code here ----- } return retValue; } //------------------------------------------------------------------------------ // Following method disposes all the class members //------------------------------------------------------------------------------ public void Dispose() { try { if (isDisposeCalled == false) { //TODO: Add your application code here } isDisposeCalled = true; } catch (NXOpen.NXException ex) { // ---- Enter your exception handling code here ----- } } public static int GetUnloadOption(string arg) { //Unloads the image explicitly, via an unload dialog //return System.Convert.ToInt32(Session.LibraryUnloadOption.Explicitly); //Unloads the image immediately after execution within NX return System.Convert.ToInt32(Session.LibraryUnloadOption.Immediately); //Unloads the image when the NX session terminates // return System.Convert.ToInt32(Session.LibraryUnloadOption.AtTermination); } } Caesar卢尚宇 2020年8月16日
两个版本没有太大变化,个别的旧方法NX11废弃了,要用新的方法
NX11+VS2013 using System; using NXOpen; using NXOpen.UF; using NXOpenUI; using NXOpen.Utilities; public class Program { // class members private static Session theSession; private static Part workPart; private static UI theUI; private static UFSession theUfSession; public static Program theProgram; public static bool isDisposeCalled; //------------------------------------------------------------------------------ // Constructor //------------------------------------------------------------------------------ public Program() { try { theSession = Session.GetSession(); workPart = theSession.Parts.Work; theUI = UI.GetUI(); theUfSession = UFSession.GetUFSession(); isDisposeCalled = false; } catch (NXOpen.NXException ex) { // ---- Enter your exception handling code here ----- // UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, ex.Message); } } //------------------------------------------------------------------------------ // Explicit Activation // This entry point is used to activate the application explicitly //------------------------------------------------------------------------------ public static int Main(string[] args) { int retValue = 0; try { theProgram = new Program(); //TODO: Add your application code here //在任务环境中绘制草图,不加就是直接草图 theSession.BeginTaskEnvironment(); NXOpen.Sketch nullNXOpen_Sketch = null; //按平面方式创建草图 NXOpen.SketchInPlaceBuilder sketchInPlaceBuilder1; sketchInPlaceBuilder1 = workPart.Sketches.CreateSketchInPlaceBuilder2(nullNXOpen_Sketch); //设置平面选项 sketchInPlaceBuilder1.PlaneOption = NXOpen.Sketch.PlaneOption.NewPlane; //创建平面(Z平面) NXOpen.Point3d origin1 = new NXOpen.Point3d(0.0, 0.0, 0.0); NXOpen.Vector3d normal1 = new NXOpen.Vector3d(0.0, 0.0, 1.0); NXOpen.Plane plane1; plane1 = workPart.Planes.CreatePlane(origin1, normal1, NXOpen.SmartObject.UpdateOption.WithinModeling); sketchInPlaceBuilder1.PlaneReference = plane1; plane1.SetMethod(NXOpen.PlaneTypes.MethodType.Fixed); //连续自动标注尺寸 theSession.Preferences.Sketch.ContinuousAutoDimensioning = false; //生成 NXOpen.NXObject nXObject1; nXObject1 = sketchInPlaceBuilder1.Commit(); //设置对象属性的名字 nXObject1.SetName("这是对象属性的名字"); //转换成Feature NXOpen.Sketch sketch1 = (NXOpen.Sketch)nXObject1; NXOpen.Features.Feature feature1; feature1 = sketch1.Feature; //设置草图特征的名字 feature1.SetName("这是草图特征的名字"); //销毁 sketchInPlaceBuilder1.Destroy(); //退出任务环境草图,不加就是直接草图 theSession.EndTaskEnvironment(); //激活草图 sketch1.Activate(NXOpen.Sketch.ViewReorient.True);//参数是否将视图定向到草图 //创建四条直线(做矩形) Point3d startPoint1 = new Point3d(0, 0, 0); Point3d endPoint1 = new Point3d(100, 0, 0); Line line1 = workPart.Curves.CreateLine(startPoint1, endPoint1); Point3d startPoint2 = new Point3d(100, 0, 0); Point3d endPoint2 = new Point3d(100, -100, 0); Line line2 = workPart.Curves.CreateLine(startPoint2, endPoint2); Point3d startPoint3 = new Point3d(100, -100, 0); Point3d endPoint3 = new Point3d(0, -100, 0); Line line3 = workPart.Curves.CreateLine(startPoint3, endPoint3); Point3d startPoint4 = new Point3d(0, -100, 0); Point3d endPoint4 = new Point3d(0, 0, 0); Line line4 = workPart.Curves.CreateLine(startPoint4, endPoint4); //添加到草图里 sketch1.AddGeometry(line1, NXOpen.Sketch.InferConstraintsOption.InferCoincidentConstraints);//参数二,自动推断出约束 sketch1.AddGeometry(line2, NXOpen.Sketch.InferConstraintsOption.InferCoincidentConstraints); sketch1.AddGeometry(line3, NXOpen.Sketch.InferConstraintsOption.InferCoincidentConstraints); sketch1.AddGeometry(line4, NXOpen.Sketch.InferConstraintsOption.InferCoincidentConstraints); //1.由创建几何约束方法使用,以指示约束应该应用于什么几何 Sketch.ConstraintGeometry geom_line1; geom_line1.Geometry = line1;//几何对象 geom_line1.PointType = Sketch.ConstraintPointType.None;//点的类型 geom_line1.SplineDefiningPointIndex = 0;//忽略,除非点类型是SplineDefiningPoint //创建一个水平约束 sketch1.CreateHorizontalConstraint(geom_line1); //2. Sketch.ConstraintGeometry geom_line2; geom_line2.Geometry = line2; geom_line2.PointType = Sketch.ConstraintPointType.None; geom_line2.SplineDefiningPointIndex = 0; //创建一个垂直约束 sketch1.CreateVerticalConstraint(geom_line2); //3. Sketch.ConstraintGeometry geom_line3; geom_line3.Geometry = line3; geom_line3.PointType = Sketch.ConstraintPointType.None; geom_line3.SplineDefiningPointIndex = 0; //创建一个水平约束 sketch1.CreateHorizontalConstraint(geom_line3); //4. Sketch.ConstraintGeometry geom_line4; geom_line4.Geometry = line4; geom_line4.PointType = Sketch.ConstraintPointType.None; geom_line4.SplineDefiningPointIndex = 0; //创建一个垂直约束 sketch1.CreateVerticalConstraint(geom_line4); //1. Sketch.ConstraintGeometry geom_line1_startPoint; geom_line1_startPoint.Geometry = line1;//几何对象(直线) geom_line1_startPoint.PointType = Sketch.ConstraintPointType.StartVertex;//通过这条线找到它的起始端点 geom_line1_startPoint.SplineDefiningPointIndex = 0;//忽略,除非点类型是SplineDefiningPoint //得到草图原点坐标 Point3d SketchOri = sketch1.Origin; //创建一个点 Point OriPoint = workPart.Points.CreatePoint(SketchOri); //2. Sketch.ConstraintGeometry geom_OriPoint; geom_OriPoint.Geometry = OriPoint;//几何对象(点) geom_OriPoint.PointType = Sketch.ConstraintPointType.None;//点的类型为空 geom_OriPoint.SplineDefiningPointIndex = 0;//忽略,除非点类型是SplineDefiningPoint //创建点到点约束 sketch1.CreateCoincidentConstraint(geom_line1_startPoint, geom_OriPoint); //标注尺寸约束(直线1) NXOpen.NXObject nullNXOpen_NXObject = null; Sketch.DimensionGeometry dimLine1_startPoint = new NXOpen.Sketch.DimensionGeometry(); dimLine1_startPoint.Geometry = line1; dimLine1_startPoint.AssocType = Sketch.AssocType.StartPoint;//起点 dimLine1_startPoint.AssocValue = 0; dimLine1_startPoint.HelpPoint.X = 0.0; dimLine1_startPoint.HelpPoint.Y = 0.0; dimLine1_startPoint.HelpPoint.Z = 0.0; dimLine1_startPoint.View = nullNXOpen_NXObject; Sketch.DimensionGeometry dimLine1_endPoint = new NXOpen.Sketch.DimensionGeometry(); dimLine1_endPoint.Geometry = line1; dimLine1_endPoint.AssocType = Sketch.AssocType.EndPoint;//终点 dimLine1_endPoint.AssocValue = 0; dimLine1_endPoint.HelpPoint.X = 0.0; dimLine1_endPoint.HelpPoint.Y = 0.0; dimLine1_endPoint.HelpPoint.Z = 0.0; dimLine1_endPoint.View = nullNXOpen_NXObject; Point3d dimOri1 = new Point3d(100, 15, 0);//尺寸位置放置的点 Expression dimExp1 = workPart.Expressions.CreateSystemExpression("A1=200");//创建表达式 sketch1.CreateDimension(NXOpen.Sketch.ConstraintType.ParallelDim, dimLine1_startPoint, dimLine1_endPoint, dimOri1, dimExp1, NXOpen.Sketch.DimensionOption.CreateAsDriving); //标注尺寸约束(直线2) Sketch.DimensionGeometry dimLine2_startPoint = new NXOpen.Sketch.DimensionGeometry(); dimLine2_startPoint.Geometry = line2; dimLine2_startPoint.AssocType = Sketch.AssocType.StartPoint;//起点 dimLine2_startPoint.AssocValue = 0; dimLine2_startPoint.HelpPoint.X = 0.0; dimLine2_startPoint.HelpPoint.Y = 0.0; dimLine2_startPoint.HelpPoint.Z = 0.0; dimLine2_startPoint.View = nullNXOpen_NXObject; Sketch.DimensionGeometry dimLine2_endPoint = new NXOpen.Sketch.DimensionGeometry(); dimLine2_endPoint.Geometry = line2; dimLine2_endPoint.AssocType = Sketch.AssocType.EndPoint;//终点 dimLine2_endPoint.AssocValue = 0; dimLine2_endPoint.HelpPoint.X = 0.0; dimLine2_endPoint.HelpPoint.Y = 0.0; dimLine2_endPoint.HelpPoint.Z = 0.0; dimLine2_endPoint.View = nullNXOpen_NXObject; Point3d dimOri2 = new Point3d(210, -100, 0);//尺寸位置放置的点 Expression dimExp2 = workPart.Expressions.CreateSystemExpression("A2=200");//创建表达式 sketch1.CreateDimension(NXOpen.Sketch.ConstraintType.ParallelDim, dimLine2_startPoint, dimLine2_endPoint, dimOri2, dimExp2, NXOpen.Sketch.DimensionOption.CreateAsDriving); //完成草图 theSession.ActiveSketch.Deactivate(NXOpen.Sketch.ViewReorient.True, NXOpen.Sketch.UpdateLevel.Model); theProgram.Dispose(); } catch (NXOpen.NXException ex) { // ---- Enter your exception handling code here ----- } return retValue; } //------------------------------------------------------------------------------ // Following method disposes all the class members //------------------------------------------------------------------------------ public void Dispose() { try { if (isDisposeCalled == false) { //TODO: Add your application code here } isDisposeCalled = true; } catch (NXOpen.NXException ex) { // ---- Enter your exception handling code here ----- } } public static int GetUnloadOption(string arg) { //Unloads the image explicitly, via an unload dialog //return System.Convert.ToInt32(Session.LibraryUnloadOption.Explicitly); //Unloads the image immediately after execution within NX return System.Convert.ToInt32(Session.LibraryUnloadOption.Immediately); //Unloads the image when the NX session terminates // return System.Convert.ToInt32(Session.LibraryUnloadOption.AtTermination); } } Caesar卢尚宇 2020年8月16日