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
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.
@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.
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.
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.