If you are getting started with the Inventor API and you need some examples for adding an event in an Inventor AddIn take a look at these examples. The code snippets below show how to handle ApplicationEvents.OnActivateDocument event using VB.Net and C#. The VB.Net allows use of traditional event handling using WithEvents statement, but also supports .NET style using delegates. There are 3 samples attached covering these ways to add events. The samples display the name of the document after the document is activated. The samples were created using Visual Studio .NET 2010. Please see this Topic in the Inventor API help file if you need instructions on how to get Inventor to load the dll files.
Download Inventor_Events_Examples.
VB.Net - WithEvents
1. Declare member variable of type ApplicationEvents.
Public Class StandardAddInServer
Implements Inventor.ApplicationAddInServer
' Inventor application object.
Private m_inventorApplication As Inventor.Application
Dim WithEvents m_AppEvents As ApplicationEvents
2. In the Activate method of your AddInServer class, assign ApplicationEvents to member variable:
Public Sub Activate(ByVal addInSiteObject As _
Inventor.ApplicationAddInSite,
ByVal firstTime As Boolean) _
Implements Inventor.ApplicationAddInServer.Activate
' This 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.
m_inventorApplication =
addInSiteObject.Application
' TODO: Add ApplicationAddInServer.Activate
' implementation. e.g. event initialization,
'command creation etc.
m_AppEvents =
m_inventorApplication.ApplicationEvents
End Sub
3. Define event handler method. In Visual Studio IDE it's possible to use Class and Method combo boxes (at the top of open document) to automatically generate it.
Private Sub m_AppEvents_OnActivateDocument _
(ByVal DocumentObject As Inventor._Document, _
ByVal BeforeOrAfter As Inventor.EventTimingEnum, _
ByVal Context As Inventor.NameValueMap, _
ByRef HandlingCode As Inventor.HandlingCodeEnum) _
Handles m_AppEvents.OnActivateDocument
If BeforeOrAfter <>
EventTimingEnum.kAfter Then
Exit Sub
End If
MessageBox.Show _
(DocumentObject.DisplayName, _
"OnActivateDocument VB - WithEvents)", _
MessageBoxButtons.OK, _
MessageBoxIcon.Information)
End Sub
VB.Net – Delegates
1. Declare member variable of type ApplicationEvents.
Public Class StandardAddInServer
Implements Inventor.ApplicationAddInServer
' Inventor application object.
Private m_inventorApplication As _
Inventor.Application
Dim m_AppEvents As _
Inventor.ApplicationEvents
2. In the Activate method of your AddInServer class, add delegate to handle ApplicationEvents.OnActivateDocument event:
Public Sub Activate(ByVal addInSiteObject As _
Inventor.ApplicationAddInSite,
ByVal firstTime As Boolean) _
Implements Inventor.ApplicationAddInServer.Activate
' Initialize AddIn members.
m_inventorApplication =
addInSiteObject.Application
m_AppEvents =
m_inventorApplication.ApplicationEvents
AddHandler m_AppEvents.OnActivateDocument,
AddressOf ApplicationEvents_OnActivateDocument
End Sub
3. Remove the event in the Deactivate method.
Public Sub Deactivate() Implements _
Inventor.ApplicationAddInServer.Deactivate
' This 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()
' Release objects.
m_inventorApplication = Nothing
RemoveHandler m_AppEvents.OnActivateDocument,
AddressOf ApplicationEvents_OnActivateDocument
m_AppEvents = Nothing
System.GC.Collect()
System.GC.WaitForPendingFinalizers()
End Sub
4. Define event handler method. It's possible to use TAB key to automatically generate it.
Private Sub ApplicationEvents_OnActivateDocument _
(ByVal DocumentObject As Inventor._Document, _
ByVal BeforeOrAfter As Inventor.EventTimingEnum, _
ByVal Context As Inventor.NameValueMap, _
ByRef HandlingCode As Inventor.HandlingCodeEnum)
If BeforeOrAfter <> EventTimingEnum.kAfter Then
Exit Sub
End If
MessageBox.Show(DocumentObject.DisplayName, _
"OnActivateDocument VB Delegates)",
MessageBoxButtons.OK, _
MessageBoxIcon.Information)
End Sub
C# - Delegates
1. Declare member variable of type ApplicationEvents.
public class StandardAddInServer :
Inventor.ApplicationAddInServer
{
// Inventor application object.
private Inventor.Application
m_inventorApplication;
private Inventor.ApplicationEvents
m_appEvents;
2. In the Activate method of your AddInServer class, add delegate to handle ApplicationEvents.OnActivateDocument event:
public void Activate(Inventor.ApplicationAddInSite
addInSiteObject, bool firstTime)
{
// This 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.
m_inventorApplication =
addInSiteObject.Application;
// TODO: Add ApplicationAddInServer.Activate
// implementation. e.g. event initialization,
//command creation etc.
m_appEvents =
m_inventorApplication.ApplicationEvents;
m_appEvents.OnActivateDocument += new
ApplicationEventsSink_OnActivateDocumentEventHandler
(ApplicationEvents_OnActivateDocument);
}
3. Remove the event in the Deactivate method
public void Deactivate()
{
// Release objects.
m_inventorApplication = null;
m_appEvents.OnActivateDocument -= new
ApplicationEventsSink_OnActivateDocumentEventHandler
(ApplicationEvents_OnActivateDocument);
m_appEvents = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
4. Define event handler method. It's possible to use TAB key to automatically generate it.
private void ApplicationEvents_OnActivateDocument
(_Document DocumentObject,
EventTimingEnum BeforeOrAfter,
NameValueMap Context,
out HandlingCodeEnum HandlingCode)
{
HandlingCode =
Inventor.HandlingCodeEnum.kEventNotHandled;
if (BeforeOrAfter !=
Inventor.EventTimingEnum.kAfter)
{
return;
}
HandlingCode =
Inventor.HandlingCodeEnum.kEventHandled;
MessageBox.Show(DocumentObject.DisplayName,
"C# - OnActivateDocument",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
-Wayne