Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

AddToolbar3 and AddToolbarCommand2 Functionality

348 views
Skip to first unread message

Chris Dubea

unread,
Mar 24, 2004, 6:00:52 PM3/24/04
to
Hi all,

It's me again. I'm using the example from the Solidworks API website
on how to create a Toolbar/Menu application in VB.

I've got the menu functioanlity all sorted out, but I simply cannot
get the toolbar functionality to work. Even using the example
application, it does not create the toolbar with the icons as called
out in the resource file.

Any suggestions?

Thanks,

Chris

Philippe Guglielmetti

unread,
Mar 25, 2004, 12:31:29 AM3/25/04
to
"Chris Dubea" wrote:

> It's me again. I'm using the example from the Solidworks API website
> on how to create a Toolbar/Menu application in VB.

So you make an add-in, not a macro, right ?
Because AddToolbar3 uses a "cookie" parameter which must match the one you
passed to SwAddin.ConnectToSW ( ThisSW, Cookie ) ...

I never did a toolbar for macros, don't even know if it's possible. I add
buttons to the SolidWorks macro toolbar (which almost no lambda user uses).
This can be made directly to the registry by a smart installer or a small
app that runs only once.

--
Philippe Guglielmetti - www.dynabits.com


Chris Dubea

unread,
Mar 25, 2004, 9:26:02 AM3/25/04
to
"Philippe Guglielmetti" <ne...@dynabits.com> wrote in message news:<40626e97$0$701$5402...@news.sunrise.ch>...

> "Chris Dubea" wrote:
>
> > It's me again. I'm using the example from the Solidworks API website
> > on how to create a Toolbar/Menu application in VB.
>
> So you make an add-in, not a macro, right ?
> Because AddToolbar3 uses a "cookie" parameter which must match the one you
> passed to SwAddin.ConnectToSW ( ThisSW, Cookie ) ...
>

Yes it is an add-in. I have downloaded and compiled the VB example
from the SolidWorks website, and created a dll. I've opened it in
SolidWorks. It creates the munu addition, but not the toolbar.

I was wondering if anyone else had gotten the toolbar functionality to
work.

Thanks,

Chris

Paul Delhanty

unread,
Mar 25, 2004, 2:04:31 PM3/25/04
to
"Philippe Guglielmetti" <ne...@dynabits.com> wrote in message
news:40626e97$0$701$5402...@news.sunrise.ch...
> "Chris Dubea" wrote:
>
>
> I never did a toolbar for macros, don't even know if it's possible. I add
> buttons to the SolidWorks macro toolbar (which almost no lambda user
uses).
> This can be made directly to the registry by a smart installer or a small
> app that runs only once.
>
Philippe,

SolidWorks API Support told me that they don't document or support the
registry format for the macro toolbar buttons, so a smart installer might
break in the next version of SolidWorks. Have you heard differently?

--
Paul Delhanty
Cadcambridge Ltd
SolidWorks Research Partner
http://www.cadcambridge.net


Philippe Guglielmetti

unread,
Mar 26, 2004, 12:21:14 AM3/26/04
to
"Paul Delhanty" wrote:

> Philippe,
> SolidWorks API Support told me that they don't document or support the
> registry format for the macro toolbar buttons, so a smart installer might
> break in the next version of SolidWorks. Have you heard differently?

Right, it's undocumented. But it works.
If they change the format, I'll make it work for the next version too ;-)
Basically you have :

--> Numbero fo macros in

[HKEY_CURRENT_USER\Software\SolidWorks\SolidWorks 2004\User Defined Macros]
"Macro Count"=dword:00000006

--> then 1 entry per button

[HKEY_CURRENT_USER\Software\SolidWorks\SolidWorks 2004\User Defined
Macros\01 - Macro Folder]
"Command Offset"=dword:00000000 // increment this by 1 for each button
"Project"="TrimExtend"
"MacroMethod"="main"
"ToolTip"="TrimExtend"
"Prompt Msg"="Trims or Extends selected segments to (the extent of) another
one"
"Source Path"="X:\\def.sworks\\Macros\\SolidPlus.swp"
"Bitmap Path"="X:\\def.sworks\\Macros\\TrimExtend.bmp"
"Accelerator"="Alt+T"

--> Then build the toolbar by specifying each buttons with 4 bytes :
67+"Command Offset",83,00,00

[HKEY_CURRENT_USER\Software\SolidWorks\SolidWorks 2004\User
Interface\Toolbars]
"uiMacroToolBar_c"=hex:67,83,00,00,68,83,00,00,69,83,00,00,6A,83,00,00,6B,83
,00,00,6C,83,00,00

TheTick

unread,
Mar 26, 2004, 12:05:27 PM3/26/04
to
'Chris:
'This is the code from the class module from a tutorial I am writing.
'It uses toolbars. The project is to turn a
'simple .exe into an addin .dll

'I plan to post this to my website this weekend, but I thought I would
'post this code now, in case the fishing is good and I don't work
'on the website tomorrow :)

'Lots of comments. Hope it helps you sort out the toolbar thang.


'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
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
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
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
'================
Set axFrame = Nothing
Set axSldWorks = Nothing

'=============
'5. Disconnect
'=============
SwAddin_DisconnectFromSW = False

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

Chris Dubea

unread,
Mar 26, 2004, 10:09:12 PM3/26/04
to
On 26 Mar 2004 09:05:27 -0800, ro...@liquidschwarz.com (TheTick) wrote:

>'Chris:
>'This is the code from the class module from a tutorial I am writing.
>'It uses toolbars. The project is to turn a
>'simple .exe into an addin .dll

Thanks,

I'll give it a whirl.

===========================================================================
Chris

Chris Dubea

unread,
Mar 29, 2004, 9:02:38 AM3/29/04
to
ro...@liquidschwarz.com (TheTick) wrote in message news:<2e958085.04032...@posting.google.com>...

> 'Chris:
> 'This is the code from the class module from a tutorial I am writing.
> 'It uses toolbars. The project is to turn a
> 'simple .exe into an addin .dll
>

...snip

> Module1.TammyFaye
> End Sub

Thanks for the code, but I get the same results. The menu works
peachy, but no tool bars. H'mmm Curious.

I've got an e-mail going with apisupport, maybe they can shed some
light on the issue.

Chris

TheTick

unread,
Mar 29, 2004, 2:16:19 PM3/29/04
to
cdu...@movingpart.com (Chris Dubea) wrote in message news:<75013a8a.04032...@posting.google.com>...

If you wish, contact me via the customer service link at
esoxrepublic.com. I will send you the entire project. Then you can
compile and see if it works.

0 new messages