AutoCAD .NET has wrapped almost all ObjectARX events through one way or another using still the event concept or something slightly different. As a whole, the AutoCAD .NET event or notification system is powerful and useful and a professional and relatively big addin cannot totally ignore the important force.
In this article, we will be talking about the AutoCAD .NET Graphics Manager Events. It is always good to start with some cool, concise and still full sample code. Here it is:
using System;
using System.Text;
using System.Linq;
using System.Xml;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.IO;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Windows;
using AcadApplication = Autodesk.AutoCAD.ApplicationServices.Application;
using AcadDocument = Autodesk.AutoCAD.ApplicationServices.Document;
using AcadWindows = Autodesk.AutoCAD.Windows;
namespace AcadNetAddinWizard_Namespace
{
public class GraphicsManagerEvents
{
[CommandMethod("TestGraphicsManagerEvents")]
public void TestGraphicsManagerEvents_Method()
{
GraphicsManagerEvents eventManager = new GraphicsManagerEvents();
eventManager.Register();
}
public void Register()
{
AcadApplication.DocumentManager.MdiActiveDocument.GraphicsManager.ConfigWasModified += new Autodesk.AutoCAD.GraphicsSystem.ConfigWasModifiedEventHandler(GraphicsManager_ConfigWasModified);
AcadApplication.DocumentManager.MdiActiveDocument.GraphicsManager.ViewWasCreated += new Autodesk.AutoCAD.GraphicsSystem.ViewWasCreatedEventHandler(GraphicsManager_ViewWasCreated);
AcadApplication.DocumentManager.MdiActiveDocument.GraphicsManager.ViewToBeDestroyed += new Autodesk.AutoCAD.GraphicsSystem.ViewToBeDestroyedEventHandler(GraphicsManager_ViewToBeDestroyed);
AcadApplication.DocumentManager.MdiActiveDocument.GraphicsManager.GsToBeUnloaded += new Autodesk.AutoCAD.GraphicsSystem.GsToBeUnloadedEventHandler(GraphicsManager_GsToBeUnloaded);
}
void GraphicsManager_GsToBeUnloaded(object sender, EventArgs e)
{
//MessageBox.Show("GsToBeUnloaded");
AcadApplication.DocumentManager.MdiActiveDocument.Editor.WriteMessage("GsToBeUnloaded\n");
}
void GraphicsManager_ViewToBeDestroyed(object sender, Autodesk.AutoCAD.GraphicsSystem.ViewEventArgs e)
{
//MessageBox.Show("ViewToBeDestroyed");
AcadApplication.DocumentManager.MdiActiveDocument.Editor.WriteMessage("ViewToBeDestroyed\n");
}
void GraphicsManager_ConfigWasModified(object sender, EventArgs e)
{
//MessageBox.Show("ConfigWasModified");
AcadApplication.DocumentManager.MdiActiveDocument.Editor.WriteMessage("ConfigWasModified\n");
}
void GraphicsManager_ViewWasCreated(object sender, Autodesk.AutoCAD.GraphicsSystem.ViewEventArgs e)
{
//MessageBox.Show("ViewWasCreated");
AcadApplication.DocumentManager.MdiActiveDocument.Editor.WriteMessage("ViewWasCreated\n");
}
}
After the command TestGraphicsManagerEvents runs, some view related Graphics Manager Events will be monitored such as GsToBeUnloaded, ViewToBeDestroyed, ConfigWasModified, and ViewWasCreated.
The code is short and self-explanatory so we will not waste our words here. Let us play around the command and look at some command outputs:
Command: TestGraphicsManagerEvents
Command: hide
Command: ViewWasCreated
ViewWasCreated
ViewToBeDestroyed
ViewToBeDestroyed
Command: SHADE
ViewWasCreated
Command: SHADEMODE
VSCURRENT
Enter an option
[2dwireframe/Wireframe/Hidden/Realistic/Conceptual/Shaded/shaded with
Edges/shades of Gray/SKetchy/X-ray/Other/cUrrent] <Current>: 2d
ViewToBeDestroyed
Regenerating model.
Command: ViewWasCreated
ViewWasCreated
ViewToBeDestroyed
ViewToBeDestroyed
SHADEMODE VSCURRENT
Enter an option
[2dwireframe/Wireframe/Hidden/Realistic/Conceptual/Shaded/shaded with
Edges/shades of Gray/SKetchy/X-ray/Other] <2dwireframe>: wireframe
ViewWasCreated
Command: ViewWasCreated
ViewWasCreated
ViewToBeDestroyed
ViewToBeDestroyed
SHADEMODE VSCURRENT
Enter an option
[2dwireframe/Wireframe/Hidden/Realistic/Conceptual/Shaded/shaded with
Edges/shades of Gray/SKetchy/X-ray/Other] <Wireframe>: hidden
Command: ViewWasCreated
ViewWasCreated
ViewToBeDestroyed
ViewToBeDestroyed
SHADEMODE VSCURRENT
Enter an option
[2dwireframe/Wireframe/Hidden/Realistic/Conceptual/Shaded/shaded with
Edges/shades of Gray/SKetchy/X-ray/Other] <Hidden>: realistic
Command: ViewWasCreated
ViewWasCreated
ViewToBeDestroyed
ViewToBeDestroyed
SHADEMODE VSCURRENT
Enter an option
[2dwireframe/Wireframe/Hidden/Realistic/Conceptual/Shaded/shaded with
Edges/shades of Gray/SKetchy/X-ray/Other] <Realistic>: conceptural
Invalid option keyword.
Enter an option
[2dwireframe/Wireframe/Hidden/Realistic/Conceptual/Shaded/shaded with
Edges/shades of Gray/SKetchy/X-ray/Other] <Realistic>: conceptual
Command: ViewWasCreated
ViewWasCreated
ViewToBeDestroyed
ViewToBeDestroyed
SHADEMODE VSCURRENT
Enter an option
[2dwireframe/Wireframe/Hidden/Realistic/Conceptual/Shaded/shaded with
Edges/shades of Gray/SKetchy/X-ray/Other] <Conceptual>: shaded
Command: ViewWasCreated
ViewWasCreated
ViewToBeDestroyed
ViewToBeDestroyed
SHADEMODE VSCURRENT
Enter an option
[2dwireframe/Wireframe/Hidden/Realistic/Conceptual/Shaded/shaded with
Edges/shades of Gray/SKetchy/X-ray/Other] <Shaded>: e
Command: ViewWasCreated
ViewWasCreated
ViewToBeDestroyed
ViewToBeDestroyed
SHADEMODE VSCURRENT
Enter an option
[2dwireframe/Wireframe/Hidden/Realistic/Conceptual/Shaded/shaded with
Edges/shades of Gray/SKetchy/X-ray/Other] <Shaded with edges>: g
Command: ViewWasCreated
ViewWasCreated
ViewToBeDestroyed
ViewToBeDestroyed
SHADEMODE VSCURRENT
Enter an option
[2dwireframe/Wireframe/Hidden/Realistic/Conceptual/Shaded/shaded with
Edges/shades of Gray/SKetchy/X-ray/Other] <Shades of Gray>: sk
Command: ViewWasCreated
ViewWasCreated
ViewToBeDestroyed
ViewToBeDestroyed
SHADEMODE VSCURRENT
Enter an option
[2dwireframe/Wireframe/Hidden/Realistic/Conceptual/Shaded/shaded with
Edges/shades of Gray/SKetchy/X-ray/Other] <Sketchy>: x
Command: ViewWasCreated
ViewWasCreated
ViewToBeDestroyed
ViewToBeDestroyed
As can be noticed, the monitored Graphics Manager view related events were fired off at right moments.
By the way, the sample code does not cover how to unsubscribe these events. I would like to leave it as an exercise to readers who have interest.
The leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) provides kinds of AutoCAD .NET Event Handler Wizards including an AutoCAD Graphics Manager Event Wizard to help create start code of event handlers automatically, quickly, flexibly and reliably.
Posted by: |