Hi all,
I would like to test, if an expression exist in NX. If not, it should show a message and terminate the program without running the rest of the code. If no error, it should run all the code. But I did not find a way to exit the try-code to go directly to the end.
Option Strict Off
Imports System
Imports NXOpen
Imports System.Windows.Forms
Module FindExpression
Sub Main ()
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 expNameToFind As String = "MassPropVolume"
Dim myExp As Expression
Try
myExp = workPart.Expressions.FindObject(expNameToFind)
Catch ex As NXException
If ex.ErrorCode = 3520016 Then
'Expression not exist
MessageBox.Show("Part has no material expression" & _
ControlChars.NewLine & "Save part first to create them!", "Error", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
'other error
MessageBox.Show("Error" & ex.ErrorCode)
End If
Finally
End Try
' Code from here should run in case of no error
' code to run
' in case of error we will go to the end immediatelly
ProgEnd:
End Sub
End Module
re: terminate journal execution
You can make use of the End or Return statements. End will terminate the program no matter where the statement is used, Return will end the current Sub or Function. If Return is used in Sub Main, it will have the effect of ending the journal. In larger, more complicated programs, you might want to do some cleanup (such as calling a builder object's .Destroy method) before terminating the program.
re: terminate journal execution
The End-statement after each message (when error in the catch ...) does not work. The VB gives me the error-message that in Line 26 and 30, where I have set the End-statements, "The end statement cannot be used in class library projects". But the Return-statement works fine!
Thank you!
re: terminate journal execution
Hi,
another question to your solution. I have addeet 2 Return-statements in the code. At the end I have an "Unload-Function". But I´m not sure if this will be done when code is endig by the Return-statement:
Option Strict Off
Imports System
Imports NXOpen
Imports System.Windows.Forms
Module FindExpression
Sub Main ()
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 expNameToFind As String = "MassPropVolume"
Dim myExp As Expression
Try
myExp = workPart.Expressions.FindObject(expNameToFind)
Catch ex As NXException
If ex.ErrorCode = 3520016 Then
'Expression not exist
MessageBox.Show("Part has no material expression" & _
ControlChars.NewLine & "Save part first to create them!", "Error", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
Else
'other error
MessageBox.Show("Error" & ex.ErrorCode)
Return
End If
Finally
End Try
' Code from here should run in case of no error
' code to run
MessageBox.Show("Anything is OK", "Information")
Goto TheEnd
' in case of any error in the try-block we will go to the end immediatelly
TheEnd:
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
re: terminate journal execution
I wouldn't worry about it, NX calls that function automatically when the journal ends. NX should call the function as necessary whether the journal hits a "Return" or "End Sub" statement.