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

So what was missing was to open the document and then set the material. Trying to set the material without opening was not working. 

 

Sub Main()
	Dim oADOC As Inventor.Document = ThisDoc.Document
	If oADOC.DocumentType <> kAssemblyDocumentObject Then
		MessageBox.Show("This rule cannot be executed in this file type.", "iLogic")
	Exit Sub
	End If

	Dim oAsm As AssemblyDocument = ThisDoc.Document
	Dim oTransaction As Transaction = ThisApplication.TransactionManager.StartTransaction(oAsm, "FG Member Material Changer") 'Make this a single transaction

	Dim oAL_List As New ArrayList
	Dim lib_List As New ArrayList

	For Each oAL As AssetLibrary In ThisApplication.AssetLibraries
		If InStr(oAL.DisplayName , "Material") <> 0 Then  
			oAL_List.Add(oAL.DisplayName)
		End If
	Next

	Dim oALSelect As String = InputListBox("Select Asset Library", oAL_List, oAL_List(0), "Change Part Material", "Available Selections")
	If oALSelect Is Nothing Then Exit Sub
	
	Dim oLib As AssetLibrary = ThisApplication.AssetLibraries(oALSelect)

	For Each libAsset In oLib.MaterialAssets
		lib_List.Add(libAsset.DisplayName)
	Next

	lib_List.Sort

	Dim oLibAssetSel As String = InputListBox("Select Material", lib_List, lib_List(0), "Change Part Material", "Available Selections")
	If oLibAssetSel Is Nothing Then Exit Sub
		
	For Each oDoc As Document In oAsm.AllReferencedDocuments 'Traverse all referenced documents
		If oDoc.DocumentInterests.HasInterest("{AC211AE0-A7A5-4589-916D-81C529DA6D17}") _	'Frame generator component
			AndAlso oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject _'Part
			AndAlso oDoc.IsModifiable _	'Modifiable 
			AndAlso oAsm.ComponentDefinition.Occurrences.AllReferencedOccurrences(oDoc).Count > 0 'Exists in assembly (not derived base component)

			Dim oPartDoc As PartDocument  = ThisApplication.Documents.Open(oDoc.FullFileName,False)
		
			Dim libMaterial As MaterialAsset
			Try
			 	libMaterial = oPartDoc.MaterialAssets.Item(oLibAssetSel)
			Catch
				libMaterial = oLib.MaterialAssets.Item(oLibAssetSel)
				libMaterial.CopyTo(oPartDoc)
			End Try
			
			oPartDoc.ActiveMaterial = libMaterial
			oPartDoc.Update
			oPartDoc.Close(True)
			
		End If
	Next
	oTransaction.End
End Sub

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan