Print all open drawings

The following journal code will print all drawing sheets from all fully loaded part files to your default printer. This can be useful if you want some quick print outs for reference. It should use the default paper size for your printer. If it does not, or you prefer a different paper size, you can uncomment or add the appropriate paper size in the PrintSheet subroutine.

Points of interest

The code uses some .net framework objects to determine the default printer.



The subroutine ChangeDisplayPart will make the given part the display part. Printing the drawing sheets didn't seem to work unless the part was either the display or work part. This subroutine could come in handy in other journal projects.



This journal also shows how to loop through the drawing sheets and update any views that are out of date.



The Code

Written and tested on NX 8.5; will probably work as far back as NX 6, though I cannot test that.


The "lw" calls are there for debugging only, uncomment them to see additional information.


Option Strict Off
Imports System
Imports System.Windows.Forms
Imports NXOpen
 
Module Module1
 
    Dim theSession As Session = Session.GetSession()
    Dim defaultPrinterName As String = ""
 
    Sub Main()
 
        Dim workPart As Part = theSession.Parts.Work
        Dim lw As ListingWindow = theSession.ListingWindow
        'lw.Open()
 
        Dim printerSettings As New System.Drawing.Printing.PrinterSettings
        Try
            defaultPrinterName = printerSettings.PrinterName
        Catch ex As System.Exception
            defaultPrinterName = ""
        Finally
            printerSettings = Nothing
        End Try
 
        If String.IsNullOrEmpty(defaultPrinterName) Then
            MessageBox.Show("No default printer found.", "Printer error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        End If
 
        For Each tempPart As Part In theSession.Parts
 
            If tempPart.IsFullyLoaded Then
                'lw.WriteLine("part: " & tempPart.FullPath)
                ChangeDisplayPart(tempPart)
 
                For Each tempSheet As Drawings.DrawingSheet In tempPart.DrawingSheets
                    'lw.WriteLine("  sheet: " & tempSheet.Name)
                    tempPart.DraftingViews.UpdateViews(Drawings.DraftingViewCollection.ViewUpdateOption.OutOfDate, tempSheet)
 
                    PrintSheet(tempPart, tempSheet)
 
                Next
                'lw.WriteLine("")
 
            End If
 
            ChangeDisplayPart(workPart)
 
        Next
 
        'lw.Close()
 
    End Sub
 
    Sub PrintSheet(ByVal thePart As Part, ByVal theSheet As Drawings.DrawingSheet)
 
        Dim printBuilder1 As PrintBuilder
        printBuilder1 = thePart.PlotManager.CreatePrintBuilder()
 
        printBuilder1.PrinterText = defaultPrinterName
 
        'printer settings, change as desired
        printBuilder1.Copies = 1
        printBuilder1.ThinWidth = 1.0
        printBuilder1.NormalWidth = 2.0
        printBuilder1.ThickWidth = 3.0
        printBuilder1.Output = PrintBuilder.OutputOption.WireframeBlackWhite
        printBuilder1.RasterImages = True
        printBuilder1.Orientation = PrintBuilder.OrientationOption.Landscape
        'printBuilder1.Paper = PrintBuilder.PaperSize.Letter
        'printBuilder1.Paper = PrintBuilder.PaperSize.A4
 
        Dim paper1 As PrintBuilder.PaperSize
        paper1 = printBuilder1.Paper
 
 
        Dim sheets1(0) As NXObject
        sheets1(0) = theSheet
        printBuilder1.SourceBuilder.SetSheets(sheets1)
 
        Dim nXObject1 As NXObject
 
        Try
            nXObject1 = printBuilder1.Commit()
        Catch ex As NXException
            'optional: add code to log/display error
        Finally
            printBuilder1.Destroy()
        End Try
 
    End Sub
 
    Public Sub ChangeDisplayPart(ByVal myPart As Part)
 
        'make the given component the display part
        Dim markId1 As Session.UndoMarkId
        markId1 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Display Part")
 
        Try
            Dim partLoadStatus1 As PartLoadStatus
            Dim status1 As PartCollection.SdpsStatus
            status1 = theSession.Parts.SetDisplay(myPart, False, False, partLoadStatus1)
 
            partLoadStatus1.Dispose()
        Catch ex As NXException
            'optional: add code to log/display error
            theSession.UndoToMark(markId1, "Display Part")
        Finally
            theSession.DeleteUndoMark(markId1, "Display Part")
        End Try
 
    End Sub
 
End Module

Comments

This works really nice! I guessed changing (letter) to (tabloid) would get me the 11x17 sheet. Do you know what te command would be for 22X34 or D sheet?

Option Strict Off
Imports System
Imports System.Windows.Forms
Imports NXOpen
 
Module Module1
 
    Dim theSession As Session = Session.GetSession()
    Dim defaultPrinterName As String = ""
 
    Sub Main()
 
        Dim workPart As Part = theSession.Parts.Work
        Dim lw As ListingWindow = theSession.ListingWindow
        'lw.Open()
 
        Dim printerSettings As New System.Drawing.Printing.PrinterSettings
        Try
            defaultPrinterName = printerSettings.PrinterName
        Catch ex As System.Exception
            defaultPrinterName = ""
        Finally
            printerSettings = Nothing
        End Try
 
        If String.IsNullOrEmpty(defaultPrinterName) Then
            MessageBox.Show("No default printer found.", "Printer error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        End If
 
        For Each tempPart As Part In theSession.Parts
 
            If tempPart.IsFullyLoaded Then
                'lw.WriteLine("part: " & tempPart.FullPath)
                ChangeDisplayPart(tempPart)
 
                For Each tempSheet As Drawings.DrawingSheet In tempPart.DrawingSheets
                    'lw.WriteLine("  sheet: " & tempSheet.Name)
                    tempPart.DraftingViews.UpdateViews(Drawings.DraftingViewCollection.ViewUpdateOption.OutOfDate, tempSheet)
 
                    PrintSheet(tempPart, tempSheet)
 
                Next
                'lw.WriteLine("")
 
            End If
 
            ChangeDisplayPart(workPart)
 
        Next
 
        'lw.Close()
 
    End Sub
 
    Sub PrintSheet(ByVal thePart As Part, ByVal theSheet As Drawings.DrawingSheet)
 
        Dim printBuilder1 As PrintBuilder
        printBuilder1 = thePart.PlotManager.CreatePrintBuilder()
 
        printBuilder1.PrinterText = defaultPrinterName
 
        'printer settings, change as desired
        printBuilder1.Copies = 1
        printBuilder1.ThinWidth = 1.0
        printBuilder1.NormalWidth = 2.0
        printBuilder1.ThickWidth = 3.0
        printBuilder1.Output = PrintBuilder.OutputOption.WireframeBlackWhite
        printBuilder1.RasterImages = True
        printBuilder1.Orientation = PrintBuilder.OrientationOption.Landscape
        printBuilder1.Paper = PrintBuilder.PaperSize.Tabloid
        'printBuilder1.Paper = PrintBuilder.PaperSize.A4
 
        Dim paper1 As PrintBuilder.PaperSize
        paper1 = printBuilder1.Paper
 
 
        Dim sheets1(0) As NXObject
        sheets1(0) = theSheet
        printBuilder1.SourceBuilder.SetSheets(sheets1)
 
        Dim nXObject1 As NXObject
 
        Try
            nXObject1 = printBuilder1.Commit()
        Catch ex As NXException
            'optional: add code to log/display error
        Finally
            printBuilder1.Destroy()
        End Try
 
    End Sub
 
    Public Sub ChangeDisplayPart(ByVal myPart As Part)
 
        'make the given component the display part
        Dim markId1 As Session.UndoMarkId
        markId1 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Display Part")
 
        Try
            Dim partLoadStatus1 As PartLoadStatus
            Dim status1 As PartCollection.SdpsStatus
            status1 = theSession.Parts.SetDisplay(myPart, False, False, partLoadStatus1)
 
            partLoadStatus1.Dispose()
        Catch ex As NXException
            'optional: add code to log/display error
            theSession.UndoToMark(markId1, "Display Part")
        Finally
            theSession.DeleteUndoMark(markId1, "Display Part")
        End Try
 
    End Sub
 
End Module

Paul Fillion

Check out the PrintBuilder.PaperSize enumeration in the API documentation; there are dozens of paper sizes listed. The one you ultimately use may also depend on your printer settings.

That said, I'd start by trying the .PaperSize.DSheet constant. If that doesn't work, try recording a journal while printing the desired size to see what gets recorded.