How to iterate and activate each row in an iAssembly or iPart without the Inventor UI.

How to iterate and activate each row in an iAssembly or iPart without the Inventor UI.

AlexFielder
Advisor Advisor
405 Views
3 Replies
Message 1 of 4

How to iterate and activate each row in an iAssembly or iPart without the Inventor UI.

AlexFielder
Advisor
Advisor

Hi all,

 

Apologies if I have overlooked an existing example but I am just about to head out for the day and wondered if I have missed or overlooked an example somewhere that shows how to iterate the table rows of an iAssembly (or iPart) without using the Inventor UI?

 

The working example I have that shows how to do so using the UI is as follows:

 

Sub Main()
CheckiTableForErrors()
End Sub
Sub CheckiTableForErrors()
    Dim oErrorManager As ErrorManager = ThisApplication.ErrorManager

    If TypeOf (ThisApplication.activedocument) Is partdocument Then
        Dim oDoc As PartDocument = ThisApplication.ActiveDocument
        Dim oiPart As iPartFactory = oDoc.ComponentDefinition.iPartFactory
        Dim oTop As BrowserNode = oDoc.BrowserPanes("Model").TopNode
        Dim bHasErrorOrWarning As Boolean
        Dim i As Integer
        InventorVb.DocumentUpdate()
        ThisApplication.SilentOperation = True
        For i = 1 To oiPart.TableRows.Count 'use first 10 rows only for debugging purposes!
            ' Highlight the 3rd iPart table row which has invalid data
            oTop.BrowserNodes("Table").BrowserNodes.Item(i).DoSelect

            ' Activate the iPart table row
            Dim oCommand As ControlDefinition = ThisApplication.CommandManager.ControlDefinitions("PartComputeiPartRowCtxCmd")
            oCommand.Execute

            ThisApplication.SilentOperation = False
            ThisApplication.CommandManager.ControlDefinitions.Item("AppZoomallCmd").Execute
            If oErrorManager.HasErrors Or oErrorManager.HasWarnings Then
                MessageBox.Show(oErrorManager.LastMessage, "Title")
            End If
        Next i
        MessageBox.Show("No errors shown = None found!", "Title")
    ElseIf TypeOf (ThisApplication.activedocument) Is assemblydocument Then
        Dim odoc As assemblydocument = ThisApplication.activedocument
        Dim iAssy As iAssemblyFactory = odoc.componentdefinition.iassemblyfactory
        Dim oTop As BrowserNode = odoc.BrowserPanes("Model").TopNode
        Dim bHasErrorOrWarning As Boolean
        Dim i As Integer
        InventorVb.DocumentUpdate()
        ThisApplication.SilentOperation = True
        For rowIndex = 1 To iAssy.tablerows.count
            oTop.BrowserNodes("Table").BrowserNodes.Item(i).DoSelect
            Dim oCommand As ControlDefinition = ThisApplication.CommandManager.ControlDefinitions("PartComputeiPartRowCtxCmd")
            oCommand.Execute
            ThisApplication.SilentOperation = False
            ThisApplication.CommandManager.ControlDefinitions.Item("AppZoomallCmd").Execute
            If oErrorManager.HasErrors Or oErrorManager.HasWarnings Then
                MessageBox.Show(oErrorManager.LastMessage, "Title")
            End If

        Next
    End If
End Sub

Which works fine if you wish to verify that each row is buildable as a user- but can I do the same without access to the Inventor UI? (for instance if the iAssembly/iPart is opened by coolOrange PowerJobs)

 

Thanks,

 

Alex.

0 Likes
406 Views
3 Replies
Replies (3)
Message 2 of 4

Frederick_Law
Mentor
Mentor

VBA Macro to update and generate all iPart/iAssembly files:

 

Sub CreateAlliPart()
    On Error Resume Next
    
    ' Open the factory document
    Dim oFacDoc As PartDocument
    ' Set oFacDoc = oInventorApp.Documents.Open(sFacFullFileName)
    Set oFacDoc = ThisApplication.ActiveDocument

    Dim oCompDef As PartComponentDefinition
    Set oCompDef = oFacDoc.ComponentDefinition

    Dim iNumRows As Integer
    Dim oiPrtFac As iPartFactory
    Set oiPrtFac = oCompDef.iPartFactory

    iNumRows = oiPrtFac.TableRows.Count

    ' Create a member for each row and update the member to make sure that it is in-synch
    ' with the factory
    Dim iRow As Integer
    Dim oMem As iPartMember
    For iRow = 1 To iNumRows
        Set oMem = oiPrtFac.CreateMember(iRow)

        Dim oMemDoc As Document
        Set oMemDoc = oMem.Parent.Document

        ' Update the member
        oMemDoc.Update
        oMemDoc.Save
        oMemDoc.Close
    Next

    oFacDoc.Close

End Sub

Sub CreateAlliAssemblies()
    On Error Resume Next
    
    ' Open the factory document
    Dim oFacDoc As AssemblyDocument
    ' Set oFacDoc = oInventorApp.Documents.Open(sFacFullFileName)
    Set oFacDoc = ThisApplication.ActiveDocument

    Dim oCompDef As AssemblyComponentDefinition
    Set oCompDef = oFacDoc.ComponentDefinition

    Dim iNumRows As Integer
    Dim oiAsmFac As iAssemblyFactory
    Set oiAsmFac = oCompDef.iAssemblyFactory

    iNumRows = oiAsmFac.TableRows.Count

    ' Create a member for each row and update the member to make sure that it is in-synch
    ' with the factory
    Dim iRow As Long
    'Dim oiMem As iAssemblyMember
    'Set oiMem = oiAsmFac.
    For iRow = 1 To iNumRows
    
        'Set oiMem = oiAsmFac.CreateMember(iRow)

        Dim oMemDoc As Document
        Set oMemDoc = oiMem.Parent.Document

        ' Update the member
        oMemDoc.Update
        oMemDoc.Save
        oMemDoc.Close
    Next

    oFacDoc.Close

End Sub

 

0 Likes
Message 3 of 4

AlexFielder
Advisor
Advisor

thanks @Frederick_Law that is really useful.

 

My next question (thinking out loud) is that I am exporting BOM data from Inventor Assemblies (iAssemblies in particular) and, because of the way iAssemblies function, exporting a BOM from an iAssembly always results in the same data within the exported data. 

 

I think I can workaround this (for the purposes of doing this from inside Vault) by wrapping a for loop of the manner you describe in your VBA sample, inside an Inventor transaction (in case something goes awry) and then, to create a new transaction inside of which I can delete the iAssembly table (thus converting the iAssembly into a regular assembly), then run the BOM export, before undoing the second transaction and continuing the loop again.

 

 

0 Likes
Message 4 of 4

Frederick_Law
Mentor
Mentor

Haven't used iPart/iAssembly for a while.

Using ModelState but idea is similar.

Activate each iPart/iAssembly/ModelState and do what you need to do.

The "problem" with iPart/iAssembly is the member files are not created until it is used.

Hence the CreateMember.

https://help.autodesk.com/view/INVNTOR/2023/ENU/?guid=GUID-6A5B70B1-5AF3-4824-9060-857F22114B4F

0 Likes