Batch Export PDF - How to add iProperties to file name?

Batch Export PDF - How to add iProperties to file name?

fredform
Advocate Advocate
2,937 Views
10 Replies
Message 1 of 11

Batch Export PDF - How to add iProperties to file name?

fredform
Advocate
Advocate

Hello!

 

I've been modifying a batch PDF export script I originally found here and gotten things to work quite well but I would like to add Title and Revision to the PDF filename that gets generated but I can't seem to access the iProperties of each occurrence. 

 

 

 

'Source: https://forums.autodesk.com/t5/inventor-customization/ilogic-batch-export-pdf-from-assembly-drawing/td-p/7325967

Sub Main()
    Dim oDoc As Document
    oDoc = ThisDoc.Document
    oDocName = System.IO.Path.GetDirectoryName(oDoc.FullFileName) & "\" & System.IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName)
    
    If Not (oDoc.DocumentType = kAssemblyDocumentObject Or oDoc.DocumentType = kDrawingDocumentObject) Then
        MessageBox.Show("Please run this rule from an assembly or drawing file.", "iLogic")
        Exit Sub
    End If
    
    'Get user input
    If MessageBox.Show ( _
        "This will create a PDF file for all of the files referenced by this document 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 referenced documents?" _
        & vbLf & "This could take a while.", "iLogic  - Batch Export PDFs ",MessageBoxButtons.YesNo) = vbNo Then
        Exit Sub
    End If
        
    Dim PDFAddIn As TranslatorAddIn
    Dim oContext As TranslationContext
    Dim oOptions As NameValueMap
    Dim oDataMedium As DataMedium
   
    Call ConfigurePDFAddinSettings(PDFAddIn, oContext, oOptions, oDataMedium)
    
	'Export location
	oFolder = "C:\temp_InventorExports\"
	'oFolder = oDocName & " PDF Files"
    If Not System.IO.Directory.Exists(oFolder) Then
        System.IO.Directory.CreateDirectory(oFolder)
    End If
   
   
 	'Integers for counting documents
	Dim iTotal As Integer
		iTotal = 0
	Dim iMissing As Integer
		iMissing = 0
	
	'List for missing drawings
	Dim sNoDrawList As String  
   
   
    '- - - - - - - - - - - - - Component Drawings - - - - - - - - - - - -
    Dim oRefDoc As Document
    Dim oDrawDoc As DrawingDocument
    
    For Each oRefDoc In oDoc.AllReferencedDocuments
		iTotal = iTotal+1
        oBaseName = System.IO.Path.GetFileNameWithoutExtension(oRefDoc.FullFileName)		
        oPathAndName = System.IO.Path.GetDirectoryName(oRefDoc.FullFileName) & "\" & oBaseName
		
        If(System.IO.File.Exists(oPathAndName & ".idw")) Then
            oDrawDoc = ThisApplication.Documents.Open(oPathAndName & ".idw", True)
            oDataMedium.FileName = oFolder & "\" & oBaseName & ".pdf"
            Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
            oDrawDoc.Close
			iCreated = iCreated + 1
        Else
			iMissing = iMissing + 1
			sNoDrawList = sNoDrawList & vbLf & iMissing & ". " & oBaseName
        End If
    Next
    '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   
   
    '- - - - - - - - - - - - - Top Level Drawing - - - - - - - - - - - -
    oBaseName = System.IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName)
    oPathAndName = System.IO.Path.GetDirectoryName(oDoc.FullFileName) & "\" & oBaseName
    oDataMedium.FileName = oFolder & "\" & oBaseName & ".pdf"
    
    If oDoc.DocumentType = kAssemblyDocumentObject Then
		iTotal = iTotal+1
        oDrawDoc = ThisApplication.Documents.Open(oPathAndName & ".idw", True)
		Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
        oDrawDoc.Close
		
    ElseIf oDoc.DocumentType = kDrawingDocumentObject Then
        Call PDFAddIn.SaveCopyAs(oDoc, oContext, oOptions, oDataMedium)    
    End If
    ''- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
   'Output prompt
    MessageBox.Show("New files created in: " & vbLf & oFolder & vbCrLf & vbCrLf & vbCrLf _
	& "Files found without drawings: " & vbLf & iMissing & " / " & iTotal & vbCrLf _
	& sNoDrawList, "iLogic")
	
    Shell("explorer.exe " & oFolder, vbNormalFocus)
	    
End Sub


Sub ConfigurePDFAddinSettings(ByRef PDFAddIn As TranslatorAddIn, ByRef oContext As TranslationContext, ByRef oOptions As NameValueMap, ByRef oDataMedium As DataMedium)

    PDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
    oContext = ThisApplication.TransientObjects.CreateTranslationContext
    oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
        
    oOptions = ThisApplication.TransientObjects.CreateNameValueMap
    oOptions.Value("All_Color_AS_Black") = 1
    oOptions.Value("Remove_Line_Weights") = 0
    oOptions.Value("Vector_Resolution") = 400
    oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
'    oOptions.Value("Custom_Begin_Sheet") = 1
'    oOptions.Value("Custom_End_Sheet") = 1

    oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
End Sub

 

 

 

 

In another script found here the following function is used to build the string of Part number and Title with some separators. This seems like a good method to use for the Batch exporter as well but so far I haven't been successful in connecting the two.

 

 

Private Function BuildName(doc As Document) As String
    Dim name As String
    Dim designTrackProps As PropertySet
    designTrackProps = doc.PropertySets.Item("Design Tracking Properties")
	
	Dim designTrackProps2 As PropertySet
	designTrackProps2 = doc.PropertySets.Item("Inventor Summary Information")
    
	If designTrackProps.Item("Part Number").Value Is Nothing
		BuildName = doc.DisplayName
	Else
    name = designTrackProps.Item("Part Number").Value & " - [" & _
           designTrackProps2.Item("Title").Value & "]"
           
    BuildName = name
	End If
End Function

 

 

Happy for any suggestions on how to fix this!

 

Regards,

Fredrik

0 Likes
Accepted solutions (2)
2,938 Views
10 Replies
Replies (10)
Message 2 of 11

WCrihfield
Mentor
Mentor

Where you get your oRefDoc variable from the line

For Each oRefDoc In oDoc.AllReferencedDocuments

Just pass that oRefDoc variable to the function, then use its returned value within the line you're using to save the PDF.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 11

rhasell
Advisor
Advisor

Hi

I have added a couple of lines to your code.

I have separated them by a --------- added by Reg-------- so that you can find the changes.

 

'Source: https://forums.autodesk.com/t5/inventor-customization/ilogic-batch-export-pdf-from-assembly-drawing/td-p/7325967

Sub Main()
    Dim oDoc As Document
    oDoc = ThisDoc.Document
    oDocName = System.IO.Path.GetDirectoryName(oDoc.FullFileName) & "\" & System.IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName)
    
    If Not (oDoc.DocumentType = kAssemblyDocumentObject Or oDoc.DocumentType = kDrawingDocumentObject) Then
        MessageBox.Show("Please run this rule from an assembly or drawing file.", "iLogic")
        Exit Sub
    End If
    
    'Get user input
    If MessageBox.Show ( _
        "This will create a PDF file for all of the files referenced by this document 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 referenced documents?" _
        & vbLf & "This could take a while.", "iLogic  - Batch Export PDFs ",MessageBoxButtons.YesNo) = vbNo Then
        Exit Sub
    End If
        
    Dim PDFAddIn As TranslatorAddIn
    Dim oContext As TranslationContext
    Dim oOptions As NameValueMap
    Dim oDataMedium As DataMedium
   
    Call ConfigurePDFAddinSettings(PDFAddIn, oContext, oOptions, oDataMedium)
    
	'Export location
	oFolder = "C:\temp_InventorExports\"
	'-------Added By Reg---
	oRevNum = iProperties.Value("Project", "Revision Number")
	'-----------------------
	'oFolder = oDocName & " PDF Files"
    If Not System.IO.Directory.Exists(oFolder) Then
        System.IO.Directory.CreateDirectory(oFolder)
    End If
   
   
 	'Integers for counting documents
	Dim iTotal As Integer
		iTotal = 0
	Dim iMissing As Integer
		iMissing = 0
	
	'List for missing drawings
	Dim sNoDrawList As String  
   
   
    '- - - - - - - - - - - - - Component Drawings - - - - - - - - - - - -
    Dim oRefDoc As Document
    Dim oDrawDoc As DrawingDocument
    
    For Each oRefDoc In oDoc.AllReferencedDocuments
		iTotal = iTotal+1
        oBaseName = System.IO.Path.GetFileNameWithoutExtension(oRefDoc.FullFileName)		
        oPathAndName = System.IO.Path.GetDirectoryName(oRefDoc.FullFileName) & "\" & oBaseName
		
        If(System.IO.File.Exists(oPathAndName & ".idw")) Then
            oDrawDoc = ThisApplication.Documents.Open(oPathAndName & ".idw", True)
'---------------Changed by Reg------------------
            oDataMedium.FileName = oFolder & "\" & oBaseName & "[" & oRevNum & "]" & ".pdf"
'--------------------------
            Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
            oDrawDoc.Close
			iCreated = iCreated + 1
        Else
			iMissing = iMissing + 1
			sNoDrawList = sNoDrawList & vbLf & iMissing & ". " & oBaseName
        End If
    Next
    '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   
   
    '- - - - - - - - - - - - - Top Level Drawing - - - - - - - - - - - -
    oBaseName = System.IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName)
    oPathAndName = System.IO.Path.GetDirectoryName(oDoc.FullFileName) & "\" & oBaseName
'---------------Changed by Reg------------------
            oDataMedium.FileName = oFolder & "\" & oBaseName & "[" & oRevNum & "]" & ".pdf"
'--------------------------
    
    If oDoc.DocumentType = kAssemblyDocumentObject Then
		iTotal = iTotal+1
        oDrawDoc = ThisApplication.Documents.Open(oPathAndName & ".idw", True)
		Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
        oDrawDoc.Close
		
    ElseIf oDoc.DocumentType = kDrawingDocumentObject Then
        Call PDFAddIn.SaveCopyAs(oDoc, oContext, oOptions, oDataMedium)    
    End If
    ''- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
   'Output prompt
    MessageBox.Show("New files created in: " & vbLf & oFolder & vbCrLf & vbCrLf & vbCrLf _
	& "Files found without drawings: " & vbLf & iMissing & " / " & iTotal & vbCrLf _
	& sNoDrawList, "iLogic")
	
    Shell("explorer.exe " & oFolder, vbNormalFocus)
	    
End Sub


Sub ConfigurePDFAddinSettings(ByRef PDFAddIn As TranslatorAddIn, ByRef oContext As TranslationContext, ByRef oOptions As NameValueMap, ByRef oDataMedium As DataMedium)

    PDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
    oContext = ThisApplication.TransientObjects.CreateTranslationContext
    oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
        
    oOptions = ThisApplication.TransientObjects.CreateNameValueMap
    oOptions.Value("All_Color_AS_Black") = 1
    oOptions.Value("Remove_Line_Weights") = 0
    oOptions.Value("Vector_Resolution") = 400
    oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
'    oOptions.Value("Custom_Begin_Sheet") = 1
'    oOptions.Value("Custom_End_Sheet") = 1

    oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
End Sub
Reg
2026.1
0 Likes
Message 4 of 11

rhasell
Advisor
Advisor

Hi

 

Apologies, I forgot to add the title, you will have to format the filename as you see fit.

 

'Source: https://forums.autodesk.com/t5/inventor-customization/ilogic-batch-export-pdf-from-assembly-drawing/td-p/7325967

Sub Main()
Dim oDoc As Document
oDoc = ThisDoc.Document
oDocName = System.IO.Path.GetDirectoryName(oDoc.FullFileName) & "\" & System.IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName)

If Not (oDoc.DocumentType = kAssemblyDocumentObject Or oDoc.DocumentType = kDrawingDocumentObject) Then
	MessageBox.Show("Please run this rule from an assembly or drawing file.", "iLogic")
	Exit Sub
End If

'Get user input
If MessageBox.Show( _
	"This will create a PDF file for all of the files referenced by this document 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 referenced documents?" _
	& vbLf & "This could take a while.", "iLogic  - Batch Export PDFs ", MessageBoxButtons.YesNo) = vbNo Then
	Exit Sub
End If

Dim PDFAddIn As TranslatorAddIn
Dim oContext As TranslationContext
Dim oOptions As NameValueMap
Dim oDataMedium As DataMedium

Call ConfigurePDFAddinSettings(PDFAddIn, oContext, oOptions, oDataMedium)

'Export location
oFolder = "C:\temp_InventorExports\"
'-------Added By Reg---
oRevNum = iProperties.Value("Project", "Revision Number")
oTitle=iProperties.Value("Summary", "Title")
'-----------------------
'oFolder = oDocName & " PDF Files"
If Not System.IO.Directory.Exists(oFolder) Then
	System.IO.Directory.CreateDirectory(oFolder)
End If


'Integers for counting documents
Dim iTotal As Integer
iTotal = 0
Dim iMissing As Integer
iMissing = 0

'List for missing drawings
Dim sNoDrawList As String


'- - - - - - - - - - - - - Component Drawings - - - - - - - - - - - -
Dim oRefDoc As Document
Dim oDrawDoc As DrawingDocument

For Each oRefDoc In oDoc.AllReferencedDocuments
	iTotal = iTotal + 1
	oBaseName = System.IO.Path.GetFileNameWithoutExtension(oRefDoc.FullFileName)
	oPathAndName = System.IO.Path.GetDirectoryName(oRefDoc.FullFileName) & "\" & oBaseName

	If (System.IO.File.Exists(oPathAndName & ".idw")) Then
		oDrawDoc = ThisApplication.Documents.Open(oPathAndName & ".idw", True)
		'---------------Changed by Reg------------------
		oDataMedium.FileName = oFolder & "\" & oBaseName & " - " & oTitle & "[" & oRevNum & "]" & ".pdf"
		'--------------------------
		Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
		oDrawDoc.Close
		iCreated = iCreated + 1
	Else
		iMissing = iMissing + 1
		sNoDrawList = sNoDrawList & vbLf & iMissing & ". " & oBaseName
	End If
Next
'- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


'- - - - - - - - - - - - - Top Level Drawing - - - - - - - - - - - -
oBaseName = System.IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName)
oPathAndName = System.IO.Path.GetDirectoryName(oDoc.FullFileName) & "\" & oBaseName
'---------------Changed by Reg------------------
oDataMedium.FileName = oFolder & "\" & oBaseName & " - " & oTitle & "[" & oRevNum & "]" & ".pdf"
'--------------------------

If oDoc.DocumentType = kAssemblyDocumentObject Then
	iTotal = iTotal + 1
	oDrawDoc = ThisApplication.Documents.Open(oPathAndName & ".idw", True)
	Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
	oDrawDoc.Close

ElseIf oDoc.DocumentType = kDrawingDocumentObject Then
	Call PDFAddIn.SaveCopyAs(oDoc, oContext, oOptions, oDataMedium)
End If
''- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

'Output prompt
MessageBox.Show("New files created in: " & vbLf & oFolder & vbCrLf & vbCrLf & vbCrLf _
& "Files found without drawings: " & vbLf & iMissing & " / " & iTotal & vbCrLf _
& sNoDrawList, "iLogic")

Shell("explorer.exe " & oFolder, vbNormalFocus)

End Sub


Sub ConfigurePDFAddinSettings(ByRef PDFAddIn As TranslatorAddIn, ByRef oContext As TranslationContext, ByRef oOptions As NameValueMap, ByRef oDataMedium As DataMedium)

	PDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
	oContext = ThisApplication.TransientObjects.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism

	oOptions = ThisApplication.TransientObjects.CreateNameValueMap
	oOptions.Value("All_Color_AS_Black") = 1
	oOptions.Value("Remove_Line_Weights") = 0
	oOptions.Value("Vector_Resolution") = 400
	oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
	'    oOptions.Value("Custom_Begin_Sheet") = 1
	'    oOptions.Value("Custom_End_Sheet") = 1

	oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
End Sub
Reg
2026.1
0 Likes
Message 5 of 11

fredform
Advocate
Advocate

Hi Reg,

 

This only adds the top level assembly, or the iproperties of the document where the script is run to the filename of every pdf generated. I would like to have for each pdf include that documents Title and Revision number in the filename. 

 

 

I will try to use the function as WCrihfield suggested and get back to you with the results.

 

 

0 Likes
Message 6 of 11

fredform
Advocate
Advocate

I have updated the code as below but I still cannot pass the information back from the function. I haven't worked with functions before so I'm kinda guessing on the syntax...

 

    '- - - - - - - - - - - - - Component Drawings - - - - - - - - - - - -
    Dim oRefDoc As Document
    Dim oDrawDoc As DrawingDocument
	    
    For Each oRefDoc In oDoc.AllReferencedDocuments
		BuildName(oRefDoc)
		'MsgBox(BuildName.Value)
		'MsgBox(BuildName)
		
		iTotal = iTotal+1
        oBaseName = System.IO.Path.GetFileNameWithoutExtension(oRefDoc.FullFileName)		
        oPathAndName = System.IO.Path.GetDirectoryName(oRefDoc.FullFileName) & "\" & oBaseName
		
        If(System.IO.File.Exists(oPathAndName & ".idw")) Then
            oDrawDoc = ThisApplication.Documents.Open(oPathAndName & ".idw", True)
            oDataMedium.FileName = oFolder & "\" & oBaseName & BuildName & ".pdf"
            Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
            oDrawDoc.Close
			iCreated = iCreated + 1
        Else
			iMissing = iMissing + 1
			sNoDrawList = sNoDrawList & vbLf & iMissing & ". " & oBaseName
        End If
    Next
    '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   

 

 

And here's the function:

 

Private Function BuildName(oRefDoc As Document) As String
    Dim name As String
    
	Dim designTrackProps As PropertySet
    designTrackProps = oRefDoc.PropertySets.Item("Inventor Summary Information")
	Dim designTrackProps2 As PropertySet
	designTrackProps2 = oRefDoc.PropertySets.Item("Design Tracking Properties")
    
    name = "_" & designTrackProps.Item("Title").Value & "_Rev" & _
           designTrackProps2.Item("Revision Number").Value

    BuildName = name
	Return BuildName

End Function

 

 

 

Attached is the full code.

 

0 Likes
Message 7 of 11

WCrihfield
Mentor
Mentor
Accepted solution

Try this.

I think I fixed it for you.

You were trying to get the Rev level from the wrong iProperties set.

You needed to set the returned value from the function as the value of a pre-defined string variable.

Etc, but I'm no longer getting errors from it.

'Source: https://forums.autodesk.com/t5/inventor-customization/ilogic-batch-export-pdf-from-assembly-drawing/td-p/7325967

Sub Main()
	Dim oThisDoc As Document = ThisDoc.Document
	oDocName = System.IO.Path.GetDirectoryName(oThisDoc.FullFileName) & "\" & System.IO.Path.GetFileNameWithoutExtension(oThisDoc.FullFileName)
	If Not (oThisDoc.DocumentType = kAssemblyDocumentObject Or oThisDoc.DocumentType = kDrawingDocumentObject) Then
	    MsgBox("Please run this rule from an assembly or drawing file.",vbOKOnly, "iLogic")
	    Exit Sub
	End If
	'Get user input
	If MsgBox("This will create a PDF file for all of the files referenced by this document that have drawing 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 referenced documents?" & vbLf & _
		"This could take a while.",vbYesNo + vbQuestion, "iLogic  - Batch Export PDFs") = vbNo Then
		Exit Sub
	End If
	Dim PDFAddIn As TranslatorAddIn
	Dim oContext As TranslationContext
	Dim oOptions As NameValueMap
	Dim oDataMedium As DataMedium
	Call ConfigurePDFAddinSettings(PDFAddIn, oContext, oOptions, oDataMedium)
	'Export location
	Dim oFolder As String = "C:\temp_InventorExports\"
	'Dim oFolder As String = oDocName & " PDF Files"
	If Not System.IO.Directory.Exists(oFolder) Then
	    System.IO.Directory.CreateDirectory(oFolder)
	End If
	'Integers for counting documents
	Dim iTotal As Integer = 0
	Dim iMissing As Integer = 0
	'List for missing drawings
	Dim sNoDrawList As String  
	'- - - - - - - - - - - - - Component Drawings - - - - - - - - - - - -
	Dim oRefDoc As Document
	Dim oDrawDoc As DrawingDocument
	Dim oBuiltName As String
	For Each oRefDoc In oThisDoc.AllReferencedDocuments
		oBuiltName = BuildName(oRefDoc)
		MsgBox(oBuiltName)
		iTotal = iTotal+1
		oBaseName = System.IO.Path.GetFileNameWithoutExtension(oRefDoc.FullFileName)		
		oPathAndName = System.IO.Path.GetDirectoryName(oRefDoc.FullFileName) & "\" & oBaseName
	    If(System.IO.File.Exists(oPathAndName & ".idw")) Then
	        oDrawDoc = ThisApplication.Documents.Open(oPathAndName & ".idw", True)
	        oDataMedium.FileName = oFolder & "\" & oBaseName & oBuiltName & ".pdf"
	        Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
	        oDrawDoc.Close
			iCreated = iCreated + 1
	    Else
			iMissing = iMissing + 1
			sNoDrawList = sNoDrawList & vbLf & iMissing & ". " & oBaseName
	    End If
	Next
	'- - - - - - - - - - - - - Top Level Drawing - - - - - - - - - - - -
	oBaseName = System.IO.Path.GetFileNameWithoutExtension(oThisDoc.FullFileName)
	oPathAndName = System.IO.Path.GetDirectoryName(oThisDoc.FullFileName) & "\" & oBaseName
	oDataMedium.FileName = oFolder & "\" & oBaseName & ".pdf"

	If oThisDoc.DocumentType = kAssemblyDocumentObject Then
		iTotal = iTotal+1
		oDrawDoc = ThisApplication.Documents.Open(oPathAndName & ".idw", True)
		PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
		oDrawDoc.Close
	ElseIf oThisDoc.DocumentType = kDrawingDocumentObject Then
		PDFAddIn.SaveCopyAs(oThisDoc, oContext, oOptions, oDataMedium)    
	End If
	'Output prompt
	MsgBox("New files created in: " & vbLf & oFolder & vbCrLf & vbCrLf & vbCrLf & _
	"Files found without drawings: " & vbLf & iMissing & " / " & iTotal & vbCrLf & _
	sNoDrawList,vbOKOnly, "iLogic")
	Shell("explorer.exe " & oFolder, vbNormalFocus)
End Sub

Function BuildName(oDoc As Document) As String
	Dim oSumProps As PropertySet = oDoc.PropertySets.Item("Inventor Summary Information")
	Dim oTitle As String = oSumProps.Item("Title").Value
	Dim oRev As String = oSumProps.Item("Revision Number").Value
	Dim oName As String = "_" & oTitle & "_Rev" & oRev
	Return oName
End Function

Sub ConfigurePDFAddinSettings(ByRef PDFAddIn As TranslatorAddIn, ByRef oContext As TranslationContext, ByRef oOptions As NameValueMap, ByRef oDataMedium As DataMedium)
	PDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
	oContext = ThisApplication.TransientObjects.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	oOptions = ThisApplication.TransientObjects.CreateNameValueMap
	oOptions.Value("All_Color_AS_Black") = 1
	oOptions.Value("Remove_Line_Weights") = 0
	oOptions.Value("Vector_Resolution") = 400
	oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
	'    oOptions.Value("Custom_Begin_Sheet") = 1
	'    oOptions.Value("Custom_End_Sheet") = 1
	oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
End Sub

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 8 of 11

WCrihfield
Mentor
Mentor

@fredform 

Just a tip.  That last post I did may have fixed some main problems, but one thing I noticed that you may need to address to avoid possible future errors, is to check if those iProperties actually have a value or not.  If either of their value are empty, that might cause problems.  So putting a check in there to catch when either of those values are empty (""), and what to do if it is, will most likely help avoid future problems.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 9 of 11

WCrihfield
Mentor
Mentor

@fredform 

Also, it you're wanting to make this batch PDF publisher to reach down to every level of all sub-assemblies, you will have to develop a loop with a GoTo that checks if the count of its AllReferencedDocuments (this sort of thing is usually done with ComponentOccurrences, and checking oOcc.SubOccurrences.Count).  If the count is more than zero, then GoTo the beginning of the loop again.  If the count is zero, continue without going back through the loop again.  Of course this is a mid-level loop though, because it needs to still proceed to the next oRefDoc, if there are any.

You can find similar workflow examples here on the forum.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 10 of 11

fredform
Advocate
Advocate
Accepted solution

Hi again,

 

Thank you so much for the help! It is now working beautifully!

One thing I don't really understand though is why the function uses "oDoc As Document" and not "oRefDoc"?

 

Also about the iProperties set, you are right, I only looked at the iLogic snippets and iProperties tab where revision is under Project and assumed that would be the same here, oh well...

 

I've added a catch to fix if the iProperty field is empty although I didn't really get an error before if it was.

I didn't manage to use the function to populate the string for the top level assembly so I used the ilogic snippets to build that string, not as elegant of a solution but it works.

 

Also for the traversing down into multiple levels of subassemblies and their documents it seems to be working. On the files I've used for testing there is 3 levels of assemblies and the part documents in the lowest level gets printed as well so I guess it works?

 

 

Here is the finished code:

 

 

'Source: https://forums.autodesk.com/t5/inventor-customization/ilogic-batch-export-pdf-from-assembly-drawing/td-p/7325967

Sub Main()
    Dim oDoc As Document
    oDoc = ThisDoc.Document
    oDocName = System.IO.Path.GetDirectoryName(oDoc.FullFileName) & "\" & System.IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName)
    
    If Not oDoc.DocumentType = kAssemblyDocumentObject Then
        MessageBox.Show("Please run this rule from an assembly file.", "iLogic")
        Exit Sub
    End If
    
    'Get user input
    If MessageBox.Show ( _
        "This will create a PDF file for all of the files referenced by this document that have drawing 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 referenced documents?" _
        & vbLf & "This could take a while.", "iLogic  - Batch Export PDFs ",MessageBoxButtons.YesNo) = vbNo Then
        Exit Sub
    End If
        
    Dim PDFAddIn As TranslatorAddIn
    Dim oContext As TranslationContext
    Dim oOptions As NameValueMap
    Dim oDataMedium As DataMedium
   
    Call ConfigurePDFAddinSettings(PDFAddIn, oContext, oOptions, oDataMedium)
    
	
	'Export location
	oFolder = "C:\temp_InventorExports\"
	'oFolder = oDocName & " PDF Files"
	
    If Not System.IO.Directory.Exists(oFolder) Then
        System.IO.Directory.CreateDirectory(oFolder)
    End If
   
   
 	'Integers for counting documents
	Dim iTotal As Integer
		iTotal = 0
	Dim iMissing As Integer
		iMissing = 0
	
	'List for missing drawings
	Dim sNoDrawList As String  
   
   
    '- - - - - - - - - - - - - Component Drawings - - - - - - - - - - - -
    Dim oRefDoc As Document
    Dim oDrawDoc As DrawingDocument
	Dim oProps As String
	    
    For Each oRefDoc In oDoc.AllReferencedDocuments
		oProps = BuildName(oRefDoc)
		iTotal = iTotal+1
        oBaseName = System.IO.Path.GetFileNameWithoutExtension(oRefDoc.FullFileName)		
        oPathAndName = System.IO.Path.GetDirectoryName(oRefDoc.FullFileName) & "\" & oBaseName
		
        If(System.IO.File.Exists(oPathAndName & ".idw")) Then
            oDrawDoc = ThisApplication.Documents.Open(oPathAndName & ".idw", True)
            oDataMedium.FileName = oFolder & "\" & oBaseName & oProps & ".pdf"
            Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
			oDrawDoc.Close
			iCreated = iCreated + 1
			
        Else
			iMissing = iMissing + 1
			sNoDrawList = sNoDrawList & vbLf & iMissing & ". " & oBaseName
        End If
    Next
    '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   
    '- - - - - - - - - - - - - Top Level Drawing - - - - - - - - - - - -
	Dim oTopProps As String
		If Not iProperties.Value("Summary", "Title") = "" Then
			oTopProps = "_" & iProperties.Value("Summary", "Title")
		End If
		
		If Not iProperties.Value("Project", "Revision Number") = "" Then
			oTopProps = oTopProps & "_Rev" & iProperties.Value("Project", "Revision Number")
		End If
	
    oBaseName = System.IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName)
	oPathAndName = System.IO.Path.GetDirectoryName(oDoc.FullFileName) & "\" & oBaseName
    oDataMedium.FileName = oFolder & "\" & oBaseName & oTopProps & ".pdf"
	iTotal = iTotal+1
    
    If oDoc.DocumentType = kAssemblyDocumentObject Then
        oDrawDoc = ThisApplication.Documents.Open(oPathAndName & ".idw", True)
		Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
        oDrawDoc.Close   
    End If
    ''- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
   'Output prompt
    MessageBox.Show("New files created in: " & vbLf & oFolder & vbCrLf & vbCrLf & vbCrLf _
	& "Files found without drawings: " & vbLf & iMissing & " / " & iTotal & vbCrLf _
	& sNoDrawList, "iLogic")
	
    Shell("explorer.exe " & oFolder, vbNormalFocus)
	    
End Sub


Private Function BuildName(oDoc As Document) As String
	Dim oSumProps As PropertySet = oDoc.PropertySets.Item("Inventor Summary Information")
	Dim oTitle As String
	Dim oRev As String
	Dim oName As String	
		
	If Not oSumProps.Item("Title").Value = "" Then
		oTitle = oSumProps.Item("Title").Value
		oName = "_" & oTitle
	End If

	If Not oSumProps.Item("Revision Number").Value = "" Then
		oRev = oSumProps.Item("Revision Number").Value
		oName = oName & "_Rev" & oRev
	End If

	Return oName	
End Function


Sub ConfigurePDFAddinSettings(ByRef PDFAddIn As TranslatorAddIn, ByRef oContext As TranslationContext, ByRef oOptions As NameValueMap, ByRef oDataMedium As DataMedium)

    PDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
    oContext = ThisApplication.TransientObjects.CreateTranslationContext
    oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
        
    oOptions = ThisApplication.TransientObjects.CreateNameValueMap
    oOptions.Value("All_Color_AS_Black") = 1
    oOptions.Value("Remove_Line_Weights") = 0
    oOptions.Value("Vector_Resolution") = 400
    oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
'    oOptions.Value("Custom_Begin_Sheet") = 1
'    oOptions.Value("Custom_End_Sheet") = 1

    oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
End Sub

 

 

Really appreciate the help, thank you!

 

Best,

Fredrik

0 Likes
Message 11 of 11

WCrihfield
Mentor
Mentor

Good to hear it's working for you now.

Yes, I considered just getting rid of the subs & functions, and just doing it all by straight code, but I didn't know if there might be more to your code, or if you may be trying to access them from other processes.

The variable name supplied within a call to a function should always be different than the equivalent variable name defined within function's definition.  This is just good coding practice.  It helps ensure that the object or value being passed to the function is being received from that call only, and not just from the general code at whatever position within the code that the function's definition appears.  Keeps it separate and clean.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)