Journal Batch Mode : Error

Hi,

I tried following journal through nx interface, works without any issue.

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpenUI
Imports NXOpen.Utilities
Imports NXOpen.Annotations

Module ChangeNoteWord
Sub Main()
Dim s As Session = Session.GetSession()
Dim dp As Part = s.Parts.Display
Dim nc As NoteCollection = dp.Notes
Dim notetext1 As String = "existing word"
notetext1 = "COOL"
Dim notetext2 As String = "new word"
notetext2 = "TEST"
Dim notestring() As String
Dim nolines As Integer = 0
Dim found1 As Boolean = False
Dim m1 As Session.UndoMarkId = s.SetUndoMark(Session.MarkVisibility.Visible, "M1")
For Each a_note As SimpleDraftingAid In nc
notestring = a_note.GetText()
nolines = notestring.Length
For i As Integer = 0 To nolines - 1
found1 = notestring(i).Contains(notetext1)
If found1 = True Then
notestring(i) = notestring(i).Replace(notetext1, notetext2)
End If
Next
a_note.SetText(notestring)
Next
s.UpdateManager.DoUpdate(m1)
End Sub

Public Function GetUnloadOption(ByVal dummy As String) As Integer
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
End Function
End Module

but when i try to run the same code in batch mode, it fails to execute.
D:\opt\NX_8.5\UGII\run_journal.exe ReplaceString.vb

Err:
Runtime error:
System.NullReferenceException: Object reference not set to an instance of an object.
at ChangeNoteWord.Main() in C:\TMP\NXJournals3852\journal.vb:line 13

I am not sure where the issue is?

Any help will be highly appreciated.

Regards,
Maayan

Is NX running with a loaded part when you run your script?

yes, it is running with a part but still raises this error.

Looks like you don't have a displayed part.
So dp in line 12 will be "Nothing".
Then, when you try to get dp.Notes in line 13, you get a NullReferenceException

I tried with the same part which i used for interactive execution.

I think that you will need to pass in the part you want to edit as a parameter to the journal. The journal will then need to open this part and act on it. Call the code below in the NX command prompt with this command:
run_journal {path to ReplaceString.vb} -args {path to part}

I've added some code to open and save the part (if you do not save the part in batch mode, you won't see the changes); the added code does no error checking, you'll want to make it more robust for actual use.

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpenUI
Imports NXOpen.Utilities
Imports NXOpen.Annotations

Module ChangeNoteWord
Dim s As Session = Session.GetSession()
dim lw as listingwindow = s.listingwindow
Dim dp as part
Dim wp as part

Sub Main(args() as string)
lw.open
OpenPart(args(0))
Dim nc As NoteCollection = dp.Notes
Dim notetext1 As String = "existing word"
notetext1 = "COOL"
Dim notetext2 As String = "new word"
notetext2 = "TEST"
Dim notestring() As String
Dim nolines As Integer = 0
Dim found1 As Boolean = False
Dim m1 As Session.UndoMarkId = s.SetUndoMark(Session.MarkVisibility.Visible, "M1")
For Each a_note As Note In nc
notestring = a_note.GetText()
nolines = notestring.Length
For i As Integer = 0 To nolines - 1
found1 = notestring(i).Contains(notetext1)
If found1 = True Then
notestring(i) = notestring(i).Replace(notetext1, notetext2)
End If
Next
a_note.SetText(notestring)
Next
s.UpdateManager.DoUpdate(m1)

Dim partSaveStatus1 As PartSaveStatus
partSaveStatus1 = dp.Save(BasePart.SaveComponents.True, BasePart.CloseAfterSave.False)
partSaveStatus1.Dispose()

End Sub

sub OpenPart(byval thePart as string)
Dim basePart1 As BasePart
Dim partLoadStatus1 As PartLoadStatus
basePart1 = s.Parts.OpenBaseDisplay(thePart, partLoadStatus1)

dp = s.Parts.Display
wp = s.parts.work

partLoadStatus1.Dispose()
End Sub

Public Function GetUnloadOption(ByVal dummy As String) As Integer
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
End Function
End Module

Super, Thanks a lot. it worked like a charm.

If possible, Can I get your contact?

email can be sent to info@nxjournaling.com

Hello,

i was wondering if someone knows how to use multiple arguments:

-args "nospace" ""argument with spaces""

The array in args(0) / args(1) does not display that. Does anyone know how to do this?

Thank you in advance

I don't think you need to double up the quotes around an argument that has spaces. Try the following:
-args "nospace" "argument with spaces"

I tested the above using run_journal from the NX command line and got the expected result. However, when I ran it with:
-args "nospace" ""argument with spaces""
It broke up ""argument with spaces"" into multiple arguments.

Works great, thank you a lot for your help. How did you check this? Is there any possibility to check this myself in the future, because listing window and message boxes do not work?

Best regards

When NX is run in batch mode, anything written to the listing window is sent to the STDOUT stream. We can redirect the STDOUT stream to a text file with the ">" operator. When I used the NX command window, it looked something like below:


command prompt> run_journal {path to journal} -args "nospace" "arg with spaces" ""another arg with spaces"" > C:\temp\test.txt

This sent the output from the journal to a text file that I could then open and inspect the output. Below is the journal I used:

Option Strict Off
Imports System
Imports NXOpen

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

Sub Main(ByVal args() As String)

Dim workPart As Part = theSession.Parts.Work
lw.Open()

Const undoMarkName As String = "pass arguments journal"
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)

If String.IsNullOrEmpty(args(0)) Then
lw.WriteLine("no arguments passed to journal")
Return
End If

Dim i As Integer = 1
For Each arg As String In args
lw.WriteLine("argument " & i.ToString & ": " & arg)
i += 1
Next

lw.Close()

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

Here are a couple of links about the STDOUT and STDERR stream that might be helpful:
https://www.codecademy.com/learn/seds-learn-the-command-line/modules/sed...

https://ss64.com/nt/syntax-redirection.html

Hello,
I have a .bat file that contains following lines,
@ECHO OFF
ECHO.
call "%UGII_ROOT_DIR%\run_journal.exe" "D:\journals\line_journal.vb"
ECHO.
PAUSE()
CLS()
EXIT

When I run this I got following error,

Journal execution results for D:\journals\line_journal.vb...
Error loading libraries needed to run a journal.

Caught unexpected error: 948995: NX Viewer does not support running scripts

====================================================
Failed to handle error condition correctly - exiting
Invalid trailer (612665676) found in block &000000007769E04A
====================================================

Where is the problem although running same code in NX journaling edit window without any problem?

Try running the "run_journal" tool through an NX command prompt (Start -> your NX version -> NX Tools -> command prompt). This opens a command prompt and initializes some options for NX (it simply runs a .bat file in the NX install directory named "ugiicmd.bat"). If this allows your journal to run successfully, locate the "ugiicmd.bat" file in your NX install directory, copy and paste the appropriate initialization code from it to your .bat file.

Unfortunately it is not working via "Start -> your NX version -> NX Tools -> command prompt" it gave some errors.
C:\Program Files (x86)\UGS\NX 7.5\UGII> "%UGII_ROOT_DIR%\run_journal.exe" "c:\nxdeneme\d1.vb"

Journal execution results for c:\nxdeneme\d1.vb...
Runtime error:
System.NullReferenceException: Nesne ba#vurusu bir nesnenin örne#ine ayarlanmad#.
konum: NXJournal.Main() C:\Users\genel\AppData\Local\Temp\NXJournals8720\journal.vb içinde: sat#r 25

When you run code in batch mode (no visible GUI), you will need to specify what part file(s) that you want the code to operate on. See the example code above in this thread:

http://nxjournaling.com/comment/1406#comment-1406

I think the code is not valid for the session in native environment. Should the part name be dedefined such as C:\smth.prt in native session?

The code in the post dated 12/1/2014 was written and tested on native NX (probably NX 8 or 8.5 at the time, but it should also work for NX 9 and above).