using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Reflection;
namespace AcadTest
{
public class ImplementClass
{
[CommandMethod("TestAddGripOverrule")]
public void AddOverrule()
{
Overrule.AddOverrule
(RXClass.GetClass(typeof(Line)),
LineGripOverrule.Instance, true);
}
}
public class LineGripOverrule: GripOverrule
{
static LineGripOverrule m_instance = new LineGripOverrule();
public static LineGripOverrule Instance => m_instance;
LineGripOverrule() { }
public override void GetGripPoints
(Entity entity, GripDataCollection grips,
double curViewUnitSize, int gripSize,
Vector3d curViewDir, GetGripPointsFlags bitFlags)
{
if (entity is Line line)
{
PropertyInfo entMapPropInfo =
GetType().GetProperty
("EntityMap",
BindingFlags.Instance
| BindingFlags.NonPublic);
if (entMapPropInfo != null)
{
var entityMap = entMapPropInfo.GetValue(this)
as Dictionary<IntPtr, List<GripData>>;
if (entityMap.ContainsKey(line.UnmanagedObject))
{
entityMap.Remove(line.UnmanagedObject);
}
}
Point3d pnt1 = line.StartPoint;
MoveGripData grip1 = new MoveGripData(pnt1, PointType.Start);
grips.Add(grip1);
Point3d pnt2 = line.EndPoint;
MoveGripData grip2 = new MoveGripData(pnt2, PointType.End);
grips.Add(grip2);
}
}
public override void MoveGripPointsAt
(Entity entity, GripDataCollection grips,
Vector3d offset, MoveGripPointsFlags bitFlags)
{
if (entity is Line line)
{
foreach (GripData grip in grips)
{
if (grip is MoveGripData myGrip)
{
Point3d newPos3d = myGrip.GripPoint + offset;
switch (myGrip.Type)
{
case PointType.Start:
line.StartPoint = newPos3d;
break;
case PointType.End:
line.EndPoint = newPos3d;
break;
}
}
}
}
}
}
public class MoveGripData: GripData
{
public PointType Type;
public MoveGripData(Point3d _point, PointType ptType) : base()
{
GripPoint = _point;
Type = ptType;
}
public override bool ViewportDraw
(ViewportDraw worldDraw, ObjectId entityId,
GripData.DrawType type, Point3d? imageGripPoint,
int gripSizeInPixels)
{
// Calculate the size of the glyph in WCS
Point2d glyphSize = worldDraw.Viewport
.GetNumPixelsInUnitSquare(this.GripPoint);
double glyphHeight = (gripSizeInPixels / glyphSize.Y);
double glyphWeight = (gripSizeInPixels / glyphSize.X);
Matrix3d mx3d = worldDraw.Viewport.ModelToEyeTransform;
Point3d pt = this.GripPoint.TransformBy(mx3d);
// Draw a glyph
Point3dCollection pnts = new Point3dCollection();
// rectangle
pnts.Add(new Point3d(pt.X - glyphWeight, pt.Y - glyphHeight, pt.Z));
pnts.Add(new Point3d(pt.X + glyphWeight, pt.Y - glyphHeight, pt.Z));
pnts.Add(new Point3d(pt.X + glyphWeight, pt.Y + glyphHeight, pt.Z));
pnts.Add(new Point3d(pt.X - glyphWeight, pt.Y + glyphHeight, pt.Z));
pnts.Add(new Point3d(pt.X - glyphWeight, pt.Y - glyphHeight, pt.Z));
worldDraw.Geometry.PolygonEye(pnts);
return base.ViewportDraw
(worldDraw, entityId, type, imageGripPoint, gripSizeInPixels);
}
}
public enum PointType
{
Start,
End,
}
}