iLogic to export part list to .xls from assembly and subassemblies

iLogic to export part list to .xls from assembly and subassemblies

Anonymous
Not applicable
1,347 Views
5 Replies
Message 1 of 6

iLogic to export part list to .xls from assembly and subassemblies

Anonymous
Not applicable

Hi,

now if I want to export part list to .xls i have to open every .idw file and do it manually. Is there a way (maybe iLogic rule) to export .xls files not from an .idw but from .iam file? I mean, when i'm at .iam and run this iLogic rule, it generates .xls files of main assembly and subassemblies.

0 Likes
1,348 Views
5 Replies
Replies (5)
Message 2 of 6

b_sharanraj
Advocate
Advocate

I just Googled it and got the below code from https://forums.autodesk.com/t5/inventor-customization/export-bom-using-ilogic/m-p/3753037#M41884

 

Sub ExportBOMSample()
    Dim oDoc As AssemblyDocument
    Set oDoc = ThisApplication.ActiveDocument
    
    ' If the current non-master LOD document is dirty you should save it before you can use the master-LOD document background
    If oDoc.Dirty Then
        If MsgBox("You should save the document first!" & vbCrLf & "Would you like to save the document?", vbYesNo) = vbYes Then
            oDoc.Save
        Else
            Exit Sub
        End If
        
    End If
    
    Dim sFileName As String
    sFileName = oDoc.FullFileName
    
    Dim oMasterLODDoc As AssemblyDocument
    Set oMasterLODDoc = ThisApplication.Documents.Open(sFileName, False)
    
    Dim oBOM As BOM
    Set oBOM = oMasterLODDoc.ComponentDefinition.BOM
    
    oBOM.PartsOnlyViewEnabled = True
    
    Dim oBOMView As BOMView
    Set oBOMView = oBOM.BOMViews.Item("Parts Only")
    
    oBOMView.Export "C:\Temp\BOM.xls", kMicrosoftExcelFormat
    
    oMasterLODDoc.Close True
    
End Sub

Regards

B.Sharan Raj

0 Likes
Message 3 of 6

Anonymous
Not applicable

Ok so i tried it and had to change things like sub () to sub main() and delete let&set. I've done it and now i've got another error.
This is my code:

Sub Main()
    Dim oDoc As AssemblyDocument
    oDoc = ThisApplication.ActiveDocument
    
    ' If the current non-master LOD document is dirty you should save it before you can use the master-LOD document background
    If oDoc.Dirty Then
        If MsgBox("You should save the document first!" & vbCrLf & "Would you like to save the document?", vbYesNo) = vbYes Then
            oDoc.Save
        Else
            Exit Sub
        End If
        
    End If
    
    Dim sFileName As String
    sFileName = oDoc.FullFileName
    
    Dim oMasterLODDoc As AssemblyDocument
    oMasterLODDoc = ThisApplication.Documents.Open(sFileName, False)
    
    Dim oBOM As BOM
    oBOM = oMasterLODDoc.ComponentDefinition.BOM
    
    oBOM.PartsOnlyViewEnabled = True
    
    Dim oBOMView As BOMView
    oBOMView = oBOM.BOMViews.Item("Parts Only")
    
    oBOMView.Export ("C:\Temp\BOM.xls", kMicrosoftExcelFormat)
    
    oMasterLODDoc.Close (True)
    
End Sub

And error sounds like that:

Błąd w regule: Reguła0, dokument: Zespół1.iam

Parametr jest niepoprawny. (Wyjątek od HRESULT: 0x80070057 (E_INVALIDARG))

System.ArgumentException: Parametr jest niepoprawny. (Wyjątek od HRESULT: 0x80070057 (E_INVALIDARG))
   w System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
   w Inventor.BOMViews.get_Item(Object Index)
   w LmiRuleScript.Main()
   w Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
   w iLogic.RuleEvalContainer.ExecRuleEval(String execRule)
0 Likes
Message 4 of 6

b_sharanraj
Advocate
Advocate

Closing the Document gives the Error. So, remove the below Line from your Code and check once

 

oMasterLODDoc.Close (True)

 

Regards

B.Sharan Raj

0 Likes
Message 5 of 6

Anonymous
Not applicable

Thank you for your answer but i still got the same error.

Now when I'm in .idw of every assembly and subassembly i'm using this rule to export part list to .xls:

path_and_name = ThisDoc.PathAndFileName(False)
Dim oDoc As Inventor.DrawingDocument
oDoc = ThisDoc.Document
Dim oSheet As Inventor.Sheet
oSheet = oDoc.Sheets("Ark:1")
Dim oPartslist As PartsList
oPartslist = oSheet.PartsLists(1)
oPartslist.Export(path_and_name & ".xls",PartsListFileFormatEnum.kMicrosoftExcel)  

I wish it to made .xlss from every subassembly too and to use it in .iam of main assembly but I'm s**t in VBA ; )

0 Likes
Message 6 of 6

Anonymous
Not applicable

Ok so i've done some by myself:

'define the active document as an assembly file
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument
oAsmName = Left(oAsmDoc.DisplayName, Len(oAsmDoc.DisplayName) )

'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
RUsure = MessageBox.Show ( _
"This will create a .xls 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

'- - - - - - - - - - - - -subass drawing - - - - - - - - - - - -
Dim oExcDocs As DocumentsEnumerator
oExcDocs = oAsmDoc.AllReferencedDocuments
Dim oExcDoc As Document
For Each oExcDoc In oExcDocs
idwPathName = Left(oExcDoc.FullDocumentName, Len(oExcDoc.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 oDrawDoc As DrawingDocument
                oDrawDoc = ThisApplication.Documents.Open(idwPathName, True)
		pathname = Left(oExcDoc.FullDocumentName, Len(oExcDoc.FullDocumentName) -3)
		Dim oSheet1 As Inventor.Sheet
		oSheet1 = oDrawDoc.Sheets("Ark:1")
		Dim oPartslist1 As PartsList
		oPartslist1 = oSheet1.PartsLists(1)
		oPartslist1.Export(pathname & ".xls",PartsListFileFormatEnum.kMicrosoftExcel)
		oDrawDoc.Close
Else
'If the model has no drawing of the same path and name - do nothing
End If
Next
'- - - - - - - - - - - - -Top Level Drawing - - - - - - - - - - - -
oAsmDrawing = ThisDoc.ChangeExtension(".idw")
oAsmDrawingDoc = ThisApplication.Documents.Open(oAsmDrawing, True)
oAsmDrawingName = Left(oAsmDrawingDoc.DisplayName, Len(oAsmDrawingDoc.DisplayName))
On Error Resume Next 
path_and_name = ThisDoc.PathAndFileName(False)
Dim oSheet As Inventor.Sheet
oSheet = oAsmDrawingDoc.Sheets("Ark:1")
Dim oPartslist As PartsList
oPartslist = oSheet.PartsLists(1)
oPartslist.Export(path_and_name & ".xls",PartsListFileFormatEnum.kMicrosoftExcel)
oAsmDrawingDoc.Close
'- - - - - - - - - - - - -

MessageBox.Show("Done " & vbLf & oFolder, "iLogic")
'open the folder where the new ffiles are saved
Shell("explorer.exe " & oFolder,vbNormalFocus)

It works good but for every assembly drawing (parts and subassemblies) but i want it to do it only for subassemblies. Is it possible?

0 Likes