Add and format parameter to all parts in assembly

Add and format parameter to all parts in assembly

DewayneH
Advocate Advocate
687 Views
4 Replies
Message 1 of 5

Add and format parameter to all parts in assembly

DewayneH
Advocate
Advocate

I'm working on an external rule to add and format a set of parameters to all parts within an assembly.

I'm trying to check if the parameter exists then add and format if it doesn't.

 

I've found a few examples and unsuccessfully tried to piece something together.

 

The attached code runs without error, but it doesn't affect the parts in the assembly. It does nothing.

Suggestions?

 

Dim oAssDoc As AssemblyDocument = ThisDoc.Document
Dim oDoc As Document

For Each oDoc In oAssDoc.AllReferencedDocuments
	If oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then

		Dim oParams As Inventor.Parameters = oDoc.ComponentDefinition.Parameters
		Dim oUserParams As UserParameters = oParams.UserParameters
		oMyParameter = oUserParams

		Try
			'Try to Change value Of param
			Parameter("RMCat") = Parameter("RMCat")

		Catch
			'Create Param as it doesn't exist
			oParameter = oMyParameter.AddByValue("RMCat", "Plate-Sheet", UnitsTypeEnum.kTextUnits)
			MultiValue.SetList("RMCat", "Plate-Sheet", "Check Plate", "Round", "Tube", "Rubber", "None")
			Parameter.Param("RMCat").Comment = "Raw Material Category"

		End Try
	End If
Next

 

Dewayne
Inventor Pro 2023
Vault Pro 2023
0 Likes
Accepted solutions (1)
688 Views
4 Replies
Replies (4)
Message 2 of 5

FINET_Laurent
Advisor
Advisor

Hi,

 

Maybe this ? For all parts referenced in the assembly :

 

Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oLeafOcc As ComponentOccurrence
Dim oParameter As UserParameter 

Dim oString() As String = {"""RMCat""", """Plate-Sheet""", """Check Plate""","""Round""","""Tube""","""Rubber""","""None"""} 'Create multi-value list

For Each oLeafOcc In oDoc.ComponentDefinition.Occurrences.AllLeafOccurrences

	Try 'Try to change parameter value
			
		oLeafOcc.Definition.Parameters.UserParameters.Item("RMCat").Value = "RMCat"	'Change to the desired value			

	Catch 'Create the parameter
			
		oParameter = oLeafOcc.Definition.Parameters.UserParameters.AddByValue("RMCat", "Plate-Sheet", UnitsTypeEnum.kTextUnits)
		oParameter.ExpressionList.SetExpressionList(oString,False)
		oParameter.Comment = "Raw Material Category"

	End Try

Next

Hope this helps,

 

Regards,

 

FINET L.

If this post solved your question, please kindly mark it as "Solution"

If this post helped out in any way to solve your question, please drop a "Like"

@LinkedIn     @JohnCockerill

Message 3 of 5

Anonymous
Not applicable
Hi,
 
There is an app in the App Store called Fyenite that allows you to push global parameters and/or properties to any number of parts, assemblies, and drawings. Might be worth checking out!
 
 
Cheers,
Jeff
0 Likes
Message 4 of 5

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi @DewayneH 

 

here's another method

 

Sub Main
	Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument
	Dim oOccs As ComponentOccurrences = oDoc.ComponentDefinition.Occurrences

	Call TraverseAssembly(oOccs)

End Sub

Function TraverseAssembly(oOccs As ComponentOccurrences)

	Dim oOcc As ComponentOccurrence
	For Each oOcc In oOccs

		'process subassemblies
		If oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then			
			Call TraverseAssembly(oOccs)
		ElseIf oOcc.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Then	
			Call AddParam(oOcc)
		End If
	Next

End Function



Sub AddParam(oOcc As ComponentOccurrence)

	oDoc = oOcc.Definition.Document

	Dim oParams As Inventor.Parameters = oDoc.ComponentDefinition.Parameters
	Dim oUserParams As UserParameters = oParams.UserParameters

	Try
		'Try to get the parameter
		oParameter = oUserParams.Item("RMCat")
	Catch
		'Create Param as it doesn't exist
		oParameter = oUserParams.AddByValue("RMCat", "Plate-Sheet", UnitsTypeEnum.kTextUnits)

	End Try
	MultiValue.SetListInComponent(oOcc.name, "RMCat", "Plate-Sheet", "Check Plate", "Round", "Tube", "Rubber", "None")
	oParameter.Comment = "Raw Material Category"

End Sub

 

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com
 

EESignature

0 Likes
Message 5 of 5

DewayneH
Advocate
Advocate

Curtis,

 

As usual, works beautifully.
I added some additional parameters with formatting, and it's works great.

Thanks!

Dewayne
Inventor Pro 2023
Vault Pro 2023
0 Likes