Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations Ron247 on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

NX Journal

graberic

Computer
Jun 4, 2025
6
Hello,
New to creating an NX Journal. Prob something dumb here but I cannot get past this. This should rotate all the step files in a folder by a matrix.
I get a dreaded C+ error.
Line 1 : A namespace cannot directly contain members such as fields or methods
 

Attachments

  • Rotate.txt
    7 KB · Views: 12
Replies continue below

Recommended for you

What version of NX are you using? Your code references some properties on the Step214Importer object that I don't think exist.
 
Your AI helper is hallucinating properties and methods that don't exist in the NXOpen API.
 
yea that makes sense. So much for ChatGPT.
I tried the lazy way out.
Thanks for the confirmation.
 
Yup, the NXOpen API is too "exotic" for AI for it to generate code reliably (not enough questions about it on Stack Overflow I guess). You can get a good structure, but the actual methods that do the stuff you want are just electric sheep.

Also, the code is in Basic, if you're getting a C# error from it, that looks like an AI prompt problem. ;-)
 
I'm actually ok with VB. The Journal editor wants to make it a .cs file, which it treats entirely different, like C# I guess.
I can rename the file to .vb and I get a host of different errors.
It looks like I am going to need find the NXOpen API documentation and actually read it.
Does anyone have a link to the API methods?
 
You can select the preferred Journal language in Customer Defaults (permanent) and also in user interface preferences (Ctrl+2 - tools - journal, temporarily).

NXOpen documentation is available on the Siemens Support Center site ( https://support.sw.siemens.com/en-US/ ) and needs an access token.

There used to be an NXOpen reference available offline when you install the "NX Programming tools" component, but nowadays it has wandered off to the "NX Help" module, afaik.

The "Programming tools" module does add the UGOPEN folder where you can find and check out sample applications in all supported programming languages.

You can also just record a Journal while doing something manually in NX, and it will spit out (mostly) working code you can then analyse and modify.
 
Thanks everyone.
I guess when all else fails you can read the instructions...
 
The overall structure isn't bad, it just needs some tweaks. I'd be glad to help get something running when I find some time...
 
instead of CHATGPT - didyou try journaling the processing and using that data for you coding
 
Here is some code that will open step files, rotate the geometry 45° about the absolute +Z axis and export the geometry out to a new step file (with "_rotated" appended to the name). It works for STEP files that do not have any assembly structure. I did not test on STEP assembly files, but I assume that it would give some undesired results.

The journal is written in VB.net and was tested on NX2206.

Code:
Imports System
Imports System.Collections.Generic
Imports System.IO
Imports NXOpen
Imports NXOpen.UF

Module rotate_step_files

    Dim theSession As Session = Session.GetSession()
    Dim lw As ListingWindow = theSession.ListingWindow

    Sub Main()

        lw.Open()

        Dim stepFolderPath As String = "C:\temp\Rotate"
        Dim stepFiles As String() = Directory.GetFiles(stepFolderPath, "*.stp")
        Dim tempPrtFiles As New List(Of String)

        For Each stepFilePath As String In stepFiles
            'open file
            'lw.WriteLine("opening file: " & stepFilePath)
            Dim basePart1 As NXOpen.BasePart = Nothing
            Dim partLoadStatus1 As NXOpen.PartLoadStatus = Nothing
            basePart1 = theSession.Parts.OpenBaseDisplay(stepFilePath, partLoadStatus1)
            partLoadStatus1.Dispose()

            'rotate bodies
            'lw.WriteLine("rotating bodies")
            'origin point and vector for rotation in absolute coordinates
            Dim origin1 As NXOpen.Point3d = New NXOpen.Point3d(0.0, 0.0, 0.0)
            Dim vector1 As NXOpen.Vector3d = New NXOpen.Vector3d(0.0, 0.0, 1.0)

            RotateBodies(vector1, origin1, 45.0)
            'save file, must be saved for STEP export to work on rotated geometry
            Dim partSaveStatus1 As NXOpen.PartSaveStatus = Nothing
            partSaveStatus1 = basePart1.Save(NXOpen.BasePart.SaveComponents.True, NXOpen.BasePart.CloseAfterSave.False)
            partSaveStatus1.Dispose()

            'export new step file
            Dim exportName As String = IO.Path.Combine(IO.Path.GetDirectoryName(stepFilePath), IO.Path.GetFileNameWithoutExtension(stepFilePath) & "_rotated.stp")
            'lw.WriteLine("exportName: " & exportName)
            If My.Computer.FileSystem.FileExists(exportName) Then
                lw.WriteLine("file: '" & exportName & "' already exists, skipping export")
            Else
                'lw.WriteLine("exporting STEP file")
                ExportStep(basePart1, exportName)
            End If

            'close temp part file without saving
            tempPrtFiles.Add(basePart1.FullPath)
            'lw.WriteLine("closing file")
            basePart1.Close(BasePart.CloseWholeTree.True, BasePart.CloseModified.UseResponses, Nothing)
        Next

        'delete temp files
        For Each tempFile As String In tempPrtFiles
            Try
                My.Computer.FileSystem.DeleteFile(tempFile)
            Catch ex As Exception
                lw.WriteLine("error deleting file: " & ex.Message)
            End Try
        Next
        lw.Close()

    End Sub

    Sub RotateBodies(ByVal rotationAxis As Vector3d, ByVal rotationAxisPoint As Point3d, ByVal degrees As Double)

        Dim nullNXOpen_Features_MoveObject As NXOpen.Features.MoveObject = Nothing

        Dim moveObjectBuilder1 As NXOpen.Features.MoveObjectBuilder = Nothing
        moveObjectBuilder1 = theSession.Parts.Work.BaseFeatures.CreateMoveObjectBuilder(nullNXOpen_Features_MoveObject)

        Dim added1 As Boolean = Nothing
        added1 = moveObjectBuilder1.ObjectToMoveObject.Add(theSession.Parts.Work.Bodies.ToArray)

        moveObjectBuilder1.TransformMotion.Option = NXOpen.GeometricUtilities.ModlMotion.Options.Angle

        'origin point and vector for rotation
        'Dim origin1 As NXOpen.Point3d = New NXOpen.Point3d(0.0, 0.0, 0.0)
        'Dim vector1 As NXOpen.Vector3d = New NXOpen.Vector3d(0.0, 0.0, 1.0)

        Dim axis1 As NXOpen.Axis = Nothing
        axis1 = theSession.Parts.Work.Axes.CreateAxis(rotationAxisPoint, rotationAxis, NXOpen.SmartObject.UpdateOption.WithinModeling)

        moveObjectBuilder1.TransformMotion.AngularAxis = axis1

        'amount of rotation (degrees)
        moveObjectBuilder1.TransformMotion.Angle.SetFormula(degrees.ToString)

        Dim nXObject1 As NXOpen.NXObject = Nothing
        nXObject1 = moveObjectBuilder1.Commit()

        Dim objects1() As NXOpen.NXObject
        objects1 = moveObjectBuilder1.GetCommittedObjects()

        moveObjectBuilder1.Destroy()

    End Sub

    Sub ExportStep(ByVal partToExport As Part, ByVal exportFileName As String)

        Dim STEP214UG_DIR As String = theSession.GetEnvironmentVariableValue("STEP214UG_DIR")
        Dim step214File As String
        step214File = IO.Path.Combine(STEP214UG_DIR, "ugstep214.def")

        If Not IO.File.Exists(step214File) Then
            'Throw New Exception("ugstep214.def file not found")
            lw.WriteLine("ugstep214.def file not found, skipping export")
            Return
        End If


        Dim stepCreator1 As StepCreator = Nothing
        stepCreator1 = theSession.DexManager.CreateStepCreator()

        With stepCreator1
            .ExportAs = StepCreator.ExportAsOption.Ap214
            .ExportFrom = StepCreator.ExportFromOption.ExistingPart
            .SettingsFile = step214File
            .ObjectTypes.Surfaces = True
            .ObjectTypes.Solids = True
            .InputFile = partToExport.FullPath
            .OutputFile = exportFileName
            .FileSaveFlag = False
            .LayerMask = "1-256"
            .ProcessHoldFlag = True
        End With

        Dim nXObject1 As NXObject
        nXObject1 = stepCreator1.Commit()

        stepCreator1.Destroy()

    End Sub


End Module
 
Last edited:

Part and Inventory Search

Sponsor