Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
JhoelForshav
in reply to: jishee

Hi @jishee 

Adding by expression might not always work. The expression could contain a name of a parameter that doesn't exist in the assembly. Either a user parameter that hasn't been copied yet or a model parameter.

 

My suggestion is to try to use the expression but if it fails, use the parameter value instead:

 

Dim asmDoc As Inventor.AssemblyDocument = ThisDoc.Document	

For Each refDoc As Inventor.Document In asmDoc.AllReferencedDocuments
Dim refDocUserParams As UserParameters = asmDoc.ComponentDefinition.Parameters.UserParameters
	'Look for part documents.
	If refDoc.DocumentType = Inventor.DocumentTypeEnum.kPartDocumentObject Then
    	Dim partDoc As Inventor.PartDocument = refDoc
        'Add the part parameters to the assembly.
        For Each partUserParam As UserParameter In refDoc.ComponentDefinition.Parameters.UserParameters
            'Check to see if the parameter already exists.
            Dim checkParam As UserParameter = Nothing
            Try
            	checkParam = refDocUserParams.Item(partUserParam.Name)
            Catch
            	checkParam = Nothing
            End Try

            If checkParam Is Nothing Then
				'Create the missing parameter.
				Try
            	refDocUserParams.AddByExpression(partUserParam.Name, partUserParam.Expression, partUserParam.Units)
				Catch
				refDocUserParams.AddByValue(partUserParam.Name, partUserParam.Value, partUserParam.Units)
				End Try
			Else
            	'Update the value of the existing parameter.
				Try
            	checkParam.Expression = partUserParam.Expression
				Catch
				checkParam.Value = partUserParam.Value
			End Try
            End If
		Next
	End If
Next