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 LayoutManager 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 LayoutManagerEvents
{
[CommandMethod("TestLayoutManagerEvents")]
public void TestLayoutManagerEvents_Method()
{
LayoutManagerEvents layoutManEvents1 = new LayoutManagerEvents();
layoutManEvents1.Register();
}
public void Register()
{
LayoutManager.Current.AbortLayoutRename += new LayoutRenamedEventHandler(Current_AbortLayoutRename);
LayoutManager.Current.LayoutRenamed += new LayoutRenamedEventHandler(Current_LayoutRenamed);
LayoutManager.Current.LayoutToBeRenamed += new LayoutRenamedEventHandler(Current_LayoutToBeRenamed);
}
void Current_LayoutToBeRenamed(object sender, LayoutRenamedEventArgs e)
{
AcadApplication.DocumentManager.MdiActiveDocument.Editor.WriteMessage(string.Format("\n{0}: {1}\n", "LayoutToBeRenamed", e.Name));
}
void Current_LayoutRenamed(object sender, LayoutRenamedEventArgs e)
{
AcadApplication.DocumentManager.MdiActiveDocument.Editor.WriteMessage(string.Format("\n{0}: {1}\n", "LayoutRenamed", e.Name));
}
void Current_AbortLayoutRename(object sender, LayoutRenamedEventArgs e)
{
AcadApplication.DocumentManager.MdiActiveDocument.Editor.WriteMessage(string.Format("\n{0}: {1}\n", "AbortLayoutRename", e.Name));
}
}
}
After the command TestLayoutManagerEvents runs, layout rename related events will be monitored such as LayoutToBeRenamed, LayoutRenamed, and AbortLayoutRename.
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: TestLayoutManagerEvents
Command: *Cancel*
Command: *Cancel*
Command: <Switching to: Layout1>
Regenerating layout.
Regenerating model - caching viewports.
Command: *Cancel*
Command: *Cancel*
Command: <Rename layout>
Command: *Cancel*
Command: *Cancel*
Command: <Rename layout>
LayoutToBeRenamed: Layout1
LayoutRenamed: Layout1
Command: *Cancel*
Command: *Cancel*
Command: <Switching to: Layout2>
Regenerating layout.
Regenerating model - caching viewports.
Command: *Cancel*
Command: *Cancel*
Command: <Rename layout>
Command: *Cancel*
Command: *Cancel*
Command: <Rename layout>
LayoutToBeRenamed: Layout2
LayoutRenamed: Layout2
Command: *Cancel*
Command: *Cancel*
Command: <Switching to: Model>
Restoring cached viewports.
As can be noticed, the events LayoutToBeRenamed and LayoutRenamed were fired off as expected.
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 LayoutManager Event Wizard to help create start code of event handlers automatically, quickly, flexibly and reliably.
Recent Comments