is there a event which is fired when last document is closed? I need to disable all
buttons in my taskpane after last document is closed.
Thanks for your help
unfortunately there is no such event (at least documented there isn't).
There is a work around however (obviously..lol).
Handle the event DSldWorksEvents_FileOpenNotify2EventHandler Delegate (SolidWorks.Interop.sldworks) to know when documents are opened...for each open document you can add a handle for the DestroyNotify2 event (first you will have to check for the document type, to then declare that proper object withevents) to a function that would check if this was the last document open / or if there are any left in memory.
That function, would be triggered by the DestroyNotify2 event of each model that was open, so in this function you would check if the number of open documents was 0 ( GetDocumentCount Method (ISldWorks) ). Also, this function would have to remove the handle from itself for the model closing to avoid memory leaks...to do this, you must pass a reference of what model is closing.
The ideal solution would be to have a collection with all the open models...and monitor for that collection to have 0 items...the problem is though, that you need to handle the event for these models, and it isn't necessarily simple (if even possible at all) to setup a collection that supports events on it's objects.
Anyway, I may have made this sound more complicated than it really needs to be...if you can't understand...I can write up a sample class for you.
Hope this helps
Thank you Filipe for that answer.
i will give this work around a try.
i get the document type in my Program by the AcvtiveDocChangedNotify. i will
use this in the other handlers.
not clear to me is this: Also, this function would have to remove the handle from itself for the model closing to avoid memory leaks...to do this, you must pass a reference of what model is closing.
Could you give me a sample how i remove the handle? is it just to set it =NOTHING?
Maybe be you could give me the whole sample class?
Well, for this specific situation you have to add handlers dynamically, right? So what I meant was, not to forget to remove them as models got closed. So if for every model you added a handle to that function we mentioned before, as you closed your models you would want to remove that handle as well.
When you get a notification that a new document is open, you can instanciate OpenModel class (see below) and it will handle the models events...but you will still have to manually add a handle to your function. Below, on FileOpenPostNotify I create an instance of OpenModel for the current model being opened and add it to my ModelC collection...and add the handler to my RemoveModel Method, which then removes the handle from itself when it's called, because that is when the model dissapears from solidworks' memory...that's basically what I was reffering to.
So here's a sample code, to keep a collection of all open models in memory as they come in/out...
Dim WithEvents myApp As SldWorks = SWIntegration.myApp
Dim ModelC As New System.Collections.ObjectModel.Collection(Of OpenModel)
Function AddModel(ByVal FilePath) As Integer Handles myApp.FileOpenPostNotify
AddHandler ModelC.Last.Closed, AddressOf RemoveModel
AddHandler ModelC.Last.Saving, AddressOf Saving
Sub RemoveModel(ByVal rModel As OpenModel)
Dim myIndex As Integer = ModelC.IndexOf(rModel)
RemoveHandler ModelC.Item(myIndex).Closed, AddressOf Me.RemoveModel
RemoveHandler ModelC.Item(myIndex).Saving, AddressOf Me.Saving
ModelC.Item(myIndex) = Nothing
Sub Saving(ByVal sModel As OpenModel, ByRef Save As Boolean)
Dim result = MsgBox("Do you wish to update the database now?" + vbCrLf + "This option will save time later...", MsgBoxStyle.YesNoCancel, "Update DataBase")
Select Case result
Save = True
Save = True
Save = False
This is the class you would instanciate for each open model...
Public Class OpenModel
Public myModel As ModelDoc2
Public myPath As String
Public myTitle As String
Public WithEvents myPart As PartDoc
Public WithEvents myAsm As AssemblyDoc
Public Event Closed(ByVal oModel As OpenModel)
Public Event Saving(ByVal oModel As OpenModel, ByRef Save As Boolean)
Sub New(ByRef pModel As ModelDoc2)
myModel = pModel
myPath = myModel.GetPathName
myTitle = myModel.GetTitle
Select Case pModel.GetType
myPart = pModel
myAsm = pModel
Function Save_Model() As Integer Handles myPart.FileSaveNotify, myAsm.FileSaveNotify
Dim Save As Boolean = False
RaiseEvent Saving(Me, Save)
If Save Then
Function Close_Model() As Integer Handles myPart.DestroyNotify2, myAsm.DestroyNotify2
Hope this helps you out..
This is an interessting way, but if i have a collection of all my models in ram, i use the double ram space, the ram from my add-in and the ram for the models load in solidworks right?
I added a handler for every typ of model (part, assembly, drawing)
AddHandler swAssemblyDoc.DestroyNotify2, AddressOf Me.swAssembly_documentClose
AddHandler swPartDoc.DestroyNotify2, AddressOf Me.swPart_documentClose
AddHandler swDrawingDoc.DestroyNotify2, AddressOf Me.swDrawing_documentClose
and functions for each handler
Private Function swAssembly_documentClose() As Integer Dim documentCounter As Integer documentCounter = swApp.GetDocumentCount If documentCounter = 0 Then DisableAllButtons() End If End Function Private Function swPart_documentClose() As Integer Dim documentCounter As Integer documentCounter = swApp.GetDocumentCount If documentCounter = 0 Then DisableAllButtons() End If End Function Private Function swDrawing_documentClose() As Integer Dim documentCounter As Integer documentCounter = swApp.GetDocumentCount If documentCounter = 0 Then DisableAllButtons() End If End Function
Private Function swAssembly_documentClose() As Integer
Dim documentCounter As Integer
documentCounter = swApp.GetDocumentCount
If documentCounter = 0 Then
Private Function swPart_documentClose() As Integer
Private Function swDrawing_documentClose() As Integer
Dim documentCounter As Integer
DisableAllButtons is a function in my Addin.
But now i have two problems:
When i open a part file and close it the counter is no 0. It is 3
Second Problem: Closing the part fires up the swAssembly_documentClose() function, but i "destroy" a part file.
Retrieving data ...