Need help On Find Expression by name

Here is the thing I am trying to achieve:
I want to search for a expression in work part by name and decide if it is exsited. If there is an expression existed already, assign/change its value; If there is no such an expression, create it and assign value.

I have been trying something like this, but it is not working as expected.

If IsNothing(workPart.Expressions.FindObject("aaaa")) = True Then
CreateInterpartExpression("DEISGN CONTROL", "p1", "aaaa")
ElseIf IsNothing(workPart.Expressions.FindObject("bbbb")) = True Then
CreateInterpartExpression("DEISGN CONTROL", "p3", "bbbb")
ElseIf IsNothing(workPart.Expressions.FindObject("cccc")) = True Then
CreateInterpartExpression("DEISGN CONTROL", "p5", "cccc")
ElseIf IsNothing(workPart.Expressions.FindObject("dddd")) = True Then
CreateInterpartExpression("DEISGN CONTROL", "p7", "dddd")
End If

Instead of processing the expression, the excution actually stopped and telling me that "NOpen.NXException: No object found with this name."
I tried IsError instead of IsNothing, can't get it to work either.

The "CreateInterpartExpression" is a function I defined my self. I tested it and it is working correctly.

Any help will be appreciated.

What version of NX are you using?

The following code was tested on NX 8.5.

Option Strict Off
Imports System
Imports NXOpen
 
Module Module1
 
    Sub Main()
 
        Dim theSession As Session = Session.GetSession()
        If IsNothing(theSession.Parts.Work) Then
            'active part required
            Return
        End If
 
        Dim workPart As Part = theSession.Parts.Work
        Dim lw As ListingWindow = theSession.ListingWindow
        lw.Open()
 
        Dim expToFind As String = "NomWallThk"
 
        Try
            Dim myExp As Expression
            myExp = workPart.Expressions.FindObject(expToFind)
            lw.WriteLine(myExp.Name)
            lw.WriteLine(myExp.RightHandSide)
 
        Catch ex As NXException
            'MsgBox(ex.Message)
            If ex.ErrorCode = 3520016 Then
                'no object found with this name
                'code to handle this error
                lw.WriteLine("expression not found with name: " & expToFind)
            Else
                'code to handle other errors
                lw.WriteLine(ex.ErrorCode & ": " & ex.Message)
            End If
        Finally
            lw.WriteLine("done processing")
            lw.WriteLine("")
        End Try
 
        lw.Close()
 
    End Sub
 
End Module

I appreciate your help. I have 2 more questions:
1. Is it possible that I can judge if this expression I found is an inter-part expression?
2. Could you please explain a little about how you get the error code "3520016"? What if I want to find an error code, how can I get it?

1. I've not tested this method, but I'm pretty sure you can search for the double colon in the expression's Equation property. If the double colon appears on the expression's left or right hand side, you can be pretty sure it is in interpart expression. Something like:

if myExp.Equation.Contains("::") then
    'this is an interpart expression
else
    'normal expression
end if

Alternatively, you can use the Expression collection's .GetInterpartReferences() method then search for the returned references in each expression's .Equation propery.

2. When you run into an error and want to test for the error code, wrap the call that throws the error in a Try block. In the Catch block, output the error code and message (much like what you see in the "Else" portion in the code above). At the time the code was written, I did not know what would happen if I tried to "find" an expression that didn't exist in the part file. I put the "find" command in the Try block and output the error code and message in the Catch block. The result I got was "Error 3520016: no object found with this name". Finally, I added an If/Else block in the Catch portion of the code. Now the "no object found" error can be handled by creating the required expression and all other errors can be handled differently.

Thank you for your help. It was very helpful.

I was trying to make something like this: if "MyExp.expression" doesn't contain "::p1", then, delete the expression that is already existed. However, when I try to use "DeleteExp("XXXX")" , I failed.

Could you please show me how "DeleteExp" works?

I'd suggest using the .Delete method in the part's expression collection object. Let's assume you have a reference to an expression that you want to delete (myExp) in the current work part. The code to delete the expression would look something like:

theSession.Parts.Work.Expressions.Delete(myExp)

Be aware that if the expression is still referenced in the part (or session) it may not allow you to delete the expression. In this case it will probably throw an exception. I'd suggest wrapping the delete expression code in a Try block and checking for errors.

Thank you. I got my journal all worked out.

Hi, I have similar problem with expression. The code below is part of much longer script. What I want to do in this part is to check if expression "ZNP_mill" exist and if yes - do something with it and if not, do something else. But this code working only in second case - if expression does not exist.

Option Strict Off
Imports System
Imports NXOpen
 
 
Module NXJournal
Sub Main (ByVal args() As String) 
 
Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
 
Try
Dim expression0 As Expression = workPart.Expressions.FindObject("ZNP_mill")
Catch ex As NXException
Dim error0 As Integer = ex.Errorcode
 
    		If error0 = 3520016 Then
lw.Writeline("Expression ZNP_mill not found, error n.: " & error0)
 
		Else
lw.Writeline("There is an expression ZNP_mill")
 
        	End If 
 
 
End try
 
End Sub
End Module

I see that you are using a Try block, this is good. The Catch portion of the block will only execute if there is an error encountered in the Try portion. If the expression exists in the part file and it is found, the Catch portion will not execute; add any code you want to execute when the expression is found to the Try portion of the block.

Option Strict Off
Imports System
Imports NXOpen
 
Module NXJournal
Sub Main (ByVal args() As String) 
 
Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
 
Try
    Dim expression0 As Expression = workPart.Expressions.FindObject("ZNP_mill")
    lw.Writeline("There is an expression ZNP_mill")
Catch ex As NXException
    Dim error0 As Integer = ex.Errorcode
 
    If error0 = 3520016 Then
        lw.Writeline("Expression ZNP_mill not found, error n.: " & error0)
        'code to execute if no expression is found
    Else
        lw.WriteLine("other error: " & ex.Message)
        'code to execute for other error
    End If 
 
End try
 
End Sub
End Module

Thanks. It looks so obvious now, I don't know why I could not figure it out myself.