PDF batch according to BOM structure

PDF batch according to BOM structure

Kassenbouw
Enthusiast Enthusiast
1,131 Views
5 Replies
Message 1 of 6

PDF batch according to BOM structure

Kassenbouw
Enthusiast
Enthusiast

Hi All,

 

I have found a ilogic rule on this forum for printing PDF files from an assembly including all parts witch works fine for this option.

 

'Set Folder options as you see fit here

oFolder = 'Enter Folder String here.

'PDF Options
PDFAddIn = oAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
oPDFContext = oTG.CreateTranslationContext
oPDFContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oPDFOptions = oTG.CreateNameValueMap
oPDFDataMedium = oTG.CreateDataMedium
oPDFOptions.Value("All_Color_AS_Black") = 0
oPDFOptions.Value("Remove_Line_Weights") = 1
oPDFOptions.Value("Vector_Resolution") = 4800
oPDFOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets

'Generate List of all Documents Referenced for this Assembly.
oRefDocs = ThisApplication.ActiveDocument.AllReferencedDocuments

'Generate Document Reference.
Dim oDoc As Document

'Peruse each document in generated Documents list.
For Each oDoc In oRefDocs

	'Generate text string of .idw file to look for in the same folder as component file.
	idwPathName = Left(oDoc.FullDocumentName, Len(oDoc.FullDocumentName) - 3) & "idw"

	'Check if generated drawing file name exists.
	If System.IO.File.Exists(idwPathName) Then

		'Open drawing file.
		oDrawDoc = ThisApplication.Documents.Open(idwPathName, False)

		'Remove idw extension for PDF save.
		oFileName = Left(oDoc.DisplayName, Len(oDoc.DisplayName) - 3)

		'Generate text string of idw name and folder location.
		oPDFDataMedium.FileName = oFolder + "\" + oFileName + "pdf"

		'Save PDF Copy.
		Call PDFAddIn.SaveCopyAs(oDrawDoc, oPDFContext, oPDFOptions, oPDFDataMedium)

		'Close drawing file.
		oDrawDoc.Close(True)

	End If
Next

'Top Level Document reference.
oDoc = ThisApplication.ActiveDocument

'Generate text string of .idw file to look for in the same folder as Assembly file.
idwPathName = Left(oDoc.FullDocumentName, Len(oDoc.FullDocumentName) - 3) & "idw"

'Check if generated drawing file name exists.
If System.IO.File.Exists(idwPathName) Then

	'Open drawing file.
	oDrawDoc = ThisApplication.Documents.Open(idwPathName, False)

	'Remove idw extension for PDF save.
	oFileName = Left(oDoc.DisplayName, Len(oDoc.DisplayName) - 3)

	''Generate text string of idw name and folder location.
	oPDFDataMedium.FileName = oFolder + "\" + oFileName + "pdf"

	'Save PDF Copy.
	Call PDFAddIn.SaveCopyAs(oDrawDoc, oPDFContext, oPDFOptions, oPDFDataMedium)

	'Close drawing file.
	oDrawDoc.Close(True)
End If

https://forums.autodesk.com/t5/inventor-customization/ilogic-pdf-batch-print-from-assembly-include-s...

 

There are some things I would like to add/change to make this rule work for me.

 

1). This rule works from file’s in the workspace. How do I get the latest files from my workspace?

 

Something like:

DownloadVaultFileToWorkspace("oDoc", False)

 

2). My sub-assemblies are full of suppressed parts. I want to exclude these files in the PDF plot. I think I need to add something like;

 

oComps = ThisDoc.Document.ComponentDefinition.Occurrences

 

For Each oComp In oComps

    If oComp.Visible Then

‘batch to PDF

    Else

‘do nothing.

End If

Next

 

3). I would like to divide the batch publish in top level only and anything else in 2 separate folders according to the bom levels ‘Structured’ and ‘Parts Only’.

 

Kassenbouw_0-1621251740762.jpeg

 

Kassenbouw_1-1621251740763.jpeg

 

I have found some Ilogic code to make the reference to the bomview;

 

Sub  Main BOMQuery() 

    ' a reference to the assembly document. 

    ' This assumes an assembly document is active. 

    Dim oDoc As AssemblyDocument 

    oDoc = ThisApplication.ActiveDocument 

 

    Dim FirstLevelOnly As Boolean 

    If MsgBox("First level only?", vbYesNo) = vbYes Then 

        FirstLevelOnly = True 

    Else 

        FirstLevelOnly = False 

    End If 

     

    ' a reference to the BOM 

    Dim oBOM As BOM 

    oBOM = oDoc.ComponentDefinition.BOM 

     

    ' whether first level only or all levels. 

    If FirstLevelOnly Then 

        oBOM.StructuredViewFirstLevelOnly = True 

    Else 

        oBOM.StructuredViewFirstLevelOnly = False 

    End If 

     

    ' Make sure that the structured view is enabled. 

    oBOM.StructuredViewEnabled = True 

     

    'a reference to the "Structured" BOMView 

    Dim oBOMView As BOMView 

    oBOMView = oBOM.BOMViews.Item("Structured") 

 

Does anyone have any idea how to add these 3 pieces of code together?

1,132 Views
5 Replies
Replies (5)
Message 2 of 6

Kassenbouw
Enthusiast
Enthusiast

Hi Guys,

 

I still didn't manage to get this work for me. In the meantime I've found a topic simular to mine on this forum:

https://forums.autodesk.com/t5/inventor-customization/print-pdf-need-help-skipping-reference/td-p/78...

 

Is there anyone who knows how to make one working rule from these pieces?

 

Thanks a lot

0 Likes
Message 3 of 6

Ralf_Krieg
Advisor
Advisor

Hello

 

To sync your workspace, maybe Vault Sync is useful. It's included in Vault Professional and Workgroup.

 

Invisible and/or suppress doesn't have any affect to the BOM. So if you want to ignore invisible occurrences, you can't use BOM structure. I think you can traverse the occurences structure of the assembly and check visibility and BOMstructure.

Is it correct that PDFs in "Structured" folder should only  be created from first level of BOM view structured?

 

Try this:

 

Sub Main()

'Pre Rule Checks
	If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
		MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
		Exit Sub
	End If

	RUsure = MessageBox.Show ( _
		"This will create a PDF file for all of the visible assembly 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 visible components?" _
		& vbLf & "This could take a while.", "iLogic  - Batch Output PDFs ",MessageBoxButtons.YesNo)

	If RUsure = vbNo Then
		Return
		Else
	End If
'/Pre Rule Checks

	Dim oAsmDoc As AssemblyDocument= ThisApplication.ActiveDocument
	Dim oAssyDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition
	
	oAsmPathAndName = Left(oAsmDoc.FullFileName, Len(oAsmDoc.FullFileName) -4)
	oAsmDisplayName = Left(oAsmDoc.DisplayName, Len(oAsmDoc.DisplayName) -4)
	oPath = ThisDoc.Path
	oFolder = oPath & "\" & oAsmDisplayName & " PDF Files"	

	Call ConfigPDFSettings(oFolder)
	
	If (System.IO.File.Exists(oAsmPathAndName & ".idw")) Then
		oAsmDrawingDoc = ThisApplication.Documents.Open(oAsmPathAndName & ".idw", True)
		'Print Top Level

		oDatamedium.FileName = oFolder & "\" & oAsmDisplayName & ".pdf"
		Call oPDFAddIn.SaveCopyAs(oAsmDrawingDoc, oContext, oOptions, oDatamedium)
		oAsmDrawingDoc.Close
	End If
	
	Dim oOccs As ComponentOccurrences = oAsmDoc.ComponentDefinition.Occurrences
	Call RecurseOccs(oOccs)
	
	Dim oOccsEnum As ComponentOccurrencesEnumerator = oAsmDoc.ComponentDefinition.Occurrences.AllLeafOccurrences 
	Call ProcessLeafOccs(oOccsEnum)
	
	MessageBox.Show("New Files Created in: " & vbLf & oFolder, "iLogic")
	Shell("explorer.exe " & oFolder,vbNormalFocus)
End Sub

	Dim oFolder As String
	Dim oContext As TranslationContext
	Dim oOptions As NameValueMap 
	Dim oDatamedium As DataMedium 
	Dim oPDFAddIn As TranslatorAddIn

Sub RecurseOccs(oOccs As ComponentOccurrences)
		For Each oOcc As ComponentOccurrence In oOccs
			If oOcc.Visible = True And oOcc.Suppressed = False Then
				If oOcc.BOMStructure = BOMStructureEnum.kPhantomBOMStructure Then
					If oOcc.SubOccurrences.Count>0 Then
						RecurseOccs(oOcc.SubOccurrences)
					End If
				Else
					PrintOcc(oOcc, "Structured")
				End If
			End If
		Next
End Sub
	
Sub ProcessLeafOccs(oOccs As ComponentOccurrencesEnumerator)
		For Each oOcc As ComponentOccurrence In oOccs
			If oOcc.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Then
				If oOcc.Visible = True And oOcc.Suppressed = False Then
					PrintOcc(oOcc, "Parts only")
				End If
			End If
		Next
End Sub
	
Sub PrintOcc(oOcc As ComponentOccurrence, sTargetfolder As String)
		Dim oCompDef As ComponentDefinition
		oCompDef = oOcc.Definition

		idwPathName = Left(oCompDef.Document.FullDocumentName, Len(oCompDef.Document.FullDocumentName) - 3) & "idw"
		If(System.IO.File.Exists(idwPathName)) Then
			oFileName = Left(oCompDef.Document.DisplayName, Len(oCompDef.Document.DisplayName) -3)
			If Not System.IO.Directory.Exists(oFolder & "\" & sTargetfolder) Then
		    	System.IO.Directory.CreateDirectory(oFolder & "\" & sTargetfolder)
			End If
			
			oDatamedium.FileName = oFolder & "\" & sTargetFolder & "\" & oFileName & "pdf"
			
			If Not (System.IO.File.Exists(oDatamedium.FileName)) Then
				Dim oDrawDoc As DrawingDocument
				oDrawDoc = ThisApplication.Documents.Open(idwPathName, True)
				Call oPDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDatamedium)
				oDrawDoc.Close(True)
			End If
		Else
			'If the model has no drawing of the same path and name - do nothing
		End If
End Sub

Sub ConfigPDFSettings(oFolder As String)
		
		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

		'If oPDFAddIn.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

		If Not System.IO.Directory.Exists(oFolder) Then
		    System.IO.Directory.CreateDirectory(oFolder)
		End If
End Sub

 

 


R. Krieg
RKW Solutions
www.rkw-solutions.com
0 Likes
Message 4 of 6

Kassenbouw
Enthusiast
Enthusiast

Hi Krieg, thanks for the reply. 

 

This rule only opens the drawing and then gives the error Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL)).

0 Likes
Message 5 of 6

Kassenbouw
Enthusiast
Enthusiast

Unfortunately I didn't get this working in the meantime. I've tried to approach only the top level with a minor change. The weird thing is that the rule skips some parts and doesn't mention them in the messagebox. Does anyone have an idea what's wrong with this code and could help me to get this working?

 

Here is the code:

 

aSub Main()
    Dim oAsmDoc As AssemblyDocument
    oAsmDoc = ThisApplication.ActiveDocument
    oAsmName = Left(oAsmDoc.DisplayName, Len(oAsmDoc.DisplayName) -4)
    
    'check that the active document is an assembly file
    If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
        MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
        Exit Sub
    End If
    
    'get user input
    If 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) = 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)
    
    oPath = ThisDoc.Path
    oFolder = oPath & "\" & oAsmName & " PDF Files"
    
    If Not System.IO.Directory.Exists(oFolder) Then
        System.IO.Directory.CreateDirectory(oFolder)
    End If
    
    '- - - - - - - - - - - - -Component Drawings - - - - - - - - - - - -'look at the files referenced by the assembly
    Dim oRefDoc As Document
    
'    I have changed this one from AllReferencedDocuments to ReferencedDocuments 
    For Each oRefDoc In oAsmDoc.ReferencedDocuments 
        idwPathName = Left(oRefDoc.FullDocumentName, Len(oRefDoc.FullDocumentName) - 3) & "idw"

        If(System.IO.File.Exists(idwPathName)) Then
            Dim oDrawDoc As DrawingDocument
            oDrawDoc = ThisApplication.Documents.Open(idwPathName, True)
            oFileName = Left(oRefDoc.DisplayName, Len(oRefDoc.DisplayName) -3)

            On Error Resume Next
                oDataMedium.FileName = oFolder & "\" & oFileName & "pdf"
                Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
                oDrawDoc.Close
            On Error GoTo 0
        End If
    Next
    '- - - - - - - - - - - - -
    
    '- - - - - - - - - - - - -Top Level Drawing - - - - - - - - - - - -
'    Dim oAsmDrawingDoc As DrawingDocument
'    oAsmDrawingDocName = ThisDoc.PathAndFileName(False) & ".idw"
    
'    If(System.IO.File.Exists(oAsmDrawingDocName)) Then
'    oAsmDrawingDoc = ThisApplication.Documents.Open(ThisDoc.ChangeExtension(".idw"), True)
'    oAsmDrawingDoc = ThisApplication.Documents.Open(oAsmDrawingDocName, True) 
'    oAsmDrawingName = Left(oAsmDrawingDoc.DisplayName, Len(oAsmDrawingDoc.DisplayName) -3) 
    
'    On Error Resume Next 
'        oDataMedium.FileName = oFolder & "\" & oAsmDrawingName & "pdf" 
'        Call PDFAddIn.SaveCopyAs(oAsmDrawingDoc, oContext, oOptions, oDataMedium)
'        oAsmDrawingDoc.Close 
'    End If 

End Sub

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

    oPath = ThisDoc.Path
    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

 

0 Likes
Message 6 of 6

A.Acheson
Mentor
Mentor

I tested this code and it seems to work ok once I put the files n the correct position. There was no error trap  if the file didn't exist which is maybe why you were not able to identify the issue. In my case I had not put the drawing in the correct location. So according to "idwPathName" the loop through the assembly was trying to open the file from a different path. I placed a message box there to check the path and also put in an error trap to show a list box if one or more files is not found.

 

You may need to adjust the "idwPathName"  according to where you store the drawing. 

 

Sub Main()
    Dim oAsmDoc As AssemblyDocument
    oAsmDoc = ThisApplication.ActiveDocument
    oAsmName = Left(oAsmDoc.DisplayName, Len(oAsmDoc.DisplayName) -4)
    
    'check that the active document is an assembly file
    If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
        MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
        Exit Sub
    End If
    
    'get user input
    If 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) = vbNo Then
        
        Exit Sub
    End If
      
	Dim i As Integer = 0  
	Dim DrawingPathError As New ArrayList
	
    Dim PDFAddIn As TranslatorAddIn
    Dim oContext As TranslationContext
    Dim oOptions As NameValueMap
    Dim oDataMedium As DataMedium
    
    Call ConfigurePDFAddinSettings(PDFAddIn, oContext, oOptions, oDataMedium)
    
    oPath = ThisDoc.Path
    oFolder = oPath & "\" & oAsmName & " PDF Files"
  
    If Not System.IO.Directory.Exists(oFolder) Then
        System.IO.Directory.CreateDirectory(oFolder)
    End If
    
    '- - - - - - - - - - - - -Component Drawings - - - - - - - - - - - -'look at the files referenced by the assembly
    Dim oRefDoc As Document
    
'    I have changed this one from AllReferencedDocuments to ReferencedDocuments 
    For Each oRefDoc In oAsmDoc.ReferencedDocuments 
		
        idwPathName = Left(oRefDoc.FullDocumentName, Len(oRefDoc.FullDocumentName) -3) & "dwg"
		oFileName = Left(oRefDoc.DisplayName, Len(oRefDoc.DisplayName) -3)
		'MessageBox.Show(idwPathName, "idwPathName")

        If (System.IO.File.Exists(idwPathName))= True Then
            Dim oDrawDoc As DrawingDocument
            oDrawDoc = ThisApplication.Documents.Open(idwPathName, True)
            On Error Resume Next
                oDataMedium.FileName = oFolder & "\" & oFileName & "pdf"
				MessageBox.Show(oDataMedium.FileName, "Title")

                Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
                oDrawDoc.Close
            On Error GoTo 0
		Else
			i=i+1
			DrawingPathError.Add(oFileName)
			'MessageBox.Show("Error Finding Path", "iLogic")
        End If
    Next
	
	If i = 1 Then
		d0 = InputListBox("Prompt", DrawingPathError, d0, Title := "Error Finding Drawing Path", ListName := "Drawing Names")
	End If
	
    '- - - - - - - - - - - - -
    
    '- - - - - - - - - - - - -Top Level Drawing - - - - - - - - - - - -
'    Dim oAsmDrawingDoc As DrawingDocument
'    oAsmDrawingDocName = ThisDoc.PathAndFileName(False) & ".idw"
    
'    If(System.IO.File.Exists(oAsmDrawingDocName)) Then
'    oAsmDrawingDoc = ThisApplication.Documents.Open(ThisDoc.ChangeExtension(".idw"), True)
'    oAsmDrawingDoc = ThisApplication.Documents.Open(oAsmDrawingDocName, True) 
'    oAsmDrawingName = Left(oAsmDrawingDoc.DisplayName, Len(oAsmDrawingDoc.DisplayName) -3) 
    
'    On Error Resume Next 
'        oDataMedium.FileName = oFolder & "\" & oAsmDrawingName & "pdf" 
'        Call PDFAddIn.SaveCopyAs(oAsmDrawingDoc, oContext, oOptions, oDataMedium)
'        oAsmDrawingDoc.Close 
'    End If 

End Sub

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

    oPath = ThisDoc.Path
    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

 

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