//MyPlugin.cs
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Internal;
using Autodesk.AutoCAD.Runtime;
using acApp = Autodesk.AutoCAD.ApplicationServices.Application;
using System;
using System.IO;
using System.Reflection;
[assembly
: ExtensionApplication
(typeof(ToBrian
.LayerEvent))]
namespace ToStefan
{
class LayerEvent : IExtensionApplication
{
#region Properties
public static ObjectId ObjectType { get; internal set; }
#endregion
#region Initialization
void IExtensionApplication.Initialize()
{
DocumentCollection acDocs = acApp.DocumentManager;
Document doc = acDocs.MdiActiveDocument;
Editor ed = doc.Editor;
acDocs.DocumentCreated += OnDocumentCreated;
doc.CommandWillStart += OnCommandWillStart;
ed.WriteMessage("\n** " +
Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().Location)
+ " loaded ** \n");
}
void IExtensionApplication.Terminate()
{
}
#endregion
#region Methods
public static void OnCommandEnded(object sender, CommandEventArgs e)
{
Document doc = acApp.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
UnRegisterEvents(doc);
try
{
using (DocumentLock docLock = doc.LockDocument())
{
using (Transaction tr =
doc.Database.TransactionManager.StartOpenCloseTransaction())
{
if (ObjectType != null)
{
Entity ent =
(Entity)tr.GetObject(ObjectType, OpenMode.ForWrite) as Entity;
if (ent == null) return;
{
Line ln = (Line)ent as Line;
ln.Layer = "C-ROAD-LINE";
ln.ColorIndex = 256;
ed.WriteMessage("** Line Entity layer changed to \"C-SSWR-TEXT\" ** \n");
}
{
Dimension dm = (Dimension)ent as Dimension;
dm.Layer = "C-SSWR-TEXT";
dm.ColorIndex = 256;
ed.WriteMessage("** Dimension Entity layer changed to \"C-SSWR-TEXT\" ** \n");
}
{
Polyline poly = (Polyline)ent as Polyline;
poly.Layer = "C-ROAD-CURV";//C-SSWR-STRC
poly.ColorIndex = 256;
ed.WriteMessage("** Polyline Entity layer changed to \"C-ROAD-CURV\" ** \n");
}
{
Circle cr = (Circle)ent as Circle;
cr.Layer = "C-SSWR-STRC";
cr.ColorIndex = 256;
ed.WriteMessage("** Circle Entity layer changed to \"C-SSWR-STRC\" ** \n");
}
if (ent
is BlockReference
) {
BlockReference br = (BlockReference)ent as BlockReference;
br.Layer = "0";
br.ColorIndex = 5;
ed.WriteMessage("** BlockReference Entity layer changed to \"0\" color blue** \n");
}
// ed.WriteMessage("** Entity layer changed to \"C-ROAD-INTS-TEXT\" ** \n");
}
tr.Commit();
}
}
}
catch (System.Exception ex)
{
ed.WriteMessage("\nSystem.Exception error: " + ex.Message);
}
}
public static void OnCommandWillStart(object sender, CommandEventArgs e)
{
string cmd = e.GlobalCommandName.ToString().ToUpper();
if (cmd != null && Utils.WcMatch(cmd, "*DIM*,LINE,PLINE,CIRCLE,INSERT"))
RegisterEvents(acApp.DocumentManager.MdiActiveDocument);
}
public static void OnDocumentCreated(Object sender, DocumentCollectionEventArgs e)
{
Document doc = e.Document;
if (doc != null)
doc.CommandWillStart += OnCommandWillStart;
}
public static void OnObjectAppended(object sender, ObjectEventArgs e)
{
ObjectId id = e.DBObject.ObjectId;
if (id != null)
ObjectType = id;
}
public static void RegisterEvents(Document doc)
{
if (doc != null)
{
Editor ed = doc.Editor;
// command events
doc.CommandCancelled += OnCommandEnded;
doc.CommandEnded += OnCommandEnded;
doc.CommandFailed += OnCommandEnded;
// database events
doc.Database.ObjectAppended += OnObjectAppended;
ed.WriteMessage("\n** ObjectType events registered ** \n");
}
}
public static void UnRegisterEvents(Document doc)
{
if (doc != null)
{
Editor ed = doc.Editor;
// command events
doc.CommandCancelled -= OnCommandEnded;
doc.CommandEnded -= OnCommandEnded;
doc.CommandFailed -= OnCommandEnded;
// database events
doc.Database.ObjectAppended -= OnObjectAppended;
ed.WriteMessage("\n** ObjectType events unregistered ** \n");
}
}
#endregion
}
}