You'll have to hook your event handlers to the ActivateSheetPostNotify of an drawing's api object when you add-in loads. ActivateSheetPostNotify is a delegate of the IDrawingDoc and not of ISldWorks.
The IDrawingDoc ActivateSheetPostNotify event only fires when a sheet is interactively activated in SOLIDWORKS; this event does not fire when a sheet is activated through the API. This is especially awful if your add-in relies on a macro or another add-in that activates the sheet.
Got it. Thanks
Hi Amen. Thanks for the reply.
Your first sentence is exactly what I don't know how to do. I see in SwAddin.vb there is an AttachSWEvents sub, but I don't have a Drawing Doc object when the AddIn loads so I don't know what object to reference here:
AddHandler xxx.ActiveSheetPostNotify, AddressOf Me.zzz.
zzz is the Function name I assign to handle the Event.
xxx is ?
Are you trying to handle the sheet activation all the drawings that will be opened after activating the add-in?
Yes. It's unlikely any will be open when the AddIn loads, but could be. So basically any that are open when the AddIn loads AND any that will be opened after it loads.
Boy, that's a recipe for frustration. Be very careful with sw delegates. Through the lifecycle of your models, if you subscribe to one event, you must unsubscribe at a certain point. Otherwise, the event handlers will be thrown here and there.
So you have two things to do:
1. When your add-in loads, you need to traverse all the open documents. You can do that using the iterator pattern like follow:
Dim swModel As ModelDoc2
Set swModel = swApp.GetFirstDocument
While Not swModel Is nothing Then
'todo: check if document is a drawing
'todo: Add the swmodel to a models collection
set swModel = swModel.GetNext
'Traverse the models collection and subscribe to the sheet activation event
2. You need to subscribe to the new document event delegate and the open document event delegate (which you can search for) and check if the document is a drawing then subscribe to the sheet activation event there.
You need to be aware of when your document gets closed so that you can remove the event handlers.
Look in the class called EventHandling.vb
in this class is a parent Class called DocumentEventHandler
and 4 other classes
in each of these there is a method that is called
in the DrawingEventHandler class you add the events from AddHandler IDrawing.SomeNotification, Address of (your function or sub)
AddHandler IDrawing.ActivateSheetPostNotify, AddressOf Me.DrawingDoc_ActivateSheetPostNotify
you must have a function called
Function DrawingDoc_ActivateSheetPostNotify(ByVal SheetName As String) As Integer
'DO SOME STUFF
Return 0 'This means OK
Return 1 ' This means NO
in detachEventHandlers YOU BETTER PUT THIS
RemoveHandler IDrawing.ActivateSheetPostNotify, AddressOf Me.DrawingDoc_ActivateSheetPostNotify
if you do not, you will have dead notifications and memory leaks from what i have read.
Perfect. Thank you.
Alright guys you've given me good stuff to chew on, and my day grows late so I will look at this in the morning.
Understood on detaching the Notifications and Event Handlers.
Thank you for the help.