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
Adding Custom material using SetMaterialPropertyName2
Adding Custom material using SetMaterialPropertyName2
Deepak Gupta
SOLIDWORKS Consultant/Blogger
SOLIDWORKS Consultant/Blogger
Re: Adding Custom material using SetMaterialPropertyName2
Posting same thing as I posted over in the SW forum, just for future here.
Is the folder already listed in the Material Databases category of Tools>options>File Locations? You can't manually add a material from a DB that's not already in one of the locations listed there, so I imagine it's the same for API.
I have not been able to get it to add materials from any DB not already listed in File Locations. I'm guessing maybe the help for SetMaterialPropertyName2 was written before the DBs were listed in File Locations? Anyway... Once I add the DB's folder to File Locations, I can add a material from it just by referencing the file name only.
This code will add a path to File Locations if it's not there already, then assign a material from that DB.
Is the folder already listed in the Material Databases category of Tools>options>File Locations? You can't manually add a material from a DB that's not already in one of the locations listed there, so I imagine it's the same for API.
I have not been able to get it to add materials from any DB not already listed in File Locations. I'm guessing maybe the help for SetMaterialPropertyName2 was written before the DBs were listed in File Locations? Anyway... Once I add the DB's folder to File Locations, I can add a material from it just by referencing the file name only.
This code will add a path to File Locations if it's not there already, then assign a material from that DB.
Code: Select all
Dim swApp As SldWorks.SldWorks
Dim swDoc As SldWorks.PartDoc
Const MATLPATH As String = "\\newnas\Documents"
Const MATLFILE As String = "TestMatFile.sldmat"
Sub main()
Set swApp = Application.SldWorks
Set swDoc = swApp.ActiveDoc
Dim DBs As String
DBs = swApp.GetUserPreferenceStringValue(swUserPreferenceStringValue_e.swFileLocationsMaterialDatabases)
If Not InStr(1, DBs, MATLPATH, vbTextCompare) > 0 Then
DBs = DBs & ";" & MATLPATH
Debug.Print swApp.SetUserPreferenceStringValue(swUserPreferenceStringValue_e.swFileLocationsMaterialDatabases, DBs)
End If
swDoc.SetMaterialPropertyName2 "Default", MATLFILE, "DaggumBoxes"
End Sub
Re: Adding Custom material using SetMaterialPropertyName2
I changed my material database name from Materials to SWaterials (such that it is not similar to default database names), it starts to work OK.
Thank you for your inputs @josh
Thank you for your inputs @josh
Deepak Gupta
SOLIDWORKS Consultant/Blogger
SOLIDWORKS Consultant/Blogger