quick background: I'm trying to write a macro that inserts a sketch, asks the user to select a face or other sketch to conver it's entities, creates a revolve cut of this, then asks the user to select component(s) for the feature scope of the revolve cut. I've got this all working except the two spots that require the user to make selection(s). As below, I've got it working if I start the sub from the VB editor, but not from Mouse Gestures or Toolbar buttons. SO.......
I know it's been asked before and answered a million and one times but I've not figured this one out!!!!
I've done everything from the YesBox here: https://forum.solidworks.com/message/88280#88280
to "sleep 5000" and DoEvents loops etc.
the simplest example of what happens is if you run this by pressing the play button in the VB Editor it works, but if you launch it from a toolbar button mapped to it you can't select something and the loop fails. If you select a bunch of times you can actually watch all your selections play back quickly AFTER the sub fails.
Sub NeedInputStephanie()
Dim swApp As SldWorks.SldWorks
Dim SelMgr As SldWorks.SelectionMgr
Dim swDoc As SldWorks.ModelDoc2
Dim failSafe As Integer
Set swApp = Application.SldWorks
Set swDoc = swApp.ActiveDoc
Set swSelMgr = swDoc.SelectionManager
failSafe = 1
swDoc.ClearSelection2 True 'Clear all selections
While swSelMgr.GetSelectedObjectCount < 1
DoEvents 'Wait for user selection
failSafe = failSafe + 1
If failSafe = 1000 Then
MsgBox "FAILURE!"
Exit Sub
End If
Wend
MsgBox "Tell them what they've won Johnny"
End Sub
SolidworksApi macros
Check this example by Matt: WaitExample (Example Code)
Paul,
Great question, and there is a solution. DoEvents and thread sleeps don't accomplish what you want on a x64 machine because the macro is running in a separate process from the macro. Consequently nothing in the SolidWorks process is being placed in the queue and it seemingly locks up.
The solution is to use notifications. With a notifications macro you create a "listener" class that listens for the firing of the event (the notification) without stalling the SolidWorks thread. The events you need, if you want this notification macro to work in all documents, are swDraw_NewSelectionNotify, swPart_NewSelectionNotify, and swAssy_NewSelectionNotify.
Just as an FYI, I cover this exact issue in Lesson 7.2 at my web site. The accompanying macro is called "alternative to DoEvents".
Also, in response to the comment you left at my site about the x64 form macro working better with dual monitors, I hope to get to that eventually. Admittedly I haven't done much with the hWnd property so it will take some research.
Keith
Video Tutorials for the SolidWorks API
How to use/replace DoEvents with 64 bit?
thanks Deepak... that one falls under "tried it and no joy"
Keith,
ahhhh, should have realized it was a different process with the annomolies I was having. This events stuff is something I've never tried before... yeah, I saw your 7.2 lesson in the list but Tapani's link is really usefull in showing a simple example of it... and I can't afford your membership
Thanks for the help on this.
Also in response to that... I've seen a few cases where the userform doesn't accually come to the top most until you run it more than once... oh well.
Paul