1. 程式人生 > 實用技巧 >NX二次開發-NXOPEN C#方式建立草圖,新增約束,標註尺寸

NX二次開發-NXOPEN C#方式建立草圖,新增約束,標註尺寸

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日