Setting Material via Add-In (2021 SP5.1)

The code to set the material automatically via our add-in no longer works after updating to 2021 SP5.1 from 2018 SP5.0.

I’ve created a simple macro script that works in 2018, but doesn’t work in 2021. I modified and tweaked all of the argument formats that I can think of an can’t get 2021 to set the material using IPartDoc.SetMaterialPropertyName2. Does anybody here have any thoughts on either a correction to using this method or a workaround using another method?

'Have part document open and ready for macro to run.
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2
Dim swPart As PartDoc

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swPart = swModel

swPart.SetMaterialPropertyName2 Empty, "C:\Vault\Common\SOLIDWORKS\MATERIALS\MaterialDatabase.sldmat", "Alloy Steel"

End Sub
1 Like

It is unlikely that the code stopped working. That method has not even been deprecated.

What is more likely is that you have not added this material database to your SolidWorks settings. I’ve run into this many times when creating my 30k fasteners in every available SolidWorks version.

@PeterBrinkhuis I have the file locations set to point to the folder(s) that contain any material databases that I would need to use.

Now, with that said, after more testing this morning I did find that it works with just some of the materials and not all of them. I can only assume that anything with a duplicate name from another database is causing an issue with setting the material. I’m not sure why this is suddenly an issue with the same code and the same settings between SW versions.

Ok, that was worth a try.

Have you set any materials to bodies, maybe?

Correct if I’m wrong but don’t you need to specify the name of the configuration? The example provided has Empty as the configuration argument.

Another thing, make sure if your material db is located in PDM that the actual file exists. SW will fail to read the file it has no local copy.

1 Like

@PeterBrinkhuis I haven’t set materials to any bodies. This is with the same add-in, same part file, same SW settings, just different versions of SW. I know you’ve also used SolidDNA and that’s what I’ve had working for quite some time now without issue. It’s just that this update seems to have thrown a wrench into the works. I even recompiled with 2021 DLLs and that didn’t change anything.

@AmenJlili I have the common files caching on every login so there’s always a local copy. It works if I manually set the material just fine. However the same command doesn’t seem to work via the API. Having the “Empty” argument applies the material to the whole file, rather than one config. I’ve tried specifying the config name as well and have had no luck. This is a very odd issue.

I’ve had setting a material fail in SolidDna because I needed to set a strange boolean flag on a material database class first. But that shouldn’t be the issue here.

As Amen said, the first step to getting help is to share your files.

I’ve tested the codes and they work OK for me on 2021 SP5.1. So seems issue with the database or database location.

Is the material applied correctly if you do it manually using the same material database?

@AmenJlili unfortunately, sharing files isn’t possible at the moment. I’ll have to see if I can make a dummy database that also shows the issue.

@Gupta9665 yes it works fine doing this manually. I just can’t get the code to work consistently in a macro or my add-in

Check if your embed interop of the SW dlls are false. If true set to false and try. Could be marshalling issue.

Can you try using the codes with the SW default material database?

Also try to copy material db file outside of the vault, perhaps permissions or no local copy causing this issue

It’s been a busy week so I haven’t had a chance to test more of these code suggestions yet.

@AmenJlili I have all SW embed interops set to false already
@Gupta9665 and @artem I’ll try both of these suggestions and try and report back my findings.

1 Like

If you ever submit a ticket to API support, do me a favor and share the SPR if they create one for it!

So after doing a lot of digging, I found that whoever created our materials in our custom database initially had unknowingly placed a space after almost every material, which explains why some worked and some did not. Now, since I didn’t change my add-in code it still seems odd that it worked and now it doesn’t, but I found the issue and it’s resolved.


Damnnnnnnn. :joy::joy::joy::rofl::rofl::rofl::rofl::rofl: