"Batch-Only" Programm


i'm trying to write a tool in VB.NET which only converts a specific reference set into a step file (and either with visible or with hidden parts).

Now i am looking for a way without using the NX Journal Playback. I have a .net author license but i dont know how to set up the code for a batch-programm.
I don't want to use an interactive NX session, just start a tool from console and let the step translator do his job (unfortunately, i have to select some parts with the API)

I think my problem is the "Session"-Part:

            theSession = Session.GetSession()
            workPart = theSession.Parts.Work
            displayPart = theSession.Parts.Display

Whats the correct way to initialize them when using batch? Is there a "full sample"? I couldn't find any tutorials for this... (Remoting is the wrong thing i guess?)

Thanks in Advance!

Best Regards,

I don't think that you will need remoting to do what you are describing. You will compile your code to an .exe file; it will need to have access to the NXOpen dll files. I have not created such a batch program, but it is my understanding that to give the exe access to the dll files, you can add the path to the dll files to the "PATH" environment variable, or you can copy the dll files to the working directory of the exe (or similarly, move the exe to the appropriate NX directory).

When your program starts in batch mode and gets the session object, it will then need to load a part to work on. I would suggest passing the path to one or more files as string arguments to your exe file. Your program can then validate the path to the file, open it, and do its work.

Also, check out the NX customization help files:

Hello, thanks for your answer!

I already pass some arguments (also the "inputfile") to my programm, i can access all DLLs etc, but unfortunately i dont have a valid session because i don't know how to declare the things i quoted/code-highlighted (without using the active/opened session).

I think Session.GetSession() is not working because i don't have an running NX process/loaded part (and i dont know how to say "Use the part i passed in the arguments).

I really dont like the documentation, it looks like the main document is written in c++?

You will need to add references to the NXOpen dll files in your project before compiling. If your project has the proper references before compiling and has access to the dll files after compiling, it should start an NX process (there will not be a visible NX GUI in batch mode). The call to .GetSession should not need to be changed.

To use the argument(s) passed to your program, it will be similar to what is shown here:
You might also want to do a websearch on using command line arguments in a compiled exe program. I'll bet there is some good tutorials or sample code available.


that sample (especially the OpenPart Sub) you linked was exactly what i was looking for. Thank you very much!
Once im finished with my code i'll post it here (though im not sure if everything is working fine)

It's been a while but finally i finished the batch-programm.
The Problem above was because of wrong NXOpen Libraries - i needed to reference the Libraries with the exact same Version number as of NX itself (while this doesnt affect NX Journaling it does affect an batch program). For example NX10.0.2.6 needs the reference (in VS) to the Libraries of this version - it doesnt help when you change the files.

Anyway, with the following code i am able to convert a specific reference set to STP with a argument to also convert hidden bodies (by default off).

Option Strict On
Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.Features
    Public modus As String = ""
    Public inputfile As String = ""
    Public outputfile As String = ""
    Public definitionfile As String = ""
    Public logfile As String = ""
    Public hidden As Boolean = False
    Public theSession As Session = Session.GetSession()
    Public workPart As Part
    Public displayPart As Part
    Public refMembers(-1) As NXObject
    Sub Main(ByVal startparam() As String)
        If CheckStartParameter(startparam) = False OrElse ValidateArguments() = False Then
            Console.WriteLine("Konvertierung aufgrund eines Fehlers beendet...")
            Environment.ExitCode = 1
            Exit Sub
        End If
    End Sub
    Sub LoadPart()
        Dim basePart1 As BasePart
        Dim partLoadStatus1 As PartLoadStatus
        basePart1 = theSession.Parts.OpenBaseDisplay(inputfile, partLoadStatus1)
        displayPart = theSession.Parts.Display
        workPart = theSession.Parts.Work
    End Sub
    Function ValidateArguments() As Boolean
        If Not IO.File.Exists(definitionfile) OrElse Not IO.File.Exists(inputfile) Then
            Console.WriteLine("Definitions- oder Inputdatei nicht gefunden")
            Return False
        End If
            If IO.File.Exists(outputfile) Then
            End If
        Catch ex As Exception
            Console.WriteLine("Überschreiben der STEP Datei fehlgeschlagen. " & ex.Message)
            Return False
        End Try
        Return True
    End Function
    Sub GetVisibleBodies()
        Dim theBodies As New List(Of DisplayableObject)
        Dim myReferenceSets As ReferenceSet() = workPart.GetAllReferenceSets()
        For Each myRefSet As ReferenceSet In myReferenceSets
            If myRefSet.Name.ToUpper() = REFSETNAME Then
                refMembers = myRefSet.AskAllDirectMembers()
                For Each myObject As DisplayableObject In refMembers
                    If hidden = True Then
                        If myObject.IsBlanked = False Then
                        End If
                    End If
            End If
        If theBodies.Count > 0 Then
            Console.WriteLine("Nichts zu exportieren...")
            Environment.ExitCode = 1
            Exit Sub
        End If
    End Sub
    Sub StepExport(ByVal theBodies As List(Of DisplayableObject))
        Console.WriteLine("Starte Konvertierung...")
        Dim step214Creator1 As Step214Creator
        step214Creator1 = theSession.DexManager.CreateStep214Creator()
            With step214Creator1
                .SettingsFile = definitionfile
                .ExportSelectionBlock.SelectionScope = ObjectSelector.Scope.SelectedObjects
                .ObjectTypes.Solids = True
                .ObjectTypes.Structures = True
                .ObjectTypes.Surfaces = True
                .ObjectTypes.Csys = True
                .ObjectTypes.Curves = True
                .OutputFile = outputfile
                .FileSaveFlag = False
                .LayerMask = "1-256"
                .ProcessHoldFlag = True
                Dim added1 As Boolean
                added1 = .ExportSelectionBlock.SelectionComp.Add(theBodies.ToArray)
            End With
            Dim nXObject1 As NXObject
            nXObject1 = step214Creator1.Commit()
        Catch ex As NXException
            Console.WriteLine("Fehler... " & ex.Message)
            Environment.ExitCode = 1
            Exit Sub
            Console.WriteLine("Beende Konvertierung...")
        End Try
    End Sub
    Function CheckStartParameter(ByVal startparam As Array) As Boolean
        If startparam.Length < 4 Then
            Console.WriteLine("Es wurden nur " & startparam.Length.ToString & " von 4 benötigten Startparametern übergeben")
            Return False
            Dim lastloop As Integer = startparam.Length - 1
            For Each value As String In startparam
                If value.ToLower.Contains("d=") And value.ToLower.Contains(".def") Then
                    definitionfile = value.Substring(value.IndexOf(Chr(61)) + 1, value.Length - 1 - value.IndexOf(Chr(61)))
                ElseIf value.ToLower.Contains("m=") And value.ToLower.Contains("online") Or value.ToLower.Contains("offline") Then
                    modus = value
                ElseIf value.ToLower.Contains("o=") And value.ToLower.Contains(".stp") Then
                    outputfile = value.Substring(value.IndexOf(Chr(61)) + 1, value.Length - 1 - value.IndexOf(Chr(61)))
                ElseIf value.ToLower.Contains("h=") Then
                    If value.ToLower.Contains("h=true") Then
                        hidden = True
                    End If
                ElseIf lastloop <= 0 And value.ToLower.Contains(".prt") Then
                    inputfile = value
                    Console.WriteLine("Falscher Kommandozeilen-Parameter übergeben, beende Konvertierung...")
                    Environment.ExitCode = 1
                    Return False
                End If
                lastloop -= 1
        End If
        Return True
    End Function
    Public Function GetUnloadOption(ByVal dummy As String) As Integer
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
    End Function
End Module

Thanks for your help!

Glad to see that you got it working and thanks for posting your finished code!

im trying to adjust the above code a little bit, it seems that it doesnt process an assembly. "No input part found"
Is there an easy way to adjust the above code so i can either process a part or assembly with it? I cant loop through all the parts because i need one single step file.

You can get all the occurrence bodies in an assembly with code similar to that in thread: