Code:
Imports Inventor
Imports System.Runtime.InteropServices
Namespace EventSample
<ProgId("EventSample.StandardAddInServer"), _
GuidAttribute("C022F229-A110-433D-BDE6-8ADD96DC8D5A")> _
Public Class StandardAddInServer
Implements Inventor.ApplicationAddInServer
Private WithEvents oDocEvents As DocumentEvents
Private WithEvents oAppEvents As ApplicationEvents
#Region "Data Members"
Dim InventorApplication As Inventor.Application
#End Region
#Region "ApplicationAddInServer Members"
Public Sub Activate(ByVal AddInSiteObject As Inventor.ApplicationAddInSite, ByVal FirstTime As Boolean) Implements Inventor.ApplicationAddInServer.Activate
'the Activate method is called by Inventor when it loads the addin
'the AddInSiteObject provides access to the Inventor Application object
'the FirstTime flag indicates if the addin is loaded for the first time
'initialize AddIn members
InventorApplication = AddInSiteObject.Application
oAppEvents = InventorApplication.ApplicationEvents
' TODO: Add ApplicationAddInServer.Activate implementation
'e.g. event initialization, command creation etc.
End Sub
Public Sub Deactivate() Implements Inventor.ApplicationAddInServer.Deactivate
'the Deactivate method is called by Inventor when the AddIn is unloaded
'the AddIn will be unloaded either manually by the user or
'when the Inventor session is terminated
' TODO: Add ApplicationAddInServer.Deactivate implementation
'e.g. command deletion
'release objects
Marshal.ReleaseComObject(InventorApplication)
InventorApplication = Nothing
System.GC.WaitForPendingFinalizers()
System.GC.Collect()
End Sub
Public ReadOnly Property Automation() As Object Implements Inventor.ApplicationAddInServer.Automation
'if you want to return an interface to another client of this addin,
'implement that interface in a class and return that class object
'through this property
Get
Return Nothing
End Get
End Property
Public Sub ExecuteCommand(ByVal CommandID As Integer) Implements Inventor.ApplicationAddInServer.ExecuteCommand
'this method was used to notify when an AddIn command was executed
'the CommandID parameter identifies the command that was executed
'Note:this method is now obsolete, you should use the new
'ControlDefinition objects to implement commands, they have
'their own event sinks to notify when the command is executed
End Sub
#End Region
#Region "COM Registration"
' Registers this class as an Add-In for Autodesk Inventor.
' This function is called when the assembly is registered for COM.
<System.Runtime.InteropServices.ComRegisterFunction()> _
Public Shared Sub RegisterFunction(ByVal t As Type)
'call the method in the AddInRegistration class to register the AddIn
AddInRegistration.RegisterInventorAddIn(t)
End Sub
' Unregisters this class as an Add-In for Autodesk Inventor.
' This function is called when the assembly is unregistered.
<System.Runtime.InteropServices.ComUnregisterFunction()> _
Public Shared Sub UnregisterFunction(ByVal t As Type)
'call the method in the AddInRegistration class to unregister the AddIn
AddInRegistration.UnregisterInventorAddIn(t)
End Sub
#End Region
Private Sub oDocEvents_OnSave(ByVal BeforeOrAfter As Inventor.EventTimingEnum, ByVal Context As Inventor.NameValueMap, ByRef HandlingCode As Inventor.HandlingCodeEnum) Handles oDocEvents.OnSave
If BeforeOrAfter = EventTimingEnum.kBefore Then
System.Windows.Forms.MessageBox.Show("OnSave")
End If
End Sub
Private Sub oDocEvents_OnActivate(ByVal BeforeOrAfter As Inventor.EventTimingEnum, ByVal Context As Inventor.NameValueMap, ByRef HandlingCode As Inventor.HandlingCodeEnum) Handles oDocEvents.OnActivate
If BeforeOrAfter = EventTimingEnum.kBefore Then
System.Windows.Forms.MessageBox.Show("OnActivate")
End If
End Sub
Private Sub oDocEvents_OnClose(ByVal BeforeOrAfter As Inventor.EventTimingEnum, ByVal Context As Inventor.NameValueMap, ByRef HandlingCode As Inventor.HandlingCodeEnum) Handles oDocEvents.OnClose
If BeforeOrAfter = EventTimingEnum.kBefore Then
System.Windows.Forms.MessageBox.Show("OnClose")
End If
End Sub
Private Sub oAppEvents_OnNewDocument(ByVal DocumentObject As Inventor._Document, ByVal BeforeOrAfter As Inventor.EventTimingEnum, ByVal Context As Inventor.NameValueMap, ByRef HandlingCode As Inventor.HandlingCodeEnum) Handles oAppEvents.OnNewDocument
If BeforeOrAfter = EventTimingEnum.kBefore Then
oDocEvents = DocumentObject.DocumentEvents
End If
End Sub
Private Sub oAppEvents_OnOpenDocument(ByVal DocumentObject As Inventor._Document, ByVal FullFileName As String, ByVal BeforeOrAfter As Inventor.EventTimingEnum, ByVal Context As Inventor.NameValueMap, ByRef HandlingCode As Inventor.HandlingCodeEnum) Handles oAppEvents.OnOpenDocument
If BeforeOrAfter = EventTimingEnum.kAfter Then
oDocEvents = DocumentObject.DocumentEvents
End If
End Sub
End Class
End Namespace