Save multisheet as multiple .pdf

Save multisheet as multiple .pdf

JoãoASilva
Advocate Advocate
1,755 Views
7 Replies
Message 1 of 8

Save multisheet as multiple .pdf

JoãoASilva
Advocate
Advocate

 

Hello all!

 

Upon thoroughly searching online for a direct solution and finding nothing, I started combining lines of code to meet my goals.

I want to save a multisheet drawing to PDF, but each sheet separately and with its own name.

I already have a code thats renames the sheet to match the object in its first view and places the revision as well (Eg.: Solid with the name "Part1" is the first view in the drawing and has revision "0", so the sheet renames from "Sheet1" to "Part1.0").

 

The destination folder of the .pdf is the same as the drawing document;

The line of code should be able to save all sheets in one go, without needing to run it individually in each sheet;

 

Sub Main
    If TypeOf ThisDoc.Document Is DrawingDocument Then
        Dim dwgDoc As DrawingDocument = ThisDoc.Document
        For Each dwgSheet As Sheet In dwgDoc.Sheets
            If dwgSheet.DrawingViews.Count > 0 Then
                modelFile = dwgSheet.DrawingViews(1).ReferencedDocumentDescriptor.FullDocumentName
                modelDoc = dwgSheet.DrawingViews(1).ReferencedDocumentDescriptor.ReferencedDocument
				'get the part number of the part in view
				prtNumber = modelDoc.PropertySets("{32853F0F-3444-11D1-9E93-0060B03C1CA6}").ItemByPropId(5).Value
				'get the part revision number
				Rev = modelDoc.PropertySets.Item("Summary Information").Item("Revision Number").Value
                
				oPath = ThisDoc.Path
				oFileName = prtNumber
				oRevNum = Rev
				oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById _
				("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
				oDocument = modelDoc
				oContext = ThisApplication.TransientObjects.CreateTranslationContext
				oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
				oOptions = ThisApplication.TransientObjects.CreateNameValueMap
				oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
					
					If oPDFAddIn.HasSaveCopyAsOptions(oDataMedium, oContext, oOptions) Then
					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
					'oOptions.Value("Custom_Begin_Sheet") = 2
					'oOptions.Value("Custom_End_Sheet") = 4
					End If

				'get PDF target folder path
				oFolder = oPath

				 'Set the PDF target file name
				oDataMedium.FileName = oFolder & "\" & oFileName & "." & oRevNum & ".pdf"

				'Publish document
				oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
            End If
        Next
    End If
End Sub

I get this error when I run it:

Screenshot_1.png

 

Any ideas how to make this work?

 

João Silva

Mechanical Engineer

 

0 Likes
1,756 Views
7 Replies
Replies (7)
Message 2 of 8

JoãoASilva
Advocate
Advocate

I removed a line of code and it worked, but not as intended:

If oPDFAddIn.HasSaveCopyAsOptions(oDataMedium, oContext, oOptions) Then

Apparently, this line doesn't work on Inventor 2018 and forward.

 

Even though it saved multiple files, it saved them in a weird way i never saw before.

PDF in attached files.

Any ideas why its happening?

João Silva

Mechanical Engineer

 

0 Likes
Message 3 of 8

chandra.shekar.g
Autodesk Support
Autodesk Support

@JoãoASilva,

 

Are you trying to print model document or drawing document?

 

In the previous code, Code for publishing drawing to PDF is used to print model document.

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 4 of 8

JoãoASilva
Advocate
Advocate

I found another way of doing what I wanted:

'['Sub Variables
oRefDocs = ThisApplication.ActiveDocument.AllReferencedDocuments
Dim oDoc As Document
oFolder = ThisDoc.Path
oAddIns = ThisApplication.ApplicationAddIns
oTG = ThisApplication.TransientObjects']
'['PDF Export
'['PDF Options
PDFAddIn = oAddIns.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
	idwPathName = Left(oDoc.FullDocumentName, Len(oDoc.FullDocumentName) -3) & "idw"
	If System.IO.File.Exists(idwPathName) Then
		oDrawDoc = ThisApplication.Documents.Open(idwPathName, True)
		For Each oSheet As Sheet In oDrawDoc.Sheets
			oSheet.Activate
			oSheetName = oSheet.Name.Split(":")(0)
			oDataMedium.FileName = oFolder & "\" & oSheetName & ".pdf"
			Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
		Next
	End If
Next']

MessageBox.Show("PDF Export completed.", "PDF Export", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)

Is it possible to incorporate in this rule a way of filtering, sheet by sheet, by distinguishing if the model in view is part or  sheetmetal?

And then, if it is a part, it opens the 3D file and saves as .step;

If sheetmetal, it saves the sheet in .dxf.

 

I should also mention that I have a rule that renames all sheets according to the model in view.

João Silva

Mechanical Engineer

 

Message 5 of 8

JoãoASilva
Advocate
Advocate

@chandra.shekar.g wrote:

@JoãoASilva,

 

Are you trying to print model document or drawing document?

 

In the previous code, Code for publishing drawing to PDF is used to print model document.

 

Thanks and regards,


I'm trying to print each sheet in drawing document.

João Silva

Mechanical Engineer

 

0 Likes
Message 6 of 8

chandra.shekar.g
Autodesk Support
Autodesk Support

@JoãoASilva,

 

Hoping that below forum discussion link would help to print all sheets of drawing document.

 

https://forums.autodesk.com/t5/inventor-forum/vba-to-print-quot-all-sheets-quot/td-p/3759036

 

Also, you can also print to pdf using VBA sample(PDF printer need to mention in code) in below link.

 

http://help.autodesk.com/view/INVNTOR/2018/ENU/?guid=GUID-4B13A1E3-9572-4096-99B3-B9F89890891E

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 7 of 8

JoãoASilva
Advocate
Advocate

Thank you for your answer, but maybe I wasn't clear enough. Smiley Indifferent

 

The idea was, using iLogic rules, to save a multisheet drawing to .pdf, but each sheet of the drawing into a single unique pdf document. (E.g.: Multisheet idw with 10 sheets should output 10 diferent pdf files.)

Since I have an iLogic rule that renames the default sheet name to the part name of the drawing view, I wanted to save each sheet individually to pdf using the sheet name, wich equals the name of the part, except what is after the two dots [:] . (E.g.: In 'Sheet:1' there is a file placed in view with the name "Part5" , so the rule changes the sheet name to "Part5:1", then the rule only gets the text until the two dots, "Part5", and saves it as the name of the pdf file. Output: "Part5.pdf" )  Smiley Wink

 

I only posted a question regarding the first step of the iLogic rule that I wanted to do:

1st: Export all sheets individually to pdf and with the right name;

2nd: Export all sheets that have a sheetmetal as part in view to dxf;

3rd: Sheets that have a part in view, open said part and save the 3D file to step;

4th: If it is an assembly, then it does nothing and goes to the next sheet;

 

I'm having a hard time getting a rule to figure out if it is a sheetmetal or a part. Was hoping I could differentiate them using the 'SubType', but no progress so far. Smiley Frustrated

I know it will be a long rule, but that is all that we need to work properly.

 

Tell me if I need to create another topic for these next steps. Smiley Happy

João Silva

Mechanical Engineer

 

0 Likes
Message 8 of 8

chandra.shekar.g
Autodesk Support
Autodesk Support

@JoãoASilva,

 

Try below suggestions provided in below forum discussion link to print each sheets in different pdf.

 

https://forums.autodesk.com/t5/inventor-forum/convert-idw-with-multiple-sheets-to-multiple-pdf-s-inv...

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes