Code:
Imports System
Imports NXOpen
Imports NXOpen.UFModule NXJournal
Sub Main (ByVal args() As String)
Dim theSession As Session = Session.GetSession()
Dim theUI As UI = UI.GetUI()
Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()
Dim expBauBen As NXOpen.Expression = Nothing
Dim expBauNum As NXOpen.Expression = Nothing
Dim expBauGew As NXOpen.Expression = Nothing
Dim expSysMass As NXOpen.Expression = Nothing
'Bauteilnummer
Dim expBauNumValue As String
expBauNumValue = "subString(ug_askCurrentWorkPart(),1,18)"
'Benennung
Dim prt As String = ".prt"
Dim kom As String = ","
Dim klam As String = ")"
Dim expBauBenValue As String = "replaceString(subString(ug_askCurrentWorkPart(),20,50)," & """" & prt & """" & kom & """" & """" & klam
' Originale Schreibweise: replaceString(subString(ug_askCurrentWorkPart(),20,50),".prt","")
'Bauteilgewicht
Dim expBauGewValue As String
expBauGewValue = "format(" & """" & "%0.2f" & """" & kom & "NX_Mass" & klam
Try
'Systemattribut
expSysMass = workPart.Expressions.CreateSystemExpressionFromReferenceString("<W@NX_MASS>")
'create expression, type: String
'add double quotes around the string value
expBauBen = workPart.Expressions.CreateExpression("String", "Bauteilname_Test = " & expBauBenValue)
expBauNum = workPart.Expressions.CreateExpression("String", "Bauteilnummer_Test = " & expBauNumValue)
expBauGew = workPart.Expressions.CreateExpression("String", "Bauteilgewicht_Test = " & expBauGewValue)
'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))
'Dem Ausdruck einen Kommentar zuweisen
'exp_4.EditComment("greetings")
'Setze Attribute
'workPart.SetAttribute("Artikelnummer_Test")' Artikelnummer / Kein Wert --> Userinput
'workPart.SetAttribute("Benennung_Test") ' Benennung = Ausdruck für hintern substring = replaceString(subString(ug_askCurrentWorkPart(),20,50),".prt","")
'workPart.SetAttribute("Bauteilnummer_Test") ' Bauteilnummer = Ausdruck für vorderen substring = subString(ug_askCurrentWorkPart(),1,18)
'workPart.SetAttribute("Gewicht_Test") ' Gewicht = formatierte NX_Mass / Systemattribut
'Setze Attribute
workPart.SetAttribute("Artikelnummer_Test", "TBD")' Artikelnummer / Kein Wert --> Userinput
'Setze Attribute und verknüpfe Werte der Ausdrücke mit Attributen
Dim objects_Attr2(0) As NXObject
objects_Attr2(0) = workPart
Dim attributePropertiesBuilder_PartDim As AttributePropertiesBuilder = theSession.AttributeManager.CreateAttributePropertiesBuilder(workPart, objects_Attr2, AttributePropertiesBuilder.OperationType.None)
attributePropertiesBuilder_PartDim.DataType = AttributePropertiesBaseBuilder.DataTypeOptions.String
attributePropertiesBuilder_PartDim.Title = "Benennung_Test"
attributePropertiesBuilder_PartDim.StringValue = ""
attributePropertiesBuilder_PartDim.Expression = expBauBen
attributePropertiesBuilder_PartDim.CreateAttribute()
attributePropertiesBuilder_PartDim.DataType = AttributePropertiesBaseBuilder.DataTypeOptions.String
attributePropertiesBuilder_PartDim.Title = "Bauteilnummer_Test"
attributePropertiesBuilder_PartDim.StringValue = ""
attributePropertiesBuilder_PartDim.Expression = expBauNum
attributePropertiesBuilder_PartDim.CreateAttribute()
attributePropertiesBuilder_PartDim.DataType = AttributePropertiesBaseBuilder.DataTypeOptions.String
attributePropertiesBuilder_PartDim.Title = "Gewicht_Test"
attributePropertiesBuilder_PartDim.StringValue = ""
attributePropertiesBuilder_PartDim.Expression = expBauGew
attributePropertiesBuilder_PartDim.CreateAttribute()
Dim nXObjectAttr As NXObject
nXObjectAttr = attributePropertiesBuilder_PartDim.Commit()
attributePropertiesBuilder_PartDim.Destroy()
Catch ex As NXException
If ex.ErrorCode = 1050017 Then
'expression already exists
expBauNum = workPart.Expressions.FindObject("Bauteilnummer_Test")
expBauBen = workPart.Expressions.FindObject("Benennung_Test")
expBauGew = workPart.Expressions.FindObject("Bauteilgewicht_Test")
Else
'other error
lw.WriteLine("NX exception: " & ex.ErrorCode & ", " & ex.Message)
End If
End Try
End Sub
Public Function GetUnloadOption(ByVal dummy As String) As Integer
GetUnloadOption = UFConstants.UF_UNLOAD_IMMEDIATELY
End Function
End Module