Adding Custom material using SetMaterialPropertyName2

Programming and macros
User avatar
gupta9665
Posts: 405
Joined: Thu Mar 11, 2021 10:20 am
Answers: 25
Location: India
x 426
x 445

Adding Custom material using SetMaterialPropertyName2

Unread post by gupta9665 »

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
Deepak Gupta
SOLIDWORKS Consultant/Blogger
User avatar
josh
Posts: 293
Joined: Thu Mar 11, 2021 1:05 pm
Answers: 16
x 22
x 500

Re: Adding Custom material using SetMaterialPropertyName2

Unread post by josh »

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.

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
User avatar
gupta9665
Posts: 405
Joined: Thu Mar 11, 2021 10:20 am
Answers: 25
Location: India
x 426
x 445

Re: Adding Custom material using SetMaterialPropertyName2

Unread post by gupta9665 »

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
Deepak Gupta
SOLIDWORKS Consultant/Blogger
Post Reply