VBA script: List first level sub components from assembly

VBA script: List first level sub components from assembly

pball
Mentor Mentor
1,515 Views
7 Replies
Message 1 of 8

VBA script: List first level sub components from assembly

pball
Mentor
Mentor

Currently I have some code which loops through all sub components of an assembly and sub assemblies. However I only need sub components inside the top level assembly and not any sub-assemblies. Is there an easy way to acomplish this?

 

This code will list all sub components inside an assembly when a drawing is open.

 

    Dim oDocument As Document
    Set oDocument = ThisApplication.ActiveDocument
    Dim oRefDocs As DocumentsEnumerator
    Set oRefDocs = oDocument.AllReferencedDocuments
    Dim oRefDoc As Document

    For Each oRefDoc In oRefDocs
        debug.print oRefDoc.FullDocumentName
    Next

 

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
0 Likes
1,516 Views
7 Replies
Replies (7)
Message 2 of 8

pball
Mentor
Mentor

Sorry for bumping this but I'm still trying to figure this one out.

 

Also perhaps something to clarify what I mean. Lets say I have the main assembly Thing.iam, I'd like to be able to get part1, assembly1, and part4. Not anything beyond the parts/assemblies in Thing.iam.

 

Thing.iam

-Part1.ipt

-Assembly1.iam

--Part2.ipt

--Part3.ipt

-Part4.ipt

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
0 Likes
Message 3 of 8

skyngu
Collaborator
Collaborator

what do you want first level? the name or part itself?some information are in the first level BOM or partslist.

Autodesk Inventor Professional 2019
0 Likes
Message 4 of 8

skyngu
Collaborator
Collaborator

try this

 

  Set oRefDocs = oDocument.ReferencedDocuments

Autodesk Inventor Professional 2019
0 Likes
Message 5 of 8

pball
Mentor
Mentor

Set oRefDocs = oDocument.ReferencedDocuments

 

That only returns the assembly included in the drawing.

 

I'd like the FullDocumentName for all the parts and sub-assemblies contained within the assembly placed in a drawing, but not any parts or assemblies deeper than that first level.

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
0 Likes
Message 6 of 8

Mike.Wohletz
Collaborator
Collaborator

The below is in VB.NET but you should be able to see how it was done and add make it work in VBA.

 

    If ThisApplication.ActiveDocument.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
            ListBox1.Items.Clear()
            Dim oDrawing As DrawingDocument = ThisApplication.ActiveDocument
            'we will go after the first view that is an assembly
            For Each oView As DrawingView In oDrawing.ActiveSheet.DrawingViews
                If oView.ReferencedDocumentDescriptor.ReferencedDocument.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
                    Dim oAssembly As AssemblyDocument = oView.ReferencedDocumentDescriptor.ReferencedDocument
                    For Each oDoc As Inventor.Document In oAssembly.ReferencedDocuments
                        ListBox1.Items.Add(oDoc.FullDocumentName)
                    Next
                    Exit For ' we only want to do this for the first assembly found.
                End If
            Next
        End If

 

0 Likes
Message 7 of 8

skyngu
Collaborator
Collaborator

well, oDocument means "assembly document" in the drawing in this case.

 

I assume that there is only one assembly in the drawing. after you can get assembly from drawing, you can get first level by referenceddocuments.

 

you may get fulldocumentname from firstlevelonly bom or partslist too.

Autodesk Inventor Professional 2019
0 Likes
Message 8 of 8

pball
Mentor
Mentor

Thank you, it took a bit of playing around but I got exactly what I wanted.

 

    Dim oDocument As Document
    Set oDocument = ThisApplication.ActiveDocument
    Dim oRefDoc As Inventor.Document
    
    If oDocument.ReferencedDocuments.Item(1).DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
        Dim oAssembly As AssemblyDocument
        Set oAssembly = oDocument.ReferencedDocuments.Item(1)
        
        For Each oRefDoc In oAssembly.ReferencedDocuments
            'add to list here oRefDoc.FullDocumentName
        Next
    ElseIf oDocument.ReferencedDocuments.Item(1).DocumentType = DocumentTypeEnum.kPartDocumentObject Then
        Set oRefDoc = oDocument.ReferencedDocuments.Item(1)
        
        'add to list here oRefDoc.FullDocumentName
    End If

 

This works nicely for me for getting either the part name or all first level components of an assembly. I needed this for a dialog which allows editing of iproperties from inside of a drawing, which saves loads of time.

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
0 Likes