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 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).