Solid Edge 2020 SDK
Handling Events
Programmer's Guide > Solid Edge > Handling Events

Solid Edge provides a wide range of COM event sets. Events are used to notify a client that something of interest has occurred in the object. Events are fired by COM objects and no response from the client is expected. In other words, they are simple notifications.

Solid Edge events are available for Mouse, Application, Assembly etc. Some of the events are hit after certain command is invoked and some occur when a command is in progress.

Before implementing any event interface in your code user should first search for the event set in the object browser. Object browser can give you an idea about the member to which this eventset belong to. For example, if you search for AssemblyChangeEvent in object browser you will find that this event set is a member of "SolidEdgeAssembly.AssemblyDocument" whereas if you search for "ApplicationEvents" in object browser you will find that it is a member of "SolidEdgeFramework". While handling any event set you should use appropriate member to avoid exceptions.

Below are two methods available in .NET which can be used to hook-up Solid Edge events.

1. Delegate Event Model

In this approach user has freedom to handle specific event as per requirement. There is a hidden class which is suffixed with _Event. For example, we have ISEApplicationEvent_Event available. Since it is a hidden class, it will not be available through intellisense. You have to add "_Event" manually to resolve this class. Please refer to below sample for more details.

2. Connection Point Model

In this approach after implementing a particular event interface, all the events raised by that interface are handled. While getting a reference to the IConnectionPointContainer make sure that you are using right member for the event set. For example, use application for ApplicationEvents or use AssemblyDocument for AssemblyChangeEvents/AssemblyRecomputeEvents. As shown in below sample code, we will need to leverage the IConnectionPointContainer and IConnectionPoint interfaces from the System.Runtime.InteropServices.ComTypes namespace.

 Delegate Event Model
Imports System
Imports System.Runtime.InteropServices
Imports System.Windows.Forms

Namespace EventTest
    Partial Public Class Form1
        Inherits Form

        Private _application As SolidEdgeFramework.Application
        Private _applicationEvents As SolidEdgeFramework.ISEApplicationEvents_Event

        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
            ' Connect to Solid Edge.
            _application = DirectCast(Marshal.GetActiveObject("SolidEdge.Application"), SolidEdgeFramework.Application)

            ' Connect to application events.
            _applicationEvents = CType(_application.ApplicationEvents, SolidEdgeFramework.ISEApplicationEvents_Event)
            AddHandler _applicationEvents.AfterDocumentSave, AddressOf _applicationEvents_AfterDocumentSave
            AddHandler _applicationEvents.BeforeDocumentSave, AddressOf _applicationEvents_BeforeDocumentSave
        End Sub

        Private Sub _applicationEvents_AfterDocumentSave(ByVal theDocument As Object)
            ' Handle AfterDocumentSave.
        End Sub

        Private Sub _applicationEvents_BeforeDocumentSave(ByVal theDocument As Object)
            ' Handle BeforeDocumentSave.
        End Sub

        Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
            ' Disconnect to application events.
            AddHandler _applicationEvents.BeforeDocumentSave, AddressOf _applicationEvents_BeforeDocumentSave
            AddHandler _applicationEvents.AfterDocumentSave, AddressOf _applicationEvents_AfterDocumentSave

            _applicationEvents = Nothing
        End Sub
    End Class
End Namespace
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace EventTest
{
    public partial class Form1 : Form
    {
        private SolidEdgeFramework.Application _application;
        private SolidEdgeFramework.ISEApplicationEvents_Event _applicationEvents;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Connect to Solid Edge.
            _application = (SolidEdgeFramework.Application)Marshal.GetActiveObject("SolidEdge.Application");

            // Connect to application events.
            _applicationEvents = (SolidEdgeFramework.ISEApplicationEvents_Event)_application.ApplicationEvents;
            _applicationEvents.AfterDocumentSave += _applicationEvents_AfterDocumentSave;
            _applicationEvents.BeforeDocumentSave += _applicationEvents_BeforeDocumentSave;
        }

        void _applicationEvents_AfterDocumentSave(object theDocument)
        {
            // Handle AfterDocumentSave.
        }

        void _applicationEvents_BeforeDocumentSave(object theDocument)
        {
            // Handle BeforeDocumentSave.
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            // Disconnect to application events.
            _applicationEvents.BeforeDocumentSave += _applicationEvents_BeforeDocumentSave;
            _applicationEvents.AfterDocumentSave += _applicationEvents_AfterDocumentSave;
            _applicationEvents = null;
        }
    }
}
 Connection Point Model
Option Infer On

Imports System
Imports System.Runtime.InteropServices
Imports System.Runtime.InteropServices.ComTypes
Imports System.Windows.Forms

Namespace EventTest
    Partial Public Class Form1
        Inherits Form
        Implements SolidEdgeFramework.ISEApplicationEvents

        Private _application As SolidEdgeFramework.Application
        Private _connectionPoint As IConnectionPoint
        Private _cookie As Integer

        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
            ' Connect to Solid Edge.
            _application = DirectCast(Marshal.GetActiveObject("SolidEdge.Application"), SolidEdgeFramework.Application)

            ConnectEvents()
        End Sub

        Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
            DisconnectEvents()
        End Sub

        Private Sub ConnectEvents()
            ' Get the event GUID.
            Dim typeGuid = GetType(SolidEdgeFramework.ISEApplicationEvents).GUID

            ' Get a reference to the IConnectionPointContainer.
            Dim container As IConnectionPointContainer = DirectCast(_application, IConnectionPointContainer)

            ' Lookup the IConnectionPoint.
            container.FindConnectionPoint(typeGuid, _connectionPoint)

            ' Advise the sink.
            _connectionPoint.Advise(Me, _cookie)
        End Sub

        Private Sub DisconnectEvents()
            ' Unadvise the sink.
            _connectionPoint.Unadvise(_cookie)

            ' Clear variables.
            _cookie = 0
            _connectionPoint = Nothing
        End Sub

        #Region "ISEApplicationEvents"

        Public Sub AfterActiveDocumentChange(ByVal theDocument As Object)
        End Sub

        Public Sub AfterCommandRun(ByVal theCommandID As Integer)
        End Sub

        Public Sub AfterDocumentOpen(ByVal theDocument As Object)
        End Sub

        Public Sub AfterDocumentPrint(ByVal theDocument As Object, ByVal hDC As Integer, ByRef ModelToDC As Double, ByRef Rect As Integer)
        End Sub

        Public Sub AfterDocumentSave(ByVal theDocument As Object)
        End Sub

        Public Sub AfterEnvironmentActivate(ByVal theEnvironment As Object)
        End Sub

        Public Sub AfterNewDocumentOpen(ByVal theDocument As Object)
        End Sub

        Public Sub AfterNewWindow(ByVal theWindow As Object)
        End Sub

        Public Sub AfterWindowActivate(ByVal theWindow As Object)
        End Sub

        Public Sub BeforeCommandRun(ByVal theCommandID As Integer)
        End Sub

        Public Sub BeforeDocumentClose(ByVal theDocument As Object)
        End Sub

        Public Sub BeforeDocumentPrint(ByVal theDocument As Object, ByVal hDC As Integer, ByRef ModelToDC As Double, ByRef Rect As Integer)
        End Sub

        Public Sub BeforeDocumentSave(ByVal theDocument As Object)
        End Sub

        Public Sub BeforeEnvironmentDeactivate(ByVal theEnvironment As Object)
        End Sub

        Public Sub BeforeQuit()
        End Sub

        Public Sub BeforeWindowDeactivate(ByVal theWindow As Object)
        End Sub

        #End Region
    End Class
End Namespace
using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Windows.Forms;

namespace EventTest
{
    public partial class Form1 : Form, SolidEdgeFramework.ISEApplicationEvents
    {
        private SolidEdgeFramework.Application _application;
        private IConnectionPoint _connectionPoint;
        private int _cookie;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Connect to Solid Edge.
            _application = (SolidEdgeFramework.Application)Marshal.GetActiveObject("SolidEdge.Application");

            ConnectEvents();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            DisconnectEvents();
        }

        private void ConnectEvents()
        {
            // Get the event GUID.
            var typeGuid = typeof(SolidEdgeFramework.ISEApplicationEvents).GUID;

            // Get a reference to the IConnectionPointContainer.
            IConnectionPointContainer container = (IConnectionPointContainer)_application;

            // Lookup the IConnectionPoint.
            container.FindConnectionPoint(ref typeGuid, out _connectionPoint);

            // Advise the sink.
            _connectionPoint.Advise(this, out _cookie);
        }

        private void DisconnectEvents()
        {
            // Unadvise the sink.
            _connectionPoint.Unadvise(_cookie);

            // Clear variables.
            _cookie = 0;
            _connectionPoint = null;
        }

        #region ISEApplicationEvents

        public void AfterActiveDocumentChange(object theDocument)
        {
        }

        public void AfterCommandRun(int theCommandID)
        {
        }

        public void AfterDocumentOpen(object theDocument)
        {
        }

        public void AfterDocumentPrint(object theDocument, int hDC, ref double ModelToDC, ref int Rect)
        {
        }

        public void AfterDocumentSave(object theDocument)
        {
        }

        public void AfterEnvironmentActivate(object theEnvironment)
        {
        }

        public void AfterNewDocumentOpen(object theDocument)
        {
        }

        public void AfterNewWindow(object theWindow)
        {
        }

        public void AfterWindowActivate(object theWindow)
        {
        }

        public void BeforeCommandRun(int theCommandID)
        {
        }

        public void BeforeDocumentClose(object theDocument)
        {
        }

        public void BeforeDocumentPrint(object theDocument, int hDC, ref double ModelToDC, ref int Rect)
        {
        }

        public void BeforeDocumentSave(object theDocument)
        {
        }

        public void BeforeEnvironmentDeactivate(object theEnvironment)
        {
        }

        public void BeforeQuit()
        {
        }

        public void BeforeWindowDeactivate(object theWindow)
        {
        }

        #endregion
    }
}