Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Module Module1
Sub Main()
Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession()
Dim workPart = theSession.Parts.Work
Dim theUI As UI = UI.GetUI()
Dim lw As ListingWindow = theSession.ListingWindow
Dim ResultString As String
Dim ValueString As String
Dim HeaderString As String
Dim i As Integer[/indent]
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "NXJ vary expression")
lw.Open()
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Change the values of the following constants to suit your needs
' specify expression name in .prt file that references
' spreadsheet value
Const expressionName As String = "Input_SweepAngle"
Const measureExpressionName As String = "Result_Mass_Lbs"
Const startValue As Double = 1
Const endValue As Double = 22
Const stepValue As Double = .5
lw.WriteLine("")
lw.WriteLine("")
lw.WriteLine("ExpressionToVary: " & expressionName & ", StartValue: " & startValue & ", EndValue: " & endValue & ", StepValue: " & stepValue)
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Dim theExpression As Expression
Try
theExpression = theSession.Parts.Work.Expressions.FindObject(expressionName)
Catch ex As NXException
If ex.ErrorCode = 3520016 Then
'no expression found with given name
lw.WriteLine("expression '" & expressionName & "' not found, journal exiting")
Else
lw.WriteLine(ex.ErrorCode & ": " & ex.Message)
End If
Return
End Try
Dim measureExpression As Expression
Try
measureExpression = theSession.Parts.Work.Expressions.FindObject(measureExpressionName)
Catch ex As NXException
If ex.ErrorCode = 3520016 Then
'no expression found with given name
lw.WriteLine("expression '" & measureExpressionName & "' not found, journal exiting")
Else
lw.WriteLine(ex.ErrorCode & ": " & ex.Message)
End If
Return
End Try
Dim currentValue As Double = startValue
Dim newFormula As String
i = 1
Do While currentValue <= endValue
'update expression with current value
newFormula = currentValue.ToString
'lw.WriteLine("newFormula: " & newFormula)
theSession.UpdateManager.ClearErrorList()
Dim markId2 As Session.UndoMarkId
markId2 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Expression edit")
theExpression.RightHandSide = newFormula
Dim nErrs1 As Integer
'update model
Try
nErrs1 = theSession.UpdateManager.DoUpdate(markId2)
Catch ex As NXException
lw.WriteLine("** Update Error with value: " & currentValue.ToString)
lw.WriteLine("** " & ex.ErrorCode & ": " & ex.Message)
lw.WriteLine("")
End Try
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Write all expressions
ResultString = i
HeaderString = "Count"
ValueString = ""
' Loop over each expression in the collection
For Each myExp As Expression In workPart.expressions
' Check if the name starts with "p"
If myExp.Name.StartsWith("p") Then
' Skip this expression and move on to the next one
Continue For
End If
'store expression value
ValueString = myExp.Value
'Check for scientific notation and convert if needed
If instr(ValueString, "E") then ValueString = myExp.Value.ToString("F15")
'Add value to result list
ResultString = ResultString & ", " & ValueString
HeaderString = HeaderString & ", " & myExp.Name
Next myExp
'Write Header for the first part loop
if i = 1 then lw.WriteLine(HeaderString)
'Write all named expressions
lw.WriteLine(ResultString)
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'update current value
currentValue += stepValue
i += 1
Loop 'Next Iteration
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