Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Automate Data Collection

Status
Not open for further replies.

JPArnett

New member
Apr 28, 2023
2
I would like to automate the task of calculating the mass and center of gravity for a model while varying a dimension over a range of values. Where do I start with this task and what tools should I use? I have an expression driven model with measurement expressions for the mass and center of gravity. I just need a way store the values to a table and iterate the dimension over a range.
 
 https://files.engineering.com/getfile.aspx?folder=b6fcbaf5-1f19-4560-ad62-1e1249c010b0&file=ExampleForMassData.prt
Replies continue below

Recommended for you

Thank you so much!! This was my first day using NX journals and I cant believe I actually did it. The thread you shared was exactly what I needed.

I made a small changes. Each iteration now prints all the named expressions on a single line.

Code:
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor