Link existing attribute to MassPrpMass

Hello,

I would like to create a journal which links the mass prop mass attribute to another on.
I recorded the below shown journal but the line 124 linkes the transfer attribute with the p40 expression.
is there a way to check which expression is created for the transfer and use this instead?

Thanks

Klaus

' NX 9.0.3.4
' Journal created by schweigk on Fri Feb 05 09:28:57 2016 W. Europe Standard Time
'
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 displayPart As Part = theSession.Parts.Display

' ----------------------------------------------
' Menu: Tools->Expressions...
' ----------------------------------------------
theSession.Preferences.Modeling.UpdatePending = False

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

Dim markId2 As Session.UndoMarkId
markId2 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Start")

Dim objects1(0) As NXObject
objects1(0) = workPart
Dim attributePropertiesBuilder1 As AttributePropertiesBuilder
attributePropertiesBuilder1 = theSession.AttributeManager.CreateAttributePropertiesBuilder(workPart, objects1, AttributePropertiesBuilder.OperationType.None)

attributePropertiesBuilder1.IsArray = False

attributePropertiesBuilder1.IsArray = False

attributePropertiesBuilder1.IsArray = False

attributePropertiesBuilder1.DataType = AttributePropertiesBaseBuilder.DataTypeOptions.String

attributePropertiesBuilder1.Units = "MilliMeter"

attributePropertiesBuilder1.DataType = AttributePropertiesBaseBuilder.DataTypeOptions.Number

Dim objects2(0) As NXObject
objects2(0) = workPart
attributePropertiesBuilder1.SetAttributeObjects(objects2)

attributePropertiesBuilder1.Units = "MilliMeter"

theSession.SetUndoMarkName(markId2, "Attributes Dialog")

attributePropertiesBuilder1.DateValue.DateItem.Day = DateItemBuilder.DayOfMonth.Day05

attributePropertiesBuilder1.DateValue.DateItem.Month = DateItemBuilder.MonthOfYear.Feb

attributePropertiesBuilder1.DateValue.DateItem.Year = "2016"

attributePropertiesBuilder1.DateValue.DateItem.Time = "00:00:00"

' ----------------------------------------------
' Dialog Begin Attributes
' ----------------------------------------------
attributePropertiesBuilder1.Title = ""

attributePropertiesBuilder1.Units = "MilliMeter"

attributePropertiesBuilder1.IsArray = False

attributePropertiesBuilder1.Units = ""

attributePropertiesBuilder1.IsArray = False

attributePropertiesBuilder1.IsArray = False

attributePropertiesBuilder1.StringValue = ""

attributePropertiesBuilder1.Units = "MilliMeter"

attributePropertiesBuilder1.IsArray = False

attributePropertiesBuilder1.IsArray = False

attributePropertiesBuilder1.Category = "Materials"

attributePropertiesBuilder1.Title = "MassPropMass"

attributePropertiesBuilder1.Units = "MilliMeter"

attributePropertiesBuilder1.IsArray = False

attributePropertiesBuilder1.Units = "Kilogram"

attributePropertiesBuilder1.Units = "Kilogram"

attributePropertiesBuilder1.NumberValue = 0.0272857775984746

attributePropertiesBuilder1.IsArray = False

attributePropertiesBuilder1.IsArray = False

Dim markId3 As Session.UndoMarkId
markId3 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Attributes")

theSession.DeleteUndoMark(markId3, Nothing)

Dim markId4 As Session.UndoMarkId
markId4 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Attributes")

theSession.DeleteUndoMark(markId4, Nothing)

theSession.SetUndoMarkName(markId2, "Attributes")

attributePropertiesBuilder1.Destroy()

Dim expression1 As Expression
expression1 = workPart.Expressions.GetAttributeExpression(workPart, "MassPropMass", NXObject.AttributeType.Real, -1)

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

Dim unit1 As Unit = CType(workPart.UnitCollection.FindObject("Kilogram"), Unit)

Dim expression2 As Expression
expression2 = workPart.Expressions.CreateWithUnits("MASS_TRANSFER=p40", unit1)

Dim nullUnit As Unit = Nothing

Dim expression3 As Expression
expression3 = workPart.Expressions.CreateWithUnits("WEIGHT_Attribute=ug_setPartAttrValue(""WEIGHT"",format(""%0.3f kg"",MASS_TRANSFER))", nullUnit)

theSession.Preferences.Modeling.UpdatePending = False

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

theSession.CleanUpFacetedFacesAndEdges()

' ----------------------------------------------
' Menu: Tools->Journal->Stop Recording
' ----------------------------------------------

End Sub
End Module

In your code, "expression1" is the expression that the system attribute "MassPropMass" is linked to. Knowing this, you can use the name of the expression in place of "p40".

Instead of:

expression2 = workPart.Expressions.CreateWithUnits("MASS_TRANSFER=p40", unit1)

Use this:

expression2 = workPart.Expressions.CreateWithUnits("MASS_TRANSFER=" & expression1.Name, unit1)

Is there any possibility to add the two expression written in journal, for example if I have created expression1 and expression2 to be added as expression1+" "+expression2 in expressions.

See the article on creating expressions:
http://nxjournaling.com/content/expressions-creating-expressions-no-units

Especially the section titled:
"Create A Number Expression (Unitless), Specifying A Formula Using Other Expressions"

Thanks for the reply
I have tried it and it works fine....
But what I need to know is, if I create an expression with name called "DESCRIPTION" and for this expression I want to link the attributes in the format p123+" "+p456 (where "p" will differ in other parts). Is there any possibility to have such expressions?. Please let me know if it is possible

What version of NX are you using?

Also, are all of the attributes that you want to link contained in the same file?

I am using NX 9.0.2, and all the attributes which have to be linked to the expressions are in the same file.
For example : I have two attributes DWG_TITLE & DWG_TITLE2, these attributes have to be linked to the expression DESCRIPTION.
So the expression would look like this
DESCRIPTION (AS A STRING) = p123+" "+p456 (Where "p123" & "p456" denotes DWG_TITLE & DWG_TITLE2 respectively).
So will it be possible to relate these attributes to the expression ?

Thank you for your support and help.

Hello,

Is there any possibility for the requirement stated in my previous reply ?
Please help me to resolve this issue.

The following code will look for part attributes named "DWG_TITLE" and "DWG_TITLE2"; if they are found, it will create/update an expression named "DESCRIPTION" that uses the attribute values.

Option Strict Off
Imports System
Imports NXOpen

Module Module4

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 attributeName1 As String = "DWG_TITLE"
Const attributeName2 As String = "DWG_TITLE2"
Const expName As String = "DESCRIPTION"

If Not workPart.HasUserAttribute(attributeName1, NXObject.AttributeType.String, -1) Then
lw.WriteLine("attribute " & attributeName1 & " not found, journal exiting")
Return
End If

If Not workPart.HasUserAttribute(attributeName2, NXObject.AttributeType.String, -1) Then
lw.WriteLine("attribute " & attributeName2 & " not found, journal exiting")
Return
End If

'get the underlying attribute expressions
Dim attExp1 As Expression = workPart.Expressions.GetAttributeExpression(workPart, attributeName1, NXObject.AttributeType.String, -1)
Dim attExp2 As Expression = workPart.Expressions.GetAttributeExpression(workPart, attributeName2, NXObject.AttributeType.String, -1)

'create/update the description expression using the attribute expressions
Dim expDescription As Expression = Nothing
Try
expDescription = workPart.Expressions.CreateExpression("String", expName & " = " & attExp1.Name & " + "" """ & " + " & attExp2.Name)
Catch ex As NXException
If ex.ErrorCode = 1050017 Then
'expression already exists

'get the existing expression
expDescription = workPart.Expressions.FindObject(expName)

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

'update the formula
expDescription.RightHandSide = attExp1.Name & " + "" """ & " + " & attExp2.Name

Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.DoUpdate(markId2)
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

Hello,
The code works as expected. Thank you very much for your kind help and support.

Regards,
Praveen