Expressions: creating expressions (no units)

Now that we've seen how to query existing expressions, this article will take a look at creating new expressions. The code in this article will demonstrate how to create number expressions (unitless), number expressions that reference existing expressions in the formula, string expressions, boolean expressions, integer expressions, point expressions, vector expressions, and list expressions. Also, the code will show how to find and edit existing expressions.

The entire code posting is pretty long, so I'll display it piece by piece with the entire code listing at the end of the article. The code was written and tested on NX 9, but should work correctly on NX 8 or above. It will also run on NX 7.5 (the oldest version I currently have installed), but you will get an error message because NX 7.5 did not have the "list" expression type.

The Expression collection .Create() method

Create a Number Expression (unitless)

Dim exp_1 As Expression = Nothing
Dim exp1Value As Double = 2.7182
Try
'create expression, type: Number [constant] (unitless)
'.Create method will not work to create an expression of type String
exp_1 = workPart.Expressions.Create("NXJ_1 = " & exp1Value.ToString)

'alternately, let the compiler convert the value to a string
'exp_1 = workPart.Expressions.Create("NXJ_1 = " & exp1Value)

'or, pass in the value directly
'exp_1 = workPart.Expressions.Create("NXJ_1 = 2.7182")

Catch ex As NXException
If ex.ErrorCode = 1050017 Then
'expression already exists
exp_1 = workPart.Expressions.FindObject("NXJ_1")
Else
'other error
lw.WriteLine("NX exception: " & ex.ErrorCode & ", " & ex.Message)

End If

End Try

The .Create() method will only create unitless expressions of type Number. However, the input to the method must be a string value; NX will interpret the input and create the expression. The input takes the form "expression_name = expression_value". The "expression_name" specified is what will show up in the NX expression editor. In the example above, "NXJ_1" will show up as the expression name in the expression editor. We have used a variable named "exp_1" to hold the new expression object that we have created; the name "exp_1" is only for our (the progrmmer's) use, it will not show up anywhere in the NX GUI. The "number" that we pass in must also be a string. If we want to assign the value of a variable to the expression, we can use the .ToString method on the variable. If you are using "Option Strict Off" at the top of your code, you can leave off the .ToString - the compiler will perform an implicit conversion in this case.

As you can see, the .Create() method is wrapped in a Try block. So, what could go wrong when creating a new expression? Well, in our case, if an expression with the name "NXJ_1" already exists in the work part, the .Create() method will throw an error. If this error occurs, we assign the existing expression to our variable instead of creating a new expression (this will be important on subsequent runs of the journal). If some other error occurs, we simply write the error information to the listing window. The following code snippets will use the same pattern: try to create the expression, if it exists - assign it to our variable, if some other error occurs - write the information to the listing window.

The Expression collection .CreateExpression() method

The .CreateExpression() method differs from the .Create() method in that it can create an expression of any type. The .CreateExpression() method takes two string parameters, the first indicates what type of expression you would like to create; the second parameter specifies the equation of the expression ["expression_name = value"]. The valid values of the expression type parameter (for NX 9) are:

  • "Number"
  • "String"
  • "Boolean"
  • "Integer"
  • "Point"
  • "Vector"
  • "List"

It seems to me that this would have been a great place to use an enumeration. This would have allowed the integrated development environment (IDE) to help us (the programmers) by listing all the available values any time we needed them. Also, the autocomplete feature would ensure we used the correct value instead of typing in a string description of the expression type. But, it is what it is...

The first .CreateExpression() example is very similar to the previous code snippet; it also creates a Number (unitless) expression.

Create a Number (unitless) expression


Dim exp_2 As Expression = Nothing
Dim exp2Value As Double = 3.1415
Try
'create expression, type: Number [constant] (unitless)
exp_2 = workPart.Expressions.CreateExpression("Number", "NXJ_2 = " & exp2Value.ToString)
Catch ex As NXException
If ex.ErrorCode = 1050017 Then
'expression already exists
exp_2 = workPart.Expressions.FindObject("NXJ_2")
Else
'other error
lw.WriteLine("NX exception: " & ex.ErrorCode & ", " & ex.Message)

End If

End Try

The expression will be named "NXJ_2" in the NX expression editor.

Create a Number Expression (unitless), specifying a formula using other expressions

The next example also creates a Number (unitless) expression, but this one uses the previously created expressions in its formula. On the first run, it will add the two previously created expressions together. On subsequent runs, if the expression "NXJ_3" already exists, it will change the formula from addition to multiplication and add a comment to the expression.

Dim exp_3 As Expression = Nothing
Try
'create expression, type: Number [constant] (unitless)
'reference the 2 previously created expressions
exp_3 = workPart.Expressions.CreateExpression("Number", "NXJ_3 = " & exp_1.Name & " + " & exp_2.Name)
Catch ex As NXException
If ex.ErrorCode = 1050017 Then
'expression already exists
exp_3 = workPart.Expressions.FindObject("NXJ_3")

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

'change the formula from addition to multiplication and add a comment to the expression
exp_3.RightHandSide = exp_1.Name & " * " & exp_2.Name & "//changed addition to multiplication"

Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.DoUpdate(markId2)
Else
'other error
lw.WriteLine("NX exception: " & ex.ErrorCode & ", " & ex.Message)

End If

End Try

The expression will be named "NXJ_3" in the NX expression editor.

Create a String Expression

The following example creates a string expression. When creating a string expression in the NX expression editor, the string value must be enclosed in double quotation marks; the same is true when creating a string expression through the API. For VB.net (and C#) programmers, this poses a bit of a problem; the syntax of those languages uses the double quotation marks to indicate a string value. So, when you create a string variable such as:

Dim str1 as string = "Hello, world!"

The value held by str1 is Hello, world! (no double quotation marks), not "Hello, world!" (the format NX wants). So how do we use the double quotation marks inside a string value? Add more of them! When the compiler interprets string values, it looks for two consecutive double quote characters. If it finds two consecutive double quote characters within a string, it treats the first as an 'escape' character and the second as a literal character to be included in the string value. The escape character (the first double quote) will not be included in the resulting string value. So, if we wanted to pass in the value "Hello, world!" (with the double quotes, as needed by the expression editor), our string variable could look like this:

Dim str2 as string = """Hello, world!"""

In the string value above, the first double quote indicates the start of a string value. The second double quote (since it is followed by another double quote) will act as an escape character, telling the compiler to treat the third double quote as a literal character to be included in the string value. The fourth double quote also acts as an escape character because it is immediately followed by the fifth double quote, which will be added as a literal character in the string value. The sixth double quote (the last non-whitespace character in the line of code) indicates the end of the string value.

As is often the case, you will get a string value from elsewhere (the return value of a function, a database query, etc) as such, you won't have full control to add the extra double quotation marks at the time of creation. We'll just use the familiar string concatenation operator to add the extra double quotes around our given string. We'll do this by adding the string: """" (that's 4 double quote characters in a row) before and after the given string. So, if variable str3 holds the given string value we want to pass into the .CreateExpression method, we could add the extra double quotes like this:

Dim str4 as string = """" & str3 & """"

Alternately, VB.net users can make use of the Chr() function to add a literal double quote character to a string. A double quote character is Chr(34); adding a double quote character to a string this way would look like this:

Dim str5 as string = Chr(34) & "Hello, world!" & Chr(34)

This method does not require the use of an escape character, as Chr(34) will be treated as a literal string value (technically a single character). In this case, we simply add one before and after the string of interest. The method you use is largely a matter of preference; however, note that the Chr(34) approach might only work with the ASCII character codeset (I've not thoroughly tested this).


Dim exp_4 As Expression = Nothing
Dim exp4Value As String = "Hello, NX programmers!"
Try
'create expression, type: String
'add double quotes around the string value
exp_4 = workPart.Expressions.CreateExpression("String", "NXJ_4 = " & """" & exp4Value & """")

'alternately (for VB.net users only), use Chr(34) [Chr(34) = "]
'exp_4 = workPart.Expressions.CreateExpression("String", "NXJ_4 = " & Chr(34) & exp4Value & Chr(34))

'another alternative: add the double quotes into the inital string value
'exp4Value = """Hello, NX programmers!"""
'exp_4 = workPart.Expressions.CreateExpression("String", "NXJ_4 = " & exp4Value)

'or, pass in a string literal
'exp_4 = workPart.Expressions.CreateExpression("String", "NXJ_4 = ""Hello, NX programmers!""")
'exp_4 = workPart.Expressions.CreateExpression("String", "NXJ_4 = " & Chr(34) & "Hello, NX programmers!" & Chr(34))

exp_4.EditComment("greetings")
Catch ex As NXException
If ex.ErrorCode = 1050017 Then
'expression already exists
exp_4 = workPart.Expressions.FindObject("NXJ_4")
Else
'other error
lw.WriteLine("NX exception: " & ex.ErrorCode & ", " & ex.Message)

End If

End Try

The expression will be named "NXJ_4" in the NX expression editor.
The example above also adds a comment to the expression. This one uses the .EditComment method rather than adding the comment directly to the equation of the expression.

Create a Boolean expression

Boolean expressions, like boolean variables can only hold one of two values: True or False. The boolean example does not hold any new surprises, it is very similar to the previous examples. On subsequent runs of the journal, if it finds the "NXJ_5" expression already exists, the journal will toggle the value of the expression and add a comment noting the date and time that the value was changed.


Dim exp_5 As Expression = Nothing
Dim val_5 As Boolean = True
Try
'create expression, type: Boolean
'set expression to value of a variable
exp_5 = workPart.Expressions.CreateExpression("Boolean", "NXJ_5 = " & val_5.ToString)
'alternately, set the value directly:
'exp_5 = workPart.Expressions.CreateExpression("Boolean", "NXJ_5 = True")
Catch ex As NXException
If ex.ErrorCode = 1050017 Then
'expression already exists
exp_5 = workPart.Expressions.FindObject("NXJ_5")

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

'toggle the value
If exp_5.Type = "Boolean" Then
exp_5.RightHandSide = (Not exp_5.BooleanValue).ToString
Dim comment As String = "value changed by journal: " & Now.ToString
exp_5.EditComment(comment)

End If

Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.DoUpdate(markId2)

Else
'other error
lw.WriteLine("NX exception: " & ex.ErrorCode & ", " & ex.Message)

End If

End Try

The expression will be named "NXJ_5" in the NX expression editor.

Create an Integer Expression

The following code will create an integer expression. Subsequent runs of the journal will add 10 to the current value of the expression.


Dim exp_6 As Expression = Nothing
Dim val_6 As Integer = 42
Try
'create expression, type: Integer
exp_6 = workPart.Expressions.CreateExpression("Integer", "NXJ_6 = " & val_6.ToString)
Catch ex As NXException
If ex.ErrorCode = 1050017 Then
'expression already exists
exp_6 = workPart.Expressions.FindObject("NXJ_6")

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

'add 10 to the current value
If exp_6.Type = "Integer" Then
Dim comment As String = "value was: " & exp_6.IntegerValue.ToString
val_6 = exp_6.IntegerValue + 10
exp_6.RightHandSide = val_6.ToString
exp_6.EditComment(comment)
End If

Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.DoUpdate(markId2)

Else
'other error
lw.WriteLine("NX exception: " & ex.ErrorCode & ", " & ex.Message)

End If

End Try

The expression will be named "NXJ_6" in the NX expression editor.

Create a Point Expression

When retrieving the value of a point expression, a Point3d object will be returned. Unfortunately, you cannot directly assign a Point3d object as the value when creating a point expression; the .ToString method of the Point3d object returns a string representing the point coordinates, but it is not in the format required by the NX expression syntax. Instead, we'll have to break down the Point3d into its individual X, Y, and Z values. On the first run, the point expression is created; on subsequent runs, the coordinates will be updated to new, random values.


Dim exp_7 As Expression = Nothing
Dim exp7Val As New Point3d(0, 0, 0)
Try
'create expression, type: Point
exp_7 = workPart.Expressions.CreateExpression("Point", "NXJ_7 = Point(" & exp7Val.X.ToString & "," & exp7Val.Y.ToString & "," & exp7Val.Z.ToString & ")")
Catch ex As NXException
If ex.ErrorCode = 1050017 Then
'expression already exists
exp_7 = workPart.Expressions.FindObject("NXJ_7")

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

'change the current X, Y, Z values
Dim r As Random = New Random
If exp_7.Type = "Point" Then
Dim thePoint As Point3d = exp_7.PointValue
Dim comment As String = "value was: " & thePoint.ToString
exp_7.RightHandSide = "Point(" & r.Next(-10, 11) & "," & r.Next(-10, 11) & "," & r.Next(-10, 11) & ")"
exp_7.EditComment(comment)
End If

Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.DoUpdate(markId2)

Else
'other error
lw.WriteLine("NX exception: " & ex.ErrorCode & ", " & ex.Message)

End If

End Try

The expression will be named "NXJ_7" in the NX expression editor.

Create a Vector Expression

A vector expression is very similar to a point expression. Retrieving the value of a vector expression will yield a Vector3d object. However, a Vector3d object cannot be used directly to assign the value of the vector expression upon creation. Again, we'll need to break the Vector3d into individual components to properly assign the value to the expression.


Dim exp_8 As Expression = Nothing
Dim exp8Val As New Vector3d(0, 0, 1)
Try
'create expression, type: Vector
exp_8 = workPart.Expressions.CreateExpression("Vector", "NXJ_8 = Vector(" & exp8Val.X.ToString & "," & _
exp8Val.Y.ToString & "," & exp8Val.Z.ToString & ")")
Catch ex As NXException
If ex.ErrorCode = 1050017 Then
'expression already exists
exp_8 = workPart.Expressions.FindObject("NXJ_8")

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

'negate the current X, Y, Z values
If exp_8.Type = "Vector" Then
Dim theVector As Vector3d = exp_8.VectorValue
Dim comment As String = "value was: " & theVector.ToString
exp_8.RightHandSide = "Vector(" & -theVector.X & "," & -theVector.Y & "," & -theVector.Z & ")"
exp_8.EditComment(comment)
End If

Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.DoUpdate(markId2)

Else
'other error
lw.WriteLine("NX exception: " & ex.ErrorCode & ", " & ex.Message)

End If

End Try

The expression will be named "NXJ_8" in the NX expression editor.

Create a List Expression

Finally, we create a list expression. The example below creates a three element list containing a number, a string, and a point. The code uses a literal string to assign the values, but variables could easily be used as in the previous examples.


Dim exp_9 As Expression = Nothing
Try
'create expression, type: List
exp_9 = workPart.Expressions.CreateExpression("List", "NXJ_9 = {1, ""two"", Point(3,4,5)}")
Catch ex As NXException
If ex.ErrorCode = 1050017 Then
'expression already exists
exp_9 = workPart.Expressions.FindObject("NXJ_9")

Else
'other error
lw.WriteLine("NX exception: " & ex.ErrorCode & ", " & ex.Message)

End If

End Try

The expression will be named "NXJ_9" in the NX expression editor.

Conclusion

We've covered the creation of the basic expression types available to us in NX 9; in a future article, we'll continue by looking at units and creating expressions with units.

Code

Here is the code listing in its entirety.

'expression_test

'NXJournaling.com
'March 27, 2015

'Example code for creating expressions of various types (number, string, boolean, integer, point, vector, list).
'These examples do not include expressions with units.

Option Strict Off
Imports System
Imports NXOpen

Module NXJ_Exp_2

Sub Main()

Dim theSession As Session = Session.GetSession()
If IsNothing(theSession.Parts.BaseWork) Then
'active part required
Return
End If

Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()

Const undoMarkName As String = "NXJ create expressions"
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)

Dim exp_1 As Expression = Nothing
Dim exp1Value As Double = 2.7182
Try
'create expression, type: Number [constant] (unitless)
'.Create method will not work to create an expression of type String
exp_1 = workPart.Expressions.Create("NXJ_1 = " & exp1Value.ToString)

'alternately, let the compiler convert the value to a string
'exp_1 = workPart.Expressions.Create("NXJ_1 = " & exp1Value)

'or, pass in the value directly
'exp_1 = workPart.Expressions.Create("NXJ_1 = 2.7182")

Catch ex As NXException
If ex.ErrorCode = 1050017 Then
'expression already exists
exp_1 = workPart.Expressions.FindObject("NXJ_1")
Else
'other error
lw.WriteLine("NX exception: " & ex.ErrorCode & ", " & ex.Message)

End If

End Try

Dim exp_2 As Expression = Nothing
Dim exp2Value As Double = 3.1415
Try
'create expression, type: Number [constant] (unitless)
exp_2 = workPart.Expressions.CreateExpression("Number", "NXJ_2 = " & exp2Value.ToString)
Catch ex As NXException
If ex.ErrorCode = 1050017 Then
'expression already exists
exp_2 = workPart.Expressions.FindObject("NXJ_2")
Else
'other error
lw.WriteLine("NX exception: " & ex.ErrorCode & ", " & ex.Message)

End If

End Try

Dim exp_3 As Expression = Nothing
Try
'create expression, type: Number [constant] (unitless)
'reference the 2 previously created expressions
exp_3 = workPart.Expressions.CreateExpression("Number", "NXJ_3 = " & exp_1.Name & " + " & exp_2.Name)
Catch ex As NXException
If ex.ErrorCode = 1050017 Then
'expression already exists
exp_3 = workPart.Expressions.FindObject("NXJ_3")

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

'change the formula from addition to multiplication and add a comment to the expression
exp_3.RightHandSide = exp_1.Name & " * " & exp_2.Name & "//changed addition to multiplication"

Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.DoUpdate(markId2)
Else
'other error
lw.WriteLine("NX exception: " & ex.ErrorCode & ", " & ex.Message)

End If

End Try

Dim exp_4 As Expression = Nothing
Dim exp4Value As String = "Hello, NX programmers!"
Try
'create expression, type: String
'add double quotes around the string value
exp_4 = workPart.Expressions.CreateExpression("String", "NXJ_4 = " & """" & exp4Value & """")

'alternately (for VB.net users only), use Chr(34) [Chr(34) = "]
'exp_4 = workPart.Expressions.CreateExpression("String", "NXJ_4 = " & Chr(34) & exp4Value & Chr(34))

'another alternative: add the double quotes into the inital string value
'exp4Value = """Hello, NX programmers!"""
'exp_4 = workPart.Expressions.CreateExpression("String", "NXJ_4 = " & exp4Value)

'or, pass in a string literal
'exp_4 = workPart.Expressions.CreateExpression("String", "NXJ_4 = ""Hello, NX programmers!""")

'exp_4 = workPart.Expressions.CreateExpression("String", "NXJ_4 = " & Chr(34) & "Hello, NX programmers!" & Chr(34))

exp_4.EditComment("greetings")
Catch ex As NXException
If ex.ErrorCode = 1050017 Then
'expression already exists
exp_4 = workPart.Expressions.FindObject("NXJ_4")
Else
'other error
lw.WriteLine("NX exception: " & ex.ErrorCode & ", " & ex.Message)

End If

End Try

Dim exp_5 As Expression = Nothing
Dim val_5 As Boolean = True
Try
'create expression, type: Boolean
'set expression to value of a variable
exp_5 = workPart.Expressions.CreateExpression("Boolean", "NXJ_5 = " & val_5.ToString)
'alternately, set the value directly:
'exp_5 = workPart.Expressions.CreateExpression("Boolean", "NXJ_5 = True")
Catch ex As NXException
If ex.ErrorCode = 1050017 Then
'expression already exists
exp_5 = workPart.Expressions.FindObject("NXJ_5")

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

'toggle the value
If exp_5.Type = "Boolean" Then
exp_5.RightHandSide = (Not exp_5.BooleanValue).ToString
Dim comment As String = "value changed by journal: " & Now.ToString
exp_5.EditComment(comment)

End If

Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.DoUpdate(markId2)

Else
'other error
lw.WriteLine("NX exception: " & ex.ErrorCode & ", " & ex.Message)

End If

End Try

Dim exp_6 As Expression = Nothing
Dim val_6 As Integer = 42
Try
'create expression, type: Integer
exp_6 = workPart.Expressions.CreateExpression("Integer", "NXJ_6 = " & val_6.ToString)
Catch ex As NXException
If ex.ErrorCode = 1050017 Then
'expression already exists
exp_6 = workPart.Expressions.FindObject("NXJ_6")

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

'add 10 to the current value
If exp_6.Type = "Integer" Then
Dim comment As String = "value was: " & exp_6.IntegerValue.ToString
val_6 = exp_6.IntegerValue + 10
exp_6.RightHandSide = val_6.ToString
exp_6.EditComment(comment)
End If

Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.DoUpdate(markId2)

Else
'other error
lw.WriteLine("NX exception: " & ex.ErrorCode & ", " & ex.Message)

End If

End Try

Dim exp_7 As Expression = Nothing
Dim exp7Val As New Point3d(0, 0, 0)
Try
'create expression, type: Point
exp_7 = workPart.Expressions.CreateExpression("Point", "NXJ_7 = Point(" & exp7Val.X.ToString & "," & exp7Val.Y.ToString & "," & exp7Val.Z.ToString & ")")
Catch ex As NXException
If ex.ErrorCode = 1050017 Then
'expression already exists
exp_7 = workPart.Expressions.FindObject("NXJ_7")

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

'change the current X, Y, Z values
Dim r As Random = New Random
If exp_7.Type = "Point" Then
Dim thePoint As Point3d = exp_7.PointValue
Dim comment As String = "value was: " & thePoint.ToString
exp_7.RightHandSide = "Point(" & r.Next(-10, 11) & "," & r.Next(-10, 11) & "," & r.Next(-10, 11) & ")"
exp_7.EditComment(comment)
End If

Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.DoUpdate(markId2)

Else
'other error
lw.WriteLine("NX exception: " & ex.ErrorCode & ", " & ex.Message)

End If

End Try

Dim exp_8 As Expression = Nothing
Dim exp8Val As New Vector3d(0, 0, 1)
Try
'create expression, type: Vector
exp_8 = workPart.Expressions.CreateExpression("Vector", "NXJ_8 = Vector(" & exp8Val.X.ToString & "," & _
exp8Val.Y.ToString & "," & exp8Val.Z.ToString & ")")
Catch ex As NXException
If ex.ErrorCode = 1050017 Then
'expression already exists
exp_8 = workPart.Expressions.FindObject("NXJ_8")

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

'negate the current X, Y, Z values
If exp_8.Type = "Vector" Then
Dim theVector As Vector3d = exp_8.VectorValue
Dim comment As String = "value was: " & theVector.ToString
exp_8.RightHandSide = "Vector(" & -theVector.X & "," & -theVector.Y & "," & -theVector.Z & ")"
exp_8.EditComment(comment)
End If

Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.DoUpdate(markId2)

Else
'other error
lw.WriteLine("NX exception: " & ex.ErrorCode & ", " & ex.Message)

End If

End Try

Dim exp_9 As Expression = Nothing
Try
'create expression, type: List
exp_9 = workPart.Expressions.CreateExpression("List", "NXJ_9 = {1, ""two"", Point(3,4,5)}")
Catch ex As NXException
If ex.ErrorCode = 1050017 Then
'expression already exists
exp_9 = workPart.Expressions.FindObject("NXJ_9")

Else
'other error
lw.WriteLine("NX exception: " & ex.ErrorCode & ", " & ex.Message)

End If

End Try

lw.Close()

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

Comments

Hello,

I try, in NX11.0 to create a Group in Expressions spredsheet, and i try to create 4 expressions in this group, you can help me ?

Group: Thread
Expressions in group Thread;
THREAD_PITCH = 1
SCREW_DIA = 1
TAP_DRILL_SIZE = 1
DETH = 1

Regards!

Pat

Have you tried recording a journal to see what code will be necessary?

Hello,
Yes, I tried to record a journal. With the journal I can create a group. But the journal does not record the displacement of an expression in a specific group.

I would like to know, VB, how to create a group and then create expressions associated with this group.

Group: Thread
Expressions in group Thread;
THREAD_PITCH = 1
SCREW_DIA = 1
TAP_DRILL_SIZE = 1
DETH = 1

Also, I would like to know how to move expressions in a group if the group is already exixing and if the expressions are also already existing, I want to make a kind of "move".

Regards !

Patrick

Pat

The code below shows how to work with expression groups. When run the first time, it will: report the existing expression groups, create a new expression (which NX will place in the currently active group), create a new expression group, and move the new expression to the newly created group.

The code is for NX 11 or higher.

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF

Module Module1

Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession()

Dim theUI As UI = UI.GetUI()
Dim lw As ListingWindow = theSession.ListingWindow

Sub Main()

Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "NXJ")

Dim workPart As Part = theSession.Parts.Work

lw.Open()

'report default expression group
lw.WriteLine("default expression group: " & workPart.ExpressionGroups.GetDefault.Name)
lw.WriteLine("")

'report active expression group
lw.WriteLine("active expression group: " & workPart.ExpressionGroups.Active.Name)
lw.WriteLine("")

'report expression groups in work part
lw.WriteLine("expression groups in current work part:")
For Each tempExpGroup As ExpressionGroup In workPart.ExpressionGroups
If tempExpGroup.Equals(workPart.ExpressionGroups.Active) Then
lw.WriteLine(" " & tempExpGroup.Name & " [Active]")
Else
lw.WriteLine(" " & tempExpGroup.Name)
End If
Next

'create/find an expression
'if new expression is created, it will belong to the currently active group
Dim exp_1 As Expression = Nothing
Dim exp1Value As Double = 42
Try
'create expression, type: Number [constant] (unitless)
'.Create method will not work to create an expression of type String
exp_1 = workPart.Expressions.Create("NXJ_1 = " & exp1Value.ToString)

Catch ex As NXException
If ex.ErrorCode = 1050017 Then
'expression already exists
exp_1 = workPart.Expressions.FindObject("NXJ_1")
Else
'other error
lw.WriteLine("NX exception: " & ex.ErrorCode & ", " & ex.Message)

End If

End Try

'report group that the new expression belongs to
lw.WriteLine("")
lw.WriteLine(exp_1.Name & " belongs to group: " & workPart.ExpressionGroups.GetGroupOfExpression(exp_1).Name)
lw.WriteLine("")

'create new expression group
Dim status1 As Boolean = Nothing
Dim newExpGroupName As String = "test1"
status1 = workPart.ExpressionGroups.CheckName(newExpGroupName)

Dim expressionGroup1 As NXOpen.ExpressionGroup = Nothing
If status1 Then
Try
expressionGroup1 = workPart.ExpressionGroups.Create(newExpGroupName)
Catch ex As NXException
If ex.ErrorCode = 1050063 Then
'group already exists
expressionGroup1 = workPart.ExpressionGroups.FindObject(newExpGroupName)
Else
'other error
lw.WriteLine("error: " & ex.ErrorCode.ToString & " journal exiting")
Return
End If
End Try
Else
lw.WriteLine("error: illegal name for expression group; new group NOT created")
Return
End If

'move expression to new group
lw.WriteLine("moving expression...")
expressionGroup1.SetExpressions({exp_1})
lw.WriteLine(exp_1.Name & " belongs to group: " & workPart.ExpressionGroups.GetGroupOfExpression(exp_1).Name)

lw.Close()

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

Tank you ! That work great !

Ragards!

Pat

I have create a new expression group added expression but so far I have not been able to make the Default Group the active expression group.

Lynn Slattery

The following line should make the default group the active group.

workPart.ExpressionGroups.Active = workPart.ExpressionGroups.GetDefault