10 Replies Latest reply on Jun 14, 2017 8:17 AM by Scott Stuart

    Part turns totally black when changing color

    Gabriel Penalba

      Hey all, I'm making a quick macro to change the color of the assembly that I have selected in the tree. I thought it would be really simple, but it's malfunctioning for some reason. First off, I'd like to change the color of the assembly I have selected, not just it's appearance in the assembly in which it's selected. Second, the color does change, but it goes totally black!! If I remove the appearance, it goes back to the default color. Any ideas?

       

      Dim swApp As SldWorks.SldWorks

      Dim Part As Object

      Dim swModel As SldWorks.ModelDoc2

      Dim swSelMgr As SldWorks.SelectionMgr

      Dim swSelComp As SldWorks.Component2

      Dim dMatProps As Variant

      Dim vNames As Variant

       

      Sub main()

       

      Set swApp = Application.SldWorks

      Set swModelDoc = swApp.ActiveDoc

      Set swSelMgr = swModelDoc.SelectionManager

       

      Set swSelComp = swSelMgr.GetSelectedObjectsComponent4(1, -1)

       

      dMatProps = swSelComp.GetMaterialPropertyValues2(1, vNames)

       

      'set the properties array to the desires color

      dMatProps(0) = 0

      dMatProps(1) = 255 / 256

      dMatProps(2) = 255 / 256

       

      'set the component properties to the changed array properties

      swSelComp.SetMaterialPropertyValues2 dMatProps, 1, vNames

       

      End Sub

        • Re: Part turns totally black when changing color
          jason van clark

          Did anyone ever find a solution to this?

            • Re: Part turns totally black when changing color
              Steven Barry

              Jason, see this page:

              2016 SOLIDWORKS API Help - SetMaterialPropertyValues2 Method (IComponent2)

               

              Per the above link:

              To see a color change, you must:

              1. Specify R, G, and B, each with a value between 0.0 and 1.0, inclusive. (These values are internally multiplied by 255.0 to determine the RGB color.)
              2. Specify the reflectivity properties (Diffuse, Specular, Shininess (1.0 - Specular spread/Blurriness)), each with a value greater than 0.0 and less than or equal to 1.0.
              3. Specify Ambient, Transparency and Emission, each with a value between 0.0 and 1.0, inclusive.
              4. Refresh the graphics area after calling this method.

               

              The code posted in the original post above only sets 3 of the 9 values.  So you must determine appropriate values for each other parameter.  Otherwise, they could default to -1 (as documented in GetMaterialPropertyValues2) if they were never set (which is not in the range 0 to 1). 

               

              The easiest thing, if you're not sure, would be just to set the other values to 0.5 and see if that suits your needs

                • Re: Part turns totally black when changing color
                  jason van clark

                  Steven

                  I have been unsuccessful in implementing this method.  The color just does not change.  I had limited success with ModelDoc2.MaterialPropertyValues but that is only working on part files.  Here's my bit of code (in vb.net), any thoughts?

                   

                                 Dim swModelDocext As ModelDocExtension = SwModel.Extension

                                  Dim swSelMgr As SelectionMgr = SwModel.SelectionManager

                                  Dim swConfigMgr As ConfigurationManager = SwModel.ConfigurationManager

                                  Dim swConfig As Configuration = swConfigMgr.ActiveConfiguration

                                  Dim currComponent As Component2

                                  Dim matValues As Object

                                  Dim matValueString As String

                   

                                 currComponent = swConfig.GetRootComponent3(True)

                                 matValues = SwModel.MaterialPropertyValues

                   

                                   matValueString = "1, 0.752941176470588, 0.752941176470588, 1, 1, 0.5, 0.3125, 0, 0"

                                  Dim tempString() As String = matValueString.Split(","c)

                   

                                  matValues(0) = Convert.ToDouble(tempString(0))

                                  matValues(1) = Convert.ToDouble(tempString(1))

                                  matValues(2) = Convert.ToDouble(tempString(2))

                                  matValues(3) = Convert.ToDouble(tempString(3))

                                  matValues(4) = Convert.ToDouble(tempString(4))

                                  matValues(5) = Convert.ToDouble(tempString(5))

                                  matValues(6) = Convert.ToDouble(tempString(6))

                                  matValues(7) = Convert.ToDouble(tempString(7))

                                  matValues(8) = Convert.ToDouble(tempString(8))

                   

                  '               SwModel.MaterialPropertyValues = matValues

                                  currComponent.SetMaterialPropertyValues2(matValues, 2, Nothing)

                                  SwModel.GraphicsRedraw2()

                                  SwModel.ForceRebuild3(True)

              • Re: Part turns totally black when changing color
                Christian Chu

                I think 256 is invalid number?

                ex. if you want to change to RGB  (128,134,200)

                then your code should be

                dMatProps(0) = 128 / 255

                dMatProps(1) = 134 / 255

                dMatProps(2) = 200 / 255

                • Re: Part turns totally black when changing color
                  Scott Stuart

                  I've had similar experience changing colors and getting an all black appearance, and I believe it's a bug in the API. At the end of my macro I do a save and reload, and that gets the colors to display correctly. That first save actually leaves the model preview with an all black appearance, so I do another save after the reload to get the preview to save with proper colors. (I'm on SW2016 sp4.)

                  • Re: Part turns totally black when changing color
                    Ivana Kolin
                        dMatProps(0) = 0
                        dMatProps(1) = 255 / 256
                        dMatProps(2) = 255 / 256
                        dMatProps(3) = Abs(dMatProps(3))
                        dMatProps(4) = Abs(dMatProps(4))
                        dMatProps(5) = Abs(dMatProps(5))
                        dMatProps(6) = Abs(dMatProps(6))
                        dMatProps(7) = Abs(dMatProps(7))
                        dMatProps(8) = Abs(dMatProps(8))