Registering for Drawing Events in vb.net AddIn

I have an AddIn in VB.NET, developed from the SolidWorks AddIn Template for Visual Studio (e.g., I'm not a full time programmer)

I have used the SolidWorks events built into the template, such as ActiveModelDocChangeNotify etc...

I want to register for a Drawing Doc event - DrawingActivateSheetPostNotify, and possibly a few others.

What I don't understand is how to set this up. The SolidWorks (application) events are all setup when the AddIn loads, because a SolidWorks object exists at that time - however a specific Drawing Doc does not obviously.

I speculate I need to listen for the ActiveDocChangeNotify, and if it's a Drawing Doc then setup the Event Handler - I just don't know exactly how to do that.

Hope this makes sense and thanks in advance for any assistance.

SolidworksApi macros
Answers 11
AJ
AJ 2018-03-20 Best Answer

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.

CC
CC 2018-03-20

R u working with ​ or just use the logo for fun?

AJ
AJ 2018-03-20

see pm.

CC
CC 2018-03-20

Got it. Thanks

JH
JH 2018-03-20

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 ?

AJ
AJ 2018-03-20

Are you trying to handle the sheet activation all the drawings that will be opened after activating the add-in?

JH
JH 2018-03-20

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.

JC
JC 2018-03-20

Look in the class called EventHandling.vb

in this class is a parent Class called DocumentEventHandler

and 4 other classes

PartEventHandler
AssemblyEventHandler

DrawingEventHandler

DocView

in each of these there is a method that is called

AttachEventHandlers

in the DrawingEventHandler class you add the events from AddHandler IDrawing.SomeNotification, Address of (your function or sub)

So

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

    End Function

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.

AJ
AJ 2018-03-20

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
Wend
'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.

JH
JH 2018-03-20

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.

JH
JH 2018-03-20

Perfect. Thank you.