Batch Export PDF to same location as .idw from assembly

Batch Export PDF to same location as .idw from assembly

elgeemail00
Participant Participant
525 Views
2 Replies
Message 1 of 3

Batch Export PDF to same location as .idw from assembly

elgeemail00
Participant
Participant

I have a code that exports PDFs from assembly from all components that have a drawing that puts them in a separate folder. Is there a way to export the PDFs to the same location as the drawing? Attached is the code I copied from Curtis at link: From the Trenches with Autodesk Inventor: PDF (inventortrenches.blogspot.com)

It works like a charm but it's not quite what I'm looking for. Any help would be appreciated. Thanks in advance and have a very Merry Christmas!

0 Likes
Accepted solutions (1)
526 Views
2 Replies
Replies (2)
Message 2 of 3

A.Acheson
Mentor
Mentor
Accepted solution

So these few lines work to get to create a pdf folder. As you said you don't need this folder so this becomes redundant and can be removed. 

'get PDF target folder path
oFolder = oPath & "\" & oAsmName & " PDF Files"

'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

New Changes for folder locations:

For Referenced drawing before the setting the FileName in the translator get the  folder 

Dim RefDrawFolder As String = IO.Path.GetDirectoryName(idwPathName)

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

For Main Assembly drawing before the setting the FileName in the translator get the  folder 

Dim AssyDrawFolder As String = IO.Path.GetDirectoryName(oAsmDrawing)
'Or because the assembly is the launching document of the rule you can use the ilogic method
Dim AssyDrawFolder As String = ThisDoc.Path
'******************** 'Set the PDF target file name oDataMedium.FileName = AssyDrawFolder & "\" & oAsmDrawingName & "pdf"

Here is a reworked version of what you started with. Changes are mostly cleaning up the formatting to make it a little easier to read and removing the On error Resume Next and changing to try catch end try to define where the error will occur if any. 

 

 

'check that the active document is an assembly file
If ThisDoc.Document.DocumentType <> kAssemblyDocumentObject Then
	MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
	Exit Sub
End If

'get user input
RUsure = MessageBox.Show _
					("This will create a PDF file for all of the asembly components that have drawings files." _						
					& vbLf & "This rule expects that the drawing file shares the same name and location as the component." _
					& vbLf & " " _
					& vbLf & "Are you sure you want to create PDF Drawings for all of the assembly components?" _
					& vbLf & "This could take a while.", "iLogic  - Batch Output PDFs ",MessageBoxButtons.YesNo)

If RUsure = vbNo Then:Return:Else:End If
	
'[PDF setup

PDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD95-2F4D-42CE-8BE0-8AEA580399E4}")  '0AC6FD95-2F4D-42CE-8BE0-8AEA580399E4
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

If PDFAddIn.HasSaveCopyAsOptions(oDataMedium, oContext, oOptions) Then
	oOptions.Value("All_Color_AS_Black") = 0
	oOptions.Value("Remove_Line_Weights") = 1
	oOptions.Value("Vector_Resolution") = 400
	oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
	'oOptions.Value("Custom_Begin_Sheet") = 2
	'oOptions.Value("Custom_End_Sheet") = 4
End If

']

'[Component Drawings 

'define the active document as an assembly file
Dim oAsmDoc As AssemblyDocument = ThisDoc.Document

'look at the files referenced by the assembly
Dim oRefDocs As DocumentsEnumerator = oAsmDoc.AllReferencedDocuments

'work the the drawing files for the referenced models
'this expects that the model has a drawing of the same path and name 

For Each oRefDoc As Document In oRefDocs

	Dim idwPathName As String = Left(oRefDoc.FullDocumentName, Len(oRefDoc.FullDocumentName) -3) & "idw"
	
	'check to see that the model has a drawing of the same path and name 
	If (System.IO.File.Exists(idwPathName)) Then
		
		Dim RefDrawFolder As String = IO.Path.GetDirectoryName(idwPathName)
		
		Dim oFileName As String = Left(oRefDoc.DisplayName, Len(oRefDoc.DisplayName) -3)
		
		Dim oDrawDoc As DrawingDocument = ThisApplication.Documents.Open(idwPathName, True)
			
		Try
			 'Set the PDF target file name
			oDataMedium.FileName = RefDrawFolder & "\" & oFileName & "pdf"
			
			'Write out the PDF
			Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
		Catch
		End Try
		
		'close the file
		oDrawDoc.Close
		
	Else
	'If the model has no drawing of the same path and name - do nothing
	End If
Next
']

'[Top Level Drawing

Dim oAsmDrawing As String  = ThisDoc.ChangeExtension(".idw")

If (System.IO.File.Exists(oAsmDrawing)) Then
	
	Dim oAsmDrawingDoc As DrawingDocument = ThisApplication.Documents.Open(oAsmDrawing, True)
	
	Dim AssyDrawFolder As String = IO.Path.GetDirectoryName(oAsmDrawing)
	
	Dim oAsmDrawingName As String = Left(oAsmDrawingDoc.DisplayName, Len(oAsmDrawingDoc.DisplayName) -3)

	'write out the PDF for the Top Level Assembly Drawing file
	Try
		 'Set the PDF target file name
		oDataMedium.FileName = AssyDrawFolder & "\" & oAsmDrawingName & "pdf"

		'Write out the PDF
		Call PDFAddIn.SaveCopyAs(oAsmDrawingDoc, oContext, oOptions, oDataMedium)
	Catch ' if PDF exists and is open or read only, resume next
	End Try
	
	'Close the top level drawing
	oAsmDrawingDoc.Close
	
End If
']

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
Message 3 of 3

elgeemail00
Participant
Participant

I tested this with a couple assemblies, and it seems to be working like I want it to. I'll post back if I run into issues in the future. Thanks for your time. Have a Merry Christmas and good cheer all year!