Showing results for 
Search instead for 
Did you mean: 

Replace a Fitted Spline with a Studio Spline


To edit a fitcurve feature one is somewhat limited.  As a test I thought I might create a journal to replace a fitted spline with a studio spline.  The studio spline is much more useful to make changes such as end point control (G1 or G2) with connecting curves.  The fitted spline would only have G0 with conecting curve end points.  This journal work on NX9 or later.  It creates a by poles studio spline.  The concept can be extended to splines through (suffient) points and for line, arcs and circles.


Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.Features
Imports NXOpen.UF

Module FitsplineToStudioSpline
    Dim s As Session = Session.GetSession()
    Dim ui As UI = UI.GetUI()
    Dim ufs As UFSession = UFSession.GetUFSession()
    Dim wp As Part = s.Parts.Work
    Sub Main()
        Dim allFeatures As FeatureCollection = wp.Features
        Dim feattype1 As String = Nothing
        Dim spline1 As Spline = Nothing
        Dim objects1(-1) As NXObject
        Dim fittedsplines(-1) As Spline
        Dim cnt1 As Integer = 0
        For Each f1 As Feature In allFeatures
            feattype1 = f1.FeatureType
            If feattype1.Contains("Fit Curve") Then
                objects1 = f1.GetEntities
                If objects1(0).ToString.Contains("Spline") Then
                    ' add to an array
                    spline1 = DirectCast(objects1(0), Spline)
                    ReDim Preserve fittedsplines(cnt1)
                    fittedsplines(cnt1) = spline1
                    cnt1 += 1
                End If
            End If
        For i As Integer = 0 To fittedsplines.Length - 1
    End Sub
    Private Sub ReplaceFittedSplineWithStudioSpline(ByVal spline1 As Spline)
        Dim markId1 As NXOpen.Session.UndoMarkId = s.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Undo1")
        ' remove parameters. should not just delete the fit curve because if the points are a group these points should be retained
        Dim removeParametersBuilder1 As NXOpen.Features.RemoveParametersBuilder = Nothing
        removeParametersBuilder1 = wp.Features.CreateRemoveParametersBuilder()
        Dim added1 As Boolean = removeParametersBuilder1.Objects.Add(spline1)
        Dim nXObject1 As NXOpen.NXObject = removeParametersBuilder1.Commit()
        ' get spline data to create a studio spline
        Dim splineorder As Integer = spline1.Order
        Dim splineperiod As Boolean = spline1.Periodic
        Dim poles4d() As Point4d = spline1.GetPoles
        Dim poles3d(poles4d.Length - 1) As Point3d
        For i As Integer = 0 To poles4d.Length - 1
            poles3d(i) = New Point3d(poles4d(i).X, poles4d(i).Y, poles4d(i).Z)
        ' create studio spline
        CreateStudioSplinebyPoles(poles3d, splineorder, splineperiod)
        ' delete the fit curve spline
        Dim objects2() As NXOpen.NXObject = {spline1}
        Dim nErrs2 As Integer = s.UpdateManager.DoUpdate(markId1)
    End Sub
    Private Sub CreateStudioSplinebyPoles(ByVal poles3d() As Point3d, ByVal splineorder As Integer, ByVal splineperiod As Boolean)
        Dim Pcount As Integer = poles3d.Length - 1
        Dim points(Pcount) As Point
        For i As Integer = 0 To Pcount
            points(i) = wp.Points.CreatePoint(poles3d(i))
        Dim nullNXObject As NXObject = Nothing
        Dim studioSplineBuilder1 As Features.StudioSplineBuilderEx
        studioSplineBuilder1 = wp.Features.CreateStudioSplineBuilderEx(nullNXObject)
        studioSplineBuilder1.Degree = splineorder - 1
        studioSplineBuilder1.IsPeriodic = splineperiod
        studioSplineBuilder1.Type = NXOpen.Features.StudioSplineBuilderEx.Types.ByPoles
        Dim constraints1(Pcount) As Features.GeometricConstraintData
        For i As Integer = 0 To Pcount
            Dim geometricConstraintData1 As Features.GeometricConstraintData
            geometricConstraintData1 = studioSplineBuilder1.ConstraintManager.CreateGeometricConstraintData()
            geometricConstraintData1.Point = points(i)
            geometricConstraintData1.AutomaticConstraintDirection = Features.GeometricConstraintData.ParameterDirection.Iso
            geometricConstraintData1.AutomaticConstraintType = Features.GeometricConstraintData.AutoConstraintType.None
            Dim nullDirection As Direction = Nothing
            geometricConstraintData1.TangentDirection = nullDirection
            Dim nullScalar As Scalar = Nothing
            geometricConstraintData1.TangentMagnitude = nullScalar
            Dim nullOffset As Offset = Nothing
            geometricConstraintData1.Curvature = nullOffset
            geometricConstraintData1.CurvatureDerivative = nullOffset
            geometricConstraintData1.HasSymmetricModelingConstraint = False
            constraints1(i) = geometricConstraintData1
        Dim splineFeature As NXOpen.Features.StudioSpline = studioSplineBuilder1.Commit()
    End Sub

    Public Function GetUnloadOption(ByVal dummy As String) As Integer
        'Unloads the image immediately after execution within NX
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
    End Function

End Module


Frank Swinkels