Hi @isocam. Here is another example Sub routine you could review / test for that task. It uses updated object types (Asset & MaterialAsset) and properties. The following online help documentation link may help in explaining the differences.
Consistent Materials (Materials and Appearances)
First of all, the material (a MaterialAsset) must exist within the Document itself (in the PartDocument.MaterialAssets), before it can be applied to anything within that Document. So, if the material you are specifying is only defined/available within a MaterialLibrary (an AssetLibrary), then you will need to copy it into the Document first, then use the Document version. This code example below will first look for the specified material within the part, then if not found there, it will look for it in the 'active' MaterialLibrary. If it is not found, nothing happens, but if found, it will try to set that as the active material of the part.
At one point within this routine (Lines 18 & 19), it is getting access to the Inventor.Application object, though other local object properties, but your standalone app will likely already have a variable representing this object which was defined outside of this routine. In that case, you could substitute your variable in Line 19, then delete Line 18.
Private Sub SetMaterial(oDoc As Inventor.Document, sMaterialName As String)
If (oDoc Is Nothing) OrElse (Not oDoc.IsModifiable) OrElse
(Not TypeOf oDoc Is PartDocument) Then
Return
End If
If sMaterialName Is Nothing OrElse sMaterialName = "" Then Return
Dim oPDoc As PartDocument = oDoc
Dim oDocMAs, oLibMAs As AssetsEnumerator, oMyMA As Inventor.MaterialAsset
Try : oDocMAs = oPDoc.MaterialAssets : Catch : End Try
If oDocMAs IsNot Nothing AndAlso oDocMAs.Count > 0 Then
For Each oDocMA As Inventor.MaterialAsset In oDocMAs
If oDocMA.Name = sMaterialName OrElse oDocMA.DisplayName = sMaterialName Then
oMyMA = oDocMA
Exit For
End If
Next oDocMA
End If
If oMyMA Is Nothing Then
Dim oInvApp As Inventor.Application = oPDoc.DocumentEvents.Application
oLibMAs = oInvApp.ActiveMaterialLibrary.MaterialAssets
For Each oLibMA As Inventor.MaterialAsset In oLibMAs
If oLibMA.Name = sMaterialName OrElse oLibMA.DisplayName = sMaterialName Then
Try : oMyMA = oLibMA.CopyTo(oPDoc, True) : Catch : End Try
Exit For
End If
Next oLibMA
End If
If oMyMA IsNot Nothing Then
Try : oPDoc.ActiveMaterial = oMyMA : Catch : End Try
End If
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) 👍.
Wesley Crihfield

(Not an Autodesk Employee)