Hi @mallorn. Here is another way you could do a task like this...using the Inventor API way, instead of the iLogic API way. The code still exists in an iLogic rule, but obviously a lot more code is involved this way. We can also see what all is going on in the process this way. We also trade out specifying which document we want to set the material of from 'document name' (which is vague, no direct property for), to specifying the actual Document object itself, which eliminates all guess work. This code example below could have been expanded further, to include searching through additional 'global' material libraries, and optionally to create the material, if it can not be found anywhere, but there are lots of specifications involved in materials, so that one optional step could potentially involve a whole extra code routine with a lot of inputs being requested.
Here is the Inventor API example code for an iLogic rule:
Sub Main
For Each oDoc As Inventor.Document In ThisApplication.Documents.VisibleDocuments
SetMaterialByName(oDoc, "Generic")
Next
End Sub
Sub SetMaterialByName(doc As Inventor.Document, MaterialName As String)
If (Not TypeOf doc Is PartDocument) Then Return
If Not doc.IsModifiable Then Return
Dim oPDoc As PartDocument = doc
Dim oMatAsset, oNeededMatAsset As MaterialAsset
For Each oMatAsset In oPDoc.MaterialAssets
If oMatAsset.DisplayName = MaterialName Then
oNeededMatAsset = oMatAsset
Exit For
End If
Next
If oNeededMatAsset Is Nothing Then
For Each oMatAsset In ThisApplication.ActiveMaterialLibrary.MaterialAssets
If oMatAsset.DisplayName = MaterialName Then
Try
oNeededMatAsset = oMatAsset.CopyTo(oPDoc)
Catch ex As Exception
Logger.Error(vbCrLf & ex.ToString)
End Try
Exit For
End If
Next
End If
If oNeededMatAsset Is Nothing Then
Logger.Warn("Could not find existing 'material' named '" & MaterialName & "'!")
Return
'or, try to create the material within the part, then set it as the active one
'Dim oNewAsset As Inventor.Asset = oPDoc.Assets.Add(AssetTypeEnum.kAssetTypeMaterial, , , "Generic")
'oNeededMatAsset = CType(oNewAsset, Inventor.MaterialAsset)
End If
Try
oPDoc.ActiveMaterial = oNeededMatAsset
oPDoc.Update2(True)
oPDoc.Save()
Catch ex As Exception
Logger.Error(vbCrLf & ex.ToString)
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) 👍.
Wesley Crihfield

(Not an Autodesk Employee)