Export all open drawings to PDF - All drawing saved as same

Export all open drawings to PDF - All drawing saved as same

gmataleite
Explorer Explorer
1,895 Views
4 Replies
Message 1 of 5

Export all open drawings to PDF - All drawing saved as same

gmataleite
Explorer
Explorer

Hello,

 

I created two external rules to export all open drawings to PDF, the first rule is only to run a main rule, where the export is made, but I'm having an issue. The only drawing exported is the one where I run the first rule, saved as different names.

 

For Each doc As Document In ThisApplication.Documents.VisibleDocuments
	If doc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
		iLogicVb.Automation.RunExternalRule(doc, "SavePDF")

	
	End If

	
Next

MessageBox.Show("All PDFs created", "Ready!", MessageBoxButtons.OK)

 

Sub Main()
	FileName = ThisDoc.FileName(True) 'with extension
	
	FileExtension = Right(FileName, 3)
	
	If FileExtension = "idw" Then
		Save_As_PDF
	Else If FileExtension = "dwg" Then
		Save_As_PDF
	Else
		ErrorMessage
	End If
	
	
End Sub

Sub Save_As_PDF
	
	oPath = ThisDoc.Path
	oFileName = ThisDoc.FileName(False)
	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
	oRevNum = iProperties.Value("Project", "Revision Number")
	
	WorkspacePath = ThisDoc.WorkspacePath()													'Gets the workspace path
	
	WorkspacePathLength = Len(WorkspacePath)												'Gets the length of the workspace path string
	
	PathOnly = ThisDoc.Path																	'Gets just the path of the file
	
	DirectoryPath = Strings.Right(PathOnly, PathOnly.Length - WorkspacePathLength)			'Removes the workspace path from fullpath
	
	PDFPath = ThisDoc.Path & "\2D_PDF"								'Sets the directory that the pdf should be saved in
	PDFName = PDFPath & "\" & ThisDoc.FileName(False) & "_" & oRevNum & ".pdf"								'Saves the pdf in the desired location
	
	oDataMedium.FileName = PDFName
	
	oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
	
	If Not System.IO.Directory.Exists(PDFPath) Then											'checks to see if that directory exists, if not, it is created
		System.IO.Directory.CreateDirectory(PDFPath)
	End If
	
	'Check to see if the PDF already exists, if it does, ask if you want to overwrite it or not.
	If System.IO.File.Exists(PDFName) = True Then
		oAnswer = MsgBox("A PDF file with this name already exists." & vbCrLf &
		"Do you want to overwrite it with this new one?",vbYesNo + vbQuestion + vbDefaultButton2, "PDF ALREADY EXISTS")
		If oAnswer = vbNo Then Exit Sub
	End If
	
	oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)	
	
	'MessageBox.Show("PDF created","Message", MessageBoxButtons.OK)
End Sub


Sub ErrorMessage
	i = MessageBox.Show("This Is Not a drawing File. No PDF will be created.", "Create PDF", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
End Sub

 

All files were create, but they're all the same.

gmataleite_0-1654784989185.png

 

I think there's some error in my code, but I couldn't figure out. Can anyone help me?

Thanks a lot.

0 Likes
Accepted solutions (1)
1,896 Views
4 Replies
Replies (4)
Message 2 of 5

JelteDeJong
Mentor
Mentor

In your 2e rule you use "ThisDoc" to get the document object. but this is always the active document. You need to pass the document object of the document that you want to export to the "Save_as_pdf function". Doing that between 2 rules is a bit difficult. Is there a reason why you dont want to combine these rules?

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 3 of 5

gmataleite
Explorer
Explorer

There's none specifically reason, I already had the rule to export, after I created the rule to all for make easier my work.

 

I tried some others ilogic that I found here, but I couldn't do correctly.

 

I appreciate if you help me how to merge this two codes.

0 Likes
Message 4 of 5

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi @gmataleite 

 

Give this a try.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

Sub Main

	For Each doc As Document In ThisApplication.Documents.VisibleDocuments
		If doc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
			Call Save_As_PDF(doc)
		End If
	Next
	
End Sub

Sub Save_As_PDF(oDoc As Document)

	oPath = IO.Path.GetDirectoryName(oDoc.FullFileName)
	oFileName = IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName)
	oRevNum = oDoc.PropertySets.Item(1).Item("Revision Number").Value
	
	oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
	oContext = ThisApplication.TransientObjects.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	oOptions = ThisApplication.TransientObjects.CreateNameValueMap
	oDataMedium = ThisApplication.TransientObjects.CreateDataMedium	

	'Sets the directory that the pdf should be saved in
	PDFPath = oPath & "\2D_PDF"
	'Saves the pdf in the desired location
	PDFName = PDFPath & "\" & oFileName & "_" & oRevNum & ".pdf"

	oDataMedium.FileName = PDFName
	oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets

	'checks to see if that directory exists, if not, it is created
	If Not System.IO.Directory.Exists(PDFPath) Then
		System.IO.Directory.CreateDirectory(PDFPath)
	End If

	'Check to see if the PDF already exists, if it does, ask if you want to overwrite it or not.
	If System.IO.File.Exists(PDFName) = True Then
		oAnswer = MsgBox("A PDF file with this name already exists." & vbCrLf &
		"Do you want to overwrite it with this new one?", vbYesNo + vbQuestion + vbDefaultButton2, "PDF ALREADY EXISTS")
		If oAnswer = vbNo Then Exit Sub
	End If

	oPDFAddIn.SaveCopyAs(oDoc, oContext, oOptions, oDataMedium)

	'MessageBox.Show("PDF created","Message", MessageBoxButtons.OK)
End Sub

 

EESignature

Message 5 of 5

gmataleite
Explorer
Explorer
It worked well!

Thanks a lot!
0 Likes