10 Replies Latest reply on Aug 26, 2018 1:41 PM by Paul Salvador

    Macro - Select surfaces by a colour

    Jozef Jurcisin

      Hello,

      Is it possible to set a macro for selection a faces/surfaces by the colour? e.g. I select one of surfaces in blue and macro select remaining blue surfaces.

      Thanks

      Jozef

        • Re: Macro - Select surfaces by a colour
          Keith Rice

          I agree with Amen. It is possible.

            • Re: Macro - Select surfaces by a colour
              Keith Rice
              'Precondition: A single face is selected in a part.
              'Result: All faces in the part that share that color (or lack of color) are selected.
              
              'Written by Keith Rice
              'CADSharp LLC
              'www.cadsharp.com
              
              Sub main()
                  Dim swApp As SldWorks.SldWorks
                  Dim swModel As SldWorks.ModelDoc2
                  Dim swPart As SldWorks.PartDoc
                  Dim swSelMgr As SldWorks.SelectionMgr
                  Dim swBody As SldWorks.Body2
                  Dim swFace As SldWorks.Face2
                  Dim swEnt As SldWorks.Entity
                  Dim vSelMatProps As Variant
                  Dim vBodies As Variant
                  Dim vFaces As Variant
                  Dim i As Integer, j As Integer
                 
                  Set swApp = Application.SldWorks
                  Set swModel = swApp.ActiveDoc
                 
                  If swModel Is Nothing Then Exit Sub
                 
                  Set swSelMgr = swModel.SelectionManager
                 
                  If swSelMgr.GetSelectedObjectCount2(-1) <> 1 Or swSelMgr.GetSelectedObjectType3(1, -1) <> swSelFACES Then
                      MsgBox "Please select a single face."
                      Exit Sub
                  End If
                 
                  Set swFace = swSelMgr.GetSelectedObject6(1, -1)
                  vSelMatProps = swFace.MaterialPropertyValues
                 
                  Set swPart = swModel
                 
                  vBodies = swPart.GetBodies2(swSolidBody, False)
                  For i = 0 To UBound(vBodies)
                      Set swBody = vBodies(i)
                      vFaces = swBody.GetFaces
                      For j = 0 To UBound(vFaces)
                          Set swFace = vFaces(j)
                          If CompareArrays(swFace.MaterialPropertyValues, vSelMatProps) Then
                              Set swEnt = swFace
                              swEnt.Select True
                          End If
                      Next j
                  Next i
              End Sub
              
              Private Function CompareArrays(arr1 As Variant, arr2 As Variant) As Boolean
                 
                  If IsEmpty(arr1) And IsEmpty(arr2) Then
                      CompareArrays = True
                      Exit Function
                  ElseIf IsEmpty(arr1) = False And IsEmpty(arr2) Then
                      CompareArrays = False
                      Exit Function
                  ElseIf IsEmpty(arr1) And IsEmpty(arr2) = False Then
                      CompareArrays = False
                      Exit Function
                  End If
                 
                  If UBound(arr1) <> UBound(arr2) Then
                      CompareArrays = False
                      Exit Function
                  Else
                      Dim i As Integer
                      For i = 0 To UBound(arr1)
                          If arr1(i) <> arr2(i) Then
                              CompareArrays = False
                              Exit Function
                          End If
                      Next i
                  End If
                 
                  CompareArrays = True
                 
              End Function
              

               

              Keith

              SolidWorks API Training

            • Re: Macro - Select surfaces by a colour
              Alin Vargatu

              If you have SW Professional or Premium, check the Power Selection options. You can use many criteria for selection at the part level, one of them being the color.

              • Re: Macro - Select surfaces by a colour
                Peter Brinkhuis

                I like the way you guys completely answer Jozef's question. I just sense there will be a follow-up question.