×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Are you an
Engineering professional?
Join Eng-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# Batch Print Code Help

## Batch Print Code Help

(OP)
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.

#### 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

'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 status1 As PartCollection.SdpsStatus
status1 = theSession.Parts.SetDisplay(myPart, False, False, partLoadStatus1)

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 

### RE: Batch Print Code Help

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

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

'Unloads the image immediately after execution within NX

'-------------------------------

End Function

End Module 

### RE: Batch Print Code Help

(OP)
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.

### RE: Batch Print Code Help

(OP)
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

### RE: Batch Print Code Help

(OP)
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.

### RE: Batch Print Code Help

(OP)
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

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

'Unloads the image immediately after execution within NX

'-------------------------------

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 status1 As PartCollection.SdpsStatus
status1 = theSession.Parts.SetDisplay(myPart, False, False, partLoadStatus1)

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 

### RE: Batch Print Code Help

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.

### RE: Batch Print Code Help

(OP)
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.

### RE: Batch Print Code Help

(OP)
(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.

### RE: Batch Print Code Help

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

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 status1 As PartCollection.SdpsStatus
status1 = theSession.Parts.SetDisplay(myPart, False, False, partLoadStatus1)

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

'Unloads the image immediately after execution within NX

'-------------------------------

End Function

End Module 

www.nxjournaling.com

### RE: Batch Print Code Help

(OP)
Thanks cowski

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

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.

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

#### Resources

White Paper - How ESI is Helping Move New Medical Device Product to Market Quicker & More Cost Effic
Early Supplier Involvement has long been a strategy employed by manufacturers to produce innovative products. Now, it almost seems like a necessity. Because decisions made in the design phase can positively affect product quality and costs, this can help add value to OEM bottom lines. This white paper will discuss many facets of ESI, including why itâ€™s so valuable today, what challenges limit the benefits of ESI, how cost is impacted, and more. Download Now
White Paper - Moving to a Driverless Future
This white paper describes what we see as the best practices to support a sustainable engineering process for autonomous vehicle design. It exposes how to use simulation and testing in common frameworks to enable design exploration, verification and validation for the development of autonomous cars at a system, software and full-vehicle level to drive a mature product development process for automated driving. Download Now
Research Report - How Engineers are Using Remote Access
Remote access enables engineers to work from anywhere provided they have an internet connection. We surveyed our audience of engineers, designers and product managers to learn how they use remote access within their organizations. We wanted to know which industries have adopted remote access, which software they are using, and what features matter most. Download Now

Close Box

# Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!