18 Replies Latest reply on Jun 21, 2018 11:04 AM by Shawn Wolters

    Can you change to a custom material using a macro?

    Jared Huffman

      Is it possible to use a macro to change a large group of files to a custom material?


      I'm using the line:

      Part.SetMaterialPropertyName2 "Default", "Q:/SOLIDWORKS/MATERIAL/MATERIALS.sldmat", "CRS"


      ... but nothing happens.  It works with any material from the standard database but not the custom material database. I have verified that the database is in the above location, and the material CRS does exist.


      Any help would be much appreciated!

        • Re: Can you change to a custom material using a macro?
          Daniel Andersson

          Tested on SolidWorks 2012 SP5. Works perfect.

          Used following code, please note that I have not considered exceptions, such as if swModel is nothing... or if its a assembly etc etc....

          Dim swApp As SldWorks.SldWorks
          Dim swModel As SldWorks.ModelDoc2
          Dim swPart As SldWorks.PartDoc

          Sub main()

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

          swPart.SetMaterialPropertyName2 "Default", "C:\SolidWorks Data\Machines_Config\Material\materials_XXXX.sldmat", "EN-GJS-500-7"

          End Sub


          uhm... just noted that we used different slashes. Try using backslash instead.

          • Re: Can you change to a custom material using a macro?
            Jared Huffman

            Thank you both so much for looking into this issue with me.  I really appreciate the help!


            When I changed the configuration in the code from "Default" to "", the macro successfully changed the material. 

            Then I changed the file path to what I originally wanted on a mapped network drive (changed earlier when troubleshooting), and it still worked!

            However, I tried to integrate the command into my actual macro to use for this project, and it doesn't work.

            So I stripped everything else out of the new macro, and it still won't work...

            I also tried replacing the variable Material with the string "CRS", and it still does not assign the material.


            Any ideas?



            Here's what's left of the code for the new macro:


            Dim swApp As SldWorks.SldWorks
            Dim Cfg As String
            Dim CmpDoc As ModelDoc2
            Const swDocPART = 1
              '' The variables for setting the custom properties
            Dim StockType As String

            Set swApp = Application.SldWorks
            Set CmpDoc = swApp.ActiveDoc
            Cfg = "Default"

            ''Read 2014 Custom Properties
            Material = CmpDoc.CustomInfo2(Cfg, "StockType")

            ''Add 2015 custom properties and assign them
            ''If (CmpDoc.GetType = swDocPART) Then
                CmpDoc.SetMaterialPropertyName2 "", "Q:\SOLIDWORKS\MATERIAL\MATERIALS.SLDMAT", Material
            ''End If

            End Sub

              • Re: Can you change to a custom material using a macro?
                Has Bal

                Hi Jared,

                I could not understand and find that "CmpDoc.CustomInfo2(string,string)"

                Here whole customInfo codes 2013 SolidWorks API Help - Obsolete and New Custom Properties Methods and Properties

                visit Here 2013 SolidWorks API Help - List Custom Properties Example (VBA) 

                and here 2012 SolidWorks API Help - Get and Set Material Visual Properties Example (VBA)

                I guess it could help you.


                Best Regards

                • Re: Can you change to a custom material using a macro?
                  Daniel Andersson

                  After just reading your code I think that you should consider that you are getting data from a specific configuration but just setting it to configuration ""....

                  You should get and set data to the same configuration in this case. I do not know your company way of working with SolidWorks. But I would recommend that you consider if  different materials has been specified for different configurations. You do that by getting the custom properties for each configuration and then assign the material to each configuration.


                  There is also a risk that the custom property that you are getting is not a valid name of a material. So I would also like to add to my recommendation that you check if the material exist or not in the material database. Did some quick google but did not found anything simple, perhaps I missed it. The material database is a XML file so with some efforts it would be possible to open it and search for the material name etc.


                  The easier way to check if the material was changed or not, is to simply get the material of the part and configuration before and compare it after it has been set. Depending on if a change was needed or not, you could easily evaluate the result and send a message to the user if neccessary.

                    • Re: Can you change to a custom material using a macro?
                      Daniel Andersson

                      I made this to show what I mean, try it on some part you have copies of... I do not check if the material exist or not in the database. I just did the simplier check to see if the material in the configuration is the same as stated in the custom property at the end.


                      It might be gaps in the behavoir of the code. e.g. it is most likely that the material in the configuration will be replaced from the new database. Even if the there would be same names in the material and the custom property from the start (read, no change needed or...?). Reason for this is that the code just push the change and then check it if the material and the custom property matches. There is also no check if a change of database was successful or not (same material name, new vs. old name. But different  database)


                      It could be changed to first check if a change is needed or not, and after that do a check if the change was successful. This is down to details how you want the macro to behave. You can add further checks as you like.


                      Give it a try and let me know how it went.


                      Option Explicit
                      Dim swApp As SldWorks.SldWorks
                      Dim swModel As SldWorks.ModelDoc2
                      Dim swModExt As SldWorks.ModelDocExtension
                      Dim swPart As SldWorks.PartDoc
                      Dim swConfMgr As SldWorks.ConfigurationManager
                      Dim swCustPropMgr As SldWorks.CustomPropertyManager

                      Dim vArrConf As Variant
                      Dim vArrNames As Variant
                      Dim vArrValues As Variant


                      Dim sCustProp As String
                      Dim sCurrMat As String
                      Dim sCurrMatDB As String
                      Dim sNewMat As String
                      Dim sNewMatDB As String
                      Dim bRetVal As Boolean
                      Dim sValOut As String


                      Sub main()
                      Dim i As Integer
                      Dim k As Integer

                      sCustProp = "StockType" 'Name of the custom property that shall be used to change the material.
                      sNewMatDB = "C:\SolidWorks Data\Material\materials_custom.sldmat" 'Material database to use


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


                      If swModel Is Nothing Then Exit Sub
                      If Not swModel.GetType = swDocumentTypes_e.swDocPART Then Exit Sub


                      Set swPart = swModel
                      Set swModExt = swModel.Extension

                      vArrConf = swModel.GetConfigurationNames 'Get all configurations in the part.


                      For i = 0 To UBound(vArrConf)

                      sCurrMat = swPart.GetMaterialPropertyName2(vArrConf(i), sCurrMatDB) 'Get current assigned material

                      Set swCustPropMgr = swModExt.CustomPropertyManager(vArrConf(i)) 'Set CustomPropMgr to configuration
                      swCustPropMgr.GetAll vArrNames, Nothing, vArrValues 'Get all Custom properties for the configuration
                      If swCustPropMgr.Count = 0 Then 'If no custom propes is there, send msg and then move to next configuration.

                      swApp.SendMsgToUser "There is no custom properties in the configuration to get data from." & vbNewLine & vbNewLine & "Configuration: " & vArrConf(i)
                      GoTo Next_Conf:

                      End If
                      bRetVal = False
                      For k = 0 To UBound(vArrNames) 'Loop thru the array of Custom Properties in the configuration

                      If vArrNames(k) = sCustProp Then 'Is there any Custom Property with correct name....

                      swPart.SetMaterialPropertyName2 vArrConf(i), sNewMatDB, vArrValues(k) 'Set the Material
                      sCurrMat = swPart.GetMaterialPropertyName2(vArrConf(i), sCurrMatDB) 'Get the Material
                      If sCurrMat = vArrValues(k) Then 'Simple evaluation. Just checks if the name in the Custom property and the Material name is same.

                      bRetVal = True
                      Exit For

                      End If

                      End If

                      If bRetVal = False Then 'If above failed, send msg to user, and move to next configuration.
                      swApp.SendMsgToUser "Failed to get or set data from custom property """ & sCustProp & """." & vbNewLine & vbNewLine & "Configuration: " & vArrConf(i)
                      End If


                      End Sub

                  • Re: Can you change to a custom material using a macro?
                    Jared Huffman

                    Also, I have determined that the macro that successfully assigns a material and uses the Q: filepath actually is assigning one from the localmaterials database.  If I remove the C: from my File Locations for Materials, it does not assign a material. 

                    • Re: Can you change to a custom material using a macro?
                      Jared Huffman

                      Thanks for all the help! Sorry for my silence; priorities shifted away from this for a bit.


                      I don't think SetMaterialPropertyName2 works with a mapped network drive as the location of the material database. It works consistently from a local drive but never from a mapped network drive. When I thought it was working with a mapped network drive, I later discovered it was somehow referencing the local database still. If I remove the custom local database from the File Locations list in Settings, no material will be assigned.


                      Even if I record a macro of changing the material, it works for custom materials on a local drive but not to those on a mapped network drive.


                      Has anyone else had success with this command to a material database on a mapped network drive?

                      • Re: Can you change to a custom material using a macro?
                        Ethan Kinney

                        I know this is an old post, but I was having a similar issue and was able to find a resolution.


                        For me the issue was that I wanted to change the material from one type to another.

                        When I ran the macro, it did indeed change the material as shown in the feature tree, however, the appearance did not update at all.


                        In order to update the material properly I first run the macro to remove the material then run it again to add the new material. Then everything updates appropriately.


                        It might not apply in the situation you listed, but it did work for me. Hope this can help someone else out.



                        • Re: Can you change to a custom material using a macro?
                          Chris Saller

                          First time seeing this thread. I'm also curious about the macro.

                          I would like to test it on my database.

                          Updated Ctopher's Material Database