AnsweredAssumed Answered

Change colour of assembly

Question asked by Andrzej Kurlapski on Apr 9, 2018
Latest reply on Apr 10, 2018 by 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

Outcomes