By Adam Nagy
Each drawing view has its own DrawingViewEvents object and the event you subscribe to through it is specific to that DrawingView. However, the event itself does not tell you which object triggered it.
If you want to listen to this event sent by only a couple of the drawing views then you can create a separate event handler function for each, and that way identify which DrawingView object sent it:
using System.Runtime.InteropServices;
using System.Collections.Generic;
using Inventor;
using MsgBox = System.Windows.Forms.MessageBox;
namespace InventorConsoleCs
{
class Program
{
static DrawingView dw1, dw2;
static void Main(string[] args)
{
Application app = (Application)
Marshal.GetActiveObject("Inventor.Application");
DrawingDocument doc =
(DrawingDocument)app.ActiveDocument;
dw1 = doc.ActiveSheet.DrawingViews[1];
dw1.DrawingViewEvents.OnViewUpdate +=
DrawingViewEvents_OnViewUpdate1;
dw2 = doc.ActiveSheet.DrawingViews[2];
dw2.DrawingViewEvents.OnViewUpdate +=
DrawingViewEvents_OnViewUpdate2;
System.Console.ReadKey();
}
static void DrawingViewEvents_OnViewUpdate1(
EventTimingEnum BeforeOrAfter, NameValueMap Context,
CommandTypesEnum ReasonsForChange,
out HandlingCodeEnum HandlingCode)
{
HandlingCode = HandlingCodeEnum.kEventNotHandled;
MsgBox.Show(dw1.Name);
}
static void DrawingViewEvents_OnViewUpdate2(
EventTimingEnum BeforeOrAfter, NameValueMap Context,
CommandTypesEnum ReasonsForChange,
out HandlingCodeEnum HandlingCode)
{
HandlingCode = HandlingCodeEnum.kEventNotHandled;
MsgBox.Show(dw2.Name);
}
}
}
However, if you want to do things dynamically so that you can listen to this event coming from any number of drawing views then the best thing might be to wrap the event handler inside a class. Each class instance will know which DrawingView's event it's listening to and so inside its event handler function we can tell who sent it:
using System.Runtime.InteropServices;
using System.Collections.Generic;
using Inventor;
using MsgBox = System.Windows.Forms.MessageBox;
namespace InventorConsoleCs
{
class Program
{
class DrawingViewEventWatcher
{
public DrawingView drawingView;
public DrawingViewEventWatcher(DrawingView v)
{
drawingView = v;
v.DrawingViewEvents.OnViewUpdate +=
DrawingViewEvents_OnViewUpdate;
}
void DrawingViewEvents_OnViewUpdate(
EventTimingEnum BeforeOrAfter, NameValueMap Context,
CommandTypesEnum ReasonsForChange,
out HandlingCodeEnum HandlingCode)
{
HandlingCode = HandlingCodeEnum.kEventNotHandled;
MsgBox.Show(drawingView.Name);
}
}
static List<DrawingViewEventWatcher> watchers =
new List<DrawingViewEventWatcher>();
static void Main(string[] args)
{
Application app = (Application)
Marshal.GetActiveObject("Inventor.Application");
DrawingDocument doc =
(DrawingDocument)app.ActiveDocument;
foreach (Sheet s in doc.Sheets)
foreach(DrawingView v in s.DrawingViews)
{
watchers.Add(new DrawingViewEventWatcher(v));
}
}
}
When running the code on a drawing with the following content then this is what we get when we modify the assembly and switch back to the drawing: