PDF Export - File Naming

PDF Export - File Naming

s.astorgaA7QVN
Explorer Explorer
251 Views
2 Replies
Message 1 of 3

PDF Export - File Naming

s.astorgaA7QVN
Explorer
Explorer

Hi All,

 

I'm currently working on an iLogic rule to export all sheets within a .dwg file. I've sourced from other posts on this forum to put together the majority of this code, however the issue I'm having now is that the rule no longer exports all sheets in the .dwg - it only exports the last sheet.

 

See code below, any help on this would be greatly appreciated, cheers!

 

Dim oDoc As DrawingDocument
oDoc = ThisApplication.ActiveDocument
Dim oSheet As Sheet
Dim i As Integer = 0
Dim j As Integer = 0

'Creates Array List
Dim SheetNamesList As List(Of String) = New List(Of String)

For Each oSheet In oDoc.Sheets
	If i <= 8 Then
		Dim sString1 As String = Right(oSheet.Name, 2)
		Dim sString2 As String = Split(oSheet.Name,sString1)(0)
		SheetNamesList.Add(sString2)
	End If
	
	If i >= 9 Then
		Dim sString1 As String = Right(oSheet.Name, 3)
		Dim sString2 As String = Split(oSheet.Name,sString1)(0)
		SheetNamesList.Add(sString2)
	End If
	i = i + 1
Next

'Sort By Sheet Name To Export
Dim oString As Object
For Each oString In SheetNamesList
	For Each oSheet In oDoc.Sheets
     	oSheet.ExcludeFromPrinting = True
			If oSheet.Name.Contains(oString) Then
     			oSheet.ExcludeFromPrinting = False
			End If

		Next
	'['Export PDF
	'Save to file location > Exports > PDF folder for any project
	oPath = ThisDoc.Path
	ooFolder = oPath & "\Exports\PDF"
	
	'Check for the PDF folder and create it if it does not exist
	If Not System.IO.Directory.Exists(ooFolder) Then
	    System.IO.Directory.CreateDirectory(ooFolder)
	End If

	oFileName = ThisDoc.FileName(False) 'without extension
	oProject = iProperties.Value("Project", "part number")
	sSheetRev = oSheet.Revision
	oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById _
	("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
	oDocument = ThisApplication.ActiveDocument
	oContext = ThisApplication.TransientObjects.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	oOptions = ThisApplication.TransientObjects.CreateNameValueMap
	oDataMedium = ThisApplication.TransientObjects.CreateDataMedium 

	If oPDFAddIn.HasSaveCopyAsOptions(oDocument, oContext, oOptions) Then
	oOptions.Value("All_Color_AS_Black") = 0
	oOptions.Value("Remove_Line_Weights") = 1
	oOptions.Value("Vector_Resolution") = 600
	oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
	End If 
	
'Split Sheet name to find Sheet title only
	sSplit = Split(oSheet.Name, "-")
	sSheetName = sSplit(1)
	sSplit2 = Split(sSheetName, ":")
	Try : sSheetTitle = sSplit2(0) : Catch : sSheetTitle = oSheet.Title : End Try
		
		
'Split Sheet name to find sheet number only
	sSplit3 = Split(oSheet.Name, "-")
	sSheetNo = sSplit3(0)
	sSplit4 = Split(sSheetNo, ":")
	Try : sSheetNo = sSplit4(0) : Catch : sSheetNo = oSheet.Name : End Try
		
	 'Set the PDF target file name
	oDataMedium.FileName = ooFolder & "\" & oProject & "-" & sSheetNo & "-" & sSheetRev & "-" & sSheetTitle & ".pdf" 

	'Publish document
	oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
	']
	
	j = j + 1
	
Next

oDoc.Sheets(1).Activate

 

0 Likes
252 Views
2 Replies
Replies (2)
Message 2 of 3

Zach.Stauffer
Advocate
Advocate

If you want to print all sheets you can simplify things by changing your pdf options:

 

oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintCurrentSheet

 

 

Then you just iterate through the drawing sheets, activate the current sheet, pdf, and then go to the next sheet.

 

For Each oSheet As Sheet In oDoc.Sheets
		oSheet.Activate
		Dim pdfFilename = path & "\" & oSheet.Name & ".pdf"
		'Set the PDF target file name
		oDataMedium.FileName = pdfFilename
		'Saves the PDF in the desired location
		oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
Next

 

That might not be perfect as far as the filename goes but should get you most of the way there. Doing this you won't need to create the SheetNamesList etc. and can remove that bit.

0 Likes
Message 3 of 3

WCrihfield
Mentor
Mentor

Hi @s.astorgaA7QVN.  At the start of your largest loop, you create a new variable named "oString" as an Object, instead of as a String, even though it must be a String, because it is supposed to represent one of the values in your List(Of String) type variable named "SheetNamesList".  If I were you, I would either completely delete that oSheet variable's declaration line, or change its Type to String, because it is already implied that it will be a String, due to where it is coming from.

I would also move your code for establishing the oPath, ooFolder, oFileName, oProject variables to before that main large loop, because they only need to be established once, not every time it loops.  And I would eliminate your line of code establishing the oDocument variable, and replace it's use in the other locations after that with your original 'oDoc' variable that you created at the start of your code.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes