Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Hi, I'm writing a journal that i 1

Status
Not open for further replies.

javaxp

Automotive
Jul 14, 2014
43
Hi,

I'm writing a journal that indexes an assembly. The journal gets the levels of indexation and writes the results on the information window, but I can't write the level of each component into component attribute ("NIVEL_ESTRUCTURA") .

Can anyone help me? Thanks in advance...


Here is the code:



Code:
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Assemblies
Imports NXOpen.Features

Module NXJournal
    Dim theSession As Session = Session.GetSession()
    Dim theUFSession As UFSession = UFSession.GetUFSession()
    Dim workPart As Part = theSession.Parts.Work
    Public displayPart As Part = theSession.Parts.Display
    Dim lw As ListingWindow = theSession.ListingWindow
    Dim nivel As String = "1"
    Dim longString As Integer = 0
    Dim longFinalString As Integer = 0
    Dim caracterNivel As String = "."

    Public Sub Main(ByVal args As String())


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

        Dim theOrders As Assemblies.ComponentOrder() = Nothing
        displayPart.ComponentAssembly.GetComponentOrders(theOrders)

        ' GetActiveOrder is new in NX10.0.3
        Dim theCurrentOrder As Assemblies.Order =
            displayPart.ComponentAssembly.GetActiveOrder()

        Echo("El orden de ensamble actual es " & theCurrentOrder.Name)


        theCurrentOrder.Activate()
        theSession.UpdateManager.DoUpdate(markId1)

        Echo(theCurrentOrder.Name & ":")

        displayPart.SetAttribute("NIVEL_ESTRUCTURA", nivel)



        WalkAssemblyTree(displayPart.ComponentAssembly.RootComponent, theCurrentOrder, 0)

        theSession.UndoToMark(markId1, "")
        theSession.DeleteUndoMarksUpToMark(markId1, "", False)

    End Sub

    Sub WalkAssemblyTree(ByVal theComponent As NXOpen.Assemblies.Component,
                        ByVal theOrder As NXOpen.Assemblies.ComponentOrder,
                        Indice As Int32)


        Dim kids As Assemblies.Component() = theComponent.GetChildren()

        Try

            If kids.Length = 0 Then
                longString = Len(nivel)

                longFinalString = InStrRev(nivel, caracterNivel)
                nivel = Left(nivel, longFinalString - 1)


                Return ' Not an assembly or sub-assembly
            Else

                kids = theOrder.AskChildrenOrder(theComponent)

                For ii As Integer = 0 To kids.Length - 1

                    nivel = nivel & "." & ii + 1
                    lw.WriteLine(nivel & "   " & kids(ii).DisplayName)

                    kids(ii).SetUserAttribute("NIVEL_ESTRUCTURA", -1, nivel, Update.Option.Now)

                    WalkAssemblyTree(kids(ii), theOrder, Indice)
                Next

                longString = Len(nivel)

                longFinalString = InStrRev(nivel, caracterNivel)

                nivel = Left(nivel, longFinalString - 1)
            End If

        Catch




        End Try


    End Sub

    Sub Echo(ByVal output As String)
        theSession.ListingWindow.Open()
        theSession.ListingWindow.WriteLine(output)
        theSession.LogFile.WriteLine(output)
    End Sub


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

End Module
 
Replies continue below

Recommended for you

If you add exception to catch:

Code:
Catch e1 As Exception
  theSession.ListingWindow.WriteLine("Failed: " & e1.ToString)

You will receive an error that length must be greater or equal 0 in line:

Code:
nivel = Left(nivel, longFinalString - 1)

What for You are using this code?

Code:
longString = Len(nivel)
longFinalString = InStrRev(nivel, caracterNivel)
nivel = Left(nivel, longFinalString - 1)

next problem is undo mark, it will revert everything - theSession.UndoToMark(markId1, "").
And finally You have to convert Component to part. I use this code, but I think there is a better one, maybe someone will send different solution:

Code:
dim MyPart As NXOpen.Part = CType(theSession.Parts.FindObject(kids(ii).displayName), NXOpen.Part)
MyPart.SetUserAttribute("NIVEL_ESTRUCTURA", -1, nivel, Update.Option.Now)

I have question - what if part will be on different levels?



With best regards
Michael
 
Hi Michael,

Thanks for your answer.

Code:
next problem is undo mark, it will revert everything - theSession.UndoToMark(markId1, "").

Yes, there is the problem. I don't control well the "undo marks".

Code:
longString = Len(nivel)
longFinalString = InStrRev(nivel, caracterNivel)
nivel = Left(nivel, longFinalString - 1)

This is for cut the level-string when i return to a previous level.

Michael said:
And finally You have to convert Component to part.
I have question - what if part will be on different levels?

If a part is in different levels, the level string must be different. Is for this that I use component attribute instead of part Attribute.


Actually, if a part is repeated in the same level, it must contain the same string-level, but I don't know how to achieve this...


Michael said:
I use this code, but I think there is a better one, maybe someone will send different solution:

Yes, I agree that there must be a better solution...I'm not a programmer...

I appreciate your answer.The code now is working.

Code:
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Assemblies
Imports NXOpen.Features

Module NXJournal
    Dim theSession As Session = Session.GetSession()
    Dim theUFSession As UFSession = UFSession.GetUFSession()
    Dim workPart As Part = theSession.Parts.Work
    Public displayPart As Part = theSession.Parts.Display
    Dim lw As ListingWindow = theSession.ListingWindow
    Dim nivel As String = "1"
    Dim longString As Integer = 0
    Dim longFinalString As Integer = 0
    Dim caracterNivel As String = "."

    Public Sub Main(ByVal args As String())



        Dim theOrders As Assemblies.ComponentOrder() = Nothing
        displayPart.ComponentAssembly.GetComponentOrders(theOrders)

        ' GetActiveOrder is new in NX10.0.3
        Dim theCurrentOrder As Assemblies.Order =
            displayPart.ComponentAssembly.GetActiveOrder()

        Echo("El orden de ensamble actual es " & theCurrentOrder.Name)


        theCurrentOrder.Activate()


        Echo(theCurrentOrder.Name & ":")

        displayPart.SetAttribute("NIVEL_ESTRUCTURA", nivel)



        WalkAssemblyTree(displayPart.ComponentAssembly.RootComponent, theCurrentOrder, 0)


    End Sub

    Sub WalkAssemblyTree(ByVal theComponent As NXOpen.Assemblies.Component,
                        ByVal theOrder As NXOpen.Assemblies.ComponentOrder,
                        Indice As Int32)


        Dim kids As Assemblies.Component() = theComponent.GetChildren()

        Try

            If kids.Length = 0 Then
                longString = Len(nivel)

                longFinalString = InStrRev(nivel, caracterNivel)
                nivel = Left(nivel, longFinalString - 1)


                Return ' Not an assembly or sub-assembly
            Else

                kids = theOrder.AskChildrenOrder(theComponent)

                For ii As Integer = 0 To kids.Length - 1

                    nivel = nivel & "." & ii + 1
                    lw.WriteLine(nivel & "   " & kids(ii).DisplayName)

                    kids(ii).SetUserAttribute("NIVEL_ESTRUCTURA", -1, nivel, Update.Option.Now)

                    WalkAssemblyTree(kids(ii), theOrder, Indice)
                Next
                'End If
                longString = Len(nivel)

                longFinalString = InStrRev(nivel, caracterNivel)
                If longFinalString = 0 Then
                    longFinalString = 1

                End If
                nivel = Left(nivel, longFinalString - 1)
            End If

        Catch ex As NXException


        '//---- Enter your exception handling code here -----
        'lw.WriteLine(ex.Message)
        End Try


    End Sub

    Sub Echo(ByVal output As String)
        theSession.ListingWindow.Open()
        theSession.ListingWindow.WriteLine(output)
        theSession.LogFile.WriteLine(output)
    End Sub


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

End Module

Best regards,

Javier
 
Hi cowski,

Thanks for your suggestion. The "indent" variable only gives the level of deep as: 0 , 1 , 2, etc.
and is mandatory for me to use the format: 1.1, 1.2, 1.2.1, 1.2.2 and so on...

I have used this cycle routine ( GTAC: report displayed assembly components in various orders) because i need to respect the order in the assembly navigator, and not the cronologycal.

Can you give me a hint to achieve to group the identical components of the same level, to assign them the same "level" ?. I can recognize them and assign the same attribute in this case, but i lose the index of level when i come back to previous level...


Thanks.

Best regards,

Javier
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor