Create Interpart Expression

The following journal was submitted by user macaber8. The code checks the current work part for the existence of a certain (interpart) expression; if the expression is not found it is created. The code illustrates finding an expression object given a name and creating an interpart expression.

Thank you macaber8 for sharing your code!
-NXJournaling

'There is a part called "DESIGN CONTROL" in the assembly file.In "DESIGN CONTROL", there's is an expression called "p1".
'Supposingly, there should be an inter-part expression called "BBPZ" in the assembly file, which links the value of the expression "p1" from "DESIGN CONTROL" to the assembly file.
'However, in work environment, it is likely that someone made a mistake to either not having create the "BBPZ" or not having this expression linked to the file where it suppose to.
'This journal is made to check the value of "BBPZ" in the assembly file.
 
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.BlockStyler
 
 
Module Module1
 
    Sub Main()
 
        Dim theSession As Session = Session.GetSession()
        If IsNothing(theSession.Parts.Work) Then
            'active part required
            Return
        End If
 
        CheckExpression("BBPZ", "p1", "DESIGN CONTROL")
 
 
    End Sub
 
 
    Public Function CreateInterpartExpression(ByVal frompartname As String, ByVal FromExpressionName As String, ByVal DestinationExpressionName As String) As NXObject
        Dim theSession As Session = Session.GetSession()
        Dim workPart As Part = theSession.Parts.Work
        Dim displayPart As Part = theSession.Parts.Display
 
        Dim interpartExpressionsBuilder1 As InterpartExpressionsBuilder
        interpartExpressionsBuilder1 = workPart.Expressions.CreateInterpartExpressionsBuilder()
 
        Dim part1 As Part = CType(theSession.Parts.FindObject(frompartname), Part)
        Dim expression1 As Expression = CType(part1.Expressions.FindObject(FromExpressionName), Expression)
 
        Dim sourceExpressions1(0) As Expression
        sourceExpressions1(0) = expression1
 
        Dim destinationNames1(0) As String
        destinationNames1(0) = DestinationExpressionName
 
        interpartExpressionsBuilder1.SetExpressions(sourceExpressions1, destinationNames1)
 
        Dim nXObject1 As NXObject
        nXObject1 = interpartExpressionsBuilder1.Commit()
        interpartExpressionsBuilder1.Destroy()
 
        Return nXObject1
    End Function
 
    Public Function CheckExpression(ByVal DestinationExpressionName As String, ByVal OriginalExpressionName As String, ByVal OriginalPart As String)
        Dim theSession As Session = Session.GetSession()
        Dim workPart As Part = theSession.Parts.Work
        Dim lw As ListingWindow = theSession.ListingWindow
        lw.Open()
 
        Dim expToFind1 As String = DestinationExpressionName
 
        Try
            Dim myExp1 As Expression
            myExp1 = workPart.Expressions.FindObject(expToFind1)
            'lw.WriteLine(myExp.Name)
            'lw.WriteLine(myExp.RightHandSide)
            If myExp1.Equation.Contains("::" & OriginalExpressionName) Then
                'lw.WriteLine(expToFind1 & " was found!")
            Else
                theSession.Parts.Work.Expressions.Delete(myExp1)
                CreateInterpartExpression(OriginalPart, OriginalExpressionName, DestinationExpressionName)
                lw.WriteLine(expToFind1 & " was found! But it was not valid! New " & DestinationExpressionName & " was Created")
            End If
 
        Catch ex As NXException
            'MsgBox(ex.Message)
            If ex.ErrorCode = 3520016 Then
                'no object found with this name
                'code to handle this error
                CreateInterpartExpression(OriginalPart, OriginalExpressionName, DestinationExpressionName)
                lw.WriteLine(expToFind1 & " was not found! New" & DestinationExpressionName & " was created")
            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()
 
        Return Nothing
    End Function
 
End Module