Showing results for 
Search instead for 
Did you mean: 

For Loop will not loop!

Valued Contributor
Valued Contributor

Hello World,


I am trying to run some excel processes through a for loop using one of the snippets of code that GTAC provided.


The problem is that the For loop inside the "ProcessThisPart" subroutine will not completely loop. It will execute the Echo command and then skip to the end of the subroutine, everytime.


I can't seem to figure out why. There is more code than this, code that initiates excel and what not, but that isn't affecting the For loop at all. You can insert other code that handles NX only and it will still skip everything after that first Echo command.


Any ideas on why this does not work?


I am running NX 10.0.4 and I have executed ths code inside of the journal editor, as well as Visual Studios and both give me the same result.



Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Assemblies
Imports System.Collections.Generic

Module Module1

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

    Public Sub Main(ByVal args As String())
        Dim displayPart As Part = theSession.Parts.Display

        Dim markId1 As NXOpen.Session.UndoMarkId = theSession.SetUndoMark( _
            NXOpen.Session.MarkVisibility.Visible, "Reorder Components")

        Dim theOrders As Assemblies.ComponentOrder() = Nothing

        Echo(displayPart.Leaf & " has " & theOrders.Length & " ComponentOrder objects")

        For Each anOrder As Assemblies.ComponentOrder In theOrders


            Echo(anOrder.Name & ":")
            WalkAssembyTree(displayPart.ComponentAssembly.RootComponent, anOrder, "")

            theUFSession.Ui.DisplayMessage(anOrder.Name, 1)

            theSession.UndoToMark(markId1, "")

        theSession.DeleteUndoMarksUpToMark(markId1, "", False)

    End Sub

    Sub WalkAssembyTree(ByVal theComponent As NXOpen.Assemblies.Component,
                        ByVal theOrder As NXOpen.Assemblies.ComponentOrder,
                        ByVal indent As String)

        Dim kids As Assemblies.Component() = theComponent.GetChildren()
        If kids.Length = 0 Then Return ' Not an assembly or sub-assembly 

        indent = indent + "  "

        ' This does not always work - see PR 7421390 
        ' Testing in the Toycar assembly only Chronological returns any Children 

        kids = theOrder.AskChildrenOrder(theComponent)

        For ii As Integer = 0 To kids.Length - 1
            Echo(indent & kids(ii).DisplayName)

            Dim thisPart As Part = kids(ii).Prototype()

            processThisPart(thisPart, indent)

            WalkAssembyTree(kids(ii), theOrder, indent)
    End Sub

    Public Sub processThisPart(ByVal p As Part, ByVal ind As String)

        Dim theBodies() As Body = p.Bodies.ToArray()
        Echo(ind & "Bodies in part: " & theBodies.GetLength(0).ToString())

        For Each thisBody As Body In theBodies 

            Echo(ind & thisBody.ToString())

            If thisBody.HasUserAttribute("DB_PART_NAME", _     NXObject.AttributeType.String, -1) = True Then

          excel.Cells(row, 2) = (ind & thisBody.GetStringAttribute("DB_PART_NAME"))


                'do nothing  

            End If

            excel.Cells(row, 3).Value = (ind & thisBody.ToString())


    End Sub

    Sub Echo(ByVal output As String)
    End Sub

    Public Function GetUnloadOption(ByVal arg As String) As Integer
        Return Session.LibraryUnloadOption.Immediately
    End Function

End Module