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!

Batch Print Code Help 1

Status
Not open for further replies.

Kenja824

Automotive
Nov 5, 2014
949
I found the below code for batch printing.

The only problem seems to be that it will only plot everything on 11x17 as it is set to Tabloid. Which is good for all of our drawings except for the BOM sheets. We print our BOMs on 8.5 x 11.

Worst case scenario is we toss a few sheets out each time we batch print. If possible I would rather change it to one of two options. If one of the following options is not too difficult of a change, could someone please fix this?

1) It would automatically change and print the BOMs on the letter size paper.

If that cant be done too easily, then possibly this...

2) It skips the BOMs in the printing process entirely. It is possible to have a dozen or more Bom sheets in a file, though its rare.
image_h4afcc.png



Code:
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
		'printBuilder1.Paper = PrintBuilder.PaperSize.11X17
		printBuilder1.Paper = PrintBuilder.PaperSize.Tabloid
 
        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
 
Replies continue below

Recommended for you

Try the code below; we'll need to adjust it if you use custom sheet sizes in NX. Might also need to adjust the orientation for the BOM sheets. I uncommented the code that reports the drawing sheet size and part name; once the code is tweaked to your preferences, those lines can be commented out.

Code:
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)
                    lw.WriteLine("  sheet width: " & tempSheet.Length.ToString)
                    lw.WriteLine("  sheet height: " & tempSheet.Height.ToString)
                    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

        If theSheet.Height < 9 Then
            'letter size
            printBuilder1.Paper = PrintBuilder.PaperSize.Letter
        Else
            'assume 11x17
            'printBuilder1.Paper = PrintBuilder.PaperSize.Inch11x17
            printBuilder1.Paper = PrintBuilder.PaperSize.Tabloid
        End If

        '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 Function GetUnloadOption(ByVal dummy As String) As Integer

        'Unloads the image when the NX session terminates
        'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination

        '----Other unload options-------
        'Unloads the image immediately after execution within NX
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately

        'Unloads the image explicitly, via an unload dialog
        'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Explicitly
        '-------------------------------

    End Function

End Module

www.nxjournaling.com
 
Thanks cowski

I am getting a small error that by now I should know how to fix because I have seen it so much in the past. lol This is how little I have taught myself. lol

Lines 35 and 50 are getting errors saying "ChangeDisplayPart" is not declared.
 
I tried just commenting those two lines out as I couldnt seem to connect them with anything else. It gave me an original error but I ddint see what lines. I couldnt find the small window box with the errors listed. So I ran it a second time and it didnt give me any error that time.

It ran prints of the right sheets on the right size. However, it only ran the prints from the work part.

The original code cycled through all open parts (or all parts in the navigator or something) and printed off any drawing sheets it found in components.

Which I am guessing is why it needs those lines I commented out? lol
 
OK

I uncommented those lines so it changed display to other components.

I got an error on line 100 and 43 then. I noticed in the original code I had to comment out line 100 to get it to run. So I commented that line out again.

It ran all of the details and did the job exactly as I wanted. Awesome.

The only problem I found is I had some blank sheets printed out first. However, I have no idea if I printed those or someone else. lol So I will need to test that as I had it happen once earlier too.


I have no idea why I had to comment out line 100 to get it to work. But it worked. lol

So I am guessing I can just compare it to the previous code to comment out the LW lines so it doesnt write to the info window amd I am set.


Thankyou very much cowski.
 
ok, Found a small issue. If a component that is purchased has a drafting sheet but nothing on it, it will still print that blank page.

My guess is this is something that wont be fixed using journal. It may need a more sophisticated script or something maybe?

If it can be fixed in Journal, please help. If not then I will just keep updating the Library and get rid of drafting sheets as they come up. lol

Here is my working code right now....

Code:
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)
                    lw.WriteLine("  sheet width: " & tempSheet.Length.ToString)
                    lw.WriteLine("  sheet height: " & tempSheet.Height.ToString)
                    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

        If theSheet.Height < 9 Then
            'letter size
            printBuilder1.Paper = PrintBuilder.PaperSize.Letter
        Else
            'assume 11x17
            'printBuilder1.Paper = PrintBuilder.PaperSize.Inch11x17
            printBuilder1.Paper = PrintBuilder.PaperSize.Tabloid
        End If

        '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 Function GetUnloadOption(ByVal dummy As String) As Integer

        'Unloads the image when the NX session terminates
        'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination

        '----Other unload options-------
        'Unloads the image immediately after execution within NX
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately

        'Unloads the image explicitly, via an unload dialog
        'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Explicitly
        '-------------------------------

    End Function

	
	
	
	    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
 
When copying and pasting, I accidentally deleted the "ChangeDisplayPart" subroutine. Sorry about that; glad to see you figured it out and put it back in.

www.nxjournaling.com
 
Cowski
I dont understand code to save my life but I can still compare codes with each other for differences. lol

Anyone
I had mentioned I was looking to stop it from printing blank pages. I am unsure if this is possible with journal code or if it takes something more sophisticated to recognize whether pages are blank or not.

I found our printer preferences has a box we can check to not print blank pages. Worst case scenario I may just need to make everyone check that box in their default preferences. On the chance the code change would be simple, that would be preferable.

I tried recording a journal of going into those preferences as I printed, hoping to see a journal line I could add. Unfortunately the journal did not record anything within the printers preferences.
 
(SIGH) Just a warning to anyone who sees this. There are 2 major problems with this code.

1) As already mentioned, it prints blank pages from any component opened that has an empty drafting sheet. Which in our case, I am learning are quite a few of the purchased components.

2) It seems to be printing any drawings that are even open. Not just the details connected to the main tool you have on display. So if I worked on a main tool with several units earlier but switched over to a small pin unit for a different tool and printed that, it would print that small pin tool and its components and the large tool I still have opened and all of its units and details. lol A lot of wasted paper if you arent careful.
 
Try this version, it should skip blank sheets.

The code (as the original) works on all open parts. If you want to limit the scope to certain parts, more work will be required.

Code:
Option Strict Off
Imports System
Imports System.Windows.Forms
Imports NXOpen
Imports NXOpen.UF

Module Module1

    Dim theSession As Session = Session.GetSession()
    Dim theUfSession As UFSession = UFSession.GetUFSession
    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)
                    lw.WriteLine("  sheet width: " & tempSheet.Length.ToString)
                    lw.WriteLine("  sheet height: " & tempSheet.Height.ToString)
                    tempPart.DraftingViews.UpdateViews(Drawings.DraftingViewCollection.ViewUpdateOption.OutOfDate, tempSheet)

                    If Not IsSheetEmpty(tempSheet) Then
                        PrintSheet(tempPart, tempSheet)
                    End If

                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

        If theSheet.Height < 9 Then
            'letter size
            printBuilder1.Paper = PrintBuilder.PaperSize.Letter
        Else
            'assume 11x17
            'printBuilder1.Paper = PrintBuilder.PaperSize.Inch11x17
            printBuilder1.Paper = PrintBuilder.PaperSize.Tabloid
        End If

        '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

    Private Function IsSheetEmpty(ByVal theSheet As Drawings.DrawingSheet) As Boolean

        theSheet.Open()
        Dim sheetTag As NXOpen.Tag = theSheet.View.Tag
        Dim sheetObj As NXOpen.Tag = NXOpen.Tag.Null
        theUfSession.View.CycleObjects(sheetTag, UFView.CycleObjectsEnum.VisibleObjects, sheetObj)
        If (sheetObj = NXOpen.Tag.Null) And (theSheet.GetDraftingViews.Length = 0) Then
            Return True
        End If

        Return False

    End Function

    Public Function GetUnloadOption(ByVal dummy As String) As Integer

        'Unloads the image when the NX session terminates
        'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination

        '----Other unload options-------
        'Unloads the image immediately after execution within NX
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately

        'Unloads the image explicitly, via an unload dialog
        'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Explicitly
        '-------------------------------

    End Function

End Module

www.nxjournaling.com
 
Thanks cowski

Sorry I havent replied sooner. Im still recuperating from that stupid Monday Night Football game. [hairpull3]

I understand what you are saying about it taking more work.

The one we had from another customer which we cannot use for new customers listed all the drawings and we selected all of the files we wanted to print. I would expect that would take a lot more work. lol

As it is, it is workable. People will need to just be careful to make sure they dont have other tools open still. The only reason that is worrisome is because we have what are called support files. These have the entire tool with all of the units in them as well as weldgun files and product files. Often they will open that file and make the unit they are working on as the display part.

To counter this, I would ask if it would be difficult to just make a pop-up warning to remind them to close any tools they are not printing and give them a choice to hit OK or CANCEL for the print. If nothing else, this is something I can work toward when I have more free time. lol

Either way, with the blank sheets fixed, this is at least workable and I truly appreciate the help you have given.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor