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