iLogic push all assembly user parameters to parts

Jelvin_
Advocate
Advocate

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

 

 

Reply
Accepted solutions (1)
5,698 Views
26 Replies
Replies (26)

BrianEkins
Mentor
Mentor
Accepted solution

Here's some pure API code that is written to run within iLogic that will copy all user parameters from the active assembly all parts referenced by the assembly, regardless of where they are in the assembly structure.  If the parameter already exists in the part, it will update the value.

 

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

    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
                    ' Create the missing parameter.
                    refDocUserParams.AddByExpression(asmUserParam.Name, asmUserParam.Expression, asmUserParam.Units)
                Else
                    ' Update the value of the existing parameter.
                    checkParam.Expression = asmUserParam.Expression
                End If
            Next
        End If
    Next
End Sub
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com

Jelvin_
Advocate
Advocate

Many thanks!

 

This is actually quite similar to my solution but yours is complete and seems a bit more streamlined. I will try and adapt this to fit my needs.

 

//J

0 Likes

Jelvin_
Advocate
Advocate

 Hi Brian, a follow up question here.

 

While the code works great, it does not trigger automatically on a parameter change from example a form in the assembly. Is there any easy way to make this code launch automatically (not by specifying a separate trigger)?

0 Likes

Anonymous
Not applicable

Hi Brian,

Thanks for submitting this rule,

I was trying to run it into one of my assemblies and had the following error message, could you please help me with this?:

Error in rule: Rule6, in document: 22298-CAB-FR.iam

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

System.ArgumentException: The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Inventor.UserParameters.AddByExpression(String Name, String Expression, Object UnitsSpecifier)
at ThisRule.CopyUserParams()
at ThisRule.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

0 Likes

BrianEkins
Mentor
Mentor

I would need to be able to run it with your assembly to be able to duplicate the problem and see what's causing it.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes

Anonymous
Not applicable

Really appreciate Brian,

I have never shared a model using these forums,

How do I give you the model, it has so many parts

can that be placed in the cloud somewhere? Pack & Go?

 

Thanks,

 

0 Likes

BrianEkins
Mentor
Mentor

Probably the easiest is to create a Pack and Go archive and then provide access to me via some shared site, (DropBox, Box, OneDrive, etc.)  You can contact me directly at the email in my signature.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com

Anonymous
Not applicable

I'll do that Brian, I'm just in the middle of something and as soon as I free some time I'll try that method, probably tomorrow.

Thanks again for your help,

 

Eric

0 Likes

Anonymous
Not applicable

Brian,

   I was doing some testing to see how might be able to utilize the code you had posted.  I am able to get it run and work correctly in an assembly with only parts.  I am able to get the same error message when trying to run it with an assembly that has sub assemblies.

 

Looking through the code it appears to only push to parts if i am understanding correctly? Although I am admittedly only knowledgeable enough with iLogic and the API to mostly make a mess of stuff.  I tried making some changes but wasn't able to get it to work pushing the parameters to a sub assembly. At which point i think i could create a rule with the original code in the subassembly to push to the parts.

Anonymous
Not applicable

Thanks!

I did so. But it don't automatically update when i change parameters of assembly. It only update when i Run Rule by the manually. Do you have a better solution?

0 Likes

RoyWickrama_RWEI
Advisor
Advisor

Great. I like it - useful.

I need to copy the selected user parameters from one part (Doc 1) to the part selected next (Doc. 2).

With the help of your coding, I modified one of the rules I have to copy custom props similarly. But, it is not going through all the way.

Could you help. Thanks.

 

If ThisDoc.Document.DocumentType <> Inventor.DocumentTypeEnum.kAssemblyDocumentObject Then
    MsgBox("The active document must be an assembly.")
    Return
End If
	
Dim oOcc As ComponentOccurrence
oOcc = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, "Select End 1:")
oOcc_Selected1 = oOcc
' If no part is selected, end the rule
If oOcc_Selected1 Is Nothing Then Exit Sub
'MessageBox.Show(oOcc_Selected1.Name, "Selection 1")
Dim oDoc_Selected1 As Document = oOcc_Selected1.Definition.Document
'oProSet = oDoc_Selected1.PropertySets.Item("Inventor User Defined Properties")
oProSet_Doc_Selected1 = oDoc_Selected1.PropertySets.Item("Inventor User Defined Properties")
Dim oProps_oOcc_Selected1 As New ArrayList
For Each oProp In oProSet_Doc_Selected1
'	MessageBox.Show("Message: " & oProp.Name, "Title")
	oProps_oOcc_Selected1.Add(oProp.Name)
Next
Dim oOcc2 As ComponentOccurrence
oOcc2 = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, "Select End 1:")

oOcc_Selected2 = oOcc2
' If no part is selected, end the rule
If oOcc_Selected2 Is Nothing Then Exit Sub

'MessageBox.Show(oOcc_Selected2.Name, "Selection 2")
Dim oDoc_Selected2 As Document = oOcc_Selected2.Definition.Document
oProSet_Doc_Selected2 = oDoc_Selected2.PropertySets.Item("Inventor User Defined Properties")

        ' Look for part documents.
        If oDoc_Selected2.DocumentType = Inventor.DocumentTypeEnum.kPartDocumentObject Then
            Dim partDoc As Inventor.PartDocument = oDoc_Selected2
            Dim oUserParam_Doc2_Selected As UserParameters = partDoc.ComponentDefinition.Parameters.UserParameters

            ' Add the assembly parameters to the part.
            For Each oUserParam_Doc1_Selected As UserParameter In oDoc_Selected1.ComponentDefinition.Parameters.UserParameters
				If oUserParam_Doc1_Selected.Name.Contains("Stairs") Then
					MessageBox.Show("oUserParam_Doc1_Selected.Name: " & oUserParam_Doc1_Selected.Name, "Title")

                ' Check to see if the parameter already exists.
                Dim checkParam As UserParameter = Nothing
                Try
                    checkParam = oUserParam_Doc2_Selected.Item(oUserParam_Doc1_Selected.Name)
					MessageBox.Show("Found", "Title")
                Catch ex As Exception
                    checkParam = Nothing
		

                End Try
                	If checkParam Is Nothing Then
						MessageBox.Show("Not Found", "Title")
'				        ' Create the missing parameter.

	               	oUserParam_Doc2_Selected.AddByExpression(oUserParam_Doc1_Selected.Name, oUserParam_Doc1_Selected.Expression, oUserParam_Doc1_Selected.Units)
				   Else
	                    ' Update the value of the existing parameter.
	                    checkParam.Expression = oUserParam_Doc1_Selected.Expression
	                End If
				End If
            Next
        End If

InventorVb.DocumentUpdate()
iLogicVb.UpdateWhenDone = True

0 Likes

Anonymous
Not applicable

the rule is working fine with numeric values only but with text user parameter or boolean it giving the below error , are there any solution for it.

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

0 Likes

Ralf_Krieg
Advisor
Advisor

Hello

 

I don't know exactly how, but boolean and text parameters are handled in a different way. I worked around by checking value type.

 

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

    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
				
				Dim svalue As String
            	Dim bvalue As Boolean
				
                If checkParam Is Nothing Then
                   ' Create the missing parameter.
                If asmUserParam.Units = "Text" Then
                    svalue = Replace(asmUserParam.Value, Chr(34), "")
                    Call refDocUserParams.AddByValue(asmUserParam.Name, svalue, asmUserParam.Units)
                ElseIf asmUserParam.Units = "Boolean" Then
                    bvalue = Replace(asmUserParam.Value, Chr(34), "")
                    Call refDocUserParams.AddByValue(asmUserParam.Name, CBool(bvalue), asmUserParam.Units)
                Else
                    Call refDocUserParams.AddByExpression(asmUserParam.Name, asmUserParam.Expression, asmUserParam.Units)
                End If
            Else
                ' Update the value of the existing parameter.
                If asmUserParam.Units = "Text" Then
                    svalue = Replace(asmUserParam.Value, Chr(34), "")
                    checkParam.Value = svalue
                ElseIf asmUserParam.Units = "Boolean" Then
                    bvalue = Replace(asmUserParam.Value, Chr(34), "")
                    checkParam.Value = bvalue
                Else
                    checkParam.Expression = asmUserParam.Expression
                End If
            End If
            Next
        End If
    Next
End Sub

 


R. Krieg
RKW Solutions GmbH
www.rkw-solutions.com

helouise
Enthusiast
Enthusiast

@BrianEkins 

 

Hi Brian,

 

I know this is an answer from 2018 but the iLogic still works.

On Inventor 2022 with model states as soon as I Create model states in parts the rule gives me an error "Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))" and no longer works.

 

Is there a workaround for this?

I just with iLogic

 

Thank you

 

 

0 Likes

tyler.duttonT8EDE
Explorer
Explorer
Excellent code, thank you.
0 Likes

myronHBBUW
Contributor
Contributor

Hello,

 

I want to change the code to skip standard parts. For instance when they are not in the same projectfile

The code now gives an error, can somebody help me? 

0 Likes

tyler.duttonT8EDE
Explorer
Explorer

it appears code broke in inventor 2023. I found a tutorial for skeletal modelling that serves the same purpose. i have linked it below. the tutorial resources are no longer available but the process still works perfectly

 

https://knowledge.autodesk.com/support/inventor/learn-explore/caas/CloudHelp/cloudhelp/2014/ENU/Inve...

0 Likes

Ralf_Krieg
Advisor
Advisor

Hello

 

You can try to work around the error message when modelstates exist. With model states it is possible to create user parameters with different values for the factory document or one of the states. Adding a user parameter needs to be done in factory document afaik.

Standard parts and other write protected could be skipped silently with a mark in debug console.

 

Try this:

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

break

    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 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
					
					Dim svalue As String
	            	Dim bvalue As Boolean
					
	                If checkParam Is Nothing Then
	                   ' Create the missing parameter.
		                If asmUserParam.Units = "Text" Then
		                    svalue = Replace(asmUserParam.Value, Chr(34), "")
		                    Call refDocUserParams.AddByValue(asmUserParam.Name, svalue, asmUserParam.Units)
		                ElseIf asmUserParam.Units = "Boolean" Then
		                    bvalue = Replace(asmUserParam.Value, Chr(34), "")
		                    Call refDocUserParams.AddByValue(asmUserParam.Name, CBool(bvalue), asmUserParam.Units)
		                Else
		                    Call refDocUserParams.AddByExpression(asmUserParam.Name, asmUserParam.Expression.ToString , asmUserParam.Units)
		                End If
		            Else
		                ' Update the value of the existing parameter.
		                If asmUserParam.Units = "Text" Then
		                    svalue = Replace(asmUserParam.Value, Chr(34), "")
		                    checkParam.Value = svalue
		                ElseIf asmUserParam.Units = "Boolean" Then
		                    bvalue = Replace(asmUserParam.Value, Chr(34), "")
		                    checkParam.Value = bvalue
		                Else
		                    checkParam.Expression = asmUserParam.Expression
		                End If
		            End If
	            Next
			Catch ex As Exception
				Logger.Debug("Skipped file: " & partDoc.FullFileName)
			End Try
        End If
    Next
End Sub

 


R. Krieg
RKW Solutions GmbH
www.rkw-solutions.com

myronHBBUW
Contributor
Contributor
Thanks, this works for me!
0 Likes