AnsweredAssumed Answered

SetMaterialProperty not updating the material

Question asked by Aaron Heathcote on Dec 20, 2018
Latest reply on Jan 2, 2019 by Aaron Heathcote

I have been trying to update material properties to custom materials which I have followed this example for:

2018 SOLIDWORKS API Help - Set Material Example (C#)

swPart = (SldWorks.PartDoc)swModel;

// Get the material schema and names

// of available materials databases

vMatDBarr = (object[])swApp.GetMaterialDatabases();

Debug.Print("Material schema pathname = " + swApp.GetMaterialSchemaPathName());

for (i = 0; i < vMatDBarr.Length; i++)

{

Debug.Print(" Material database: " + vMatDB);

}

Debug.Print("");

Bodies = (object[])swPart.GetBodies2((int)swBodyType_e.swAllBodies, false);

for (j = 0; j < Bodies.Length; j++)

{

swBody = (SldWorks.Body2)Bodies[j];

// Get the name of the body

Debug.Print(swBody.Name);

 

 

// Set the SOLIDWORKS material for that body

swBody.Select2(false, null);

BodyMaterialError = swBody.SetMaterialProperty("Default", "LinktoMaterials", "AISI 304 Stainless Steel");

 

// Get the names of the body's material and the database to which it belongs

sMatName = swBody.GetMaterialPropertyName("", out sMatDB);

if (string.IsNullOrEmpty(sMatName))

{

Debug.Print("Body " + j + "'s material name: No material applied");

}

else

{

Debug.Print("Body " + j + "'s material name: " + sMatName);

Debug.Print("Body " + j + "'s material database: " + sMatDB);

Debug.Print(" ");

}

}

The code is finding the correct database and material name but it isn't applying it to the part.

"BodyMaterialError" is returning a value of 1 ( swBodyMaterialApplicationError_NoError 1 )

 

During the last IF statement it returns my database and material name which is correct but the material is still not updating:

3.PNG

 

Can anyone point me in the right direction as to what I'm doing incorrectly?

Outcomes