iLogic push all assembly user parameters to parts

iLogic push all assembly user parameters to parts

Jelvin_
Advocate Advocate
6,691 Views
26 Replies
Message 1 of 27

iLogic push all assembly user parameters to parts

Jelvin_
Advocate
Advocate

Hi

 

I'm trying to push my assembly user parameters to the assembly parts. I have all my assembly user parameters added to a list called UserParamList(j)

 

i would like to achieve this

 

 

Parameter(FileName, "Length") = Length

With this

 

 

 

Parameter(FileName, UserParamList(j)) = UserParamList(j)

But i notice that UserParamList(j) to the right is expecting a parameter and not a string.

 

Im using this to collect all my user parameters and stor in my arraylist

 

For Each oParam In userParams
	If oParam.Units <> "Text" And oParam.Units <> "Boolean" Then
		UserParamList.Add(oParam.Name)
	End If
Next

 

 

 

What is the best way to solve this issue? If i just type the parameter name in the ilogic code, it turns blue. I would like my UserParamList(j) to "mimic" this behaviour.

 

/J

 

 

Accepted solutions (1)
6,692 Views
26 Replies
Replies (26)
Message 21 of 27

myronHBBUW
Contributor
Contributor

I want to try it from a different direction.

 

I want to just select one part in the same folder.

From that part i derive my other parts and link my (sub)assemblys.

 

Can you help me tweak your code to push the properties to below part?

 

many thanks

docFName = ThisDoc.PathAndFileName(False).Replace( 100 ,"stuurschets")& ".ipt"

 

0 Likes
Message 22 of 27

myronHBBUW
Contributor
Contributor

@Ralf_Krieg , can you help me change your code to push the properties to a single part by full filename?

0 Likes
Message 23 of 27

Ralf_Krieg
Advisor
Advisor

Hello

 

Sorry, I'm currently not here every day.

I assume the source file is not always the same? I added a file open dialog to select the part file to use as source. The version with a hard coded source file is included, but commented.

I'm not able to test it, so please be careful and test it with a test assembly or copy first.

Public Sub Main()
	CopyUserParams()
End Sub

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

	'Variant A - hard coded SourceFile
	'Dim oSourceDoc As PartDocument = ThisApplication.Documents.Open(ThisDoc.PathAndFileName(False).Replace( 100 ,"stuurschets")& ".ipt", False)
	
	'Variant B - Select SourceFile in a dialog
	Dim oDlg As Inventor.FileDialog
	ThisApplication.CreateFileDialog(oDlg)
	oDlg.CancelError=False
	oDlg.DialogTitle = "Select parameter source file..."
	oDlg.Filter="Inventor Part Files (*.ipt)|*.ipt"
	oDlg.InitialDirectory=ThisDoc.Path
	oDlg.MultiSelectEnabled = False
	oDlg.OptionsEnabled = False

	oDlg.ShowOpen
	
	Dim oSourceDoc As PartDocument
    If oDlg.FileName <> "" Then
		MsgBox("File " & oDlg.FileName & " was selected.")
		oSourceDoc = ThisApplication.Documents.Open(oDlg.FileName, False)
	Else
		Exit Sub
    End If
	'End of Variant B

    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 
			Try
				refDocUserParams = partDoc.ComponentDefinition.FactoryDocument.componentdefinition.Parameters.UserParameters
			Catch ex As Exception
				refDocUserParams = partDoc.ComponentDefinition.Parameters.UserParameters
			End Try
			
			Try
	            ' Add the part parameters of source file to the part.
	            For Each sourceUserParam As UserParameter In oSourceDoc.ComponentDefinition.Parameters.UserParameters
	                ' Check to see if the parameter already exists.
	                Dim checkParam As UserParameter = Nothing
	                Try
	                    checkParam = refDocUserParams.Item(sourceUserParam.Name)
	                Catch ex As Exception
	                    checkParam = Nothing
	                End Try
					
					Dim svalue As String
	            	Dim bvalue As Boolean
					
	                If checkParam Is Nothing Then
	                   ' Create the missing parameter.
		                If sourceUserParam.Units = "Text" Then
		                    svalue = Replace(sourceUserParam.Value, Chr(34), "")
		                    Call refDocUserParams.AddByValue(sourceUserParam.Name, svalue, sourceUserParam.Units)
		                ElseIf sourceUserParam.Units = "Boolean" Then
		                    bvalue = Replace(sourceUserParam.Value, Chr(34), "")
		                    Call refDocUserParams.AddByValue(sourceUserParam.Name, CBool(bvalue), sourceUserParam.Units)
		                Else
		                    Call refDocUserParams.AddByExpression(sourceUserParam.Name, sourceUserParam.Expression.ToString , sourceUserParam.Units)
		                End If
		            Else
		                ' Update the value of the existing parameter.
		                If sourceUserParam.Units = "Text" Then
		                    svalue = Replace(sourceUserParam.Value, Chr(34), "")
		                    checkParam.Value = svalue
		                ElseIf sourceUserParam.Units = "Boolean" Then
		                    bvalue = Replace(sourceUserParam.Value, Chr(34), "")
		                    checkParam.Value = bvalue
		                Else
		                    checkParam.Expression = sourceUserParam.Expression
		                End If
		            End If
	            Next
			Catch ex As Exception
				Logger.Debug("Skipped file: " & partDoc.FullFileName)
			End Try
        End If
    Next
	
	If oSourceDoc IsNot Nothing Then 
		If oSourceDoc.Views.Count=0 Then
			oSourceDoc.Close(True)
		End If
	End If
		
End Sub

 


R. Krieg
RKW Solutions
www.rkw-solutions.com
0 Likes
Message 24 of 27

jnewon
Advocate
Advocate

Thanks to all the worked on this. It works great for me using inventor 2023. 

 

I have one question, I noticed that if the parameter doesn't exist it adds the pushed parameters to all the parts. Is there a way to prevent this? 

 

I'm working with a top level assembly. I have 3 different length parameters I want pushed (Length_1, Length_2, Length_3). Only curtain parts use Length_1 and have a matching parameter in that part. The code puts all pushed parameters in the parts used or not. This I don't need. Only to update the parameter that exists in the part.

 

Thanks

John

0 Likes
Message 25 of 27

Ralf_Krieg
Advisor
Advisor

Hello

 

Comment this part of the code, that creates a non existing parameter. In this case, the "If checkParam Is Nothing Then" statement will just do nothing and process the next parameter.

' Create the missing parameter.
If sourceUserParam.Units = "Text" Then
    svalue = Replace(sourceUserParam.Value, Chr(34), "")
    Call refDocUserParams.AddByValue(sourceUserParam.Name, svalue, sourceUserParam.Units)
ElseIf sourceUserParam.Units = "Boolean" Then
    bvalue = Replace(sourceUserParam.Value, Chr(34), "")
    Call refDocUserParams.AddByValue(sourceUserParam.Name, CBool(bvalue), sourceUserParam.Units)
Else
    Call refDocUserParams.AddByExpression(sourceUserParam.Name, sourceUserParam.Expression.ToString , sourceUserParam.Units)
End If

 


R. Krieg
RKW Solutions
www.rkw-solutions.com
Message 26 of 27

jnewon
Advocate
Advocate
Thanks for pointing that out. I totally missed that piece of code.
0 Likes
Message 27 of 27

johan
Enthusiast
Enthusiast

This code works perfectly!
So I made a template assembly with all parameters imported from an xml.
I pushed them forward to all underlying parts, works like a charm, so all userparameters in all assemblies and or parts are the same, and of course unique.

Now I made a form, where I change one parameter. 
I created the event trigger on change parameter, but I can't seem to getting it to work to populate only that changed parameter to the below lying parts.

It's a tank, on which I want to change it's diameter, length etc, to be able to simulate afterwards.

What I would need:

On change parameter t_l

Run through all underlying parts and update t_l with that value.
thisassembly.document.update lets Inventor crash. 
Calling the complete code (thus running over all of the parameters, gives an error) on parameterchange gives an error.
Pack&go attached Version 2024

0 Likes