Batch print and sort all part drawings

Batch print and sort all part drawings

skyleWX4C5
Contributor Contributor
380 Views
10 Replies
Message 1 of 11

Batch print and sort all part drawings

skyleWX4C5
Contributor
Contributor

Hello all,

I'm having an issue trying to get a working version of a code I came across here on the forums. My coding experience is very limited, but what I'm looking for: put all .dwgs for all .ipts  into an array, display the missing parts before continuing, sort the array by file name or part number, then finally, print all the .dwgs. Currently, the missing parts portion works fine, but I can't get it over the finish line.

skyleWX4C5_0-1744662281041.png

 

Here is my latest attempt to fit snippets I found here:

Sub Main

'---CHECK THAT THE ACTIVE DOCUMENT IS AN ASSEMBLY---
If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
	MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
Exit Sub
End If


'---CREATE LIST OF DWGs & MISSING DWGs---
Dim dwgList As New ArrayList
Dim MissingList As New ArrayList
Dim oAsm As AssemblyDocument = ThisDoc.Document
numFiles = 0
	For Each oRefDoc As Document In oAsm.AllReferencedDocuments
        dwgPathName = Left(oRefDoc.FullDocumentName, Len(oRefDoc.FullDocumentName) -3) & "dwg"
		dwgName = oRefDoc.DisplayName

If (System.IO.File.Exists(dwgPathName)) Then
				numFiles = numFiles + 1
				dwgList.Add(dwgPathName)
			Else

				MissingList.Add(dwgName)

		End If
Next
dwgList.Sort
MissingList.Sort


Dim oText As String 'Added
			
For i As Integer = 0 To MissingList.Count - 1
	If i = 0 Then 
		oText = MissingList(i)
		Else
			oText = oText & vbLf & MissingList(i)
	End If
Next

'---SHOW MISSING DWG LIST---
If oText = "" Then
	Else
RUsure = MessageBox.Show( _
	"No .dwg found for.." _
	& vbLf & "" _
	& vbLf & oText _
	& vbLf & "" _
	& vbLf & "Continue?", "Notice", MessageBoxButtons.YesNo)
If RUsure = vbNo Then
Return
Else
End If
End If


	For Each dwgName In dwgList
		Call PrintDocument(DwgPath)
	
	Next


End Sub

Function PrintDocument(DwgPath As String)

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

	With oPrintMgr
		.Printer = "\\lse-cep-data\Copier Office"
		.AllColorsAsBlack = True
		.ScaleMode = kPrintBestFitScale
		.ColorMode = kPrintDefaultColorMode
		.PrintRange = kPrintAllSheets
		.NumberOfCopies = 1
		.Orientation = kLandscapeOrientation
'		.PaperSize = oPaperSize
		.SubmitPrint
	End With

	oDrawDoc.Close

End Function

Any help appreciated!

0 Likes
Accepted solutions (2)
381 Views
10 Replies
Replies (10)
Message 2 of 11

Stakin
Collaborator
Collaborator
.PaperSize = oPaperSize

should get sheetsize to assign it,or assign it to the size which printer can accept

0 Likes
Message 3 of 11

skyleWX4C5
Contributor
Contributor

Thanks! I uncommented the paper size, but theres still the issue of the error on Line 69: 

Dim oDrawDoc As DrawingDocument = ThisApplication.Documents.Open(DwgPath, OpenVisible)

skyleWX4C5_0-1744725483452.png

 

 

0 Likes
Message 4 of 11

Stakin
Collaborator
Collaborator
	For Each dwgName In dwgList
		Call PrintDocument(Dwgname)
	
	Next
0 Likes
Message 5 of 11

ryan.rittenhouse
Advocate
Advocate

Generally when I see that error for a Document Open it's because the file path is not correct. Try logging the path and make sure that the file it's pointing too has that exact name is in the right place.

 

Logger.Info(DwgPath)
Dim oDrawDoc As DrawingDocument = ThisApplication.Documents.Open(DwgPath, OpenVisible)
If this solved your problem, or answered your question, please click Accept Solution.
0 Likes
Message 6 of 11

skyleWX4C5
Contributor
Contributor

This did help to clear the error, ran the code a couple times just to verify. The biggest issue that I still have is that the overall intention behind putting the files into an array didn't actually work. When run, the code does iterate through all parts and assemblies and prints them all. It does NOT print them in order. At least not by part number. I don't actually see a rhyme or reason to how its printing currently. Heres the updated code: 

Sub Main

'---CHECK THAT THE ACTIVE DOCUMENT IS AN ASSEMBLY---
If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
	MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
Exit Sub
End If


'---CREATE LIST OF DWGs & MISSING DWGs---
Dim dwgList As New ArrayList
Dim MissingList As New ArrayList
Dim oAsm As AssemblyDocument = ThisDoc.Document
numFiles = 0
	For Each oRefDoc As Document In oAsm.AllReferencedDocuments
        dwgPathName = Left(oRefDoc.FullDocumentName, Len(oRefDoc.FullDocumentName) -3) & "dwg"
		dwgName = oRefDoc.DisplayName

If (System.IO.File.Exists(dwgPathName)) Then
				numFiles = numFiles + 1
				dwgList.Add(dwgPathName)
			Else

				MissingList.Add(dwgName)

		End If
Next
dwgList.Sort
MissingList.Sort


Dim oText As String 'Added
			
For i As Integer = 0 To MissingList.Count - 1
	If i = 0 Then 
		oText = MissingList(i)
		Else
			oText = oText & vbLf & MissingList(i)
	End If
Next

'---SHOW MISSING DWG LIST---
If oText = "" Then
	Else
RUsure = MessageBox.Show( _
	"No .dwg found for.." _
	& vbLf & "" _
	& vbLf & oText _
	& vbLf & "" _
	& vbLf & "Continue?", "Notice", MessageBoxButtons.YesNo)
If RUsure = vbNo Then
Return
Else
End If
End If

	For Each dwgName In dwgList
		Call PrintDocument(Dwgname)
	
	Next

End Sub

Function PrintDocument(DwgPath As String)

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

	With oPrintMgr
		.Printer = "\\lse-cep-data\Copier Office"
		.AllColorsAsBlack = True
		.ScaleMode = kPrintBestFitScale
		.ColorMode = kPrintDefaultColorMode
		.PrintRange = kPrintAllSheets
		.NumberOfCopies = 1
		.Orientation = kLandscapeOrientation
		.PaperSize = oPaperSize
		.SubmitPrint
	End With

	oDrawDoc.Close

End Function

Thanks ! 

0 Likes
Message 7 of 11

ryan.rittenhouse
Advocate
Advocate
Accepted solution

If you want them sorted by part number, the off the cuff answer I have is to stuff them in a sorted dictionary with the part number as the key. Then you can iterate through the dictionary and get the files out in order. Something like this:

 

'Create a sorted dictionary to store part number and file name
Dim dwgDictionary As New SortedDictionary(Of String, String) ' {partNumber, fileName}

For Each oRefDoc As Document In oAsm.AllReferencedDocuments
	
	Dim partNumber As String = oRefDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
	dwgPathName = Left(oRefDoc.FullDocumentName, Len(oRefDoc.FullDocumentName) -3) & "dwg"
	
	'Add partnumbers and file names to the dictionary
	dwgDictionary.Add(partNumber, dwgPathName)
	
Next oRefDoc

'Loop through the dictionary to create the prints in order by partnumber
For Each dwg As KeyValuePair(Of String, String) In dwgDictionary
	Call PrintDocument(dwg.Value)
Next dwg
If this solved your problem, or answered your question, please click Accept Solution.
0 Likes
Message 8 of 11

Stakin
Collaborator
Collaborator
	For Each oRefDoc As Document In oAsm.AllReferencedDocuments
        dwgPathName = Left(oRefDoc.FullDocumentName, Len(oRefDoc.FullDocumentName) -3) & "dwg"
		dwgName = oRefDoc.DisplayName

If (System.IO.File.Exists(dwgPathName)) Then
				numFiles = numFiles + 1
				dwgList.Add(dwgPathName)
			Else
				dwgList.Add(dwgPathName)
				MissingList.Add(dwgName)
		End If
Next
dwgList.Sort() Rem Thisline is sort by filename.,if you want to print by partnumber order,you should change dwg file name to part number format.
MissingList.Sort()


Dim oText As String 'Added
oText = String.Join(vbLf, dwgList.ToArray)	
Rem Please check,the dwglist is be ordered by file name or not?If not,should sort it without directory.
Logger.Info(oText)

 

0 Likes
Message 9 of 11

skyleWX4C5
Contributor
Contributor

There it is! Thanks for all your help! This worked like a charm, however there's one more thing I'd like to change that I think should be a pretty easy fix. Hoping to work it into the code--I don't want the assembly files to print, only parts. Heres the updated code with your suggested method:

 

Sub Main

'---CHECK THAT THE ACTIVE DOCUMENT IS AN ASSEMBLY---
If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
	MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
Exit Sub
End If

'---CREATE LIST OF DWGs & MISSING DWGs---
Dim dwgList As New ArrayList
Dim MissingList As New ArrayList
Dim oAsm As AssemblyDocument = ThisDoc.Document
numFiles = 0
Dim dwgDictionary As New SortedDictionary(Of String, String) ' {partNumber, fileName}
	For Each oRefDoc As Document In oAsm.AllReferencedDocuments
        dwgPathName = Left(oRefDoc.FullDocumentName, Len(oRefDoc.FullDocumentName) -3) & "dwg"
		dwgName = oRefDoc.DisplayName

If (System.IO.File.Exists(dwgPathName)) Then
				numFiles = numFiles + 1
				dwgList.Add(dwgPathName)
	'Create a sorted dictionary to store part number and file name
	Dim partNumber As String = oRefDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
	dwgPathName = Left(oRefDoc.FullDocumentName, Len(oRefDoc.FullDocumentName) -3) & "dwg"
	
	'Add partnumbers and file names to the dictionary
	dwgDictionary.Add(partNumber, dwgPathName)
				
			Else

				MissingList.Add(dwgName)

		End If
Next
dwgList.Sort
MissingList.Sort


Dim oText As String 'Added
			
For i As Integer = 0 To MissingList.Count - 1
	If i = 0 Then 
		oText = MissingList(i)
		Else
			oText = oText & vbLf & MissingList(i)
	End If
Next

'---SHOW MISSING DWG LIST---
If oText = "" Then
	Else
RUsure = MessageBox.Show( _
	"No .dwg found for.." _
	& vbLf & "" _
	& vbLf & oText _
	& vbLf & "" _
	& vbLf & "Continue?", "Notice", MessageBoxButtons.YesNo)
If RUsure = vbNo Then
Return
Else
End If
End If


'Loop through the dictionary to create the prints in order by partnumber
For Each dwg As KeyValuePair(Of String, String) In dwgDictionary
	Call PrintDocument(dwg.Value)
Next dwg

End Sub

Function PrintDocument(DwgPath As String)

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

	With oPrintMgr
		.Printer = "\\lse-cep-data\Copier Office"
		.AllColorsAsBlack = True
		.ScaleMode = kPrintBestFitScale
		.ColorMode = kPrintDefaultColorMode
		.PrintRange = kPrintAllSheets
		.NumberOfCopies = 1
		.Orientation = kLandscapeOrientation
		.PaperSize = oPaperSize
		.SubmitPrint
	End With

	oDrawDoc.Close

End Function

Thanks again!

0 Likes
Message 10 of 11

ryan.rittenhouse
Advocate
Advocate
Accepted solution

You can add a check right inside your for loop to check the definition type

 

For Each oRefDoc As Document In oAsm.AllReferencedDocuments
	If Not TypeOf oRefDoc.ComponentDefinition Is PartComponentDefinition Then Continue For
If this solved your problem, or answered your question, please click Accept Solution.
0 Likes
Message 11 of 11

skyleWX4C5
Contributor
Contributor

Perfect! Works great. Thanks!

0 Likes