using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using AcDb = Autodesk.AutoCAD.DatabaseServices;
namespace AcadTest
{
public class PlineDrawOverrule : DrawableOverrule
{
public override bool WorldDraw(Drawable drawable, WorldDraw wd)
{
AcDb.Polyline pline = (AcDb.Polyline)drawable;
ObjectId plineId = pline.Id;
if (plineId.IsNull)
{
Commands.CopiesDict.TryGetValue(pline, out plineId);
EntityColor oldCol = wd.SubEntityTraits.TrueColor;
AcDb.LineWeight oldLw = wd.SubEntityTraits.LineWeight;
short index = (short)(plineId.IsNull ? 1 : 3);
wd.SubEntityTraits.TrueColor = new EntityColor(ColorMethod.ByAci, index);
wd.SubEntityTraits.LineWeight = AcDb.LineWeight.LineWeight050;
for (int i = 1; i < pline.NumberOfVertices; i++)
{
Point3d
ptEnd = pline.GetPoint3dAt(i),
ptStart = pline.GetPoint3dAt(i - 1);
wd.Geometry.WorldLine(ptStart, ptEnd);
}
wd.SubEntityTraits.TrueColor = oldCol;
wd.SubEntityTraits.LineWeight = oldLw;
return true;
}
else
{
return base.WorldDraw(drawable, wd);
}
}
}
public class Commands
{
static RXClass m_plineClass = RXObject.GetClass(typeof(AcDb.Polyline));
static List<ObjectId> m_prevSelIds = new List<ObjectId>();
static PlineDrawOverrule m_drawOverrule;
internal static Dictionary<DBObject, ObjectId>
CopiesDict = new Dictionary<DBObject, ObjectId>();
[CommandMethod("TOG")]
public static void ToggleOverrule()
{
Document adoc = Application.DocumentManager.MdiActiveDocument;
Editor ed = adoc.Editor;
if (m_drawOverrule == null)
{
m_drawOverrule = new PlineDrawOverrule();
Overrule.AddOverrule(m_plineClass, m_drawOverrule, false);
Overrule.Overruling = true;
ed.SelectionAdded += Ed_SelectionAdded;
Application.Idle += Application_Idle;
ed.WriteMessage("\nOverrule on\n");
}
else
{
Overrule.RemoveOverrule(m_plineClass, m_drawOverrule);
m_drawOverrule = null;
ed.SelectionAdded -= Ed_SelectionAdded;
Application.Idle -= Application_Idle;
ed.WriteMessage("\nOverrule off\n");
}
ed.Regen();
}
private static void Application_Idle(object sender, EventArgs e)
{
Document adoc = Application.DocumentManager.MdiActiveDocument;
if (adoc is null
|| (string.IsNullOrEmpty(adoc.CommandInProgress)
&& adoc.Editor.SelectImplied().Status != PromptStatus.OK))
{
ClearBuffer();
}
}
private static void Ed_SelectionAdded(object sender, SelectionAddedEventArgs e)
{
ObjectId[] curSelIds = e.AddedObjects
.GetObjectIds()
.Where(id => id.IsValid && id.ObjectClass.Equals(m_plineClass))
.Except(m_prevSelIds)
.ToArray();
foreach (ObjectId id in curSelIds)
{
using (DBObject obj = id.Open(OpenMode.ForRead))
{
obj.Copied += Obj_Copied;
}
}
m_prevSelIds.AddRange(curSelIds);
}
private static void ClearBuffer()
{
foreach (ObjectId id in m_prevSelIds)
{
using (DBObject obj = id.Open(OpenMode.ForRead))
{
obj.Copied -= Obj_Copied;
}
}
m_prevSelIds.Clear();
CopiesDict.Clear();
}
private static void Obj_Copied(object sender, ObjectEventArgs e)
{
CopiesDict[e.DBObject] = ((DBObject)sender).Id;
}
}
}