iLogic copy user parameters from parts to assembly

iLogic copy user parameters from parts to assembly

jishee
Enthusiast Enthusiast
2,732 Views
16 Replies
Message 1 of 17

iLogic copy user parameters from parts to assembly

jishee
Enthusiast
Enthusiast

Using iLogic, I want to be able to copy user parameters from part files into an assembly. I found an example that copies user parameters from an assembly into part files and tried modifying it to suit my needs and it runs without errors but does not copy over any of the user parameters. The code I have is below.

 

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 = refDoc.ComponentDefinition.Parameters.UserParameters

        '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 ex As Exception
            	checkParam = Nothing
            End Try

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

 Any help with what I am doing wrong is appreciated. 

Accepted solutions (1)
2,733 Views
16 Replies
Replies (16)
Message 2 of 17

marcin_otręba
Advisor
Advisor

change 

Dim refDocUserParams As UserParameters = refDoc.ComponentDefinition.Parameters.UserParameters

into

 

Dim refDocUserParams As UserParameters = asmdoc.ComponentDefinition.Parameters.UserParameters

 and move it above

For Each refDoc As Inventor.Document In asmDoc.AllReferencedDocuments

Hi, maybe you want to check my apps:


DrawingTools   View&ColoringTools   MRUFolders

0 Likes
Message 3 of 17

jishee
Enthusiast
Enthusiast

I know get a 'Parameter is incorrect' error. It runs until the line highlighted below.

jishee_0-1589401545635.png

 

0 Likes
Message 4 of 17

JhoelForshav
Mentor
Mentor
Accepted solution

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
0 Likes
Message 5 of 17

marcin_otręba
Advisor
Advisor

@JhoelForshav  have right that reason must be in missing parameter from expression.

 

If you really must add expression then you can start with this code if you want to determine wchich parameter missing:

 

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

	'Look for part documents.
	'If refDoc.DocumentType = Inventor.DocumentTypeEnum.kPartDocumentObject Then
    	Dim partDoc As Inventor.Document = 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.
				Dim checkParamdependants As Parameter = Nothing 
					Dim pardependants As String = ""
				
					
					For Each param As Parameter In refDoc.ComponentDefinition.Parameters
						If param.Type <> 50348800 Then
							If param.Dependents.count>0 Then
						For Each paramdep As Parameter In param.Dependents
						If paramdep.name = partUserParam.Name Then
					Try
			            	checkParamdependants = refDocUserParams.Item(param.Name)
			            Catch
							
			            '	If pardependants.Contains(param.name)=False Then
							pardependants = pardependants  & param.name & ";"
					'	End If
			            End Try
					End If
					Next
				End If
				End If
				Next
			Try
			If pardependants = "" Then
				refDocUserParams.AddByExpression(partUserParam.Name, partUserParam.Expression, partUserParam.Units)
			Else
				MessageBox.Show("Error, in assembly dependant parameters missing: " & pardependants)
			End If
			Catch
			 MessageBox.Show ("Error, in assembly dependant parameters missing: " & pardependants )
				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

 

 

Hi, maybe you want to check my apps:


DrawingTools   View&ColoringTools   MRUFolders

0 Likes
Message 6 of 17

jishee
Enthusiast
Enthusiast

@JhoelForshav it works correctly now. Thank you for your help! 

 

Thank you also @marcin_otręba !

Message 7 of 17

robertast
Collaborator
Collaborator

good afternoon @JhoelForshav 
Can you tell me which iLogic can transfer all user parameters from the assembly to all the part in this assembly?

 

in advance

thank you so much

0 Likes
Message 8 of 17

JhoelForshav
Mentor
Mentor

Hi @robertast 

The type of parameter copy that's done in this thread, but from assembly to all its parts would look something like this:

iLogicVb.UpdateWhenDone = True
Dim oAsm As AssemblyDocument = ThisDoc.Document
For Each oDoc As Document In oAsm.AllReferencedDocuments
	If oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject AndAlso oAsm.ComponentDefinition.Occurrences.AllReferencedOccurrences(oDoc).Count > 0
		Dim oDef As PartComponentDefinition = oDoc.ComponentDefinition
		For Each oParam As Inventor.Parameter In oAsm.ComponentDefinition.Parameters.UserParameters
			Dim paramInPart As Inventor.Parameter
			Try
				paramInPart = oDef.Parameters.UserParameters.Item(oParam.Name)
				Try
					paramInPart.Expression = oParam.Expression
				Catch
					paramInPart.Value = oParam.Value
				End Try
			Catch
				Try
					oDef.Parameters.UserParameters.AddByExpression(oParam.Name, oParam.Expression, oParam.Units)
				Catch
					oDef.Parameters.UserParameters.AddByValue(oParam.Name, oParam.Expression, oParam.Units)
				End Try
			End Try
		Next
	End If
Next
Message 9 of 17

robertast
Collaborator
Collaborator

@JhoelForshav 

Thank you very much, I am your debtor

0 Likes
Message 10 of 17

robertast
Collaborator
Collaborator

@JhoelForshav 

But when it’s good, it wants more.🤗  But is this most likely impossible?  🤔
If SUBASSEMBLY appears in ASSEMBLY, it passes parameters to all parts, including SUBASSEMBLY parts.
And he wants the ASSEMBLY to switch over to its parts, and not to trade SUBASSEMBLY. SUBASSEMBLY on their part.

Is it possible? If yes - the wine is from me...

Param to part.png

Message 11 of 17

JhoelForshav
Mentor
Mentor

@robertast 

Thats possible for sure. Only problem will be if the same part exists in multiple subassemblies...

I’ll probably not get home in time to write this code tonight. But if no one beats me to it i’ll fix it tomorrow😊

0 Likes
Message 12 of 17

JhoelForshav
Mentor
Mentor

@robertast 

Here you go 🙂

Sub Main
	Dim asm As AssemblyDocument = ThisDoc.Document
	copyParams(asm)
	iLogicVb.UpdateWhenDone = True
End Sub
Sub copyParams(oAsm As AssemblyDocument)
	For Each oDoc As Document In oAsm.ReferencedDocuments
		If oAsm.ComponentDefinition.Occurrences.AllReferencedOccurrences(oDoc).Count > 0
			If oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject
				Dim oDef As PartComponentDefinition = oDoc.ComponentDefinition
				For Each oParam As Inventor.Parameter In oAsm.ComponentDefinition.Parameters.UserParameters
					Dim paramInPart As Inventor.Parameter
					Try
						paramInPart = oDef.Parameters.UserParameters.Item(oParam.Name)
						Try
							paramInPart.Expression = oParam.Expression
						Catch
							paramInPart.Value = oParam.Value
						End Try
					Catch
						Try
							oDef.Parameters.UserParameters.AddByExpression(oParam.Name, oParam.Expression, oParam.Units)
						Catch
							oDef.Parameters.UserParameters.AddByValue(oParam.Name, oParam.Expression, oParam.Units)
						End Try
					End Try
				Next
			ElseIf oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject
				'if the referenced document is an assembly
				'the sub will call itself with that document as the argument
				copyParams(oDoc)
			End If
		End If
	Next
End Sub
Message 13 of 17

robertast
Collaborator
Collaborator

@JhoelForshav 

Something went wrong, the code swears ... 🙄

Message 14 of 17

robertast
Collaborator
Collaborator

Everything is in order, you must disable the "Straight VB code"Thanks again

Message 15 of 17

donaldleigh
Advocate
Advocate

Hi @JhoelForshav 

 

Just come across your reply here, If you only wanted certain parameters from certain parts in the assembly how would you do that please.

 

Eg, say we have "Parameter1" in the part "PartA" and I would like to copy this "Parameter1" to the assembly that contains "PartA"

 

Cheers

Donald

0 Likes
Message 16 of 17

alan.wedge
Enthusiast
Enthusiast

I am having issues with this code, im getting this error: 

 

The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))

 

any idea what this could be?

0 Likes
Message 17 of 17

damian_dymczyk
Contributor
Contributor
Hi GREAT WORK again!
I'm wondering is there a chance that instead of all the occurrences parameters would be only added from selected one ?
0 Likes