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

iLogic to Update Subassembly

Hello,
I have a need to update all of the parameters in all of my sub assemblies as long as they have matching names. (For example, if the main assembly has a user parameter called "parameter1" any sub assemblies or parts that also have a user parameter called parameter1 should be updated) These sub assemblies may change, but I still want to have everything updated . I can update all my parts with the code below, but my assembly section of my code doesn't function as I am expecting. I feel like it should be updating based on my current knowledge, but I will admit that I am not iLogic Fluent. I have highlighted the area that isn't working. I feel like I should be able to get it to work with only one if loop, but I've gotten stuck.
TIA for any assistance.

Public Sub Main()
CopyUserParams()
iLogicVb.UpdateWhenDone = True

End Sub

 

Private Sub CopyUserParams()
If ThisDoc.Document.DocumentType <> Inventor.DocumentTypeEnum.kAssemblyDocumentObject Then
MsgBox("The active document must be an assembly.")
Return
End If

Dim asmDoc As Inventor.AssemblyDocument = ThisDoc.Document
For Each refDoc As Inventor.Document In asmDoc.AllReferencedDocuments
' Look for part documents.
If refDoc.DocumentType = Inventor.DocumentTypeEnum.kPartDocumentObject Then
Dim partDoc As Inventor.PartDocument = refDoc
Dim refDocUserParams As UserParameters = partDoc.ComponentDefinition.Parameters.UserParameters

' Add the assembly parameters to the part.
For Each asmUserParam As UserParameter In asmDoc.ComponentDefinition.Parameters.UserParameters
' Check to see if the parameter already exists.
Dim checkParam As UserParameter = Nothing
Try
checkParam = refDocUserParams.Item(asmUserParam.Name)
Catch ex As Exception
checkParam = Nothing
End Try
If checkParam Is Nothing Then 'do nothing
Else
' Update the value of the existing parameter.
checkParam.Expression = asmUserParam.Expression
End If
Next


Else If refDoc.DocumentType = Inventor.DocumentTypeEnum.kAssemblyDocumentObject Then
Dim partdoc As Inventor.AssemblyDocument = refDoc
Dim refDocUserParams As UserParameters = asmDoc.ComponentDefinition.Parameters.UserParameters
' Add the top level parameters to the assembly.
For Each asmUserParam As UserParameter In asmDoc.ComponentDefinition.Parameters.UserParameters
' Check to see if the parameter already exists.
Dim checkParam As UserParameter = Nothing
Try
checkParam = refdocuserparams.Item(asmUserParam.Name)
Catch ex As Exception
checkParam = Nothing
End Try
If checkParam Is Nothing Then 'do nothing
Else
' Update the value of the existing parameter.
checkParam.Expression = asmUserParam.Expression
End If
Next
End If
Next
End Sub

Anonymous
in reply to: Anonymous

I solved my own problem. The issue was that I was calling out the inventor partfile. Working code below. This is intended to update all parameters in all files as long as they match. 

Public Sub Main()
	CopyUserParams()
	iLogicVb.UpdateWhenDone = True

End Sub

Private Sub CopyUserParams()
    If ThisDoc.Document.DocumentType <> Inventor.DocumentTypeEnum.kAssemblyDocumentObject Then
        MsgBox("The active document must be an assembly.")
        Return
    End If

    Dim asmDoc As Inventor.AssemblyDocument = ThisDoc.Document	
    For Each refDoc As Inventor.Document In asmDoc.AllReferencedDocuments
                 Dim partDoc As Inventor.Document = refDoc
            Dim refDocUserParams As UserParameters = partDoc.ComponentDefinition.Parameters.UserParameters

            ' Add the assembly parameters to everything.
            For Each asmUserParam As UserParameter In asmDoc.ComponentDefinition.Parameters.UserParameters
                ' Check to see if the parameter already exists.
                Dim checkParam As UserParameter = Nothing
                Try
                    checkParam = refDocUserParams.Item(asmUserParam.Name)
                Catch ex As Exception
                    checkParam = Nothing
                End Try
				If checkParam Is Nothing Then 'do nothing
					Else					
                  ' Update the value of the existing parameter.
                    checkParam.Expression = asmUserParam.Expression
				End If
                Next
    Next
End Sub
'Original Code Copied from https://forums.autodesk.com/t5/inventor-customization/ilogic-push-all-assembly-user-parameters-to-parts/m-p/7961847#M83748
'Modified to not add a missing parameter and to update subassemblies instead of just parts
trevorauman
in reply to: Anonymous

I was looking for a variety of solutions to this same thing and many resulted in faults and other problems. Then I recalled one of my co-workers doing something similar and he had a very simple bit of code.

 

'Define the open document
Dim openDoc As Document
openDoc = ThisDoc.Document

'Look at all of the files referenced in the open document
Dim docFile As Document
For Each docFile In openDoc.AllReferencedDocuments                
'format  file name                   
Dim FNamePos As Long
FNamePos = InStrRev(docFile.FullFileName, "\", -1)                        
Dim docFName As String 
docFName = Right(docFile.FullFileName, Len(docFile.FullFileName) - FNamePos) 
Parameter.Quiet = True
Parameter(docFName, "Parameter1") = Parameter1
Parameter(docFName, "Parameter2") = Parameter2
Parameter(docFName, "Parameter3") = Parameter3
Next

iLogicVb.UpdateWhenDone=True
InventorVb.DocumentUpdate()
ThisApplication.ActiveView.Fit

 This will run just the listed parameters and it will go into each part. There were problems 10 years ago that didn't always push the parameters into some of the subpart and assemblies, but I have not run into that recently. If that is an issue we simply put the code in some of the subassemblies.