cancel
Showing results for 
Search instead for 
Did you mean: 

Updating Expressions Not Working

Creator
Creator

I have created a dialog box using the Block UI Styler in NX 8.5.  The dialog box was working but I changed a couple of the expession names and used interpart linking and made the appropriate name changes in my code.  However, now some of the code no longer works.  I'm not sure why this is because some of it works while other parts don't even though the code looks nearly identical.  

 

The first error I get is under the taper update section at the line: 

workPart.Expressions.Edit(expression4, """" & var4 & """")

With the error:

NXOpen.NXException: The specified expression variable does not exist.

 

If I comment this section out I then get an error in the diameter update section at the line:

nErrs6 = theSession.UpdateManager.DoUpdate(markId6)

With the error:

NXOpen.NXException: Update undo happened

 

I attached a portion of the code below, any direction on to what could be causing these errors would be appreciated.

 

 

'------------------------------------------------------------------------------
'Callback Name: apply_cb
'------------------------------------------------------------------------------
Public Function apply_cb() As Integer
Dim errorCode as Integer = 0
Try

'---- Enter your callback code here -----
'Analysis Component Update
Dim var0 As String = ComponentSelecter.ValueAsString()
Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim Exp_title0 As String = "UI_SELECTED_ANALYSIS_1"
Dim exp_0 As Expression = Nothing

Dim markId0 As Session.UndoMarkId
markId0 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Expression edit 0")

Dim expression0 As Expression
Dim nErrs0 As Integer

Try
'Create the expression if it does not already exist
exp_0 = workPart.Expressions.CreateExpression("String", "UI_SELECTED_ANALYSIS_1" & var0)
nErrs0 = theSession.UpdateManager.DoUpdate(markId0)
Catch ex As Exception
'Modify the existing expression
expression0 = CType(workPart.Expressions.FindObject(Exp_title0), Expression)
workPart.Expressions.Edit(expression0, """" & var0 & """")
If var0 = "JD" Then
Dim drawingSheet1 As Drawings.DrawingSheet = CType(workPart.DrawingSheets.FindObject("Sheet 1"), Drawings.DrawingSheet)

drawingSheet1.Open()
Else
theSession.DataManager.LoadFile("C:\Users\hsalama2\AppData\Local\Temp\DEV000070786_15\THA_ROM.afu")
End If
nErrs0 = theSession.UpdateManager.DoUpdate(markId0)
End Try

'StemSystem Update
Dim var1 As String = StemSystem.ValueAsString()
Dim Exp_title1 As String = "UI_SELECTED_STEM_SYSTEM"
Dim exp_1 As Expression = Nothing

Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Expression edit 1")

Dim expression1 As Expression
Dim nErrs1 As Integer

Try
' Create the expression if it does not already exist
exp_1 = workPart.Expressions.CreateExpression("String", "UI_SELECTED_STEM_SYSTEM" & var1)
nErrs1 = theSession.UpdateManager.DoUpdate(markId1)
Catch Ex As Exception
' Modify the existing expression
expression1 = CType(workPart.Expressions.FindObject(Exp_title1), Expression)
workPart.Expressions.Edit(expression1, """" & var1 & """")
nErrs1 = theSession.UpdateManager.DoUpdate(markId1)
End Try

'StemStyle Update
Dim var2 As String = StemStyle.ValueAsString()
Dim Exp_title2 As String = "UI_SELECTED_STEM_STYLE_1"
Dim exp_2 As Expression = Nothing

Dim markId2 As Session.UndoMarkId
markId2 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Expression edit 2")

Dim expression2 As Expression
Dim nErrs2 As Integer

Try
'Create the expression if it does not already exist
exp_2 = workPart.Expressions.CreateExpression("String", "UI_SELECTED_STEM_STYLE_1" & var2)
nErrs2 = theSession.UpdateManager.DoUpdate(markId2)
Catch ex As Exception
'Modify the existing expression
expression2 = CType(workPart.Expressions.FindObject(Exp_title2), Expression)
workPart.Expressions.Edit(expression2, """" & var2 & """")
nErrs2 = theSession.UpdateManager.DoUpdate(markId2)
End Try

'StemSize Update
Dim var3 As String = StemSize.ValueAsString()
Dim Exp_title3 As String = "UI_SELECTED_STEM_SIZE"
Dim exp_3 As Expression = Nothing

Dim markId3 As Session.UndoMarkId
markId3 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Expression edit 3")

Dim expression3 As Expression
Dim nErrs3 As Integer

Try
'Create the expression if it does not already exist
exp_3 = workPart.Expressions.CreateExpression("Number", "UI_SELECTED_STEM_SIZE" & var3)
nErrs3 = theSession.UpdateManager.DoUpdate(markId3)
Catch ex As Exception
'Modify the existing expression
expression3 = CType(workPart.Expressions.FindObject(Exp_title3), Expression)
workPart.Expressions.Edit(expression3, var3)
nErrs3 = theSession.UpdateManager.DoUpdate(markId3)
End Try

'Taper Update
Dim var4 As String = Taper.ValueAsString()
Dim Exp_title4 As String = "UI_SELECTED_TAPER"
Dim exp_4 As Expression = Nothing

Dim markId4 As Session.UndoMarkId
markId4 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Expression edit 4")

Dim expression4 As Expression
Dim nErrs4 As Integer

Try
'Create the expression if it does not already exist
exp_4 = workPart.Expressions.CreateExpression("String", "UI_SELECTED_TAPER" & var4)
nErrs4 = theSession.UpdateManager.DoUpdate(markId4)
Catch Ex As Exception
'Modify the existing expression
expression4 = CType(workPart.Expressions.FindObject("UI_SELECTED_TAPER"), Expression)
workPart.Expressions.Edit(expression4, """" & var4 & """")
nErrs4 = theSession.UpdateManager.DoUpdate(markId4)
End Try

'MaterialFinish Update
Dim var5 As String = MaterialFinish.ValueAsString()
Dim Exp_title5 As String = "UI_SELECTED_MATERIALFINISH_1"
Dim exp_5 As Expression = Nothing

Dim markId5 As Session.UndoMarkId
markId5 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Expression edit 5")

Dim expression5 As Expression
Dim nErrs5 As Integer

Try
'Create the expression if it does not already exist
exp_5 = workPart.Expressions.CreateExpression("String", "UI_SELECTED_MATERIALFINISH_1" & var5)
nErrs5 = theSession.UpdateManager.DoUpdate(markId5)
Catch ex As Exception
'Modify the existing expression
expression5 = CType(workPart.Expressions.FindObject(Exp_title5), Expression)
workPart.Expressions.Edit(expression5, """" & var5 & """")
nErrs5 = theSession.UpdateManager.DoUpdate(markId5)
End Try

'Diameter Update
Dim var6 As String = Diameter.ValueAsString()
Dim Exp_title6 As String = "UI_SELECTED_DIAMETER"
Dim exp_6 As Expression = Nothing

Dim markId6 As Session.UndoMarkId
markId6 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Expression edit 6")

Dim expression6 As Expression
Dim nErrs6 As Integer

Try
'Create the expression if it does not already exist
exp_6 = workPart.Expressions.CreateExpression("Number", "UI_SELECTED_DIAMETER" & var6)
nErrs6 = theSession.UpdateManager.DoUpdate(markId6)
Catch ex As Exception
'Modify the existing expression
expression6 = CType(workPart.Expressions.FindObject(Exp_title6), Expression)
workPart.Expressions.Edit(expression6, var6)
nErrs6 = theSession.UpdateManager.DoUpdate(markId6)
End Try

Catch ex As Exception

'---- Enter your exception handling code here -----
errorCode = 1
theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString)
End Try
apply_cb = errorCode
End Function

5 REPLIES

Re: Updating Expressions Not Working

Honored Contributor
Honored Contributor

Concerning expression4, I think the error is really happening when you attempt to create the expression. When creating an expression, there needs to be an equal sign between the name of the expression and the value (or formula). Also, the string value of the expression will need to evaluate to a string with double quotes around it. Since the expression creation is in the Try block, the Catch block is activated; however, the only error that is accommodated for is "expression already exists". Therefore it attempts to edit an expression that does not yet exist, throwing another error. I'd suggest checking the error value and taking appropriate action.

 

'Taper Update
Dim var4 As String = Taper.ValueAsString()
Dim Exp_title4 As String = "UI_SELECTED_TAPER"
Dim exp_4 As Expression = Nothing

Dim markId4 As Session.UndoMarkId
markId4 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Expression edit 4")

Dim nErrs4 As Integer

Try
'Create the expression if it does not already exist
exp_4 = workPart.Expressions.CreateExpression("String", Exp_title4 & " = " & """" & var4 & """")
nErrs4 = theSession.UpdateManager.DoUpdate(markId4)

Catch ex As NxException

    If ex.ErrorCode = 1050017 Then
        'expression already exists
        exp_4 = workPart.Expressions.FindObject(Exp_title4)
        workPart.Expressions.Edit(exp_4, """" & var4 & """")
        nErrs4 = theSession.UpdateManager.DoUpdate(markId4)
    Else
        'other error
        lw.WriteLine("NX exception: " & ex.ErrorCode & ", " & ex.Message)
        'other error handling code
    End If

End Try

Re: Updating Expressions Not Working

Creator
Creator

Thank you for the quick response.  I updated my code so that I can check for other errors by using your suggestions but it still gets stuck at the same point and states the same error.  The expression does already exist as it's currently being referenced by other expressions in other parts.  If I change the title to something that doesn't exist this code will successfully create a new expression.  Since it goes into the first If statement and gets stuck there I can't see if there are any other errors causing this issue.

Re: Updating Expressions Not Working

Honored Contributor
Honored Contributor

You previously mentioned interpart expressions; your code checks the work part for the expression. Are you sure that the current work part is the one that owns the expression you are interested in?

Re: Updating Expressions Not Working

Creator
Creator

The current work part has this expression listed in the expressions dialog box.  (This is also where I first created the expression) So from what I understand, yes the current work part owns the expression I want to update.

Re: Updating Expressions Not Working

Honored Contributor
Honored Contributor

In your code, before attempting to edit expression 4, I suggest writing some information to the listing window such as: workPart.FullPath and all the workPart expression names. This will help to verify that the code is doing what you think it is doing.