Print all part drawings without printing assemblies

Print all part drawings without printing assemblies

skyleWX4C5
Contributor Contributor
467 Views
15 Replies
Message 1 of 16

Print all part drawings without printing assemblies

skyleWX4C5
Contributor
Contributor

Hello, new to a lot of this, and trying to expedite some of my common mundane processes. In this code, the end goal is to get a working rule to allow me to print each individual part that already had an associated part .dwg, without printing the assemblies. Later on, the goal is to set up a form to print the options of: Print all part .dwg's on letter paper, and print .iam's on ledger paper. Attached is the current code, it errors out on the attempt to exclude line. Any help appreciated!

 

 

'Check that the active document is an assembly file
If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
Exit Sub
End If

'Define the active document as an assembly file
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument
oAsmName = Left(oAsmDoc.DisplayName, Len(oAsmDoc.DisplayName) -4)

'get user input
userConfirm = MessageBox.Show ( _
"This will print the dwg for all of the assembly components that have" _
& vbLf & "drawings files. This rule expects that the drawing file shares the same" _
& vbLf & " name and location as the component." _
& vbLf & "" _
& vbLf & "Make sure the file extensions are set to Visible in Windows Explorer." _
& vbLf & "" _
& vbLf & "This could take a while.  Are you sure you want to do this?" _
& vbLf & "" _
& vbLf & "", "iLogic  - Batch Output dwgs ",MessageBoxButtons.YesNo)

If userConfirm = vbNo Then
Return
Else
End If

'- - - - - - - - - - - - -Print setup - - - - - - - - - - - -

    If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
        MsgBox ("This is NOT an assembly document!")
        Exit Sub
    End If

    Dim oPrintMgr As PrintManager
    oPrintMgr = ThisApplication.ActiveDocument.PrintManager
    Dim oRefDocs As DocumentsEnumerator
    oRefDocs = oAsmDoc.AllReferencedDocuments 
    Dim oRefDoc As Document
    numFiles = 0
	

'- - - - - - - -- - -Component Drawings - - - - - - - - 
For Each oRefDoc In oRefDocs
			oFileType = Left(oRefDoc.FullDocumentName, Len(oRefDoc.FullDocumentName))
			
		If oFileType.Contains("iam") 
			Component.InventorComponent(oFileType).ExcludeFromPrinting = True
			
		Else If oFileType.Contains("ipt")
			Component.InventorComponent(oFileType).ExcludeFromPrinting = False
		End If
		
		partDrawings = Left(oRefDoc.FullDocumentName, Len(oRefDoc.FullDocumentName) - 3) & "dwg"
		
		If (System.IO.File.Exists(partDrawings)) Then
            numFiles = numFiles + 1
			
        Dim oDrawDoc As DrawingDocument
            oDrawDoc = ThisApplication.Documents.Open(partDrawings, True)
            oDrawDoc.Activate
            oDrawDoc = ThisApplication.ActiveDocument
            oDrgPrintMgr = oDrawDoc.PrintManager
			oDrgPrintMgr.AllColorsAsBlack = True
            oDrgPrintMgr.ScaleMode = kPrintBestFitScale
            oPrintMgr = ThisApplication.ActiveDocument.PrintManager

            ' Printer setup, default printer
            'oPrintMgr.ColorMode = kPrintDefaultColorMode ' Set to default. Uses printer settings
            oPrintMgr.PrintRange = kPrintAllSheets 'Prints all sheets in the dwg.
            oPrintMgr.NumberOfCopies = 1 ' Set to print one copies.
            oPrintMgr.Orientation = kLandscapeOrientation ' Set to print using landscape orientation.
            oPrintMgr.PaperSize = kPaperSizeLetter 'Set the paper size.
            oPrintMgr.SubmitPrint ' Submit the print.
            oDrawDoc.Close (True)
        End If
        
 Next
MessageBox.Show ("There were " & numFiles & " files sent to the printer."," Job Complete ")
0 Likes
Accepted solutions (1)
468 Views
15 Replies
Replies (15)
Message 2 of 16

C_Haines_ENG
Collaborator
Collaborator

So your final goal is to have an assembly open, cycle through all parts in that assembly and print their drawing on letter paper, and then print the assembly drawing on legal paper? Your code is quite complicated for what it seems like you're going for. 

0 Likes
Message 3 of 16

skyleWX4C5
Contributor
Contributor

It does seem a bit complicated for what I'm looking to do. However, the end goal would be to print all of the subassembly dwgs on legal paper, not just the top level. 

0 Likes
Message 4 of 16

C_Haines_ENG
Collaborator
Collaborator

This code will print all parts in the open assembly document, including parts in sub assemblies. It is searching for parts in the exact same location of the part and assembly file. It will print the parts in letter size and the assembly on legal.

 

Sub Main

	Dim oAsm As AssemblyDocument = ThisDoc.Document
	Dim DwgPath As String
	Dim oDrawDoc As DrawingDocument
	Dim oPrintMgr As PrintManager
	Dim OpenVisible As Boolean = True

	'[ THIS SECTION PRINTS ALL PARTS IN THE ASSEMBLY, INCLUDING SUB ASSEMBLIES
	For Each oComp As ComponentOccurrence In oAsm.ComponentDefinition.Occurrences.AllLeafOccurrences

		If oComp.Definition.Document.DocumentType = kPartDocumentObject

			DwgPath = Replace(oComp.Definition.Document.FullFileName, ".ipt", ".dwg")

			If System.IO.File.Exists(DwgPath)

				oDrawDoc = ThisApplication.Documents.Open(DwgPath, OpenVisible)
				oPrintMgr = PrintDocument(oDrawDoc, kPaperSizeLetter)
				oDrawDoc.Close(True)

			End If


		End If

	Next
	']

	'[ THIS SECTION PRINTS THE MAIN ASSEMBLY DRAWING
	DwgPath = Replace(oAsm.ComponentDefinition.Document.FullFileName, ".iam", ".dwg")
	If System.IO.File.Exists(DwgPath)

		oDrawDoc = ThisApplication.Documents.Open(DwgPath, OpenVisible)
		oPrintMgr = PrintDocument(oDrawDoc, kPaperSizeLegal)
		oDrawDoc.Close(True)

	End If
	']

End Sub

Function PrintDocument(oDrawDoc As DrawingDocument, oPaperSize As PaperSizeEnum)

	Dim oPrintMgr As PrintManager = oDrawDoc.PrintManager

	With oPrintMgr
		.Printer = "PRINTER ADDRESS GOES HERE"
		.AllColorsAsBlack = True
		.ScaleMode = kPrintBestFitScale
		.ColorMode = kPrintDefaultColorMode
		.PrintRange = kPrintAllSheets
		.NumberOfCopies = 1
		.Orientation = kLandscapeOrientation
		.PaperSize = oPaperSize
		.SubmitPrint
	End With

End Function
0 Likes
Message 5 of 16

skyleWX4C5
Contributor
Contributor

I commend you on cleaning up the code! Unfortunately, it did not perform as hoped. It did cycle through and print/close all part drawings, but didn't do anything with the assembly/sub-assemblies.

0 Likes
Message 6 of 16

C_Haines_ENG
Collaborator
Collaborator

Can you describe the exact scenario you have in more detail? What things do you want printed? Where are these things located?

If it didn't do anything with the assembly, its because the drawing is not in the same file location as the assembly or is named differently. As for sub-assemblies I hadn't programmed it to print them. Please describe your desired outcome in detail. 

0 Likes
Message 7 of 16

skyleWX4C5
Contributor
Contributor

Sure thing, I'll provide as much information as possible. What I am looking to be printed: All part drawings to print on letter size paper (Their .dwg resides in the same folder as the ipt). Along with that, I'm trying to get all of the subassemblies to print on ledger size paper (Their .dwg also resides in the same folder as the .iam). I'm also hoping to separate this into two options on a form. That part should be pretty easy if I could manage to get the code right. Thanks for the help!

0 Likes
Message 8 of 16

C_Haines_ENG
Collaborator
Collaborator

Try this. It will give you a message box at the end of what drawings couldn't be found. 

 

Sub Main

	Dim oAsm As AssemblyDocument = ThisDoc.Document
	Dim DwgPath As String
	Dim oDrawDoc As DrawingDocument
	Dim oPrintMgr As PrintManager
	Dim OpenVisible As Boolean = True
	Dim oCantFind As String = "COULDNT FIND THE FOLLOWING DRAWINGS: " & vbLf & vbLf
	Dim oPaperSize As PaperSizeEnum

	Dim oOption As Boolean = InputRadioBox("Select Items to Print", "Parts Only", "Assemblies and Parts", True, "Print Options")

	'[ THIS SECTION PRINTS ALL PARTS IN THE ASSEMBLY, INCLUDING SUB ASSEMBLIES
	For Each oComp As ComponentOccurrence In oAsm.ComponentDefinition.Occurrences.AllLeafOccurrences

		Select Case oComp.Definition.Document.DocumentType
			Case kPartDocumentObject
				DwgPath = Replace(oComp.Definition.Document.FullFileName, ".ipt", ".dwg")
				oPaperSize = kPaperSizeLetter
			Case kAssemblyDocumentObject 
				If oOption = False Then Continue For 
				DwgPath = Replace(oComp.Definition.Document.FullFileName, ".iam", ".dwg")
				oPaperSize = kPaperSizeLegal
		End Select

		If System.IO.File.Exists(DwgPath)
			oDrawDoc = ThisApplication.Documents.Open(DwgPath, OpenVisible)
			oPrintMgr = PrintDocument(oDrawDoc, kPaperSizeLetter)
			oDrawDoc.Close(True)
		Else
			oCantFind = oCantFind & "PART: " & DwgPath & vbLf
		End If

	Next
	']

	'[ THIS SECTION PRINTS THE MAIN ASSEMBLY DRAWING
	If oOption = False
		DwgPath = Replace(oAsm.ComponentDefinition.Document.FullFileName, ".iam", ".dwg")
		If System.IO.File.Exists(DwgPath)
			oDrawDoc = ThisApplication.Documents.Open(DwgPath, OpenVisible)
			oPrintMgr = PrintDocument(oDrawDoc, oPaperSize)
			oDrawDoc.Close(True)
		Else
			oCantFind = oCantFind & "ASSEM: " & DwgPath & vbLf
		End If
	End If
	']

	MsgBox(oCantFind)

End Sub

Function PrintDocument(oDrawDoc As DrawingDocument, oPaperSize As PaperSizeEnum)

	Dim oPrintMgr As PrintManager = oDrawDoc.PrintManager

	With oPrintMgr
		.Printer = "PRINTER ADDRESS GOES HERE"
		.AllColorsAsBlack = True
		.ScaleMode = kPrintBestFitScale
		.ColorMode = kPrintDefaultColorMode
		.PrintRange = kPrintAllSheets
		.NumberOfCopies = 1
		.Orientation = kLandscapeOrientation
		.PaperSize = oPaperSize
		.SubmitPrint
	End With

End Function

 

 

0 Likes
Message 9 of 16

skyleWX4C5
Contributor
Contributor

Edit: I entered the wrong printer address briefly after responding, and checked again, that code was much more successful. It works as intended! Would it be possible to add a third option in the dialogue specifically for "assemblies only"? Thanks again!

0 Likes
Message 10 of 16

C_Haines_ENG
Collaborator
Collaborator

You can't use a RadioMessageBox for that as its limited to two, so you'll have to use InputListBoxes. They don't look as nice but they work! Be sure to mark the solution as accepted!

 

Sub Main

	Dim oAsm As AssemblyDocument = ThisDoc.Document
	Dim DwgPath As String
	Dim oDrawDoc As DrawingDocument
	Dim OpenVisible As Boolean = True
	Dim oCantFind As String = "COULDNT FIND THE FOLLOWING DRAWINGS: " & vbLf & vbLf
	Dim oPaperSize As PaperSizeEnum

	Dim oOptions As New List(Of String)
	oOptions.AddRange({"Parts Only", "", "Both", "", "Assemblies Only" })

	oOption = InputListBox("Select Print Option", oOptions, "Both", "Assembly Printer", "")
	If oOption = "" Then Exit Sub

	'[ THIS SECTION PRINTS ALL PARTS IN THE ASSEMBLY, INCLUDING SUB ASSEMBLIES
	For Each oComp As ComponentOccurrence In oAsm.ComponentDefinition.Occurrences.AllLeafOccurrences

		Select Case oComp.Definition.Document.DocumentType
			Case kPartDocumentObject And oOption <> "Assemblies Only"
				DwgPath = Replace(oComp.Definition.Document.FullFileName, ".ipt", ".dwg")
				oPaperSize = kPaperSizeLetter
			Case kAssemblyDocumentObject And oOption <> "Parts Only"
				If oOption = False Then Continue For
				DwgPath = Replace(oComp.Definition.Document.FullFileName, ".iam", ".dwg")
				oPaperSize = kPaperSizeLegal
		End Select

		If System.IO.File.Exists(DwgPath)
			oDrawDoc = ThisApplication.Documents.Open(DwgPath, OpenVisible)
			Call PrintDocument(oDrawDoc, oPaperSize)
			oDrawDoc.Close(True)
		Else
			oCantFind = oCantFind & DwgPath & vbLf
		End If

	Next
	']

	'[ THIS SECTION PRINTS THE MAIN ASSEMBLY DRAWING
	If oOption <> "Parts Only"
		DwgPath = Replace(oAsm.ComponentDefinition.Document.FullFileName, ".iam", ".dwg")
		If System.IO.File.Exists(DwgPath)
			oDrawDoc = ThisApplication.Documents.Open(DwgPath, OpenVisible)
			Call PrintDocument(oDrawDoc, oPaperSize)
			oDrawDoc.Close(True)
		Else
			oCantFind = oCantFind & "MAIN ASSEM: " & DwgPath & vbLf
		End If
	End If
	']

	MsgBox(oCantFind)

End Sub

Function PrintDocument(oDrawDoc As DrawingDocument, oPaperSize As PaperSizeEnum)

	Dim oPrintMgr As PrintManager = oDrawDoc.PrintManager

	With oPrintMgr
		.Printer = "PRINTER ADDRESS GOES HERE"
		.AllColorsAsBlack = True
		.ScaleMode = kPrintBestFitScale
		.ColorMode = kPrintDefaultColorMode
		.PrintRange = kPrintAllSheets
		.NumberOfCopies = 1
		.Orientation = kLandscapeOrientation
		.PaperSize = oPaperSize
		.SubmitPrint
	End With

End Function

 

 

 

0 Likes
Message 11 of 16

tim11_manhhieu
Advocate
Advocate

I just passed by but found  C_Haines_ENG to be a very enthusiastic person.

I wish I had questions too.

Message 12 of 16

skyleWX4C5
Contributor
Contributor

I wanted to try this again today and make sure that its functional, turns out the assembly I had it in yesterday didn't have any subassemblies in it. My apologies. The current code is almost entirely there, though there are some issues. It doesn't print the subassemblies. It does print the top level assembly, but it seems to not pick up the other subassemblies. Also, its printing one of each part. What I'm aiming for is one of each unique part. As is, the parts that appear multiple times in the assemblies, prints one for each appearance of the part. You've been very helpful thus far!

0 Likes
Message 13 of 16

C_Haines_ENG
Collaborator
Collaborator
Accepted solution

Yeah I figured out where I goofed. Going through every single component in the assembly didn't make any sense. I am now accessing all ReferencedDocuments, which will grab each unique document in the top level assembly. This next code should work much better, and is much simpler!:

Sub Main

	Dim oAsm As AssemblyDocument = ThisDoc.Document
	Dim DwgPath As String, CantFind As String = "DRAWINGS COULDNT BE FOUND:" & vbLf & vbLf 

	Dim oDocs2Print As New Dictionary(Of String, PaperSizeEnum)

	Dim oOption As String = InputListBox("Select Print Option", {"Parts Only", "", "Both", "", "Assemblies Only" }, "Both", "Assembly Printer", "")
	If oOption = "" Then Exit Sub

	For Each oRefDoc As Document In oAsm.AllReferencedDocuments

		Select Case oRefDoc.DocumentType
			Case kPartDocumentObject And oOption <> "Assemblies Only"
				DwgPath = Replace(oRefDoc.FullFileName, ".ipt", ".dwg")
				oDocs2Print.Add(DwgPath, kPaperSizeLetter)
			Case kAssemblyDocumentObject And oOption <> "Parts Only"
				DwgPath = Replace(oRefDoc.FullFileName, ".iam", ".dwg")
				oDocs2Print.Add(DwgPath, kPaperSizeLegal)
		End Select

	Next

	If oOption <> "Parts Only"
		DwgPath = Replace(oAsm.ComponentDefinition.Document.FullFileName, ".iam", ".dwg")
		oDocs2Print.Add(DwgPath, kPaperSizeLegal)
	End If

	For Each DwgPath In oDocs2Print.Keys
		If System.IO.File.Exists(DwgPath)
			Call PrintDocument(DwgPath, oDocs2Print(DwgPath))
		Else
			CantFind = CantFind & System.IO.Path.GetFileName(DwgPath) & vbLf
		End If
	Next
	
	MsgBox(CantFind)

End Sub

Function PrintDocument(DwgPath As String, oPaperSize As PaperSizeEnum)

	Dim OpenVisible As Boolean = True
	Dim oDrawDoc As DrawingDocument = ThisApplication.Documents.Open(DwgPath, OpenVisible)
	Dim oPrintMgr As PrintManager = oDrawDoc.PrintManager

	With oPrintMgr
		.Printer = "PRINTER ADDRESS GOES HERE"
		.AllColorsAsBlack = True
		.ScaleMode = kPrintBestFitScale
		.ColorMode = kPrintDefaultColorMode
		.PrintRange = kPrintAllSheets
		.NumberOfCopies = 1
		.Orientation = kLandscapeOrientation
		.PaperSize = oPaperSize
		.SubmitPrint
	End With

	oDrawDoc.Close

End Function

 

Message 14 of 16

skyleWX4C5
Contributor
Contributor

Awesome job! Theres a couple things I want to work out, but as far as functionality, it worked great! It would be incredibly helpful if I could get them to print with some sort of order. Preferred order would be ordered by part number starting at the lowest and continuing from there. The other thing is it prints fine on legal paper, as well as letter, but when attempting to print on ledger or 11x17, it will just go by the default. This however, I think has more so to do with my local system/printer settings. Regardless, I can live with this working version! Thanks!

0 Likes
Message 15 of 16

C_Haines_ENG
Collaborator
Collaborator

Can you describe the specific order you're going for? I understand the part numbers, but what about assemblies? Should they be treated the same or should assemblies and parts be grouped? Should parts of a sub assembly print under their sub assembly?

0 Likes
Message 16 of 16

skyleWX4C5
Contributor
Contributor

Actually yeah, that seems best for what I'm trying to do. Best order would be parts of a sub assembly would print under their sub assembly. Also, I was considering adding a very small wait period in between prints (maybe 1 second?) it seems that after 20 or prints, my pc can no longer keep up and it starts to leave the drawings open.

0 Likes