3 Replies Latest reply on Apr 10, 2018 1:08 AM by Andrzej Kurlapski

    Change colour of assembly

    Andrzej Kurlapski

      Hello

       

      I have problem with macro to change part and assembly color. The section for changing color of part is working correct but for assembly it doesn't. When I run the macro in the assembly, the color is changes ,but I can not see it. I must close the assembly and re-open to see the changes. Is it possible to change the color in assembly without re-open document? Below is my code:

       

      Option Explicit

      Public Enum swBodyType_e

          swSolidBody = 0

          swSheetBody = 1

          swWireBody = 2

          swMinimumBody = 3

          swGeneralBody = 4

          swEmptyBody = 5

      End Enum

       

      Sub ProcessBodyArray(swApp As SldWorks.SldWorks, swModel As SldWorks.ModelDoc2, vBodyArr As Variant)

          Dim vBody As Variant

          Dim swBody As SldWorks.Body2

          Dim vMatProp(8) As Double

          Dim boolstatus As Boolean

          For Each vBody In vBodyArr

              Set swBody = vBody

              vMatProp(0) = 0

              vMatProp(1) = 1

              vMatProp(2) = 1

              vMatProp(3) = 1

              vMatProp(4) = 1

              vMatProp(5) = 0.5

              vMatProp(6) = 0.4

              vMatProp(7) = 0

              vMatProp(8) = 0

              swBody.MaterialPropertyValues2 = vMatProp

              Set swApp = Application.SldWorks

              Set swModel = swApp.ActiveDoc

              boolstatus = swModel.ForceRebuild3(True)

          Next

      End Sub

      Sub main()

          Dim swApp As SldWorks.SldWorks

          Dim swModel As SldWorks.ModelDoc2

          Dim swPart As SldWorks.PartDoc

          Dim swAss As SldWorks.AssemblyDoc

          Dim vBodyArr As Variant

          Dim i  As Long

          Dim bool As Boolean

          Dim name As String

          Dim vMat(8) As Double

          Set swApp = Application.SldWorks

          Set swModel = swApp.ActiveDoc

          name = swModel.GetPathName

          If Right(name, 6) = "sldprt" Or Right(name, 6) = "SLDPRT" Then

              Set swPart = swModel

              vBodyArr = swPart.GetBodies2(i, False)

              For i = 0 To UBound(vBodyArr)

                  If Not IsEmpty(vBodyArr) Then

                      ProcessBodyArray swApp, swModel, vBodyArr

                  End If

              Next i

          Else

              Set swAss = swModel

              vMat(0) = 0

              vMat(1) = 1

              vMat(2) = 1

              vMat(3) = 1

              vMat(4) = 1

              vMat(5) = 0.5

              vMat(6) = 0.4

              vMat(7) = 0

              vMat(8) = 0

              swModel.MaterialPropertyValues = vMat

              bool = swModel.ForceRebuild3(True)

          End If

      End Sub

        • Re: Change colour of assembly
          Michael Spens

          Try adding in a GraphicsRedraw2 call to ModelDoc2.

           

          2018 SOLIDWORKS API Help - Redraw Graphics Example (VBA)

           

          Mike

            • Re: Change colour of assembly
              Andrzej Kurlapski

              this unfortunately doesn't work

                • Re: Change colour of assembly
                  Andrzej Kurlapski

                  I solved the problem. I had to add random material to the look of the band, and then change the color. Below is changed code:

                  Sub main()

                      Dim swApp As SldWorks.SldWorks

                      Dim swModel As SldWorks.ModelDoc2

                      Dim swPart As SldWorks.PartDoc

                      Dim swAss As SldWorks.AssemblyDoc

                      Dim swModelDocExt As SldWorks.ModelDocExtension

                      Dim swAppearance As SldWorks.RenderMaterial

                      Dim vBodyArr As Variant

                      Dim i  As Long

                      Dim boolstatus As Boolean

                      Dim name As String

                      Dim vMat(8) As Double

                      Dim nDecalID As Long

                      Set swApp = Application.SldWorks

                      Set swModel = swApp.ActiveDoc

                      Set swModelDocExt = swModel.Extension

                      name = swModel.GetPathName

                      If Right(name, 6) = "sldprt" Or Right(name, 6) = "SLDPRT" Then

                          Set swPart = swModel

                          vBodyArr = swPart.GetBodies2(i, False)

                          For i = 0 To UBound(vBodyArr)

                              If Not IsEmpty(vBodyArr) Then

                                  ProcessBodyArray swApp, swModel, vBodyArr

                              End If

                          Next i

                      Else

                          Set swAss = swModel

                          vMat(0) = 0 * 255#

                          vMat(1) = 255 * 255#

                          vMat(2) = 255 * 255#

                          vMat(3) = 1

                          vMat(4) = 1

                          vMat(5) = 0.5

                          vMat(6) = 0.4

                          vMat(7) = 0

                          vMat(8) = 0

                          Set swAppearance = swModelDocExt.CreateRenderMaterial("C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS\data\graphics\Materials\metal\aluminum\brushed aluminum.p2m")

                          boolstatus = swAppearance.AddEntity(swModel)

                          boolstatus = swModelDocExt.AddRenderMaterial(swAppearance, nDecalID)

                          swModel.MaterialPropertyValues = vMat

                          swModel.GraphicsRedraw2

                          Call swModel.Rebuild(swRebuildAll)

                      End If

                  End Sub