4 Replies Latest reply on Jun 13, 2016 4:59 AM by Vincent Li

    Change color of selected faces

    Slavomir Kubasky

      Hi,

       

      I'm trying to modify solidworks example macro "Change color of selected face" for situation when I have selected more than one face of part. It sounds quite

      simple, but my knowledge of API poor. Can anybody help me with this?

      Thank you

       

       

      Sub main()

          Dim swApp                       As Object

          Dim swModel                     As Object

          Dim swSelMgr                    As Object
         
          Dim swSelFace()                 As Object
         
          Dim nSelCount                   As Long

          Dim vFaceProp                   As Variant

          Dim bRet                        As Boolean
         
          Dim i                           As Long
         
          Dim vFaceList                   As Variant

       

         

          Set swApp = Application.SldWorks

          Set swModel = swApp.ActiveDoc

          Set swSelMgr = swModel.SelectionManager

          nSelCount = swSelMgr.GetSelectedObjectCount

          ReDim swSelFace(nSelCount - 1)

          For i = 1 To nSelCount

              Set swSelFace(i - 1) = swSelMgr.GetSelectedObject5(i)

          Next
         

           vFaceProp = swSelFace(i).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(0, 0, 255), _
                  vFaceProp(3), vFaceProp(4), vFaceProp(5), _
                  vFaceProp(6), vFaceProp(7), vFaceProp(8), _
                  False, ""): Debug.Assert bRet
        

          ' Deselect face to see new color

          swModel.ClearSelection2 True

      End Sub

        • Re: Change color of selected faces
          Martin Valdhans

          Try this

           

          Sub main()

              Dim swApp                       As SldWorks.SldWorks
              Dim swModel                     As SldWorks.ModelDoc2
              Dim swSelMgr                    As SldWorks.SelectionMgr
              Dim swFace                      As SldWorks.Face2
              Dim bRet                        As Boolean
              Dim PocPloch                    As Integer
              Dim i                           As Long
             

              Set swApp = Application.SldWorks
              Set swModel = swApp.ActiveDoc
              Set swSelMgr = swModel.SelectionManager
             
              PocPloch = swSelMgr.GetSelectedObjectCount 'nuber of selected faces
             
              For i = PocPloch To 1 Step -1
                  Set swFace = swSelMgr.GetSelectedObject(i) 'get the last selected face
                  swModel.SelectedFaceProperties 65535, 0.5, 0.5, 1, 0.315, 0, 0, 0, "" 'setts the colour of last selected face
                  bRet = swFace.DeSelect 'deselect last face
              Next i
                 
          End Sub

          • Re: Change color of selected faces
            Keith Rice

            Slavomir,

             

            Here's an approach that allows you to keep the faces selected after the color has been changed:

             

            =======

             

            Dim swApp As SldWorks.SldWorks
            Dim swModel As SldWorks.ModelDoc2
            Dim swSelMgr As SldWorks.SelectionMgr
            Dim swFace As SldWorks.Face2
            Dim i As Integer
            Dim vProps As Variant
                
            Sub main()
                 Set swApp = Application.SldWorks
                 Set swModel = swApp.ActiveDoc
                 Set swSelMgr = swModel.SelectionManager
                 For i = 1 To swSelMgr.GetSelectedObjectCount2(-1)
                     Set swFace = swSelMgr.GetSelectedObject6(i, -1)
                     vProps = swFace.GetMaterialPropertyValues2(1, Empty)
                     vProps(0) = 1
                     vProps(1) = 0
                     vProps(2) = 0
                     vProps(3) = 1
                     vProps(4) = 1
                     vProps(5) = 0.8
                     vProps(6) = 0.3125
                     vProps(7) = 0
                     vProps(8) = 0
                     swFace.SetMaterialPropertyValues2 vProps, 1, Empty
                 Next i
            End Sub

             

            =======

             

            Keith

            Video Tutorials for the SolidWorks API

              • Re: Change color of selected faces
                Vincent Li

                Hi Keith,

                 

                thanks for providing a very good sample for changing the face color.

                 

                However, if I selected 20 faces, then the changing face color would loop 20 times, it just set the color for face one by one, seems the performance is not very good.

                 

                is there any method to enhance the code to set the face color at 1 time , not matther how many faces are selected.

                 

                regards

                Vincent

              • Re: Change color of selected faces
                Slavomir Kubasky

                Thank's a lot guys, this is exactly what I need.