UG NX二次开发(C#)-建模-分割曲线

Posted GimiGimmy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UG NX二次开发(C#)-建模-分割曲线相关的知识,希望对你有一定的参考价值。

分割曲线

1、UG NX交互界面的功能介绍

在UG NX的交互界面中,分割曲线的功能在:菜单->编辑->曲线->裁剪,如下图所示。

打开“分割曲线”的功能界面,如下图所示。

通过分割曲线功能,将一条曲线分割为多段曲线,如下图所示。


![在这里插入图片描述](https://img-blog.csdnimg.cn/327c37ea19e14ee1841702af962782af.png

2、UG NX二次开发代码

采用UG NX二次开发来实现这个功能,最简单的方法是采用录制并修改录制代码,并将其作为一个方法来使用。修改后的代码如下:

/// <summary>
        /// 分割曲线
        /// </summary>
        /// <param name="curve">分割曲线的对象</param>
        /// <param name="segmentNumber">将曲线分割的段数</param>
        public static void DivideCurve(Curve curve,int segmentNumber)
        
            NXOpen.Session theSession = NXOpen.Session.GetSession();
            NXOpen.Part workPart = theSession.Parts.Work;

            NXOpen.Session.UndoMarkId markId1;
            markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "起点");

            NXOpen.Features.Feature nullNXOpen_Features_Feature = null;
            NXOpen.Features.DivideCurveBuilder divideCurveBuilder1;
            divideCurveBuilder1 = workPart.BaseFeatures.CreateDivideCurveBuilder(nullNXOpen_Features_Feature);

            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);

            NXOpen.Unit unit1 = ((NXOpen.Unit)workPart.UnitCollection.FindObject("MilliMeter"));
            NXOpen.Expression expression1;
            expression1 = workPart.Expressions.CreateSystemExpressionWithUnits("0", unit1);

            NXOpen.Expression expression2;
            expression2 = workPart.Expressions.CreateSystemExpressionWithUnits("0", unit1);

            divideCurveBuilder1.EqualParameterSegments = segmentNumber;

            NXOpen.GeometricUtilities.BoundingObjectBuilder boundingObjectBuilder1;
            boundingObjectBuilder1 = workPart.CreateBoundingObjectBuilder();

            NXOpen.Point3d origin2 = new NXOpen.Point3d(0.0, 0.0, 0.0);
            NXOpen.Vector3d normal2 = new NXOpen.Vector3d(0.0, 0.0, 1.0);
            NXOpen.Plane plane2;
            plane2 = workPart.Planes.CreatePlane(origin2, normal2, NXOpen.SmartObject.UpdateOption.WithinModeling);

            boundingObjectBuilder1.BoundingPlane = plane2;

            NXOpen.Expression expression3;
            expression3 = workPart.Expressions.CreateSystemExpressionWithUnits("0", unit1);

            NXOpen.Expression expression4;
            expression4 = workPart.Expressions.CreateSystemExpressionWithUnits("0", unit1);

            NXOpen.Plane nullNXOpen_Plane = null;
            boundingObjectBuilder1.BoundingPlane = nullNXOpen_Plane;

            divideCurveBuilder1.BoundingObjects.Append(boundingObjectBuilder1);

            theSession.SetUndoMarkName(markId1, "分割曲线 对话框");

            NXOpen.GeometricUtilities.CurveLocation []curveLocation = curve.GetLocations();          
            NXOpen.Point3d point1 = curveLocation[0].Location;
            divideCurveBuilder1.DividingCurve.SetValue(curve, workPart.ModelingViews.WorkView, point1);

            NXOpen.Session.UndoMarkId markId2;
            markId2 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "分割曲线");

            theSession.DeleteUndoMark(markId2, null);

            NXOpen.Session.UndoMarkId markId3;
            markId3 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "分割曲线");

            NXOpen.NXObject nXObject1;
            nXObject1 = divideCurveBuilder1.Commit();

            NXOpen.NXObject[] objects1;
            objects1 = divideCurveBuilder1.GetCommittedObjects();

            theSession.DeleteUndoMark(markId3, null);

            theSession.SetUndoMarkName(markId1, "分割曲线");

            divideCurveBuilder1.Destroy();

            try
            
                // 表达式仍然在使用中。
                workPart.Expressions.Delete(expression2);
            
            catch (NXException ex)
            
                ex.AssertErrorCode(1050029);
            

            try
            
                // 表达式仍然在使用中。
                workPart.Expressions.Delete(expression4);
            
            catch (NXException ex)
            
                ex.AssertErrorCode(1050029);
            

            try
            
                // 表达式仍然在使用中。
                workPart.Expressions.Delete(expression1);
            
            catch (NXException ex)
            
                ex.AssertErrorCode(1050029);
            

            plane1.DestroyPlane();

            try
            
                // 表达式仍然在使用中。
                workPart.Expressions.Delete(expression3);
            
            catch (NXException ex)
            
                ex.AssertErrorCode(1050029);
            

            plane2.DestroyPlane();

            NXOpen.Session.UndoMarkId id1;
            id1 = theSession.GetNewestUndoMark(NXOpen.Session.MarkVisibility.Visible);

            int nErrs1;
            nErrs1 = theSession.UpdateManager.DoUpdate(id1);

            theSession.CleanUpFacetedFacesAndEdges();
        

3、测试效果

编制一段测试程序,调用分割曲线的方法,代码如下:

Curve[] curves = workPart.Curves.ToArray();//工作部件的第一条曲线
DivideCurve(curves[0],3);//将曲线分割为3段

实现效果如下图所示。

![在这里插入图片描述](https://img-blog.csdnimg.cn/b663afdfd6b94db99e2681b05f1b3525.png

以上是关于UG NX二次开发(C#)-建模-分割曲线的主要内容,如果未能解决你的问题,请参考以下文章

UG NX二次开发(C#)-建模-获得点所在的圆弧曲线

UG NX二次开发(C#)-建模-判断一条曲线是不是封闭

UG NX二次开发(C#)-建模-判断一条曲线是不是封闭

UG NX二次开发(C#)-建模-创建填充曲面(CreateFillHoleBuilder)

UG NX二次开发(C#)-曲线-用CurveLengthBuilder延长曲线

UG NX二次开发(C#)-曲线-用CurveLengthBuilder延长曲线