Adding Custom material using SetMaterialPropertyName2

I’m trying to add a custom material from a material database which is on a network drive. Below is the line I’m using to add the desired material. And it is not working. If I move the material to default location a

swPart.SetMaterialPropertyName2 sConfigName, “\Master Material File\materials.sldmat”, sMaterialName

If I move the material to default location, then below lines work as desired. Anyone having success in adding a custom material from a network location via API? Thank in advance.

swPart.SetMaterialPropertyName2 sConfigName, “materials.sldmat”, sMaterialName

1 Like

You need to add the material library folder to the material libraries folder list in the settings. Have you done?

I have used the PartDoc::SetMaterialPropertyName2 from a network location for years now. What I did first was get all materialdatabases with SldWorks::GetMaterialDatabases and then get the filename for the database from that list:

Dim swApp As Object
Dim swModel As ModelDoc2
Dim swPart As PartDoc
Dim materialDatabaseArray As Variant
Dim materialDatabase As String

Sub main()
    Set swApp = Application.SldWorks

    Set swModel = swApp.ActiveDoc
    Set swPart = swModel
    materialDatabaseArray = swApp.GetMaterialDatabases
    For i = 0 To UBound(materialDatabaseArray)
        If InStr(materialDatabaseArray(i), "databaseName.sldmat") > 0 Then
            materialDatabase = materialDatabaseArray(i)
        End If
    Next i
    swPart.SetMaterialPropertyName2 swModel.ConfigurationManager.ActiveConfiguration.Name, materialDatabase, "materialName"
End Sub
1 Like