Attribute VB_Name = "MateStateToggle1"
' --------------------------------------------------------------------04/03/2007
' MateStateToggle.swp               Written by Leonard Kikstra,
'                                   Copyright 2007, Leonard Kikstra
'                                   Downloaded from Lenny's SolidWorks Resources
'                                        at http://www.lennyworks.com/solidworks
' ------------------------------------------------------------------------------
' MateStateToggle1:     Macro is launched from "main" subroutine in this module.
' ------------------------------------------------------------------------------
' Notes:                Mates must be selected before launching macro.
' ------------------------------------------------------------------------------
' Revision History
'   04/03/2007  v1.00   Initial release for blog
'   10/31/2007  v1.10   Updated "generic" selection placeholders
' ------------------------------------------------------------------------------
Option Explicit

Function myMsgBox(msgText As String, Optional msgControl As Integer)
  myMsgBox = MsgBox(msgText, msgControl + vbExclamation, "MateStateToggle")
End Function

Sub main()
  
  Dim swApp             As Object                       ' SolidWorks
  Dim swModel           As Object                       ' Document
  Dim FileTyp           As Integer                      ' Document type
  Dim swSelMgr          As SldWorks.SelectionMgr        ' Selection manager
  Dim boolstat          As Boolean                      ' Status
  Dim longstat          As Long                         ' Status
  Dim longwarn          As Long                         ' Status
  Dim selCount          As Integer                      ' Counter     - Select set
  Dim mySelect          As Object                       ' Placeholder - Select set
  Dim myCount           As Integer                      ' Counter     - For-Next
  Dim mySelectPro()     As String                       ' Placeholder - Promote
  Dim mySelectDem()     As String                       ' Placeholder - Demote
  Dim myCountPro        As Integer                      ' Counter     - Promote
  Dim myCountDem        As Integer                      ' Counter     - Demote
  Dim SelType           As String                       ' Check Type  - Select type

' longstat = MyMsgBox("Warnings:" _
   & Chr(13) & Chr(13) & "Using this macro may cause errors in assembly mates." _
   & Chr(13) & Chr(13) & "This macro is provided only as a working example." _
   & Chr(13) & Chr(13) & "Proceed with changes?", vbYesNo)
' If longstat = vbYes Then
  
    ' Attach to SolidWorks
    Set swApp = Application.SldWorks
    ' Get active document
    Set swModel = swApp.ActiveDoc
    
    If Not swModel Is Nothing Then
      FileTyp = swModel.GetType
      
      If FileTyp = swDocASSEMBLY Then
        Set swSelMgr = swModel.SelectionManager           ' Get selection mgr
        selCount = swSelMgr.GetSelectedObjectCount2(-1)   ' Count selections
        
        If selCount > 0 Then                              ' Anything selected?
          ReDim mySelectPro(selCount)                     ' Resize holder
          ReDim mySelectDem(selCount)                     ' Resize holder
          myCountPro = 0                                  ' Zero counter
          myCountDem = 0                                  ' Zero counter

          ' First time thru, collect selections and status.  This must be done
          ' because selections will be cleared when used, and we want to save
          ' these selections.
          ' We will recall selections by their names in the second time thru.
          For myCount = 1 To selCount                     ' For each selected item
            
            ' What type of object was selected
            If swSelMgr.GetSelectedObjectType3(myCount, -1) = swSelMATES Then
              Set mySelect = swSelMgr.GetSelectedObject6(myCount, -1)
              
              If Not mySelect Is Nothing Then             ' Got an object?
                
                If mySelect.IsSuppressed Then             ' Check current state
                  ' Items to promote go here
                  mySelectPro(myCountPro) = mySelect.Name ' Add to holder
                  myCountPro = myCountPro + 1             ' Increment count
                Else
                  ' Items to demote go here
                  mySelectDem(myCountDem) = mySelect.Name ' Add to holder
                  myCountDem = myCountDem + 1             ' Increment count
                End If
              
              End If          ' Got selection
            
            End If            ' Is it a mate
          
          Next myCount
    
          swModel.ClearSelection2 True                    ' Clear selections
    
          ' Second time thru, we will invert the status of valid selections.
          ' We will recall selections by the names saved the first time thru.
          ' We are only woking with what we want beyond this point, and first
          ' selections have already been cleared.
        
          ' Process items to demote
          If myCountDem > 0 Then
            
            For myCount = 0 To myCountDem - 1
              
              If swModel.Extension.SelectByID2(mySelectDem(myCount), "MATE", _
                 0, 0, 0, False, 0, Nothing, 0) = True Then swModel.EditSuppress
              swModel.ClearSelection2 True                ' Clear selections
            
            Next myCount
          
          End If
                    
          ' Process items to promote
          If myCountPro > 0 Then
            
            For myCount = 0 To myCountPro - 1
              
              If swModel.Extension.SelectByID2(mySelectPro(myCount), "MATE", _
                 0, 0, 0, False, 0, Nothing, 0) = True Then swModel.EditUnsuppress
              swModel.ClearSelection2 True                ' Clear selections
            
            Next myCount
          
          End If
        
          ' Tell user what happened
          If myCountDem > 0 Or myCountPro > 0 Then
            myMsgBox myCountDem & " mates Suppressed." & Chr(13) & _
                     myCountPro & " mates Unsuppressed."
          
          Else: myMsgBox "No mates selected."
          End If
   
        Else: myMsgBox "Nothing selected."
        End If
      
      Else: myMsgBox "Document is not an assembly."
      End If
    
    Else: myMsgBox "No document loaded."
    End If
' End If          ' Warn user

End Sub
