MACRO LIBRARY

Select all faces with same color as pre-selected face

Free public access. No account or subscription required.

Files

Select all faces with same color as pre-selected face

Code

Module:

'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