Ilogic create step from first level parts

Ilogic create step from first level parts

erich.thommen
Explorer Explorer
1,440 Views
19 Replies
Message 1 of 20

Ilogic create step from first level parts

erich.thommen
Explorer
Explorer

Hi

I found alot of nice solution to create step from all parts in assembly and it works perfect.

But I want to create Step files just from the first level parts in assembly (.ipt & .iam)

How can I reach the files from the first level in assembly?

 

Dim oRefDocs As DocumentsEnumerator
oRefDocs = oAsmDoc.AllReferencedDocuments    
Dim oRefDoc As Document
'work the referenced models
For Each oRefDoc In oRefDocs
	Dim oCurFile As Document

 the above mentioned code section addresses all parts from all levels.

 

For a short suggestion how I can solve the problem I would be grateful

 

Thx

 

Erich

0 Likes
1,441 Views
19 Replies
Replies (19)
Message 2 of 20

raith-mb
Advocate
Advocate

change this line:

oRefDocs = oAsmDoc.AllReferencedDocuments  'all files
oRefDocs = oAsmDoc.ReferencedDocuments     '1st level only

 

Roland

Message 3 of 20

Kassenbouw
Enthusiast
Enthusiast

Hi,

 

Could you share the code you are using. I'm trying to do the same but for PDF batching.

 

Thanks!

0 Likes
Message 4 of 20

erich.thommen
Explorer
Explorer

Hi Raith-mb

 

Thank you soo much. Just change a small word and it has an huge efect.

It works no perfect. Thank you.

0 Likes
Message 5 of 20

erich.thommen
Explorer
Explorer

Hi Kassenbouw

 

In .txt file you'll find the code for create pdf from assembly. Now only for first level parts.

Maybe helpfull for you: The code generate pdf with the revision in the filename.

 

cheers

0 Likes
Message 6 of 20

Kassenbouw
Enthusiast
Enthusiast

Thanks erich.thommen,

 

I took the reference part from your code and placed in mine. I see it is only creating PDF files from the top level now. But for some reason it skips files. I did a couple of tests in assemblys but it keeps skipping files. All the files have a drawning so this should not be the case. 

 

Sub Main()
    Dim oAsmDoc As Document
    oAsmDoc = ThisDoc.Document
    oAsmDocName = System.IO.Path.GetDirectoryName(oAsmDoc.FullFileName) & "\" & System.IO.Path.GetFileNameWithoutExtension(oAsmDoc.FullFileName)
    
   If Not (oAsmDoc.DocumentType = kAssemblyDocumentObject Or oAsmDoc.DocumentType = kDrawingDocumentObject) Then
        MessageBox.Show("Please run this rule from the assembly or drawing files.", "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 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)
    
    oFolder = oAsmDocName & " PDF Files"
    If Not System.IO.Directory.Exists(oFolder) Then
        System.IO.Directory.CreateDirectory(oFolder)
    End If
    
    '- - - - - - - - - - - - -Component Drawings - - - - - - - - - - - -
    Dim oRefDoc As Document
    Dim oDrawDoc As DrawingDocument
    
	For Each oRefDoc In oAsmDoc.ReferencedDocuments
        oBaseName = System.IO.Path.GetFileNameWithoutExtension(oRefDoc.FullFileName)
        oPathAndName = System.IO.Path.GetDirectoryName(oRefDoc.FullFileName) & "\" & oBaseName
        If(System.IO.File.Exists(oPathAndName & ".dwg")) Then
            oDrawDoc = ThisApplication.Documents.Open(oPathAndName & ".dwg", False)
            oDataMedium.FileName = oFolder & "\" & oBaseName & ".pdf"
            Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
            oDrawDoc.Close
        Else
            oNoDwgString = oNoDwgString & vbLf & dwgPathName
        End If
    Next
	
	For Each oRefDoc In oAsmDoc.ReferencedDocuments
        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", False)
            oDataMedium.FileName = oFolder & "\" & oBaseName & ".pdf"
            Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
            oDrawDoc.Close
        Else
           oNoDwgString = oNoDwgString & vbLf & idwPathName
        End If
    Next
	
    '- - - - - - - - - - - - -
    
    '- - - - - - - - - - - - -Top Level Drawing - - - - - - - - - - - -
'    oBaseName = System.IO.Path.GetFileNameWithoutExtension(oAsmDoc.FullFileName)
'    oPathAndName = System.IO.Path.GetDirectoryName(oAsmDoc.FullFileName) & "\" & oBaseName
'    oDataMedium.FileName = oFolder & "\" & oBaseName & ".pdf"
    
'    If oAsmDoc.DocumentType = kAssemblyDocumentObject Then
'        oDrawDoc = ThisApplication.Documents.Open(oPathAndName & ".dwg", False)
'        Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
'        oDrawDoc.Close
'    ElseIf oAsmDoc.DocumentType = kDrawingDocumentObject Then
'        Call PDFAddIn.SaveCopyAs(oAsmDoc, oContext, oOptions, oDataMedium)    
'    End If
    '- - - - - - - - - - - - -
    
    MessageBox.Show("New Files Created in: " & vbLf & oFolder, "iLogic")
    MsgBox("Files found without drawings: " & vbLf & oNoDwgString)
    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

 

Is there something not correct or am I missing something?

 

Thanks!

 

 

0 Likes
Message 7 of 20

erich.thommen
Explorer
Explorer

Hi Kassenbow

 

It's importand that the .idw has the same path like the 3D Modell.

If not, no Pdf will generated.

If you work with vault, you have to load .idw also to your local work space.

 

 

I hope I solved your problem.

 

Best regards

 

Erich

0 Likes
Message 8 of 20

Kassenbouw
Enthusiast
Enthusiast

Hi Guys,

 

Is there a way to include the first level children from a phantom assembly? I would like to include these in the batch process. 

 

Thanks

 

0 Likes
Message 9 of 20

Ralf_Krieg
Advisor
Advisor

Hello

 

Is it right to skip the phantom assemblies and export the first level children instead? Can you try this one?

 

Sub Main()
    Dim oAsmDoc As Document
    oAsmDoc = ThisDoc.Document
    oAsmDocName = System.IO.Path.GetDirectoryName(oAsmDoc.FullFileName) & "\" & System.IO.Path.GetFileNameWithoutExtension(oAsmDoc.FullFileName)
    
   If Not (oAsmDoc.DocumentType = kAssemblyDocumentObject Or oAsmDoc.DocumentType = kDrawingDocumentObject) Then
        MessageBox.Show("Please run this rule from the assembly or drawing files.", "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 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)
    
    oFolder = oAsmDocName & " PDF Files"
    If Not System.IO.Directory.Exists(oFolder) Then
        System.IO.Directory.CreateDirectory(oFolder)
    End If
    
	
	
    '- - - - - - - - - - - - -Component Drawings - - - - - - - - - - - -
    Dim oRefColl As New List(Of String)
	Dim oRefDoc As Document
	For Each oRefdoc In oAsmDoc.ReferencedDocuments
		oRefColl.Add(oRefDoc.FullFileName)
	Next
	
    Dim oDrawDoc As DrawingDocument
    
	For Each oRefDoc In oAsmDoc.ReferencedDocuments
		If oRefDoc.ComponentDefinition.BOMStructure = BOMStructureEnum.kPhantomBOMStructure Then
			If oRefDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
				ProcessPhantomChilds(oRefDoc, oRefColl, oDataMedium, PDFAddIn)
			End If
		Else
	        oBaseName = System.IO.Path.GetFileNameWithoutExtension(oRefDoc.FullFileName)
	        oPathAndName = System.IO.Path.GetDirectoryName(oRefDoc.FullFileName) & "\" & oBaseName
	        If(System.IO.File.Exists(oPathAndName & ".dwg")) Then
	            oDrawDoc = ThisApplication.Documents.Open(oPathAndName & ".dwg", False)
	            oDataMedium.FileName = oFolder & "\" & oBaseName & ".pdf"
	            Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
	            oDrawDoc.Close
	        Else
	            oNoDwgString = oNoDwgString & vbLf & dwgPathName
	        End If
	        If(System.IO.File.Exists(oPathAndName & ".idw")) Then
	            oDrawDoc = ThisApplication.Documents.Open(oPathAndName & ".idw", False)
	            oDataMedium.FileName = oFolder & "\" & oBaseName & ".pdf"
	            Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
	            oDrawDoc.Close
	        Else
	           oNoDwgString = oNoDwgString & vbLf & idwPathName
	        End If
		End If
    Next
	
    '- - - - - - - - - - - - -
    
    '- - - - - - - - - - - - -Top Level Drawing - - - - - - - - - - - -
'    oBaseName = System.IO.Path.GetFileNameWithoutExtension(oAsmDoc.FullFileName)
'    oPathAndName = System.IO.Path.GetDirectoryName(oAsmDoc.FullFileName) & "\" & oBaseName
'    oDataMedium.FileName = oFolder & "\" & oBaseName & ".pdf"
    
'    If oAsmDoc.DocumentType = kAssemblyDocumentObject Then
'        oDrawDoc = ThisApplication.Documents.Open(oPathAndName & ".dwg", False)
'        Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
'        oDrawDoc.Close
'    ElseIf oAsmDoc.DocumentType = kDrawingDocumentObject Then
'        Call PDFAddIn.SaveCopyAs(oAsmDoc, oContext, oOptions, oDataMedium)    
'    End If
    '- - - - - - - - - - - - -
    
    MessageBox.Show("New Files Created in: " & vbLf & oFolder, "iLogic")
    MsgBox("Files found without drawings: " & vbLf & oNoDwgString)
    Shell("explorer.exe " & oFolder,vbNormalFocus)
End Sub

Private Sub ProcessPhantomChilds(ByVal oAsmDoc As AssemblyDocument, ByVal oRefColl As List(Of String),ByRef oDataMedium As DataMedium, ByRef PDFAddIn As TranslatorAddIn)
	
	Dim oRefDoc As Document
    Dim oDrawDoc As DrawingDocument
	
	For Each oRefDoc In oAsmDoc.ReferencedDocuments
		If Not oRefColl.Contains(oRefDoc.FullFileName) Then
			' Uncomment these lines if phantom assemblies should be recursive traversed
			'If oRefDoc.ComponentDefinition.BOMStructure = BOMStructureEnum.kPhantomBOMStructure Then
			'	If oRefDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
			'		ProcessPhantomChilds(oRefDoc, oDataMedium, PDFAddIn)
			'	End If
			'Else
				oBaseName = System.IO.Path.GetFileNameWithoutExtension(oRefDoc.FullFileName)
		        oPathAndName = System.IO.Path.GetDirectoryName(oRefDoc.FullFileName) & "\" & oBaseName
		        If(System.IO.File.Exists(oPathAndName & ".dwg")) Then
		            oDrawDoc = ThisApplication.Documents.Open(oPathAndName & ".dwg", False)
		            oDataMedium.FileName = oFolder & "\" & oBaseName & ".pdf"
		            Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
		            oDrawDoc.Close
		        Else
		            oNoDwgString = oNoDwgString & vbLf & dwgPathName
		        End If
		        If(System.IO.File.Exists(oPathAndName & ".idw")) Then
		            oDrawDoc = ThisApplication.Documents.Open(oPathAndName & ".idw", False)
		            oDataMedium.FileName = oFolder & "\" & oBaseName & ".pdf"
		            Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
		            oDrawDoc.Close
		        Else
		           oNoDwgString = oNoDwgString & vbLf & idwPathName
		        End If
			'End If
		End If
	Next
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

 

 


R. Krieg
RKW Solutions
www.rkw-solutions.com
0 Likes
Message 10 of 20

Kassenbouw
Enthusiast
Enthusiast

Hi Krieg,

 

The phantom assembly is a sub-assembly in my assembly. I want to run this rule and get the PDF files from all first level children, including the first level children in this phantom sub-assembly. 

When I run this rule I'm getting an error:

 

System.ArgumentException: The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Inventor.TranslatorAddIn.SaveCopyAs(Object SourceObject, TranslationContext Context, NameValueMap Options, DataMedium TargetData)
at ThisRule.ProcessPhantomChilds(AssemblyDocument oAsmDoc, List`1 oRefColl, DataMedium& oDataMedium, TranslatorAddIn& PDFAddIn)
at ThisRule.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

Thanks

0 Likes
Message 11 of 20

Ralf_Krieg
Advisor
Advisor

Hello

 

Sorry for the error message, I can not try out the code myself at the moment. I think it's cause there are missing variables oContext and oOptions in the subroutine. Can you test the version below please?

 

 

Option Explicit on
Sub Main()
    Dim oAsmDoc As Document
    oAsmDoc = ThisDoc.Document
    Dim oAsmDocName As String  = System.IO.Path.GetDirectoryName(oAsmDoc.FullFileName) & "\" & System.IO.Path.GetFileNameWithoutExtension(oAsmDoc.FullFileName)
    
   If Not (oAsmDoc.DocumentType = kAssemblyDocumentObject Or oAsmDoc.DocumentType = kDrawingDocumentObject) Then
        MessageBox.Show("Please run this rule from the assembly or drawing files.", "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 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)
    
    Dim oFolder As String = oAsmDocName & " PDF Files"
    If Not System.IO.Directory.Exists(oFolder) Then
        System.IO.Directory.CreateDirectory(oFolder)
    End If
    
	
	
    '- - - - - - - - - - - - -Component Drawings - - - - - - - - - - - -
    Dim oRefColl As New List(Of String)
	Dim oRefDoc As Document
	For Each oRefdoc In oAsmDoc.ReferencedDocuments
		oRefColl.Add(oRefDoc.FullFileName)
	Next
	
    Dim oDrawDoc As DrawingDocument
    Dim oNoDwgString As String
	
	For Each oRefDoc In oAsmDoc.ReferencedDocuments
		If oRefDoc.ComponentDefinition.BOMStructure = BOMStructureEnum.kPhantomBOMStructure Then
			If oRefDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
				ProcessPhantomChilds(oRefDoc, oRefColl, PDFAddIn, oContext, oOptions, oDataMedium, oFolder)
			End If
		Else
	        Dim oBaseName As String = System.IO.Path.GetFileNameWithoutExtension(oRefDoc.FullFileName)
	        Dim oPathAndName As String = System.IO.Path.GetDirectoryName(oRefDoc.FullFileName) & "\" & oBaseName
	        If(System.IO.File.Exists(oPathAndName & ".dwg")) Then
	            oDrawDoc = ThisApplication.Documents.Open(oPathAndName & ".dwg", False)
	            oDataMedium.FileName = oFolder & "\" & oBaseName & ".pdf"
	            Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
	            oDrawDoc.Close
	        Else
	            oNoDwgString = oNoDwgString & vbLf & oRefDoc.FullFileName 'dwgPathName
	        End If
	        If(System.IO.File.Exists(oPathAndName & ".idw")) Then
	            oDrawDoc = ThisApplication.Documents.Open(oPathAndName & ".idw", False)
	            oDataMedium.FileName = oFolder & "\" & oBaseName & ".pdf"
	            Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
	            oDrawDoc.Close
	        Else
	           oNoDwgString = oNoDwgString & vbLf & oRefDoc.FullFileName 'idwPathName
	        End If
		End If
    Next
	
    '- - - - - - - - - - - - -
    
    '- - - - - - - - - - - - -Top Level Drawing - - - - - - - - - - - -
'    oBaseName = System.IO.Path.GetFileNameWithoutExtension(oAsmDoc.FullFileName)
'    oPathAndName = System.IO.Path.GetDirectoryName(oAsmDoc.FullFileName) & "\" & oBaseName
'    oDataMedium.FileName = oFolder & "\" & oBaseName & ".pdf"
    
'    If oAsmDoc.DocumentType = kAssemblyDocumentObject Then
'        oDrawDoc = ThisApplication.Documents.Open(oPathAndName & ".dwg", False)
'        Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
'        oDrawDoc.Close
'    ElseIf oAsmDoc.DocumentType = kDrawingDocumentObject Then
'        Call PDFAddIn.SaveCopyAs(oAsmDoc, oContext, oOptions, oDataMedium)    
'    End If
    '- - - - - - - - - - - - -
    
    MessageBox.Show("New Files Created in: " & vbLf & oFolder, "iLogic")
    MsgBox("Files found without drawings: " & vbLf & oNoDwgString)
    Shell("explorer.exe " & oFolder,vbNormalFocus)
End Sub

Private Sub ProcessPhantomChilds(ByVal oAsmDoc As AssemblyDocument, ByVal oRefColl As List(Of String), ByRef PDFAddIn As TranslatorAddIn, ByVal oContext As TranslationContext, ByVal oOptions As NameValueMap, ByRef oDataMedium As DataMedium , ByVal oFolder As String)
	
	Dim oRefDoc As Document
    Dim oDrawDoc As DrawingDocument
	Dim oNoDwgString As String
	
	For Each oRefDoc In oAsmDoc.ReferencedDocuments
		If Not oRefColl.Contains(oRefDoc.FullFileName) Then
			' Uncomment these lines if phantom assemblies should be recursive traversed
			'If oRefDoc.ComponentDefinition.BOMStructure = BOMStructureEnum.kPhantomBOMStructure Then
			'	If oRefDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
			'		ProcessPhantomChilds(oRefDoc, oDataMedium, PDFAddIn)
			'	End If
			'Else
				Dim oBaseName As String = System.IO.Path.GetFileNameWithoutExtension(oRefDoc.FullFileName)
		        Dim oPathAndName As String  = System.IO.Path.GetDirectoryName(oRefDoc.FullFileName) & "\" & oBaseName
		        If(System.IO.File.Exists(oPathAndName & ".dwg")) Then
		            oDrawDoc = ThisApplication.Documents.Open(oPathAndName & ".dwg", False)
		            oDataMedium.FileName = oFolder & "\" & oBaseName & ".pdf"
		            Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
		            oDrawDoc.Close
		        Else
		            oNoDwgString = oNoDwgString & vbLf & oRefDoc.FullFileName' dwgPathName
		        End If
		        If(System.IO.File.Exists(oPathAndName & ".idw")) Then
		            oDrawDoc = ThisApplication.Documents.Open(oPathAndName & ".idw", False)
		            oDataMedium.FileName = oFolder & "\" & oBaseName & ".pdf"
		            Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
		            oDrawDoc.Close
		        Else
		           oNoDwgString = oNoDwgString & vbLf & oRefDoc.FullFileName 'idwPathName
		        End If
			'End If
		End If
	Next
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

 


R. Krieg
RKW Solutions
www.rkw-solutions.com
0 Likes
Message 12 of 20

Kassenbouw
Enthusiast
Enthusiast

This works great! 

To make this perfect, is there a way to exclude the 'Reference' parts in the assembly?

 

Thanks Krieg!

0 Likes
Message 13 of 20

Ralf_Krieg
Advisor
Advisor

Hello

 

We can check the document BOM structure and skip these documents. But, if you override this document setting by right clicking an occurrence in your assembly and set the Reference BOM structure, this is not recognized.

 

Option Explicit on
Sub Main()
    Dim oAsmDoc As Document
    oAsmDoc = ThisDoc.Document
    Dim oAsmDocName As String  = System.IO.Path.GetDirectoryName(oAsmDoc.FullFileName) & "\" & System.IO.Path.GetFileNameWithoutExtension(oAsmDoc.FullFileName)
    
   If Not (oAsmDoc.DocumentType = kAssemblyDocumentObject Or oAsmDoc.DocumentType = kDrawingDocumentObject) Then
        MessageBox.Show("Please run this rule from the assembly or drawing files.", "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 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)
    
    Dim oFolder As String = oAsmDocName & " PDF Files"
    If Not System.IO.Directory.Exists(oFolder) Then
        System.IO.Directory.CreateDirectory(oFolder)
    End If
    
	
	
    '- - - - - - - - - - - - -Component Drawings - - - - - - - - - - - -
    Dim oRefColl As New List(Of String)
	Dim oRefDoc As Document
	For Each oRefdoc In oAsmDoc.ReferencedDocuments
		oRefColl.Add(oRefDoc.FullFileName)
	Next
	
    Dim oDrawDoc As DrawingDocument
    Dim oNoDwgString As String
	
	For Each oRefDoc In oAsmDoc.ReferencedDocuments
		If oRefDoc.ComponentDefinition.BOMStructure = BOMStructureEnum.kPhantomBOMStructure Then
			If oRefDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
				ProcessPhantomChilds(oRefDoc, oRefColl, PDFAddIn, oContext, oOptions, oDataMedium, oFolder)
			End If
		ElseIf Not oRefDoc.ComponentDefinition.BOMStructure = BOMStructureEnum.kReferenceBOMStructure Then
	        Dim oBaseName As String = System.IO.Path.GetFileNameWithoutExtension(oRefDoc.FullFileName)
	        Dim oPathAndName As String = System.IO.Path.GetDirectoryName(oRefDoc.FullFileName) & "\" & oBaseName
	        If(System.IO.File.Exists(oPathAndName & ".dwg")) Then
	            oDrawDoc = ThisApplication.Documents.Open(oPathAndName & ".dwg", False)
	            oDataMedium.FileName = oFolder & "\" & oBaseName & ".pdf"
	            Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
	            oDrawDoc.Close
	        Else
	            oNoDwgString = oNoDwgString & vbLf & oRefDoc.FullFileName 'dwgPathName
	        End If
	        If(System.IO.File.Exists(oPathAndName & ".idw")) Then
	            oDrawDoc = ThisApplication.Documents.Open(oPathAndName & ".idw", False)
	            oDataMedium.FileName = oFolder & "\" & oBaseName & ".pdf"
	            Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
	            oDrawDoc.Close
	        Else
	           oNoDwgString = oNoDwgString & vbLf & oRefDoc.FullFileName 'idwPathName
	        End If
		End If
    Next
	
    '- - - - - - - - - - - - -
    
    '- - - - - - - - - - - - -Top Level Drawing - - - - - - - - - - - -
'    oBaseName = System.IO.Path.GetFileNameWithoutExtension(oAsmDoc.FullFileName)
'    oPathAndName = System.IO.Path.GetDirectoryName(oAsmDoc.FullFileName) & "\" & oBaseName
'    oDataMedium.FileName = oFolder & "\" & oBaseName & ".pdf"
    
'    If oAsmDoc.DocumentType = kAssemblyDocumentObject Then
'        oDrawDoc = ThisApplication.Documents.Open(oPathAndName & ".dwg", False)
'        Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
'        oDrawDoc.Close
'    ElseIf oAsmDoc.DocumentType = kDrawingDocumentObject Then
'        Call PDFAddIn.SaveCopyAs(oAsmDoc, oContext, oOptions, oDataMedium)    
'    End If
    '- - - - - - - - - - - - -
    
    MessageBox.Show("New Files Created in: " & vbLf & oFolder, "iLogic")
    MsgBox("Files found without drawings: " & vbLf & oNoDwgString)
    Shell("explorer.exe " & oFolder,vbNormalFocus)
End Sub

Private Sub ProcessPhantomChilds(ByVal oAsmDoc As AssemblyDocument, ByVal oRefColl As List(Of String), ByRef PDFAddIn As TranslatorAddIn, ByVal oContext As TranslationContext, ByVal oOptions As NameValueMap, ByRef oDataMedium As DataMedium , ByVal oFolder As String)
	
	Dim oRefDoc As Document
    Dim oDrawDoc As DrawingDocument
	Dim oNoDwgString As String
	
	For Each oRefDoc In oAsmDoc.ReferencedDocuments
		If Not oRefColl.Contains(oRefDoc.FullFileName) Then
			If Not oRefDoc.ComponentDefinition.BOMStructure = BOMStructureEnum.kReferenceBOMStructure Then
			' Uncomment these lines if phantom assemblies should be recursive traversed
			'If oRefDoc.ComponentDefinition.BOMStructure = BOMStructureEnum.kPhantomBOMStructure Then
			'	If oRefDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
			'		ProcessPhantomChilds(oRefDoc, oDataMedium, PDFAddIn)
			'	End If
			'Else
				Dim oBaseName As String = System.IO.Path.GetFileNameWithoutExtension(oRefDoc.FullFileName)
		        Dim oPathAndName As String  = System.IO.Path.GetDirectoryName(oRefDoc.FullFileName) & "\" & oBaseName
		        If(System.IO.File.Exists(oPathAndName & ".dwg")) Then
		            oDrawDoc = ThisApplication.Documents.Open(oPathAndName & ".dwg", False)
		            oDataMedium.FileName = oFolder & "\" & oBaseName & ".pdf"
		            Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
		            oDrawDoc.Close
		        Else
		            oNoDwgString = oNoDwgString & vbLf & oRefDoc.FullFileName' dwgPathName
		        End If
		        If(System.IO.File.Exists(oPathAndName & ".idw")) Then
		            oDrawDoc = ThisApplication.Documents.Open(oPathAndName & ".idw", False)
		            oDataMedium.FileName = oFolder & "\" & oBaseName & ".pdf"
		            Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
		            oDrawDoc.Close
		        Else
		           oNoDwgString = oNoDwgString & vbLf & oRefDoc.FullFileName 'idwPathName
		        End If
			'End if
			End If
		End If
	Next
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

 

 


R. Krieg
RKW Solutions
www.rkw-solutions.com
0 Likes
Message 14 of 20

Kassenbouw
Enthusiast
Enthusiast

Hi Krieg,

 

Is there a way to modify this code to create PDF files from all files except the first level files? 

Thanks for your help!

0 Likes
Message 15 of 20

Ralf_Krieg
Advisor
Advisor

Hello

 

Can we reconstruct most of the code? The basic idea is to use the already existing BOM structure. Phantom and Reference occurrences are already sort out. Level one in the structured BOM view is the same we collect "by hand". All ChildRows of the Level one and there Childrows and so on are the sublevels you need now. You can select on start if first level or all sublevels should be exported. The export creates a separate directory for each case. At this time, a part existing at first level and in any sublevel to, will be exported in both cases.

 

Please give it a try:

Option Explicit On

Sub Main

	If Not ThisDoc.Document.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("no Assembly",MsgBoxStyle.Critical,"iLogic")
		Exit Sub
	End If

	'get user input
	Dim MsgResult As MsgBoxResult = _
    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 of first level or all sublevels?" _
		& vbLf & "Click YES to create first level, NO for all sublevels, Cancel to abort." _
		& vbLf & "This could take a while.", "iLogic  - Batch Output PDFs ", MessageBoxButtons.YesNoCancel)
		
	If MsgResult= vbCancel Then
        Exit Sub
    End If
	
	Dim oAsmDoc As AssemblyDocument = ThisDoc.Document
	Dim oAsmDocName As String = System.IO.Path.GetDirectoryName(oAsmDoc.FullFileName) & "\" & System.IO.Path.GetFileNameWithoutExtension(oAsmDoc.FullFileName)
    
	Dim sFolder As String
	If MsgResult=vbYes Then
    	sFolder = oAsmDocName & " PDF Files_LevelOne"
	Else
		sFolder = oAsmDocName & " PDF Files_LevelDeep"
	End If
	
    If Not System.IO.Directory.Exists(sFolder) Then
        System.IO.Directory.CreateDirectory(sFolder)
    End If

	Dim LevelOne As New List(Of String)
	Dim LevelDeep As New List(Of String)

	Dim oBOM As BOM = oAsmDoc.ComponentDefinition.BOM
	Dim oBOMView As BOMView

	oBOM.StructuredViewEnabled = True
	oBOM.StructuredViewFirstLevelOnly = False

	For Each oBOMView In oBOM.BOMViews
		If oBOMView.ViewType = BOMViewTypeEnum.kStructuredBOMViewType Then Exit For
	Next

	If oBOMView Is Nothing Then
		MsgBox("Could not get structred BOM view. Exit",MsgBoxStyle.Critical,"iLogic")
		Exit Sub
	End If

	ProcessBOMRowsOne(LevelOne,LevelDeep,oBOMView.BOMRows )

	If MsgResult = vbYes Then
		Export2PDF(LevelOne,sFolder)
	Else
		Export2PDF(LevelDeep,sFolder)
	End If
	
    Shell("explorer.exe " & sFolder,vbNormalFocus)
End Sub

Private Sub ProcessBOMRowsOne(ByRef LevelOne As List(Of String), ByRef LevelDeep As List(Of String), oBOMRows As BOMRowsEnumerator)
	Dim oCompDef As ComponentDefinition
	Dim oRefedDoc As Inventor.Document
	Dim oBOMRow As BOMRow
	
	For Each oBOMRow In oBOMRows
		For Each oCompDef In oBOMRow.ComponentDefinitions
			oRefedDoc = oCompDef.Document
			If Not LevelOne.Contains(oRefedDoc.FullFileName) Then
				LevelOne.Add(oRefedDoc.FullFileName)
			End If
		Next
		oCompDef = Nothing
		If oBOMRow.ChildRows IsNot Nothing Then
			ProcessBOMRowsDeep(LevelDeep,oBOMRow.ChildRows)
		End If
	Next
End Sub

Private Sub ProcessBOMRowsDeep(ByRef LevelDeep As List(Of String), oBOMRows As BOMRowsEnumerator)
	Dim oCompDef As ComponentDefinition
	Dim oRefedDoc As Inventor.Document
	Dim oBOMRow As BOMRow
	
	For Each oBOMRow In oBOMRows
		For Each oCompDef In oBOMRow.ComponentDefinitions
		oRefedDoc = oCompDef.Document
		If Not LevelDeep.Contains(oRefedDoc.FullFileName) Then
			LevelDeep.Add(oRefedDoc.FullFileName)
		End If
		Next
		oCompDef = Nothing
		If oBOMRow.ChildRows IsNot  Nothing Then
			ProcessBOMRowsDeep(LevelDeep,oBOMRow.ChildRows)
		End If
	Next
End Sub

Private Sub Export2PDF(ByVal FileList As List(Of String),ByVal sFolder As String)
	Dim oDrawDoc As DrawingDocument
    Dim oNoDwg As New List(Of String)
	Dim sNoDwgString As String
	Dim PDFAddIn As TranslatorAddIn
    Dim oContext As TranslationContext
    Dim oOptions As NameValueMap
    Dim oDataMedium As DataMedium
    
    Call ConfigurePDFAddinSettings(PDFAddIn, oContext, oOptions, oDataMedium)
	
	Dim sFullFileName As String
	For Each sFullFileName In FileList
		Dim oBaseName As String = System.IO.Path.GetFileNameWithoutExtension(sFullFileName)
        Dim oPathAndName As String = System.IO.Path.GetDirectoryName(sFullFileName) & "\" & oBaseName
        
		oDataMedium.FileName = sFolder & "\" & oBaseName & ".pdf"
		
		If (System.IO.File.Exists(oPathAndName & ".dwg")) Then
            oDrawDoc = ThisApplication.Documents.Open(oPathAndName & ".dwg", False)
            Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
            oDrawDoc.Close
        Else
			If Not oNoDwg.Contains(sFullFileName) Then
				oNoDwg.Add(sFullFileName) 'dwgPathName
			End If
        End If
        If(System.IO.File.Exists(oPathAndName & ".idw")) Then
            oDrawDoc = ThisApplication.Documents.Open(oPathAndName & ".idw", False)
            Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
            oDrawDoc.Close
        Else
           If Not oNoDwg.Contains(sFullFileName) Then
				oNoDwg.Add(sFullFileName) 'idwPathName
			End If
        End If
	Next
	
	MessageBox.Show("New Files Created in: " & vbLf & sFolder, "iLogic")
	
	oNoDwg.sort
	For Each item As String In oNoDwg
		sNoDwgString=sNoDwgString & vbCrLf & item
	Next
    MsgBox("Files found without drawings: " & vbLf & sNoDwgString)
End Sub
	
Private 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

 


R. Krieg
RKW Solutions
www.rkw-solutions.com
0 Likes
Message 16 of 20

Kassenbouw
Enthusiast
Enthusiast

Hi,

 

This is allmost what I'm looking for. Is it possible to make 'LevelDeep' look at all the parts displayed in the BOM at 'Parts Only' ?

0 Likes
Message 17 of 20

Ralf_Krieg
Advisor
Advisor

Hello

 

That makes no sense. Parts only shows all part documents from all levels in a flat list. There is no first level or sublevel to differentiate.

What exactly do you want to achive? Shall the assembly files filtered out? I'm a bit confused about your goal.


R. Krieg
RKW Solutions
www.rkw-solutions.com
0 Likes
Message 18 of 20

Kassenbouw
Enthusiast
Enthusiast

Hi,

 

What I want is to create PDF files for the top level in a folder, and all other parts in a second folder. In the assembly BOM this is diveded as 'Structured' and 'Parts Only'. 

If I use Leveldeep I will get the drawings from phantom and derrived assemblys to I think. 

 

 

 

0 Likes
Message 19 of 20

Ralf_Krieg
Advisor
Advisor

Hello

 

The parts only view flattens the complete assembly structure. This has nothing to do with the first level of an assembly. I think we talk about different things.

Maybe the two pictures can describe what I mean. It's two times the same assembly, but one time with a phantom assembly. You can see what the Level One/Level Deep icludes and that the version with the phantom differs. That's the same the structured BOM view would do. It removes the phantom and promotes all occurrences within up one level.

 

Normal.JPGPhantom.JPG

 


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

Kassenbouw
Enthusiast
Enthusiast

You are right. This is what I was looking for. Thanks for your help!

0 Likes