using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Gi = Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.Colors;
namespace testgrips
{
public class ImplementClass : IExtensionApplication
{
public void Initialize()
{
if (Application.DocumentManager.MdiActiveDocument == null)
return;
ObjectOverrule.AddOverrule(RXClass.GetClass(typeof(Polyline)), CableLineGripOverruleClass.Instance, true);
}
public void Terminate() { }
}
public class CableLineGripOverruleClass : GripOverrule
{
public static CableLineGripOverruleClass Instance = new CableLineGripOverruleClass();
public override void GetGripPoints(Entity entity, GripDataCollection grips, double curViewUnitSize, int gripSize, Vector3d curViewDir, GetGripPointsFlags bitFlags)
{
Polyline pline = (Polyline)entity;
if (pline.NumberOfVertices > 0)
{
Point3d pnt1 = pline.StartPoint;
MoveGripClass grip1 = new MoveGripClass(pnt1);
grips.Add(grip1);
Point3d pnt2 = pline.EndPoint;
MoveGripClass grip2 = new MoveGripClass(pnt2);
grips.Add(grip2);
}
}
public override void MoveGripPointsAt(Entity _ent, GripDataCollection grips, Vector3d offset, MoveGripPointsFlags bitFlags)
{
if (grips.Count == 0) return;
Polyline pline = (Polyline)_ent;
Point3d pnt = grips[0].GripPoint;
Point3d pntm = grips[0].GripPoint + offset;
Point2d pnt2 = new Point2d(pntm.X, pntm.Y);
int n = pline.NumberOfVertices;
if (pnt == pline.StartPoint)
pline.SetPointAt(0, pnt2);
if (pnt == pline.EndPoint)
pline.SetPointAt(n - 1, pnt2);
}
}
public class MoveGripClass : GripData
{
public MoveGripClass(Point3d _point) : base()
{
GripPoint = _point;
}
public override bool ViewportDraw(Gi.ViewportDraw _wd, ObjectId _entid, DrawType _type, Point3d? _imageGripPoint, int _gripSizeInPixels)
{
Point2d unit = _wd.Viewport.GetNumPixelsInUnitSquare(GripPoint);
double dR = 1 / unit.X;
double dr = dR / 3.0;
Point3dCollection pnts = new Point3dCollection();
pnts.Add(new Point3d(GripPoint.X - 1, GripPoint.Y - 1, 0));
pnts.Add(new Point3d(GripPoint.X, GripPoint.Y + 1, 0));
pnts.Add(new Point3d(GripPoint.X + 1, GripPoint.Y - 1, 0));
_wd.SubEntityTraits.FillType = Gi.FillType.FillAlways;
_wd.SubEntityTraits.Color = 1;
_wd.Geometry.Polygon(pnts);
_wd.SubEntityTraits.FillType = Gi.FillType.FillNever;
_wd.SubEntityTraits.TrueColor = new EntityColor(0, 0, 0);
_wd.Geometry.Polygon(pnts);
return true;
}
}
}