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 Document Editor 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 EditorEvents
{
[CommandMethod("RegisterEditorEvents")]
public void RegisterEditorEvents_Method()
{
EditorEvents eventManager = new EditorEvents();
eventManager.Register();
}
[CommandMethod("UnRegisterEditorEvents")]
public void UnRegisterEditorEvents_Method()
{
EditorEvents eventManager = new EditorEvents();
eventManager.UnRegister();
}
public void Register()
{
AcadApplication.DocumentManager.MdiActiveDocument.Editor.SelectionAdded += new SelectionAddedEventHandler(editor_SelectionAdded);
AcadApplication.DocumentManager.MdiActiveDocument.Editor.SelectionRemoved += new SelectionRemovedEventHandler(editor_SelectionRemoved);
}
public void UnRegister()
{
AcadApplication.DocumentManager.MdiActiveDocument.Editor.SelectionAdded -= new SelectionAddedEventHandler(editor_SelectionAdded);
AcadApplication.DocumentManager.MdiActiveDocument.Editor.SelectionRemoved -= new SelectionRemovedEventHandler(editor_SelectionRemoved);
}
static void editor_SelectionRemoved(object sender, SelectionRemovedEventArgs e)
{
AcadApplication.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nSelectionRemoved\n");
}
static void editor_SelectionAdded(object sender, SelectionAddedEventArgs e)
{
AcadApplication.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nSelectionAdded\n");
}
}
}
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: RegisterEditorEvents
Command: select
Select objects:
SelectionAdded
1 found
Select objects:
SelectionAdded
1 found, 2 total
Select objects:
SelectionAdded
1 found, 3 total
Select objects:
SelectionRemoved
1 found, 1 removed, 2 total
Select objects:
SelectionRemoved
1 found, 1 removed, 1 total
Select objects:
SelectionAdded
SelectionAdded
SelectionAdded
SelectionAdded
SelectionAdded
SelectionAdded
Command:
SelectionAdded
SelectionAdded
SelectionAdded
*Cancel*
Command: UnRegisterEditorEvents
Command: select
Select objects: 1 found
Select objects: 1 found, 2 total
Select objects: 1 found, 3 total
Select objects: 1 found, 1 removed, 2 total
Select objects: 1 found, 1 removed, 1 total
Select objects:
The leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) provides kinds of AutoCAD .NET Event Handler Wizards including an AutoCAD Document Editor Event Wizard to help create start code of event handlers automatically, quickly, flexibly and reliably.
Recent Comments