VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "SWConnectFaceScrub"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
'Tell VB that you are going to provide functionality for the SwAddin interface
Implements SWPublished.SwAddin

'Declarations for addin SW connection
Dim axSldWorks As SldWorks.SldWorks
Dim axCookie As Long 'holds value created in SwAddin_ConnectToSW
'cookie needed for menus, toolbars, CallbackInfo
Dim axToolbarID As Long 'toolbar ID if toolbars used
'==================================
'other class-wide delcarations here
'==================================
Dim axActiveDoc As SldWorks.ModelDoc2
Dim axTargetDoc As SldWorks.ModelDoc2

Private Function SwAddin_ConnectToSW(ByVal ThisSW As Object, ByVal Cookie As Long) As Boolean
Dim bRet As Boolean 'boolean return
Dim lRet As Long 'long return
Dim axMenuID As String
Dim lngToolbarDocTypes As Long

'=======================================================
'1. capture SW session and cookie to class-wide variable
'=======================================================
' store reference to SW session
Set axSldWorks = ThisSW
' store cookie from SW
axCookie = Cookie
'=======================================================

'==========================
'2. set Addin Callback info
'==========================
'Inform SW about the object that contains the callbacks
bRet = axSldWorks.SetAddinCallbackInfo(App.hInstance, Me, axCookie)
'App.hInstance = program's handle
'Me = this class module
'==========================

'============
'3. add menus
'============
    'a. get SW menu string if adding to existing SW-owned menu
    axMenuID = axSldWorks.GetLocalizedMenuName(4) '4="Tools"
    'If you wish to add menu items to existing SW menus such as "File",
    '"Tools", "Insert", etc., you need to get the menu name with this call.
    'to create a new menu, use "SldWorks.AddMenu"
    'For instance, to create a new "Esox Republic" menu...
    'axMenuID = "Esox Republic"
    'lRet = axSldWorks.AddMenu(swDocPART, axMenuID, 5)
    
    'b. set up menu strings
        'we have two functions, we need two menu picks
    Dim axMenu1 As String, axMenu2 As String
    axMenu1 = "Remove all face colors@Esox Republic Tools@" & axMenuID
    axMenu2 = "Color faces with random colors@Esox Republic Tools@" & axMenuID

    'c. add menus using axSldWorks.AddMenuItem2
    'this call needs to be made once for each document type:
    'NONE, Part, Assembly, and Drawing
    'PARTS:
    bRet = axSldWorks.AddMenuItem2(swDocPART, axCookie, axMenu1, 0, "CallRemoveColors", "EnableIfPart", "Removes face colors from all bodies.")
    bRet = axSldWorks.AddMenuItem2(swDocPART, axCookie, axMenu2, 0, "CallRandomColors", "EnableIfPart", "Places random colors on all faces on all bodies.")
    'ASSEMBLIES:
    bRet = axSldWorks.AddMenuItem2(swDocASSEMBLY, axCookie, axMenu1, 0, "CallRemoveColors", "EnableIfPart", "Removes face colors from all bodies.")
    bRet = axSldWorks.AddMenuItem2(swDocASSEMBLY, axCookie, axMenu2, 0, "CallRandomColors", "EnableIfPart", "Places random colors on all faces on all bodies.")
    '---------------------------------
    'from SW API documentation:
    'IsMenuItemAdded = SldWorks.AddMenuItem2(DocumentType, Cookie, MenuItem, position, MenuCallback, MenuEnableMethod, HintString)
    'MenuCallback: this is the subroutine that will be called by the menu
        'This subroutine must reside in this class module.
        'If you wish to call a function or sub contained in another module,
        'do it from a subroutine in this module.
    'MenuEnableMethod:
        'function that returns a long value 0 to 3
        'must reside in this class module
        'see SW API documentation for values and settings.
        'typically returns value of 1 for "deselected and enabled"
    '---------------------------------
'==================================

'================
'4. add TOOLBARS!
'================
'Toolbar bitmaps must reside in a VB resource (*.res) file
'one good article on resoure files can be found at http://www.zarr.net/vb/articles/vb6res.asp (as of Feb. 20, 2004)
'or try Google search with keywords: visual basic resource file bitmap
'IMPORTANT: to add, edit or view a resource module, you must have the
'VB 6 Resource Editor loaded under "Add-ins --> Add-in Manager..."

    '4a. Add Toolbar
    'create the toolbar in each desired document type
    lngToolbarDocTypes = swDocTemplateTypeASSEMBLY + swDocTemplateTypePART
    'note use of "swDocTemplateTypeASSEMBLY + swDocTemplateTypePART" and not "swDocASSEMBLY + swDocPART"
    axToolbarID = axSldWorks.AddToolbar3(axCookie, "Face Colors", 101, 102, 0, lngToolbarDocTypes)
    'NOTE: this is where the Toolbar's "Cookie" or "ResourceID" is estalished
    'for future use with SldWorks.AddToolbarCommand2
    '-----------------------------------
    'from SW API documentation:
    'NewToolBarID = SldWorks.AddToolbar3 ( Cookie, Title, SmallBitmapResourceID, LargeBitmapResourceID, MenuPositionForToolbar, DocumentType )
    'SmallBitmapResourceID, LargeBitmapResourceID are from resource file as defined by programmer
    'see SW API help under this command for specs regarding toolbar bitmap size and color palette
    '-----------------------------------

    '4b. Add commands
    'Toolbars have been added to SW
    'Now add commands to toolbars
    bRet = axSldWorks.AddToolbarCommand2(axCookie, axToolbarID, 0, "CallRemoveColors", "EnableIfPart", "Removes face colors from all bodies.", "Removes face colors from all bodies.")
    bRet = axSldWorks.AddToolbarCommand2(axCookie, axToolbarID, 1, "CallRandomColors", "EnableIfPart", "Places random colors on all faces on all bodies.", "Places random colors on all faces on all bodies.")
    '---------------------------------------------
    'from SW API help:
    'IsToolbarCommandAdded = SldWorks.AddToolbarCommand2 ( Cookie, ToolbarID, ToolbarIndex, ButtonCallback, ButtonEnableMethod, ToolTip, HintString )
    '"Cookie" = axCookie = cookie specified at beginning of ConnectToSW module
    '"ToolbarID" = axToolbarID = result from "axSldWorks.AddToolbar3" call
    '"ToolbarIndex": toolbarbuttons are numbered starting at zero
        'each toolbar button is 16 or 22 pixels wide (for small and large toolbars, respectively)
        'i.e. for a small toolbar, button 0 would use pixel columns 1 to 16,
        'button 1 would use, pixel columns 17-32, etc.
        'When designing toolbars, don't worry about leaving a margin.  Use all
        'available space for design.  SW will add margin between buttons.
    '"ButtonCallback": same restrictions as callback for menu items
    '"ButtonEnableMethod": similar to menu enable methods

'======================
'5. Connect Addin to SW
'======================
SwAddin_ConnectToSW = True

End Function

Private Function SwAddin_DisconnectFromSW() As Boolean
Dim bRet As Boolean
Dim axMenu0 As String 'for SW menu ID ("File", "Insert", "Tools", etc.)
Dim axMenu1 As String, axMenu2a As String, axMenu2b As String
Dim TargetMenu As String
Dim SubMenuCount As Long
Dim axFrame As SldWorks.Frame

'====================
'1. Initialize values
'====================
Set axFrame = axSldWorks.Frame 'needed for Frame.GetSubMenuCount
axMenu0 = axSldWorks.GetLocalizedMenuName(4) '4 = "Tools" menu
axMenu1 = "Esox Republic Tools"
axMenu2a = "Remove all face colors"
axMenu2b = "Color faces with random colors"

'===============
'2. Remove Menus
'===============
'if submenus are arranged more than one level deep
'remove menus incrementally by level, checking if there
'are other items under the upper level menus before attempting to remove
    
    '2a. remove bottom level menus
    TargetMenu = axMenu2a & "@" & axMenu1 & "@" & axMenu0
    bRet = axSldWorks.RemoveMenu(1, TargetMenu, "CallRemoveColors") 'part
    bRet = axSldWorks.RemoveMenu(2, TargetMenu, "CallRemoveColors") 'assembly
    
    TargetMenu = axMenu2b & "@" & axMenu1 & "@" & axMenu0
    bRet = axSldWorks.RemoveMenu(1, TargetMenu, "CallRandomColors") 'part
    bRet = axSldWorks.RemoveMenu(2, TargetMenu, "CallRandomColors") 'assembly

    '2b. remove "Esox Republic Tools" if it has no submenus
    TargetMenu = axMenu1 & "@" & axMenu0
    'part
    SubMenuCount = axFrame.GetSubMenuCount(1, TargetMenu)
    If SubMenuCount <= 0 Then
        bRet = axSldWorks.RemoveMenu(1, TargetMenu, "") 'part
    End If
    'assembly
    SubMenuCount = axFrame.GetSubMenuCount(2, TargetMenu)
    If SubMenuCount <= 0 Then
        bRet = axSldWorks.RemoveMenu(2, TargetMenu, "") 'part
    End If

'==================
'3. Remove Toolbars
'==================
bRet = axSldWorks.RemoveToolbar2(axCookie, axToolbarID)
'------------------------------------------------------
'from SW API help re: use of SldWorks.RemoveToolbar2...
'If the SolidWorks application is exiting and your application is
'still added-in, then you should not call this method.
'------------------------------------------------------
'BUT, there is a "Catch-22" here!
'Addins created in VB are destroyed before the SW session is destroyed,
'therefore the addin is not around to catch the SldWorks.DestroyNotify event
'So far I have not found a way to determine if an add-in has been
'removed in-session or is being closed by a terminating SW session.
'If anyone knows a way to do this, let me know.  So far, I have seen
'no ill effects using SldWorks.RemoveToolbar2 every time.

'================
'4. Clear objects
'================
'good VB programming to set objects to "Nothing" when done
Set axFrame = Nothing
Set axSldWorks = Nothing

'=============
'5. Disconnect
'=============
SwAddin_DisconnectFromSW = True


End Function

Public Function EnableIfPart() As Long
'Used for SldWorks.AddMenuItem2 "MenuEnableMethod"
    'and SldWorks.AddToolbarCommand2 "ButtonEnableMethod"
'Returns 0 if active doc or assembly edit target is not a SW part
'------------------
'For menus:
    '0 = deselected (unchecked) and disabled (greyed out)
    '1 = deselected and enabled
    '2 = selected (checked) and disabled
    '3 = selected and enabled
'For toolbars:
    '0 = disabled (greyed out)
    '1 = enabled
    '2 = pressed in and disabled
    '3 = pressed in and enabled

EnableIfPart = 0
'Dim axActiveDoc As SldWorks.ModelDoc2
Dim axActiveType As Long
'Dim axTargetDoc As SldWorks.ModelDoc2
Dim axTargetType As Long
Dim SelfEdit As Boolean
Dim ButtonStat As Long

Set axTargetDoc = Nothing
axTargetType = 0
ButtonStat = 0

'test edit target to make sure it is a part
Set axActiveDoc = axSldWorks.ActiveDoc
axActiveType = axActiveDoc.GetType

If axActiveType = 1 Then 'active doc is part
    ButtonStat = 1
    GoTo ClearObjects_EnableIfPart
End If

If axActiveType = 2 Then 'ASSEMBLY: check if edit target is part
ButtonStat = 0
    SelfEdit = axActiveDoc.IsEditingSelf
    Set axTargetDoc = axActiveDoc.GetEditTarget
    axTargetType = axTargetDoc.GetType
    If axTargetType = 1 Then ButtonStat = 1
    If SelfEdit Then ButtonStat = 0
End If

ClearObjects_EnableIfPart:
EnableIfPart = ButtonStat
'clear objects
Set axActiveDoc = Nothing
Set axTargetDoc = Nothing

End Function

Sub CallRemoveColors()
'subroutine called by menu pick "Remove all face colors"
Module1.ScrubFacesClean
End Sub

Sub CallRandomColors()
'subroutine called by menu pick "Color faces with random colors"
Module1.TammyFaye
End Sub

