check for unused parts

check for unused parts

GosponZ
Collaborator Collaborator
532 Views
7 Replies
Message 1 of 8

check for unused parts

GosponZ
Collaborator
Collaborator

I'm trying to check if there is any  unused (ghost) parts in assembly. Sometimes i see parts in BOM and in browser tree they are not belong to assembly. Usually when copy design happened when part(s) aredeleted from assembly but they still in BOM. I'm trying with this code but error on line 18

 

Dim oDoc As Document
oDoc = ThisApplication.ActiveDocument

' Ensure that the document is an assembly
If oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then

    ' Get the assembly document
    Dim oAsmDoc As AssemblyDocument
    oAsmDoc = oDoc

    ' Create a collection to store unused parts
    Dim unusedParts As New List(Of String)

    ' Iterate through all components in the assembly
    Dim oComp As ComponentOccurrence
    For Each oComp In oAsmDoc.ComponentOccurrences

        ' Check if the component is a part document
        If oComp.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Then

            ' Check if the part is not suppressed and is placed in the assembly
            If Not oComp.Suppressed Then
                ' Add the component name to the unused parts list
                unusedParts.Add(oComp.Name)
            End If
        End If
    Next

    ' Check if any unused parts were found
    If unusedParts.Count > 0 Then
        ' Display unused parts
        Dim msg As String = "Unused parts in assembly:" & vbCrLf
        For Each part In unusedParts
            msg &= part & vbCrLf
        Next
        MessageBox.Show(msg, "Unused Parts Check")
    Else
        ' No unused parts found
        MessageBox.Show("No unused parts found in the assembly.", "Unused Parts Check")
    End If

Else
    MessageBox.Show("This is not an assembly document.", "Error")
End If
0 Likes
Accepted solutions (2)
533 Views
7 Replies
Replies (7)
Message 2 of 8

Curtis_Waguespack
Consultant
Consultant

Hi @GosponZ,

 

see attached rule, I had on hand for this issue. It's been a while so I don't recall all the ins and outs of this.

I hope this helps.
Curtis

EESignature

0 Likes
Message 3 of 8

GosponZ
Collaborator
Collaborator

Curtis,

thank you for response on this. I tried but do not see that is working for me. Rule is good. Is it another way to check if all parts in browser tree belong to assembly and if not whick one.

 

0 Likes
Message 4 of 8

C_Haines_ENG
Collaborator
Collaborator

Can you attach a document that has this "unused" error. I'm struggling to understand what you mean by "unused".

0 Likes
Message 5 of 8

Viktor.Lukjanow
Advocate
Advocate
Accepted solution

Change this Line
For Each oComp In oAsmDoc.ComponentOccurrences 
to 
For Each oComp In oAsmDoc.ComponentDefinition.Occurrences
Try this:

Dim oDoc As Document
oDoc = ThisApplication.ActiveDocument

' Ensure that the document is an assembly
If oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then

    ' Get the assembly document
    Dim oAsmDoc As AssemblyDocument
    oAsmDoc = oDoc

    ' Create a collection to store unused parts
    Dim unusedParts As New List(Of String)

    ' Iterate through all components in the assembly
    Dim oComp As ComponentOccurrence
    For Each oComp In oAsmDoc.ComponentDefinition.Occurrences

        ' Check if the component is a part document
        If oComp.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Then

            ' Check if the part is not suppressed and is placed in the assembly
            If Not oComp.Suppressed Then
                ' Add the component name to the unused parts list
                unusedParts.Add(oComp.Name)
            End If
        End If
    Next

    ' Check if any unused parts were found
    If unusedParts.Count > 0 Then
        ' Display unused parts
        Dim msg As String = "Unused parts in assembly:" & vbCrLf
        For Each part In unusedParts
            msg &= part & vbCrLf
        Next
        MessageBox.Show(msg, "Unused Parts Check")
    Else
        ' No unused parts found
        MessageBox.Show("No unused parts found in the assembly.", "Unused Parts Check")
    End If

Else
    MessageBox.Show("This is not an assembly document.", "Error")
End If

 

 

Message 6 of 8

GosponZ
Collaborator
Collaborator

thank you that is it

0 Likes
Message 7 of 8

_dscholtes_
Advocate
Advocate

Just like @C_Haines_ENG, I fail to see what you are looking for. By default, any part placed in an assembly is part of the assembly BOM.

 

Your script just list all unsuppressed components and reports them as unused (which is a contradiction) and does nothing with the BOM (object) at all.

 

FYI, you can restructure the first 9 lines of code to remove the oDoc variable:

' Ensure that the document is an assembly
If ThisApplication.ActiveDocument = DocumentTypeEnum.kAssemblyDocumentObject Then

    ' Get the assembly document
    Dim oAsmDoc As AssemblyDocument
    oAsmDoc = ThisApplication.ActiveDocument

    <rest of code>

Else
    MessageBox.Show("This is not an assembly document.", "Error")
End If

 

0 Likes
Message 8 of 8

GosponZ
Collaborator
Collaborator
Accepted solution

Great rule. Thank you Viktor.

0 Likes