Export Multi-Sheet DWG To Separate PDFs

Export Multi-Sheet DWG To Separate PDFs

Anonymous
Not applicable
1,780 Views
6 Replies
Message 1 of 7

Export Multi-Sheet DWG To Separate PDFs

Anonymous
Not applicable

Hello Everyone,

 

I am working on revising code I have found online that will take a multi-sheet drawing file and export each sheet to its own PDF using the sheet names as the PDF names. I have found many ways to do this, but we use .dwg drawing files instead of .idw drawing files. Whenever I run the code listed below, I get empty 0 byte files named after the sheet names. Inside the "For Each" command I revised the original code from "idw" to "dwg".  The rule opens any related .dwg files which is great, but instead of creating a PDF at that point, it creates the 0 byte file.

 

I have been working with inventor for just a few months now, so I am hoping that someone can spot something that I am completely missing.

 

Link to code source:

https://forums.autodesk.com/t5/inventor-forum/export-multiple-sheets-to-separate-pdf-s/td-p/8303860

 

'['Sub Variables
oRefDocs = ThisApplication.ActiveDocument.AllReferencedDocuments
Dim oDoc As Document
oFolder = ThisDoc.Path
oPDFLOC = "G:\PDFS\"
oAddIns = ThisApplication.ApplicationAddIns
oTG = ThisApplication.TransientObjects']
'['PDF Export
'['PDF Options
oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
oContext = oTG.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = oTG.CreateNameValueMap
oDataMedium = oTG.CreateDataMedium
oOptions.Value("All_Color_AS_Black") = 1
oOptions.Value("Remove_Line_Weights") = 1
oOptions.Value("Vector_Resolution") = 4800
oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintCurrentSheet']
For Each oDoc In oRefDocs
	dwgPathName = Left(oDoc.FullDocumentName, Len(oDoc.FullDocumentName) -3) & "dwg"   '(I changed from idw to dwg)
	If System.IO.File.Exists(dwgPathName) Then
		oDrawDoc = ThisApplication.Documents.Open(dwgPathName, True)
		For Each oSheet As Sheet In oDrawDoc.Sheets
			oSheet.Activate
			oDataMedium.FileName = oPDFLOC + oSheet.Name + ".pdf"
			oPDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
		Next
		oDrawDoc.Close(True)
	End If
Next']
MessageBox.Show("PDF Export completed.", "PDF Export", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)

Thanks,

 

ndelaughter

0 Likes
Accepted solutions (2)
1,781 Views
6 Replies
Replies (6)
Message 2 of 7

chandra.shekar.g
Autodesk Support
Autodesk Support
Accepted solution

@Anonymous,

 

Actually, code in the below link seems to be work for assembly document. Try below code to export multiple sheet into multiple pdf.

 

https://forums.autodesk.com/t5/inventor-forum/export-multiple-sheets-to-separate-pdf-s/td-p/8303860

oPath = ThisDoc.Path
oFileName = ThisDoc.FileName(False) 'without extension
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

'Define the drawing
Dim oDrawing As DrawingDocument
oDrawing = ThisDoc.Document

Dim oSheet As Sheet
Dim lPos As Long
Dim rPos As Long
Dim sLen As Long
Dim sSheetName As String
Dim iSheetNumber As Integer

'step through each drawing sheet
For Each oSheet In oDrawing.Sheets

	'find the seperator in the sheet name:number
	lPos = InStr(oSheet.Name, ":")
	'find the number of characters in the sheet name
	sLen = Len(oSheet.Name)
	'find the sheet name
	sSheetName = Left(oSheet.Name, lPos -1)
	'find the sheet number
	iSheetNumber = Right(oSheet.Name, sLen -lPos)

	 
	oOptions.Value("All_Color_AS_Black") = 1
	oOptions.Value("Remove_Line_Weights") = 1
	oOptions.Value("Vector_Resolution") = 400
	oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintCurrentSheet 
	 

	'get PDF target folder path
	oFolder = Left(oPath, InStrRev(oPath, "\")) & "PDF"

	'Check for the PDF folder and create it if it does not exist
	If Not System.IO.Directory.Exists(oFolder) Then
	    System.IO.Directory.CreateDirectory(oFolder)
	End If


	'Set the PDF target file name
	oDataMedium.FileName = oFolder & "\" & oFileName & " " & sSheetName & " " & iSheetNumber  & ".pdf"
	MessageBox.Show(oDataMedium.FileName)
	'Publish document
	oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)

Next

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 3 of 7

Anonymous
Not applicable

Thanks for the reply.

 

I ran the code you posted in a DWG with 8 pages and it created a PDF for each sheet named after the sheet name. But when I opened the PDFs, they all display the first sheet image. I believe there is an issue in the code because it is not stepping through each sheet and then creating the PDFs.

0 Likes
Message 4 of 7

chandra.shekar.g
Autodesk Support
Autodesk Support

@Anonymous,

 

Can you please provide non confidential dwg file to test the same?

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 5 of 7

Anonymous
Not applicable

Thanks for the quick response. Here is the DWG. 

FYI I revised the oFolder path to a specific location instead of the drawing location.

 

Thanks,

 

ndelaughter

0 Likes
Message 6 of 7

raith-mb
Advocate
Advocate
Accepted solution

You have to cativate the sheet before exporting:

 

oSheet.activate

'Publish document
oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)

 

In your Code the active sheet is the same all time the Export will be donne.

 

Roland

Message 7 of 7

Anonymous
Not applicable

Thank you so much @raith-mb  and @chandra.shekar.g! It works!

0 Likes