NX crash with pdf export journal

Dear all,

I'm currently working on a journal file. The journal should export only the current drawing sheet to the file location of the part file of the current sheet.

However, something strange happens. During testing I discovered something weird. If I use the journal on a sheet it works fine. However, if I change the sheet size and use the journal file again NX crashes. I need windows task manager to get me out the mess I created.

The problems started when I changed the journal to incoperate the sheet size into the pdf. Before this the journal seemed to work fine but generated strange size pdf files.

I use NX10 on windows 7. I'm using the metric system (no idea why this should be important btw).

Any help would be greatly appreciated!

Kind regards,

Frank

Option Strict Off
Imports System
Imports System.IO
Imports System.Collections
Imports System.Windows.Forms
Imports System.Windows.Forms.MessageBox
Imports NXOpen
Imports NXOpen.UF
'MsgBox

Module NXJournal
Sub Main (ByVal args() As String)

'0) Start
Dim theSession As NXOpen.Session = NXOpen.Session.GetSession()
Dim workPart As NXOpen.Part = theSession.Parts.Work
Dim displayPart As NXOpen.Part = theSession.Parts.Display
Dim sheet As Drawings.DrawingSheet
Dim FileLocation as string

'Bepaal de volledige file locatie met part name
FileLocation = theSession.Parts.Work.FullPath
FileLocation = FileLocation.Substring(0, FileLocation.Length - 4) 'remove the last 4 characters of the path (de .prt)

'1) Open drafting
theSession.ApplicationSwitchImmediate("UG_APP_DRAFTING")

'2) Updates all views in all drawings maar verandert niet de current sheets zodat enkel de current sheet geëxporteerd kan worden na pdf verderop in de code.
For Each tempSheet As Drawings.DrawingSheet In workPart.DrawingSheets
For Each tempView As Drawings.DraftingView In tempSheet.GetDraftingViews
If tempView.IsOutOfDate Then
tempView.Update()
End If
Next
Next

'3) Export PDF + 4)Overschrijf PDF
Dim dwg As Drawings.DrawingSheet = displayPart.DrawingSheets().CurrentDrawingSheet
Dim printPDFBuilder1 As NXOpen.PrintPDFBuilder
printPDFBuilder1 = workPart.PlotManager.CreatePrintPdfbuilder()
printPDFBuilder1.Scale = 1.0
printPDFBuilder1.Action = PrintPDFBuilder.ActionOption.Native
printPDFBuilder1.Colors = PrintPDFBuilder.Color.BlackOnWhite
printPDFBuilder1.Size = NXOpen.PrintPDFBuilder.SizeOption.ScaleFactor
printPDFBuilder1.Units = NXOpen.PrintPDFBuilder.UnitsOption.English
printPDFBuilder1.XDimension = dwg.height
printPDFBuilder1.YDimension = dwg.length
printPDFBuilder1.OutputText = NXOpen.PrintPDFBuilder.OutputTextOption.Polylines
printPDFBuilder1.RasterImages = True
printPDFBuilder1.Watermark = ""
Dim sheets1(0) As NXOpen.NXObject
Dim drawingSheet1 As Drawings.DrawingSheet = CType(dwg, Drawings.DrawingSheet)
sheets1(0) = drawingSheet1
printPDFBuilder1.SourceBuilder.SetSheets(sheets1)
printPDFBuilder1.Filename = FileLocation & ".pdf"
Dim nXObject2 As NXOpen.NXObject
nXObject2 = printPDFBuilder1.Commit()
printPDFBuilder1.Destroy()

End Sub
End Module

I'm not sure that this is the cause of all your problems, but I'd start by changing this line to match the part units.

printPDFBuilder1.Units = NXOpen.PrintPDFBuilder.UnitsOption.English

Thank you verry much for the response.
I changed the line you mentioned. Sadly enoough it didn't help.

I did try something else. I removed the following line. This seams to stabilise the journal. I have no idea why.
theSession.ApplicationSwitchImmediate("UG_APP_DRAFTING")

If you are only exporting PDF's of the drawings, there is no need to switch to the drafting application. There is rarely a need to switch applications while your code is running. I'd suggest NOT switching applications in your case.

The .ApplicationSwitchImmediate function can be a bit flaky. The one time that I had need of it, I wrapped it in a Try block. This seemed to take care of any issues related to switching.

Try
theSession.ApplicationSwitchImmediate("UG_APP_MODELING")

Catch ex As NXException
lw.WriteLine(ex.ErrorCode & ": " & ex.Message)
End Try

Thank you for your response.
Switching application is indeed not strictly necessary in this code. I removed the line.

I am glad to hear that I am not the only one with problems with the .ApplicationSwitchImmediate function.

Again, thank you so much for your help!