4 Replies Latest reply on Mar 10, 2017 9:31 AM by Jesse Pautsch

    Possible to change material based on Custom Property?

    Jesse Pautsch

      I am trying to make a temporary work around to update my Part's material based on a custom property. I need to do this since I am creating models through DriveWorks but I cannot get the functionality for DW and SW correct when selecting from custom materials. I can still drive the Custom Property "DWMaterial" from DW and I can also setup DW to run a macro before finishing the part. The only problem being I do not understand macros enough to write my own... any help?

       

      Basically => Part Material = Custom Property DWMaterial

       

      CP DWMATERIAL.png

        • Re: Possible to change material based on Custom Property?
          Amen Allah Jlili

          This will do it for you partner.

           

           

          'Macro by Amen JLLI

          'https://www.cadhero.com

          Option Explicit

          Sub main()

              Dim swApp                       As SldWorks.SldWorks

              Dim swModel                     As SldWorks.ModelDoc2

              Dim swPart                      As SldWorks.PartDoc

              Set swApp = Application.SldWorks

              Set swModel = swApp.ActiveDoc

              Set swPart = swModel

              Dim material_val As String

              material_val = swModel.Extension.CustomPropertyManager("").Get("DW-Material")

              If material_val <> "" Then

                 swPart.SetMaterialPropertyName2 "", "", material_val 

               End If

          End Sub

            • Re: Possible to change material based on Custom Property?
              Jesse Pautsch

              Awesome - This is working although it doesn't pick up the correct appearance when switching materials. It will either slightly change the hue of the original color or change to the default grey color (no material). Any insight?

               

               

              Edit: It seems to select the correct appearance type, "color" in this case but it doesn't apply the custom color attached to the appearance. See the below grey part when it should come out pink.

               

              MATERIAL LIST.png

                • Re: Possible to change material based on Custom Property?
                  Amen Allah Jlili

                  Hmmm interesting!

                   

                   

                   

                  'Macro by Amen JLILI - modified from sw api help example

                  'https://www.cadhero.com

                  Option Explicit

                   

                  Sub main()
                      Dim swApp                      As SldWorks.SldWorks
                      Dim swModel                    As SldWorks.ModelDoc2
                      Dim myPart                      As SldWorks.PartDoc
                      Dim myMatVisProps As SldWorks.MaterialVisualPropertiesData
                      Dim configName As String, databaseName As String
                      Dim newPropName As String
                      Dim orgBlend As Boolean, orgApply As Boolean
                      Dim orgAngle As Double
                      Dim orgScale As Double
                      Dim longstatus As Long
                  
                  
                      Set swApp = Application.SldWorks
                      Set swModel = swApp.ActiveDoc
                      Set myPart = swModel
                      Set myMatVisProps = myPart.GetMaterialVisualProperties()
                  
                  
                      Dim material_val As String
                      material_val = swModel.Extension.CustomPropertyManager("").Get("DW-Material")
                      If material_val <> "" Then
                        myPart.SetMaterialPropertyName2 "", "", material_val
                       
                        ' Set the material visual properties to be just color, no advanced graphics
                  
                  
                      Set myMatVisProps = myPart.GetMaterialVisualProperties()
                  
                  
                      If Not myMatVisProps Is Nothing Then
                          longstatus = myPart.SetMaterialVisualProperties(myMatVisProps, swThisConfiguration, Nothing)
                          Call dump_material_visual_properties(myMatVisProps, myPart)
                         
                  
                  
                      ' Set the material visual properties to be RealView
                          myMatVisProps.RealView = True
                          longstatus = myPart.SetMaterialVisualProperties(myMatVisProps, swThisConfiguration, Nothing)
                  
                  
                  
                  
                       
                      Call dump_material_visual_properties(myMatVisProps, myPart)
                          myMatVisProps.ApplyMaterialHatchToSection = orgApply
                          longstatus = myPart.SetMaterialVisualProperties(myMatVisProps, swThisConfiguration, Nothing)
                          Call dump_material_visual_properties(myMatVisProps, myPart)
                         
                  
                  
                          ' Toggle the apply appearance flag
                          If myMatVisProps.ApplyAppearance = 0 Then
                              orgApply = False
                          Else
                              orgApply = True
                          End If
                  
                  
                          myMatVisProps.ApplyAppearance = Not orgApply
                          longstatus = myPart.SetMaterialVisualProperties(myMatVisProps, swThisConfiguration, Nothing)
                          Call dump_material_visual_properties(myMatVisProps, myPart)
                          myMatVisProps.ApplyAppearance = orgApply
                          longstatus = myPart.SetMaterialVisualProperties(myMatVisProps, swThisConfiguration, Nothing)
                          Call dump_material_visual_properties(myMatVisProps, myPart)
                      End If
                  
                  
                  
                  
                      End If
                     
                  End Sub
                  
                  
                  Private Sub dump_material_visual_properties(myMatVisProps As SldWorks.MaterialVisualPropertiesData, myPart As SldWorks.PartDoc)
                  
                  
                     
                      Dim configName As String, databaseName As String
                      Dim propName As String
                      Dim bRealView As Boolean
                      Dim dScale As Double, dAngle As Double
                      Dim bBlendColor As Boolean, bApplyColor As Boolean, bApplyHatch As Boolean, bApplyAppearance As Boolean
                      configName = "default"
                      propName = myPart.GetMaterialPropertyName2(configName, databaseName)
                      Debug.Print ""
                      Debug.Print "Config: """ & configName & """, Database: """ & databaseName & """, Material: """ & propName & """"
                  
                  
                      If Not myMatVisProps Is Nothing Then
                          bRealView = myMatVisProps.RealView
                          dScale = myMatVisProps.Scale2
                          dAngle = myMatVisProps.Angle
                          bBlendColor = myMatVisProps.BlendColor
                          bApplyColor = myMatVisProps.ApplyMaterialColorToPart
                          bApplyHatch = myMatVisProps.ApplyMaterialHatchToSection
                          bApplyAppearance = myMatVisProps.ApplyAppearance
                  
                  
                         
                          If bRealView = 0 Then
                              Debug.Print "Advanced graphics - SOLIDWORKS standard textures."
                          Else
                              Debug.Print "Advanced graphics - RealView textures."
                          End If
                          Debug.Print "  SOLIDWORKS standard texture scale = " & dScale & ", Angle = " & dAngle
                          If bBlendColor = 0 Then
                              Debug.Print "  Do not blend part color with SOLIDWORKS standard texture."
                          Else
                              Debug.Print "  Blend part color with SOLIDWORKS standard texture."
                          End If
                  
                  
                  
                          If bApplyColor = 0 Then
                              Debug.Print "Do not apply material color to part."
                          Else
                              Debug.Print "Apply material color to part."
                          End If
                  
                  
                          If bApplyHatch = 0 Then
                              Debug.Print "Do not apply material hatch to drawing section."
                          Else
                              Debug.Print "Apply material hatch to drawing section."
                  
                  
                          End If
                          If bApplyAppearance = 0 Then
                              Debug.Print "Do not apply appearance."
                          Else
                              Debug.Print "Apply appearance."
                          End If
                  
                  
                      End If
                  
                  
                  End Sub
                  

                   

                   

                   

                   

                  I think this will fix it partner .