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.
From now on, let us focus on some such AutoCAD .NET event topics. Particularly, we will be talking about the command related AutoCAD .NET Document events. It is always good to start with some cool, concise and still full sample code. Here it is:
#region Namespaces
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.Diagnostics;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.Drawing;
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;
#endregion
public class TestDocEvents
{
[CommandMethod("MonitorCommandEvents")]
public void MonitorCommandEvents_Method()
{
SubscribeToDoc(AcadApplication.DocumentManager.MdiActiveDocument);
}
public static void SubscribeToDoc(AcadDocument doc)
{
doc.CommandWillStart += new CommandEventHandler(doc_CommandWillStart);
doc.CommandEnded += new CommandEventHandler(doc_CommandEnded);
doc.CommandCancelled += new CommandEventHandler(doc_CommandCancelled);
doc.CommandFailed += new CommandEventHandler(doc_CommandFailed);
doc.UnknownCommand += new UnknownCommandEventHandler(doc_UnknownCommand);
}
static void doc_UnknownCommand(object sender, UnknownCommandEventArgs e)
{
(sender as AcadDocument).Editor.WriteMessage(string.Format("\nCommand {0} unknown.\n", e.GlobalCommandName));
}
static void doc_CommandFailed(object sender, CommandEventArgs e)
{
(sender as AcadDocument).Editor.WriteMessage(string.Format("\nCommand {0} failed.\n", e.GlobalCommandName));
}
static void doc_CommandCancelled(object sender, CommandEventArgs e)
{
(sender as AcadDocument).Editor.WriteMessage(string.Format("\nCommand {0} cancelled.\n", e.GlobalCommandName));
}
static void doc_CommandEnded(object sender, CommandEventArgs e)
{
(sender as AcadDocument).Editor.WriteMessage(string.Format("\nCommand {0} ended.\n", e.GlobalCommandName));
}
static void doc_CommandWillStart(object sender, CommandEventArgs e)
{
(sender as AcadDocument).Editor.WriteMessage(string.Format("\nCommand {0} will start.\n", e.GlobalCommandName));
}
}
The command MonitorCommandEvents as indicated will monitor all command related events such as CommandWillStart, CommandEnded, CommandCancelled, CommandFailed,and UnknownCommand for the active document.
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: MONITORCOMMANDEVENTS
Command MONITORCOMMANDEVENTS ended.
Command: C
CIRCLE
Command CIRCLE will start.
Specify center point for circle or [3P/2P/Ttr (tan tan radius)]:
Specify radius of circle or [Diameter]:
Command CIRCLE ended.
Command: C
CIRCLE
Command CIRCLE will start.
Specify center point for circle or [3P/2P/Ttr (tan tan radius)]: *Cancel*
Command CIRCLE cancelled.
Command: L
LINE
Command LINE will start.
Specify first point:
Specify next point or [Undo]:
Specify next point or [Undo]:
Command LINE ended.
Command: M
MOVE
Command MOVE will start.
Select objects: 1 found
Select objects:
Specify base point or [Displacement] <Displacement>:
Specify second point or <use first point as displacement>:
Command MOVE ended.
Command: ERASE
Command ERASE will start.
Select objects: 1 found
Select objects:
Command ERASE ended.
Command: DELETE
Command DELETE unknown.
Unknown command "DELETE". Press F1 for help.
Command:
As can be noticed, the notifications about CommandWillStart, CommandEnded, CommandCancelled and UnknownCommand have all been captured successfully. These four events are straightforward. However, the CommandFailed notification has not found its chance to fire. Tried various other commands but got no avail.
Command: SAVEAS
Command SAVEAS will start.
Current file format: AutoCAD 2010 Drawing
Enter file format
[R14(LT98<97)/2000(LT2000)/2004(LT2004)/2007(LT2007)/2010(LT2010)/Standards/DX
F/Template] <2010>:
aaaaaaaaaaaaaaaaaaaadkfjs//sdfjslkfsd\eu82472935729842``21#$@%@%^&^%&^@#$%
Invalid option keyword.
Current file format: AutoCAD 2010 Drawing
Enter file format
[R14(LT98<97)/2000(LT2000)/2004(LT2004)/2007(LT2007)/2010(LT2010)/Standards/DX
F/Template] <2010>: 33333333333333
Invalid option keyword.
Current file format: AutoCAD 2010 Drawing
Enter file format
[R14(LT98<97)/2000(LT2000)/2004(LT2004)/2007(LT2007)/2010(LT2010)/Standards/DX
F/Template] <2010>:
Save drawing as <C:\Temp\Test.dwg>: d#$%@#%@$%^@$%$%680953~////\\\\\\\
"d#$%@#%@$%^@$%$%680953~////\\\\\\\": Invalid file name.
Command SAVEAS ended.
Command CIRCLE will start.
Specify center point for circle or [3P/2P/Ttr (tan tan radius)]: 'move
** That command may not be invoked transparently **
Point or option keyword required.
Specify center point for circle or [3P/2P/Ttr (tan tan radius)]:
Point or option keyword required.
Specify center point for circle or [3P/2P/Ttr (tan tan radius)]:
Specify radius of circle or [Diameter] <6.3688>:
Command CIRCLE ended.
Command: HATCH
Command HATCH will start.
Pick internal point or [Select objects/seTtings]: s
Select objects or [picK internal point/seTtings]:1 found
Select objects or [picK internal point/seTtings]:t
Select objects or [picK internal point/seTtings]:1 found
Select objects or [picK internal point/seTtings]:
Command HATCH ended.
Command HATCH will start.
Pick internal point or [Select objects/seTtings]: Selecting everything...
Selecting everything visible...
Analyzing the selected data...
Valid hatch boundary not found.
Pick internal point or [Select objects/seTtings]: Selecting everything...
Selecting everything visible...
Analyzing the selected data...
Valid hatch boundary not found.
Pick internal point or [Select objects/seTtings]: *Cancel*
Command HATCH cancelled.
Command: EXTRUDE
Command EXTRUDE will start.
Current wire frame density: ISOLINES=4, Closed profiles creation mode = Solid
Select objects to extrude or [MOde]: 1 found
Select objects to extrude or [MOde]:
Specify height of extrusion or [Direction/Path/Taper angle/Expression]:
Command EXTRUDE ended.
If readers have experienced some command failure cases that will get the CommandFailed event fired out, please give me a chance to learn too. Thanks in advance. By the way, the sample code does not cover how to unsubscribe these events. I would like to leave it as an exercise to anybody having interest.
The leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) provides kinds of AutoCAD .NET Event Handler Wizards including an AutoCAD Document Event Wizard to help create start code of event handlers automatically, quickly, flexibly and reliably.
Posted by: |