have a look here.. its been a long time since I've done c# soooo ...
command
using System.Collections.Generic;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
[assembly: CommandClass(typeof(CsMgdAcad1.Commands))]
[assembly: ExtensionApplication(typeof(CsMgdAcad1.ExtensionApp))]
namespace CsMgdAcad1
{
public class ExtensionApp : Autodesk.AutoCAD.Runtime.IExtensionApplication
{
static DocEvents loader = new DocEvents();
public void Initialize()
{
try
{
loader.Do();
}
catch(System.Exception ex)
{
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.
MdiActiveDocument.Editor.WriteMessage(ex.Message);
}
}
public void Terminate()
{
loader.Undo();
}
}
//
public static class Commands
{
[CommandMethod("addstart", CommandFlags.Modal)]
public static void naddstart()
{
foreach(KeyValuePair<Document,CmdEvents> item in DocEvents.docs)
{
item.Value.DoStart();
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.
MdiActiveDocument.Editor.WriteMessage(item.Key.Name);
}
}
[CommandMethod("killstart", CommandFlags.Modal)]
public static void nkillstart()
{
foreach (KeyValuePair<Document, CmdEvents> item in DocEvents.docs)
{
item.Value.UndoStart();
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.
MdiActiveDocument.Editor.WriteMessage(item.Key.Name);
}
}
[CommandMethod("addend", CommandFlags.Modal)]
public static void naddend()
{
foreach(KeyValuePair<Document,CmdEvents> item in DocEvents.docs)
{
item.Value.DoEnd();
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.
MdiActiveDocument.Editor.WriteMessage(item.Key.Name);
}
}
[CommandMethod("killend", CommandFlags.Modal)]
public static void nkillend()
{
foreach (KeyValuePair<Document, CmdEvents> item in DocEvents.docs)
{
item.Value.UndoEnd();
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.
MdiActiveDocument.Editor.WriteMessage(item.Key.Name);
}
}
}
}
doc reactor
using System.Collections.Generic;
using Autodesk.AutoCAD.ApplicationServices;
using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;
namespace CsMgdAcad1
{
public class DocEvents
{
public static Dictionary<Document, CmdEvents> docs = new Dictionary<Document, CmdEvents>();
private bool m_bDone;
//
public DocEvents()
{
m_bDone = false;
}
public void Do()
{
if (m_bDone)
return;
m_bDone = true;
try
{
foreach (Document doc in acadApp.DocumentManager)
{
if (!docs.ContainsKey(doc))
{
CmdEvents evnt = new CmdEvents(doc);
evnt.DoStart();
evnt.DoEnd();
evnt.DoCancel();
docs.Add(doc, evnt);
}
}
acadApp.DocumentManager.DocumentCreated +=
new DocumentCollectionEventHandler(onDocumentCreated);
acadApp.DocumentManager.DocumentToBeDestroyed +=
new DocumentCollectionEventHandler(onDocumentToBeDestroyed);
}
catch (System.Exception ex)
{
acadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.Message);
}
}
public void Undo()
{
if (!m_bDone)
return;
try
{
foreach (Document doc in acadApp.DocumentManager)
{
if (docs.ContainsKey(doc))
{
docs[doc].UndoStart();
docs[doc].UndoEnd();
docs[doc].UndoCancel();
docs.Remove(doc);
}
}
acadApp.DocumentManager.DocumentCreated -=
new DocumentCollectionEventHandler(onDocumentCreated);
acadApp.DocumentManager.DocumentToBeDestroyed -=
new DocumentCollectionEventHandler(onDocumentToBeDestroyed);
}
catch (System.Exception ex)
{
acadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.Message);
}
m_bDone = false;
}
static void onDocumentCreated(object sender, DocumentCollectionEventArgs e)
{
if (!docs.ContainsKey(e.Document))
{
CmdEvents evnt = new CmdEvents(e.Document);
evnt.DoStart();
evnt.DoEnd();
evnt.DoCancel();
docs.Add(e.Document, evnt);
}
}
static void onDocumentToBeDestroyed(object sender, DocumentCollectionEventArgs e)
{
if (docs.ContainsKey(e.Document))
{
docs[e.Document].UndoStart();
docs[e.Document].UndoEnd();
docs[e.Document].UndoCancel();
docs.Remove(e.Document);
}
}
}
}
command reactor
using Autodesk.AutoCAD.ApplicationServices;
using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;
namespace CsMgdAcad1
{
public class CmdEvents
{
private bool m_Start;
private bool m_End;
private bool m_Cancel;
private Document m_doc;
//
public CmdEvents(Document doc)
{
m_Start = false;
m_End = false;
m_Cancel = false;
m_doc = doc;
}
public void DoStart()
{
if (m_Start)
return;
m_Start = true;
m_doc.CommandWillStart +=
new CommandEventHandler(MdiActiveDocument_CommandWillStart);
}
public void DoEnd()
{
if (m_End)
return;
m_End = true;
m_doc.CommandEnded +=
new CommandEventHandler(MdiActiveDocument_CommandEnded);
}
public void DoCancel()
{
if (m_Cancel)
return;
m_Cancel = true;
m_doc.CommandCancelled +=
new CommandEventHandler(MdiActiveDocument_CommandCancelled);
}
public void UndoStart()
{
if (!m_Start)
return;
m_doc.CommandWillStart -=
new CommandEventHandler(MdiActiveDocument_CommandWillStart);
m_Start = false;
}
public void UndoEnd()
{
if (!m_End)
return;
m_doc.CommandEnded -=
new CommandEventHandler(MdiActiveDocument_CommandEnded);
m_End = false;
}
public void UndoCancel()
{
if (!m_Cancel)
return;
m_doc.CommandCancelled -=
new CommandEventHandler(MdiActiveDocument_CommandCancelled);
m_Cancel = false;
}
private void MdiActiveDocument_CommandWillStart(object sender, CommandEventArgs e)
{
m_doc.Editor.WriteMessage("\n*start*\n");
}
private void MdiActiveDocument_CommandEnded(object sender, CommandEventArgs e)
{
m_doc.Editor.WriteMessage("\n*nend*\n");
}
private void MdiActiveDocument_CommandCancelled(object sender, CommandEventArgs e)
{
m_doc.Editor.WriteMessage("\n*ncancel*\n");
}
}
}