Josh Brady had posted a macro that monitors document notifications. I am able to modify his macro to fit most of my needs, but I would like to port it to VB.NET. I would like to take his idea and mold it to fit here where I work. The code listed below is working fine except for the notification portion. For the life of me, I can't understand the new 2009 API help. If someone could look over my modified code and possibly let me know what I am missing, I would appreciate it. I have never attempted this notification stuff before.
[VB.NET CODE START]
Public Class Form1
Dim WithEvents swApp As SldWorks.SldWorks
Dim WithEvents swAssy As SldWorks.AssemblyDoc
Dim WithEvents swDwg As SldWorks.DrawingDoc
Dim WithEvents swPart As SldWorks.PartDoc
Dim swDoc As SldWorks.ModelDoc2
Public Enum swDocumentTypes_e
swDocNONE = 0
swDocPART = 1
swDocASSEMBLY = 2
swDocDRAWING = 3
End Enum
Private Function swApp_ActiveDocChangeNotify() As Integer
Report("Active Document Change")
DocEventRefresh()
UpdateForm()
End Function
Private Function swAssy_RegenNotify() As Integer
Report("Assembly Regen")
UpdateForm()
End Function
Private Function swDwg_RegenNotify() As Integer
Report("Drawing Regen")
UpdateForm()
End Function
Private Function swPart_RegenNotify() As Integer
Report("Part Regen")
UpdateForm()
End Function
Sub DocEventRefresh()
swDoc = swApp.ActiveDoc
Me.Text = swDoc.GetTitle
Select Case swDoc.GetType
Case swDocumentTypes_e.swDocPART
swPart = swDoc
swDwg = Nothing
swAssy = Nothing
Case swDocumentTypes_e.swDocDRAWING
swPart = Nothing
swDwg = swDoc
swAssy = Nothing
Case swDocumentTypes_e.swDocASSEMBLY
swPart = Nothing
swDwg = Nothing
swAssy = swDoc
Case Else
swPart = Nothing
swDwg = Nothing
swAssy = Nothing
End Select
End Sub
Sub UpdateForm()
Report("Form Updated")
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
swApp = GetObject(, "SldWorks.Application")
Report("Connected To Current SolidWorks Session")
Call DocEventRefresh()
UpdateForm()
End Sub
Public Sub Report(ByRef NotificationText As String)
Report1.Items.Add(NotificationText)
Report1.SelectedIndex = Report1.Items.Count - 1
End Sub
End Class
[VB.NET CODE END]
SolidworksApi macros
In VB.NET the events should be catched different way then in VBA.
Please take a look at the following code snippet of correct Part's RegenNotify notification listening:
- - - - - - - - - - - - - - - - -
Public Class Form1
Dim swApp As SolidWorks.Interop.sldworks.SldWorks
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LoadDim WithEvents swPart As SolidWorks.Interop.sldworks.PartDoc
swApp = GetObject(, "SldWorks.Application")
swPart = swApp.ActiveDoc
AddHandler swPart.RegenNotify, AddressOf Me.swPart_RegenNotify
End Sub
Private Function swPart_RegenNotify() As Integer
Report("Part Regen")
UpdateForm()
End Function
End Class
- - - - - - - - - - - - - - - - -
Works like a charm Artem - THANKS!
This has now uncovered another issue. I really like this method of reporting things to a list box. I had this working fine. Now that I got the notifications working, nothing will write to the index box from the swPart_RegenNotify() Function. If I comment out the Report(("Whatever Here")) and add a debug.print("Whatever Here"), it works fine. With the Report(("Whatever Here")), I get "A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll"
For testing, after connecting to SolidWorks in the form1 Load Sub, I put in another report and it works here.
Any ideas?
Thanks for your help!
Here is my entire code:
Public Class Form1
Dim swApp As SldWorks.SldWorks
Dim swModelDoc As SldWorks.ModelDoc2
Dim WithEvents swPart As SldWorks.PartDoc
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
swApp = GetObject(, "SldWorks.Application")
Report(("Connected To SolidWorks"))
swPart = swApp.ActiveDoc
swModelDoc = swApp.ActiveDoc
Me.Text = swModelDoc.GetTitle
AddHandler swPart.RegenNotify, AddressOf Me.swPart_RegenNotify
End Sub
Private Function swPart_RegenNotify() As Integer
Report(("Working"))
End Function
Private Sub Report(ByRef ReportText As String)
Report1.Items.Add(ReportText)
Report1.SelectedIndex = Report1.Items.Count - 1
End Sub
End Class
Tony,
What of the lines actually causes an exception:
Report1.Items.Add(ReportText)
or
Report1.SelectedIndex = Report1.Items.Count - 1
?
Try also to transfer these lines to directly to handler (swPart_RegenNotify). What the result?
I believe that it has something to do with the handler itself.
I can get a message box to work in the handler, but that is about it. It seems that when I try to interact with the form from within there are problems. Please keep in mind that an error window doesn't pop up, I just see the "A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll" in the Immediate Window.
Me.Text = "Title Changed" doesn't work.
I have added a button to the form. When I click the button, the code adds "-------------------------" to the list box. That works fine, without errors.
Tony,
I have found the issue.
The form controls created in another thread then the notification called so the cross-thread operation caused.
To fix this issue you should use delegates and ::Invoke method.
Please refer the following sample:
Public Class Form1
Public Delegate Sub ListDel()
Dim swApp As SolidWorks.Interop.sldworks.SldWorks
Dim WithEvents swPart As SolidWorks.Interop.sldworks.PartDoc
Dim del As ListDel
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
swApp = GetObject(, "SldWorks.Application")
swPart = swApp.ActiveDoc
AddHandler swPart.RegenNotify, AddressOf Me.swPart_RegenNotify
End Sub
Private Function swPart_RegenNotify() As Integer
del = New ListDel(AddressOf WriteLog)
lstList.Invoke(del)
End Function
Private Sub WriteLog()
lstList.Items.Add("Regen")
End Sub
End Class
Hope this help