expression questions

Good day everybody.
I am working on a journal that I'd going to create a parameter which has to be equal to the tap.drill. Diameter of high lighted hole.
I had some success but it only displays number without digits after coma.
I am using NX 7.5
Please help)

I am stuck. Totally.
I can not show you the code because it is on other PC But here is a general idea.
I need to have a parameter that will be equal to tap drill diameter for the hole.
So i want to high light the hole, run my journal and then have new created parameter in the expression list.
If the hole Tap Drill Diameter = 12.1 my code give me only 12 and i can not make it to display 12,1.
No idea what should i do.
What i done so far.
Please help.

If you have a hole feature that was created as a "threaded hole", the feature already created an expression for the tap drill diameter. Do you need to make a new expression, or can you use the existing one?

I need to create a new one which is going to be equal to the existing one.

If the hole feature changes, do you want the value of your new expression to update to the new tap drill diameter, or should it keep the original tap drill diameter value?

Update, please.

In that case, I'm confused as to why you cannot just use the existing expression that is created by the hole feature? Why create a new one to mirror an existing expression?

The hole purpose of this code is to use this parameters in the drawing.
I need it create a new one because i will assign a custom name to it, and them when i am in the drawing it will be easy for me to find it in the list of expressions and use it.
That is why.
Maybe this is not a best idea, but i would like to finish it)

The following journal will look for a "tapped" hole feature in the work part; if one is found it will create a new expression to reference the existing tap drill diameter expression. Alternately, it shows how you could rename the existing expression.

If you are using NX in a language other than english, we might have to tweak the code to get it working. It looks for the word "Tap" in the hole feature's expressions to find the tap drill expression. If these descriptions are not in english, use the equivalent word in the current language setting.

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()

Const undoMarkName As String = "tap diameter expression"
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)

'###################################################################
'# change this constant to control the name of your new expression
Const tapDrillExpName As String = "tapDrill"
'###################################################################

Dim newExp As Expression = Nothing
Dim found As Boolean = False

For Each tempFeature As Features.Feature In workPart.Features
If TypeOf (tempFeature) Is Features.HolePackage Then
'is it a threaded hole?
Dim theHoleBuilder As Features.HolePackageBuilder = workPart.Features.CreateHolePackageBuilder(tempFeature)
If theHoleBuilder.Type = Features.HolePackageBuilder.Types.ThreadedHole Then

Dim holeExpressions() As Expression = tempFeature.GetExpressions
For Each tempExp As Expression In holeExpressions
'lw.WriteLine("name: " & tempExp.Name)
'lw.WriteLine("description: " & tempExp.Description)
If tempExp.Description.Contains("Tap") Then
'lw.WriteLine("tap diameter expression found")
Try
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'@ create new expression referencing existing expression
newExp = workPart.Expressions.CreateWithUnits(tapDrillExpName & "=" & tempExp.Name, tempExp.Units)
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'% alternately, rename the existing expression
'workPart.Expressions.SystemRename(tempExp, tapDrillExpName)
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

found = True

Catch ex As NXException
If ex.ErrorCode = 1050017 Then
'expression already exists
'try new name
lw.WriteLine("expression already exists")
Else
lw.WriteLine("error " & ex.ErrorCode & ": " & ex.Message)
End If
End Try
Exit For
End If
Next

If found Then
'do not check any more features
Exit For
End If

End If

theHoleBuilder.Destroy()

End If
Next

lw.Close()

End Sub

End Module

"The hole purpose of this code..."
I see what you did there :)

Thank you so much. I will try that!
Thank you.

"The hole purpose of this code..."
I see what you did there :)

))))))))))

Thank you again it work!
I did some modifications to it, and it is fine)
But when I run it it creates a lot of new parameters in the expression tab. Is there any way to have only my new parameter there, every time I use the code it creates more and more of these parameters. And it is impossible to delete them. Just go to expressions - user defined expressions and see for your self.

It seems that I wasn't calling the HolePackageBuilder's .Destroy method at the right time. The updated code below will eliminate the vast majority of the extraneous expressions. I'm still getting a few extra expressions created (4 in my test file); they appear to be "hidden" expressions that are being made visible for some reason. I've yet to track down why they are appearing.

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()

Const undoMarkName As String = "tap diameter expression"
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)

'###################################################################
'# change this constant to control the name of your new expression
Const tapDrillExpName As String = "tapDrill"
'###################################################################

Dim newExp As Expression = Nothing
Dim found As Boolean = False

For Each tempFeature As Features.Feature In workPart.Features
If TypeOf (tempFeature) Is Features.HolePackage Then
'is it a threaded hole?
Dim thdHole As Boolean = False
Dim theHoleBuilder As Features.HolePackageBuilder = workPart.Features.CreateHolePackageBuilder(tempFeature)
If theHoleBuilder.Type = Features.HolePackageBuilder.Types.ThreadedHole Then
thdHole = True
End If
theHoleBuilder.Destroy()

If Not thdHole Then
'skip this one
Continue For
End If

Dim holeExpressions() As Expression = tempFeature.GetExpressions
For Each tempExp As Expression In holeExpressions
'lw.WriteLine("name: " & tempExp.Name)
'lw.WriteLine("description: " & tempExp.Description)
If tempExp.Description.Contains("Tap") Then
'lw.WriteLine("tap diameter expression found")
Try
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'@ create new expression referencing existing expression
newExp = workPart.Expressions.CreateWithUnits(tapDrillExpName & "=" & tempExp.Name, tempExp.Units)
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'% alternately, rename the existing expression
'workPart.Expressions.SystemRename(tempExp, tapDrillExpName)
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

found = True

Catch ex As NXException
If ex.ErrorCode = 1050017 Then
'expression already exists
'try new name
lw.WriteLine("expression already exists")
Else
lw.WriteLine("error " & ex.ErrorCode & ": " & ex.Message)
End If
End Try
Exit For
End If
Next

If found Then
'do not check any more features
Exit For
End If

End If
Next

lw.Close()

End Sub

End Module

Thank you!
You are doing so much work.
Thank you for the code, I will try to use it on Monday.
There are some more questions.
How does NX names its parameters, that are created by NX?

For example, I need to create an expression, I need it to have custom name, that is easy, but what if this expression is a sums of values presented in other parameters, how can I give to those ones I am not interested in Default names, no NX just gives them first available number?
Hope my explanations are not to confusing.
And hove to hide the expression you do no need to see?
Is there a way to hide it?

You can create system expressions; NX will automatically generate a name for these expressions.

workPart.Expressions.CreateSystemExpression("12345")

workPart.Expressions.CreateSystemExpressionWithUnits("54", workPart.UnitCollection.FindObject("MilliMeter"))

Alternately, you could attempt to create an expression and increment the expression name until it works. Something like the following:

Const expBaseName As String = "exp"
Dim i As Integer = 1
Dim success As Boolean = False
Do While Not success
Try
workPart.Expressions.Create(expBaseName & i & "=789")
success = True
Catch ex As NXException
If ex.ErrorCode = 1050017 Then
i += 1
Else
'unexpected error
lw.WriteLine("error " & ex.ErrorCode & ": " & ex.Message)
End If
End Try
Loop

Thank you this is really helpful.
Now just have to figure out how to hide expressions.

So there is one more question to all of you.
I have an expressions A
And expression B
And expression C = A+B
Expression A name = ("A"+name1)
Expression B name = ("B"+name1)
Name 1 is a something a have form the input box.
Then I need to do something like that
Workpart,expression.Createexpression ("String", Dimensions = sting value (A)+""x""+string value B)
And this is not working.
code is not accurate, but i have the proper one in place.
Please advise on the meter.

It looks like the new expression, C, is going to be a string expression. What type of expression is A and B? (string, number, vector, etc)?

If A and B are both string expressions, the following should work:
workpart.Expressions.CreateExpression("String", "Cname = A + B")

If A and/or B is not a string expression, you may need to get the value and convert it to a string before creating the new expression.

This is a String expression, And the values if expression A and B are numbers.
Before last changes, expression A and B were named A and B and it all worked fine, Now they are different.
expression A name is ("A"+name1)
In expression C i would just use soemthing like that - Stringvalue(A)+Stringvalues(B)
but now the names are different and if i use stringValues("A"+name1)+.....
It is not working.

Is your code creating the expression named "A" + name1? or are you iterating through the existing expressions looking for expression "A" + name1?

Below is some code that should do what you need. If I have misunderstood the issue, please let me know.

Option Strict Off
Imports System
Imports NXOpen

Module Module1

Sub Main()

Dim theSession As Session = Session.GetSession()
Dim theUISession As UI = UI.GetUI

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()

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

Dim expA As Expression
Dim expB As Expression
Dim expC As Expression

Dim suffix As String = ""

'input box: prompt, title, and initial text
suffix = NXOpenUI.NXInputBox.GetInputString("Enter expression name suffix", "Expression", "1")

Try
expA = workPart.Expressions.CreateExpression("Number", "A" & suffix & " = 12")
expB = workPart.Expressions.CreateExpression("Number", "B" & suffix & " = 34")
expC = workPart.Expressions.CreateExpression("String", "expC = format(""%.2f"", " & expA.Name & ") + "" x "" + format(""%.2f"", " & expB.Name & ")")

Catch ex As NXException
'theSession.UndoToMark(markId1, undoMarkName)
MsgBox(ex.Message)

Finally

End Try

lw.Close()

End Sub

End Module

Hi.
Sorry I dint reply, yesterday, very busy day.
Thank you very much for the code. I will try it and then let you know if it work for me or not.
But anyway thank you.

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpenUI

Module NXJournal
Sub Main

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work

Dim displayPart As Part = theSession.Parts.Display

Dim name1 As String = ""
name1 = NXInputBox.GetInputString("Please enter a name", "Name for your parameter")

'-------------------------------------------

Dim theUI As UI = UI.GetUI()

Dim holePackage2 As Features.HolePackage = CType(theUI.SelectionManager.GetSelectedObject(0), Features.HolePackage)

Dim holePackageBuilder2 As Features.HolePackageBuilder
holePackageBuilder2 = workPart.Features.CreateHolePackageBuilder(holePackage2)

'---------------
Dim unit1 As Unit = CType(workPart.UnitCollection.FindObject("MilliMeter"), Unit)
Dim DnewExp As Expression
DnewExp = workPart.Expressions.CreateWithUnits("D" & name1 & "=" & holePackageBuilder2.ThreadedHoleDepth.Value.ToString, Unit1)
'DnewExp = workPart.Expressions.CreateExpression("Number", "D" & name1 & "=" & & holePackageBuilder2.ThreadedHoleDepth.Value.ToString)
holePackageBuilder2.Destroy()
'------------------

Dim holeExpressions() As Expression = holePackage2.GetExpressions
'------------------------------------------------------------------------------------

For Each tempExp As Expression In holeExpressions

If tempExp.Description.Contains("Major Diameter") Then

Dim expA As expression
expA = workPart.Expressions.CreateExpression("Number", "A" & name1 & " =" & tempExp.Name)

End If

If tempExp.Description.Contains("Thread Depth") Then

Dim BExp As expression
BExp = workPart.Expressions.CreateExpression("Number", "B" & name1 & "=" & tempExp.Name)

End if

If tempExp.Description.Contains("Tap Drill Diameter") Then

Dim CnewExp As expression
CnewExp = workPart.Expressions.CreateExpression("Number", "C" & name1 & "=" & tempExp.Name)

End if

Next

'-----------------------Final Expression----------------------
Dim finalExp As Expression

finalExp = workPart.Expressions.CreateExpression("String", "finalExp = format(""%.2f"", " & ExpA.Name & ")+""x""+format(""%.2f"", " & ExpC.Name & ") ")

End Sub
End Module

So here is what i got so far.
I know it is not the best, but i am proud of it.
however the final expression is not working.
I tried everything i could think about, but it doesn't work.
Any ideas why?
And one more thing. The format is currently set to two decimals after the dot, but i need it to display exactly what is the value of the expression that is referenced.
If you got any ideas. Please introduce them.

The final expression does not work because you are trying to use ExpC.Name; you do not have a valid expression variable named ExpC.

The ".2" in format(""%.2f"" controls the number of decimal places. Change the "2" to the number of decimal places you need.

Dim finalExp As Expression

finalExp = workPart.Expressions.CreateExpression("String", "finalExp = format(""%.2f"", " & ExpA.Name & ")+""x"" ")
I changed the expression to that and it is still not working, There is a problem with variable expA. Can you please run the code and see what is the problem.
I use NX 7.5
Regarding the format numbers, it is great to have an ability to influence that but I would like it to display exact values of the expression which it is linked to.
For example if it 40 make it 40 , not 40,00 or if it 16.7 i need it to be 16.7
This last expression is the only issue that is left in this case, can you please help me solve it.

The variables "expA", "BExp", and "CnewExp" have all been declared inside If blocks within a For loop. The scope (aka: lifetime) of the variable is determined by where it is created. In your case, when the If block ends, your variables go out of scope and can no longer be referenced.

Move the variable declarations outside of any conditional blocks or loops (but keep them within Sub Main). This will allow the variables to be used anywhere in Sub Main.

But this is going to ruin the hole structure, and I will have to start over.
And I think the problem is For loop function.
I tried to put if then condition in your code and it all worked fine.
Have to create a way to reference all this parameters without looping.

I think you misunderstand my suggestion, it won't ruin anything. In the code below, note that "ExpA", "BExp", and "ExpC" are all declared before the For loop. You can still assign values to the variables in the For loop, you just don't want to declare them there.

Dim holeExpressions() As Expression = holePackage2.GetExpressions
'------------------------------------------------------------------------------------

Dim ExpA As expression
Dim BExp As expression
Dim ExpC As expression

For Each tempExp As Expression In holeExpressions

If tempExp.Description.Contains("Major Diameter") Then

expA = workPart.Expressions.CreateExpression("Number", "A" & name1 & " =" & tempExp.Name)

End If

If tempExp.Description.Contains("Thread Depth") Then

BExp = workPart.Expressions.CreateExpression("Number", "B" & name1 & "=" & tempExp.Name)

End if

If tempExp.Description.Contains("Tap Drill Diameter") Then

ExpC = workPart.Expressions.CreateExpression("Number", "C" & name1 & "=" & tempExp.Name)

End if

Next

'-----------------------Final Expression----------------------
Dim finalExp As Expression

finalExp = workPart.Expressions.CreateExpression("String", "finalExp = format(""%.2f"", " & ExpA.Name & ")+""x""+format(""%.2f"", " & ExpC.Name & ") ")

End Sub
End Module

Oh, and I strongly suggest using an Integrated Development Environment (IDE) such as Microsoft VB.net express (free); it would have warned you about the scope issue even before you tried running the code.

Thank you.
Have you considered writing a book about journalling?

Hello.
Thank you for helping me again.
I got a new question now.
There is a really nice parameter called : Tread size it is description of the hole the Major diameter and then pitch.
I tried to reference it by this:

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpenUI

Module NXJournal
Sub Main

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work

Dim displayPart As Part = theSession.Parts.Display

Dim DExp As Expression

Dim name1 As String = ""
name1 = NXInputBox.GetInputString("Please enter a name", "Name for your parameter")

'-------------------------------------------

Dim theUI As UI = UI.GetUI()

Dim holePackage2 As Features.HolePackage = CType(theUI.SelectionManager.GetSelectedObject(0), Features.HolePackage)

Dim holePackageBuilder2 As Features.HolePackageBuilder
holePackageBuilder2 = workPart.Features.CreateHolePackageBuilder(holePackage2)

'---------------
Dim unit1 As Unit = CType(workPart.UnitCollection.FindObject("MilliMeter"), Unit)

DExp = workPart.Expressions.CreateExpression("D" & name1 & "=" & holePackageBuilder2.TreadSize)

holePackageBuilder2.Destroy()

End Sub
End Module

And it is not working.
I got a syntax error in Dexp but I do not know where.
Any suggestions?

And there is one more question regarding selection
Dim theUI As UI = UI.GetUI()

Dim holePackage2 As Features.HolePackage = CType(theUI.SelectionManager.GetSelectedObject(0), Features.HolePackage)

This is the code I am using to work with selected my mouse feature, but I would like it to give a massage box when nothing is selected
So you hit play and if nothing is selected you get a nice MSGbox which says "Please select ......"

I tried to do it like that
If CType(theUI.SelectionManager.GetSelectedObject(0), Features.HolePackage) is nothing Then
Msgbox("Please....")
End If

And it is not working I mean the Msg box pops up but there is no checking...
Gentlemen I need your advise.

1) Might be a simple misspelling. Are you sure it is "TreadSize" and not "ThreadSize"?

2) Check how many objects are selected in the selection manager before proceeding with your code, see example below.

Option Strict Off
Imports System
Imports NXOpen

Module Module30

Dim theSession As Session = Session.GetSession()
Dim theUi As UI = UI.GetUI
Dim workPart As Part = theSession.Parts.Work

Sub Main()

If theUi.SelectionManager.GetNumSelectedObjects() = 0 Then
'no object selected
MsgBox("no object selected")
Return
End If

'rest of code to process selected object(s)
MsgBox(theUi.SelectionManager.GetNumSelectedObjects.ToString & " selected object(s)")

End Sub

End Module

Thank you for this helpful tip. I will try it tomorrow.

I am very sorry for my spelling
The proper way is ThreadSize and it is still not working.
DExp = workPart.Expressions.CreateExpression("D" & name1 & "=" & holePackageBuilder2.ThreadSize)
If you try to run a code if will give you an error
Please forgive me spelling.

1) Make sure you are specifying the type of expression in the .CreateExpression method

2) NX expects the text of the string expression to be wrapped in double quotes. To do this in VB, we need to do the following:

DExp = workPart.Expressions.CreateExpression("String", "D" & name1 & "=" & """" & holePackageBuilder2.ThreadSize & """")

I know it looks goofy with 4 double quote marks in a row, but here is how it breaks down:
The first double quote (dq) tells the VB compiler that this is a string value
The second dq acts as an escape character, it tells the compiler to treat the next dq as a literal character and not a delimiter of a string value
The third dq is the literal dq that we are adding to our string value
The fourth dq ends the string

So, if we want the value "testing" (with the quotes) to show up in a message box, we would code that as:

MsgBox("""testing""")

The first and last dq's contain the entire string value.
The 2nd and 4th dq's are the escape characters, telling the compiler to treat the following (3rd and 5th) dq's as literal characters.

Finally, please be aware that the value of the expression you are creating will not be associative to the thread feature. Previously (my first posted code example), we found the tap drill diameter with:

If tempExp.Description.Contains("Tap") Then

You might want to do similar with the "Thread Size" and make your new expression reference the thread feature expression.

If tempExp.Description.Contains("Thread Size") Then

Thank you so much.
I will try it tomorrow.
Hopefully it all works fine)
Thank you again
Please write a book
I will buy it.

Hello again!)
Got more questions.
1) how to reference thread hole depth?
If tempExp.Description.Contains("Threaded hole depth") Then
This way is not going to working. If you try just depth because the name of the expression created by NX is just depth it is not going to work to.
I think this is cause I use work depth to define ("Thread Depth") and I think that when you have a word depth in two names of expression NX going to use the first one it gets.
So Is there a way to create expression which will reference threaded hole depth?
Now I use this
DnewExp = workPart.Expressions.CreateExpression("Number", "D" & name1 & "=" & & holePackageBuilder2.ThreadedHoleDepth.Value.ToString)
And as you know it is not associated with the expression.

2) If tempExp.Description.Contains("Thread Size") Then is not going to work , I tried and it just doesn't create an expression, No errors but no expression)
I think that is because the expression names thread size does not belong to the holePackageBuilder. when you search for it in NX expressions tap you will find it in "All" and not in "feature parameters" (or something)
So i have no idea what is going on

3) This is the simplest one.
I should know how to do it by now but.....)

So I tryed this code
If expA.Value = 035 Then

DExp = workPart.Expressions.CreateExpression.........
end If

But expression D is not created. No error but no expression.
I know that Dexp is 100% working because without this condition it work fine.

Please help me with this.

It is odd that you can't get those expressions from the feature in NX 7.5. They do show up correctly in NX 9, so somewhere along the line it got fixed. Anyway, we can do some brute force searching to find the correct expressions. The code below searches through all the expressions for ones that have the feature name in the .Description property. It collects all the matches into a list, then searches the list for the "Thread Size" and "Thread Depth" expressions.

Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpenUI

Module Module2
Sub Main()

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

lw.Open()

Dim thrdDepthExp As Expression = Nothing
Dim thrdSizeExp As Expression = Nothing

For Each tempFeat As Features.Feature In workPart.Features
If TypeOf (tempFeat) Is Features.HolePackage Then

lw.WriteLine(tempFeat.GetFeatureName)

Dim holeExps As New List(Of Expression)

For Each tempExp As Expression In workPart.Expressions

If tempExp.Description.Length < tempFeat.GetFeatureName.Length Then
Continue For
End If

If tempExp.Description.Substring(1, tempFeat.GetFeatureName.Length) = tempFeat.GetFeatureName Then
holeExps.Add(tempExp)
End If

Next

'search feature expressions for "Thread Size" and "Thread Depth"
For Each tempExp As Expression In holeExps
If tempExp.Description.Contains("Thread Size") Then
thrdSizeExp = tempExp
End If

If tempExp.Description.Contains("Thread Depth") Then
thrdDepthExp = tempExp
End If
Next

lw.WriteLine("Thread Depth: " & thrdDepthExp.Value.ToString)
lw.WriteLine("Thread Size: " & thrdSizeExp.StringValue)

lw.WriteLine("")

End If
Next

lw.Close()

End Sub
End Module

Thank you.
Will try to use this in my code.
Can you please explain how to set up this condition
If Aexp.Value = 1 then
......
End If

What is wrong with this?

Do you get an error message, or it just doesn't work as expected?
What type of expression is Aexp? (string, number, etc)

Expression a is a major diameter, and I need to have something like that:
If expa.value =35 then
Create new expression.....

I created a new file and tested an expression value similar to what you have shown. It worked in my test file.

Try writing the value of the expression to the listing window to see if the reported value matches what you expect.

Do the units of the expression match the base units of the part file? The .Value property returns the value in the base part units. If you are working in an inch file and create a metric hole (major diameter = 35mm), the .Value property would return: 1.3779... (representing the 35mm value converted to inches). If you are working in a mixed file, you can use the expression's .GetValueUsingUnits method to report the expression value in the units it was defined in.

Got one more question, what if I have the parameter, which value is 13-65 ABC5 and I need to create expression with modifyied value of it, like 13-56, is there any way to reference original value and then delete everything but first 4 characters And create a new expression with it?

Yes, if we are talking about string expressions. You can use the subString expression function to extract a part of a given string. Let's say your first string expression is named "greeting" and it has a value of "hello World"; you can create a new expression with a formula of "subString(greeting, 1, 5)" and it will return "hello".

Could you please provide a sample of the cod? Can't figure how to put it together, getting a data type error.
Exp2=workpart.expression.createexpression("string ","subdtring(exp1,1,5)")

In my test part I created a string expression named "greetings" with the value "Hello World". The code below looks for the "greetings" expression and if found makes a new expression named "nxj" that references the first five characters of the "greetings" expression.

Option Strict Off
Imports System
Imports NXOpen

Module Module4
Sub Main()

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

lw.Open()

Try
Dim myExp As Expression = workPart.Expressions.FindObject("greetings")

Dim newExp As Expression = workPart.Expressions.CreateExpression("String", "nxj = subString(greetings, 1, 5)")
Catch ex As NXException
lw.WriteLine(ex.ErrorCode & ": " & ex.Message)
End Try

lw.Close()

End Sub
End Module

Substring *

If tempExp.Description.Contains("Thread Size") Then
       Aexp = workPart.Expressions.CreateExpression("String", "A" & name1 & " =" & tempExp.Name)
BExp = workPart.Expressions.CreateExpression("String", "B" & name1 & "= subString(Aexp., 1,4 )")

End If

How to define the name in this case?

BExp = workPart.Expressions.CreateExpression("String", "B" & name1 & "= subString(" & Aexp.name & ", 1,4 )")

Hi.
Thank you again for all the helpful tips.
Thank great to have someone who knows so much.
I finished the code it works great.
The purpose of it was to help display thread dimension on the drawings and maybe i dint go for most intelligent way but it works.
If you have a better idea for it I am listening.

Glad to hear that you got it working!

HI,

Is the code working for the threaded holes created using Symbolic thread option?