Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

BOM structured view in an txt

Anonymous

BOM structured view in an txt

Anonymous
Not applicable

Hi all, I'm trying to reproduce BOM structured view in an .txt file. Struggling with iLogic, and thank to internet, I'm now able to export an indented list of parts based on assembly tree order (note that I do not repeat a part or assembly if it is present more than one time for the specific level).

Now I'm facing the problem to calculate quantity of each part in my txt BOM. What is difficult for me is to sum the quantity of a part or assembly only at a specified level, without considering upper or lower levels. Does anybody could help me?

 

Hereafter my code (I know that probably could be better, any suggestion is REALLY REALLY REALLY welcome )

 

SyntaxEditor Code Snippet

Public Module GlobalVariables
   'bom contiene i nomi delle parti
   Public bom As New ArrayList()
    'livelli contiene i valori di liv per ciascuna parte considerata (serve per l'indentatura del file)
   Public livelli As New ArrayList()
   'liv � il livello a cui si trova la parte/assieme
   Public liv As Integer
      'cont contatore per il numero di parti nell'assieme
   Public cont As Integer
End Module

Public Sub Main
Dim oTopAss As AssemblyDocument
oTopAss = ThisApplication.ActiveDocument
Dim oEachRefDoc As Document
'definisce il nome dell assieme proncipale
CurFileName = oTopAss.fullFileName
'defines backslash as the subdirectory separator
Dim strCharSep As String = System.IO.Path.DirectorySeparatorChar
'find the postion of the last backslash in the path
FNamePos = InStrRev(CurFileName, "\", -1)   
'get the file name with the file extension
Name = Right(CurFileName, Len(CurFileName) - FNamePos)
'get the file name (without extension)
ShortName = Left(Name, Len(Name) - 4)
 
 liv=0
 'aggiunge il nome dell'assieme proncipale alla lista
 GlobalVariables.bom.add(ShortName)
 cont=1
 'aggiunge alla variabile livelli il valore del livello zero per l'assieme principale
 GlobalVariables.livelli.add(liv)
 liv=1
 'ciclo sulle parti/assiemi di livello 1
    For Each oEachRefDoc In oTopAss.ReferencedDocuments
    Dim oSubAssFullPath As String
            oSubAssFullPath = oEachRefDoc.FullFileName
            'definisce il nome dell assieme/parte trattata
             CurFileName = oSubAssFullPath
            'find the postion of the last backslash in the path
            FNamePos = InStrRev(CurFileName, "\", -1)   
            'get the file name with the file extension
            Name = Right(CurFileName, Len(CurFileName) - FNamePos)
            'get the file name (without extension)
            ShortName = Left(Name, Len(Name) - 4)
            'aggiunge al bom il nome della parte
            GlobalVariables.bom.add(ShortName)
            cont=cont+1
            'aggiunge alla variabile livelli il valore del livello a cui si trova la parte
            GlobalVariables.livelli.add(liv)
            'verifica se la parte � un assieme e fa partire la funzione ricorsiva
            If TypeOf oEachRefDoc Is AssemblyDocument Then
            liv=liv+1
            recurseSubAss(oEachRefDoc)
             End If
             
    Next
    
    
'crea il file di testo
oWrite = System.IO.File.CreateText("c:\prova" & ".txt")
oWrite.WriteLine("bom")
oWrite.Close()

    For i=0 To cont-1
'scrive nel file di testo
Dim oAppend As System.IO.StreamWriter
oFile = "c:\prova" & ".txt"
oAppend = IO.File.AppendText(oFile)
oAppend.WriteLine(Space( GlobalVariables.livelli(i) * 3) & GlobalVariables.bom(i))
oAppend.Flush()
oAppend.Close()
'ThisDoc.Launch(oFile)
   Next
    
End Sub


Public Sub recurseSubAss(ByVal oParentDoc As AssemblyDocument)
Dim oAssDef As AssemblyComponentDefinition
    oAssDef = oParentDoc.ComponentDefinition
    
    Dim oEachRefDoc As Document
    For Each oEachRefDoc In oParentDoc.ReferencedDocuments
        Dim oSubAssFullPath As String
            oSubAssFullPath =oEachRefDoc.FullFileName
            'definisce il nome dell assieme/parte trattata
             CurFileName = oSubAssFullPath
            'find the postion of the last backslash in the path
            FNamePos = InStrRev(CurFileName, "\", -1)   
            'get the file name with the file extension
            Name = Right(CurFileName, Len(CurFileName) - FNamePos)
            'get the file name (without extension)
            ShortName = Left(Name, Len(Name) - 4)
            'aggiunge al bom il nome della parte
            GlobalVariables.bom.add(ShortName)
            cont=cont+1
            'aggiunge alla variabile livelli il valore del livello a cui si trova la parte
            GlobalVariables.livelli.add(liv)
                        If TypeOf oEachRefDoc Is AssemblyDocument Then
                        liv=liv+1
             recurseSubAss(oEachRefDoc)
             End If
    Next
    liv=liv-1
End Sub
0 Likes
Reply
Accepted solutions (1)
769 Views
2 Replies
Replies (2)

ekinsb
Alumni
Alumni
Accepted solution

Here's a slightly modified version of your program that uses the AllReferencedOccurrences method to get the number of occurrences at each level.

 

Public Sub Main()
    Dim oTopAss As AssemblyDocument
    oTopAss = ThisApplication.ActiveDocument
    Dim oEachRefDoc As Document

    Dim CurFileName As String = oTopAss.FullFileName
    Dim Name As String = System.IO.Path.GetFileName(CurFileName)
    Dim ShortName As String = System.IO.Path.GetFileNameWithoutExtension(CurFileName)

    liv = 0
    'aggiunge il nome dell'assieme proncipale alla lista
    GlobalVariables.bom.Add(ShortName)
    cont = 1

    'aggiunge alla variabile livelli il valore del livello zero per l'assieme principale
    GlobalVariables.livelli.Add(liv)
    liv = 1

    'ciclo sulle parti/assiemi di livello 1
    For Each oEachRefDoc In oTopAss.ReferencedDocuments
        Dim oSubAssFullPath As String
        oSubAssFullPath = oEachRefDoc.FullFileName

        'definisce il nome dell assieme/parte trattata
        CurFileName = oSubAssFullPath
        Name = System.IO.Path.GetFileName(CurFileName)
        ShortName = System.IO.Path.GetFileNameWithoutExtension(CurFileName)

        ' Determine the number of times this document is used in the top assembly.
        Dim refOccs As ComponentOccurrencesEnumerator
        refOccs = oTopAss.ComponentDefinition.Occurrences.AllReferencedOccurrences(oEachRefDoc)
        Dim count = refOccs.Count

        'aggiunge al bom il nome della parte
        GlobalVariables.bom.Add(ShortName & " (" & count & ")")
        cont = cont + 1

        'aggiunge alla variabile livelli il valore del livello a cui si trova la parte
        GlobalVariables.livelli.Add(liv)

        'verifica se la parte � un assieme e fa partire la funzione ricorsiva
        If TypeOf oEachRefDoc Is AssemblyDocument Then
            liv = liv + 1
            recurseSubAss(oEachRefDoc)
        End If
    Next

    'crea il file di testo
    Dim writer As System.IO.StreamWriter = System.IO.File.CreateText("c:\prova" & ".txt")
    writer.WriteLine("bom")
    writer.Close()

    For i = 0 To cont - 1
        'scrive nel file di testo
        Dim oAppend As System.IO.StreamWriter
        Dim filename As String = "c:\prova" & ".txt"
        oAppend = IO.File.AppendText(filename)
        oAppend.WriteLine(Space(GlobalVariables.livelli(i) * 3) & GlobalVariables.bom(i))
        oAppend.Flush()
        oAppend.Close()
        'ThisDoc.Launch(oFile)
    Next
End Sub


Public Sub recurseSubAss(ByVal oParentDoc As AssemblyDocument)
    Dim oAssDef As AssemblyComponentDefinition
    oAssDef = oParentDoc.ComponentDefinition

    Dim oEachRefDoc As Document
    For Each oEachRefDoc In oParentDoc.ReferencedDocuments
        Dim oSubAssFullPath As String
        oSubAssFullPath = oEachRefDoc.FullFileName

        'definisce il nome dell assieme/parte trattata
        Dim CurFileName As String = oSubAssFullPath
        Dim Name As String = System.IO.Path.GetFileName(CurFileName)
        Dim ShortName As String = System.IO.Path.GetFileNameWithoutExtension(CurFileName)

        ' Determine the number of times this document is used in the top assembly.
        Dim refOccs As ComponentOccurrencesEnumerator
        refOccs = oParentDoc.ComponentDefinition.Occurrences.AllReferencedOccurrences(oEachRefDoc)
        Dim count = refOccs.Count

        'aggiunge al bom il nome della parte
        GlobalVariables.bom.Add(ShortName & " (" & count & ")")
        cont = cont + 1

        'aggiunge alla variabile livelli il valore del livello a cui si trova la parte
        GlobalVariables.livelli.add(liv)
        If TypeOf oEachRefDoc Is AssemblyDocument Then
            liv = liv + 1
            recurseSubAss(oEachRefDoc)
        End If
    Next
    liv = liv - 1
End Sub

 


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes

Anonymous
Not applicable

WOW, thanks a lot, very effective solution.

I'm just wondering if there is a reason why it seems to begin from the bottom of the assembly tree and not from the top...

0 Likes