Access assembly from drawing using excel VBA

Access assembly from drawing using excel VBA

asE47S8
Explorer Explorer
245 Views
2 Replies
Message 1 of 3

Access assembly from drawing using excel VBA

asE47S8
Explorer
Explorer

Sorry if this is the wrong section.

 

I'm currently working on a VBA script in Excel that interfaces with Inventor Professional 2023. The code i've got right now grabs the BOM from an assembly and compares it with data from our ERP system. This is to ensure part numbers and naming conventions are correctly typed in both systems. This comparison functionality is split into different modules to handle both legacy data and this inventor generated data. 

 

My problem arises when i try to handle the case where the user has the assembly drawing open rather than the 3d model of the assembly. I'd like to somehow access/open the assembly, and get its document, namely in the form of a "kAssemblyDocumentObject", using the "kDrawingDocumentObject" that I currently have. If i can find a way to do that then the rest of my code will work perfectly.

 

I'm fairly new to coding in general, and this is my first project with inventor so if you guys have any suggestions, it would be greatly appreciated 🙂

 

Below is a snippit of my code:

 

Public Sub GenerateRecursiveBOMFromActiveInventorAssemblyV2()
    Dim invApp As Inventor.Application
    
    ' Attempt to connect to a running Inventor instance
    On Error Resume Next
    Set invApp = GetObject(, "Inventor.Application")
    On Error GoTo 0
    
    If invApp Is Nothing Then
        MsgBox "Inventor was not running."
        Exit Sub
    End If
    
    Dim oDoc As Inventor.Document
    Set oDoc = invApp.ActiveDocument
    
    If oDoc Is Nothing Then
        MsgBox "No active document in Inventor.", vbExclamation
        Exit Sub
    End If
    
    ' Handle edge cases where the current document isnt an assembly
    If oDoc.DocumentType <> kAssemblyDocumentObject Then

        ' Is it a drawing? Then try to access linked assembly
        If oDoc.DocumentType = kDrawingDocumentObject Then
            MsgBox "This is a drawing, here it should change the focused document to the attached assembly"
        End If
        
        Exit Sub
    End If
    
    Dim oAsmDoc As Inventor.AssemblyDocument
    Set oAsmDoc = oDoc

    '... Rest of the script that handles comparison of BOM with ERP system below here
End Sub

 

 

The full code can be seen in the attached file. The forum doesnt accept .bas files, so its stored as a .txt 🙂

0 Likes
Accepted solutions (1)
246 Views
2 Replies
Replies (2)
Message 2 of 3

WCrihfield
Mentor
Mentor
Accepted solution

Hi @asE47S8.  It looks like you are in the right place.  Welcome to the Inventor automation / programming forum.  I no longer use VBA with Inventor, in favor of the newer vb.net coding system that Inventor's own iLogic system uses, but I used to use VBA quite a bit, so I may still be able to help here.

 

How to do what you want to do here sort of depends on how do your assembly drawings.  Some, like me, only document the assembly itself within the assembly's drawing, and have different drawing files for each of the parts &/or sub assemblies that may be within that assembly.  Other though will include extra sheets in their assembly drawings for each of the parts & sub assemblies within the whole assembly.  If your assembly drawing only directly references that one, main assembly, and does not include any direct references to any other documents, then this will be pretty simple & easy.  However, if your assembly drawings directly reference many different documents (not just that assembly file), then it will be a bit more complex to do.

 

The simple route (only one model file being referenced):

Document.ReferencedDocuments (only the ones being 'directly' referenced by the Document)

DocumentsEnumerator (the Type of value you get from that previous property)

DocumentsEnumerator.Item (property of that collection to get one of its elements), used in this case to simply get the first item.  That Item will be another Document object, and that should be the AssemblyDocument (a Type derived from Document Type).

 

When your assembly drawing directly references multiple documents, then that collection may contain multiple Documents, and figuring out which one you want would be your next challenge.  If your main assembly drawing also contains views which directly reference sub assembly files, then simply checking the DocumentType of each referenced Document will not be good enough, but may be good enough if that is not the case.  A longer route, but maybe one that may make more sense for you, is to get a specific drawing sheet, or iterate the drawings sheets, then get a specific drawing view on that sheet, or iterate the drawing views on the current sheet as you are iterating those, and once you have a view object, get the document that the view is directly referencing (DrawingView.ReferencedDocumentDescriptor.ReferencedDocument).  Then inspect that, to see if it is the one you want.  There are other ways too, but I can't show examples of all here.

Below is just a portion of your rule, near the beginning, where I have copied it, then modified it a bit.  However, if your drawing directly references multiple documents, and multiple of those are assembly type documents, there is no guarantee that this will 'get' the correct assembly.  That inner-most iteration of the drawings referenced documents should likely be changed to a 'For i = 1 To Count' type of loop, instead of a 'For Each' type of loop, to make sure they are encountered in their 'natural' order (order of first existing or oldest), which would most likely make the 'main' assembly the first in that collection to be encountered.  Just some suggestions.

    If oDoc Is Nothing Then
        MsgBox "No active document in Inventor.", vbExclamation
        Exit Sub
    End If
    
    Dim oAsmDoc As Inventor.AssemblyDocument
    
    ' Check if the user has a
    If oDoc.DocumentType = kAssemblyDocumentObject Then
        Set oAsmDoc = oDoc
    ElseIf oDoc.DocumentType = kDrawingDocumentObject Then
        Dim oIDW_RefDocs As DocumentsEnumerator
        Set oIDW_RefDocs = oDoc.ReferencedDocuments
        If oIDW_RefDocs.Count = 0 Then
            Exit Sub
        ElseIf oIDW_RefDocs.Count = 1 Then
            If oIDW_RefDocs.Item(1).DocumentType = kAssemblyDocumentObject Then
                Set oAsmDoc = oIDW_RefDocs.Item(1)
            End If
        ElseIf oIDW_RefDocs.Count > 1 Then
            Dim oIDW_RefDoc As Inventor.Document
            For Each oIDW_RefDoc In oIDW_RefDocs
                If oIDW_RefDoc.DocumentType = kAssemblyDocumentObject Then
                    Set oAsmDoc = oIDW_RefDoc
                    Exit For 'skip checking any others (may not be a good idea, if not correct assembly)
                End If
            Next
        End If
    End If
    If oAsmDoc Is Nothing Then
        Exit Sub
    End If

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 3

asE47S8
Explorer
Explorer

Hi Wesley,

 

Thankfully, my parts and assemblies have seperate drawings attached, so the easy approach you outlined worked exacly as I imagined. The solution was simply to add the line "Set oDoc = oDoc.ReferencedDocuments.Item(1)" and then everything ran perfectly.

 

For any future users that stumble upon this thread, the changes can be seen on line 27 to 29.

 

Public Sub GenerateRecursiveBOMFromActiveInventorAssemblyV2()
    Dim invApp As Inventor.Application
    
    ' Attempt to connect to a running Inventor instance
    On Error Resume Next
    Set invApp = GetObject(, "Inventor.Application")
    On Error GoTo 0
    
    If invApp Is Nothing Then
        MsgBox "Inventor was not running."
        Exit Sub
    End If
    
    Dim oDoc As Inventor.Document
    Set oDoc = invApp.ActiveDocument
    
    If oDoc Is Nothing Then
        MsgBox "No active document in Inventor.", vbExclamation
        Exit Sub
    End If
    
    ' Handle edge cases where the current document isnt an assembly
    If oDoc.DocumentType <> kAssemblyDocumentObject Then

        ' Is it a drawing? Then try to access linked assembly
        If oDoc.DocumentType = kDrawingDocumentObject Then
            If oDoc.ReferencedDocuments.Item(1).DocumentType = kAssemblyDocumentObject Then
                Set oDoc = oDoc.ReferencedDocuments.Item(1)
            End If
        End If
        
        Exit Sub
    End If
    
    Dim oAsmDoc As Inventor.AssemblyDocument
    Set oAsmDoc = oDoc

    '... Rest of the script that handles comparison of BOM with ERP system below here
End Sub

 

 

Great explanation btw. I stumbled across the DocumentsEnumerator documentation earlier but couldnt make any sense of it while your explanation was straight to the point.  🙂

0 Likes