cancel
Showing results for 
Search instead for 
Did you mean: 

workPart.DrawingSheets.ToArray does not follow NX sheet order

Pioneer
Pioneer

If the sheets are reordered in NX by editing the sheet numbers and then a vb journal using workPart.DrawingSheets.ToArray is used the order of the sheets in the vb array is not the same as in NX. I have tried to use Array.Sort to sort by sheet number but my vb skills are not quiet good enough to get it to work.


 Example code where this is an issue is on the GTAC site "Sample NX Open .NET Visual Basic program : report all drawing sheet names and numbers". Create a multiple sheet NX drawing file, reorder the sheet numbers and then run this code.

Regards,

Clayton

NX9.0.3.4

Regards,

Clayton McPherson
TC 10.1, NX 9.0.3.4 MP7
6 REPLIES

Re: workPart.DrawingSheets.ToArray does not follow NX sheet order

Esteemed Contributor
Esteemed Contributor

An object collection is not in any order. It may be there is a virtual order that equals the order of creation.

If you need an ordered array, you will have to do this by yourself.

You may want to introduce a custom structure and a sorting interface, if the drawing class doesn't contain a sorting interface.

The web has plenty of examples for creating custom structures and sorting interfaces for them.

Stefan Pendl, Systemmanager CAx, HAIDLMAIR GmbH
Production: NX10.0.3, VERICUT 8.0, FBM, MRL 3.1.4 | TcUA 10.1 MP7 Patch 0 (10.1.7.0) | TcVis 10.1
Development: VB.NET, Tcl/Tk    Testing: NX11.0 EAP, NX12.0 EAP

How to Get the Most from Your Signature in the Community

Re: workPart.DrawingSheets.ToArray does not follow NX sheet order

Siemens Phenom Siemens Phenom
Siemens Phenom

There is another example on the Solution Center called "sort drawing sheets by name alphanumerically", which you can easliy find if you search for "nx_api4391".

 

Or this link might work:

 

https://solutions.industrysoftware.automation.siemens.com/view.php?sort=desc&q=sample+program+alphan...

 

 

Re: workPart.DrawingSheets.ToArray does not follow NX sheet order

Pioneer
Pioneer

That's just what I was looking for, thanks!

Regards,

Clayton McPherson
TC 10.1, NX 9.0.3.4 MP7

Re: workPart.DrawingSheets.ToArray does not follow NX sheet order

Phenom
Phenom

Steve's code will sort the sheets by the sheet name. The code below will sort the sheets by the sheet number (visible and modifiable on the "edit sheet" dialog).

 


Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.UF

Module Module1

    Dim theSession As Session = Session.GetSession()
    Dim theUfSession As UFSession = UFSession.GetUFSession()

    Dim theUI As UI = UI.GetUI()
    Dim lw As ListingWindow = theSession.ListingWindow

    Sub Main()

        Dim markId1 As Session.UndoMarkId
        markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "NXJ")

        lw.Open()

        Dim dwgSheets As New List(Of Drawings.DrawingSheet)

        For Each temp As Drawings.DrawingSheet In theSession.Parts.Work.DrawingSheets
            dwgSheets.Add(temp)
        Next

        'report current sheet order
        For Each temp As Drawings.DrawingSheet In dwgSheets
            lw.WriteLine("sheet name: " & temp.Name & ", page number: " & SheetNumber(temp).ToString)
        Next
        lw.WriteLine("")

        'sort sheets by sheet number
        dwgSheets.Sort(AddressOf CompareSheetNumbers)

        'report sorted sheet order
        For Each temp As Drawings.DrawingSheet In dwgSheets
            lw.WriteLine("sheet name: " & temp.Name & ", page number: " & SheetNumber(temp).ToString)
        Next
        lw.WriteLine("")

        ' Roll back to avoid marking the part as Modified
        theSession.UndoToMark(markId1, "NXJ")
        theSession.DeleteUndoMark(markId1, "NXJ")

        lw.Close()

    End Sub

    Private Function CompareSheetNumbers(ByVal x As Drawings.DrawingSheet, ByVal y As Drawings.DrawingSheet) As Integer

        Dim xNum As Integer = SheetNumber(x)
        Dim yNum As Integer = SheetNumber(y)

        If xNum > yNum Then
            Return 1
        End If

        If xNum < yNum Then
            Return -1
        End If

        If xNum = yNum Then
            Return 0
        End If

    End Function

    Function SheetNumber(ByVal theSheet As Drawings.DrawingSheet) As Integer

        Dim sheetNum As Integer
        Dim theSheetBuilder As Drawings.DrawingSheetBuilder = theSession.Parts.Work.DrawingSheets.DrawingSheetBuilder(theSheet)
        sheetNum = theSheetBuilder.Number

        theSheetBuilder.Destroy()

        Return sheetNum

    End Function

    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

Re: workPart.DrawingSheets.ToArray does not follow NX sheet order

Esteemed Contributor
Esteemed Contributor

Did NX9 support sheet numbers, or was that added in NX10?

Ken Akerboom Sr CAx Systems Engr, Moog, Inc.
Production: NX10.0.3.5 MP5 + patch/TC11.2
I'd rather be e-steemed than e-diseaseled


Re: workPart.DrawingSheets.ToArray does not follow NX sheet order

Phenom
Phenom

NX9 definitely has them (that is the version I tested with). They are also available in NX 8.5; that is the oldest version I currently have installed.