Macro to apply .p2m based on material

I have been finding lots of different macros to apply appearance and/or materials. However the material of the part must be known before, and the appearance is only an RGB number. I'm trying to figure out how to apply a .p2m appearance file to a part, based on the already applied material. I was hoping to do a lookup and an If/Then action. Can anyone help?

SolidworksApi macros
Answers 8
NH
NH 2017-11-16

Thank you for your input. Can you verify that this will update current configuration and display state. I don't want to add an extra display state, I want to update the one already there.

JC
JC 2017-11-16

status = swModelDocExt.AddDisplayStateSpecificRenderMaterial(swRenderMaterial, swAllDisplayState, displayStateNames, materialID1, materialID2)

this will add it to specific display states.  the example shows you extra stuff. you do not need to add configurations nor display states.

NH
NH 2017-11-16

Reading that, it looks like it will apply to all display states, not just the active one.

Also it keeps opening the bracket part even after removing the line, nice.

JB
JB 2017-11-16

Just  a little concern if you're not aware of this

Using macro to change the appearance sometimes doesn't work the way you want unless you manually remove the current appearance before you run the macro; otherwise, your model might look different as you expect

NH
NH 2017-11-16

I was not aware, thank you. I can work out removing the appearance, after I get the appearance to change at all. So far I'm removing a bunch of lines from the example and the only thing I can get it to do is open the example bracket. Other wise its a bunch of debug errors.

JB
JB 2017-11-16

The appearance/color is still the issue you have to deal with SW when the part is inserted into different level of assemblies if you accidentally change the part color at the assembly then color will drive you nut

NH
NH 2017-11-16

SW in general can drive you nuts. I'm trying change the appearance of the open part file to the updated material appearance, or a specified .p2m file.

If anyone could modify the example to run on the open part file, It would be a great help.