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

Hi @JohnBoySarcia.  Almost all Autodesk provided sample codes are provided in VBA, instead of iLogic, unfortunately.  And that 'Set' keyword is used in VBA when setting the value of a variable who's Type is something other than (String, Integer, Double, and similar data types).  That keyword is not needed in iLogic rules.  There are a few other differences between VBA and vb.net also (iLogic uses vb.net).  Both coding languages are pretty similar, and both based on Visual Basic, but VBA is older and not properly maintained anymore, while vb.net is a newer (but not really new) coding language that continues to be maintained.

 

There is a handy iLogic shortcut snippet that we may be able to use in this situation:

iProperties.Material("ComponentName") = "MaterialName"

I do not recall its 'scope' (if it will only work on top level components, or if it will work on components at all levels), so it may require some testing on your end.  The online documentation does not mention its scope.  I do not really know if you need this code to only work on top level components, or if you need it to work on all components in all levels of the assembly, so the example code is for the all levels scenario, and is a bit longer.  There is another way of changing the 'material' of the parts being referenced by the components, but the other way requires much more lines of Inventor API code, and can get a bit more complicated.

Here is the iLogic rule example code you can try out.

Sub Main
	'Material is the name of a multi-value text type user parameter in the assembly
	sMaterial = Material
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
		Exit Sub
	End If
	Dim oADoc As AssemblyDocument = ThisDoc.Document
	Dim oOccs As ComponentOccurrences = oADoc.ComponentDefinition.Occurrences
	If oOccs.Count = 0 Then Exit Sub
	RecurseComponents(oOccs, AddressOf ProcessComponent)
	If oADoc.RequiresUpdate Then oADoc.Update2(True)
	MsgBox("This rule's work has finished.", vbInformation, "Job Is Done!")
End Sub

Dim sMaterial As String 'shared among routines

Sub RecurseComponents(oComps As ComponentOccurrences, ComponentProcess As Action(Of ComponentOccurrence))
	If oComps Is Nothing OrElse oComps.Count = 0 Then Exit Sub
	For Each oComp As ComponentOccurrence In oComps
		ComponentProcess(oComp)
		If oComp.Suppressed = False AndAlso _
			oComp.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
			RecurseComponents(oComp.SubOccurrences, ComponentProcess)
		End If
	Next
End Sub

Sub ProcessComponent(oComp As ComponentOccurrence)
	If oComp Is Nothing OrElse oComp.Suppressed Then Exit Sub
	If TypeOf oComp.Definition Is VirtualComponentDefinition Then Exit Sub
	If TypeOf oComp.Definition Is WeldsComponentDefinition Then Exit Sub
	Try 'only works on components that reference parts
		iProperties.Material(oComp.Name) = sMaterial
	Catch oEx As Exception
		Logger.Error("Error setting Material of component named:  " & oComp.Name _
		& vbCrLf & oEx.Message & vbCrLf & oEx.StackTrace & vbCrLf)
	End Try
End Sub

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) :thumbs_up:.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)