3 Replies Latest reply on Mar 6, 2018 5:00 AM by Voverrr Mironoff

    change color of face

    Sam Shiffler

      How do I use the api to change color of faces?

      Sam

        • Re: change color of face
          Jacob Corder

          2017 SOLIDWORKS API Help - MaterialPropertyValues Property (IFace2)

           

          colors are RGB and are from 0 to 1

          to get rgb to a decimal its

          Red / 255 

          Green /255

          Blue / 255

          so RGB (255,255,255) is

          1,1,1

          • Re: change color of face
            Voverrr Mironoff

            Hello.

            Try this macro

             

             

             

            Dim swApp As Object

            Dim Part As Object

            Dim boolstatus As Boolean

            Dim longstatus As Long, longwarnings As Long

            Sub main()

                Dim swApp                       As SldWorks.SldWorks

                Dim swModel                     As SldWorks.ModelDoc2

                Dim swSelMgr                    As SldWorks.SelectionMgr

                Dim swFace                      As SldWorks.Face2

                Dim vFaceProp                   As Variant

                Dim bRet                        As Boolean

                Set swApp = Application.SldWorks

                Set swModel = swApp.ActiveDoc

                Set swSelMgr = swModel.SelectionManager

                Set swFace = swSelMgr.GetSelectedObject5(1)

                vFaceProp = swFace.MaterialPropertyValues

                If IsEmpty(vFaceProp) Then

                    ' Is empty if face-level colors were not specified, so get them from underlying model

                    vFaceProp = swModel.MaterialPropertyValues: Debug.Assert Not IsEmpty(vFaceProp)

                End If

                'Current color

                Debug.Print "RGB            = [" & vFaceProp(0) * 255# & ", " & vFaceProp(1) * 255# & ", " & vFaceProp(2) * 255# & "]"

                Debug.Print "Ambient        = " & vFaceProp(3)

                Debug.Print "Diffuse        = " & vFaceProp(4)

                Debug.Print "Specular       = " & vFaceProp(5)

                Debug.Print "Shininess      = " & vFaceProp(6)

                Debug.Print "Transparency   = " & vFaceProp(7)

                Debug.Print "Emission       = " & vFaceProp(8)

                ' New color

                bRet = swModel.SelectedFaceProperties(RGB(255, 255, 0), vFaceProp(3), vFaceProp(4), vFaceProp(5), vFaceProp(6), vFaceProp(7), vFaceProp(8), False, ""): Debug.Assert bRet

                  'point on the center of face

                Set Part = swApp.ActiveDoc

            Dim vRefPointFeatures As Variant

            vRefPointFeatures = Part.FeatureManager.InsertReferencePoint(4, 0, 0.01, 1)

            boolstatus = Part.Extension.SelectByID2("point", "DATUMPOINT", 0, 0, 0, False, 0, Nothing, 0)

            Part.BlankRefGeom

            ' Deselect face to see new color

                swModel.ClearSelection2 True

            End Sub