using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
namespace testgrips
{
public class ImplementClass
{
[CommandMethod("TestGripOverrule")]
public void AddOverrule()
{
ObjectOverrule.AddOverrule(RXClass.GetClass(typeof(Line)), LineGripOverruleClass.Instance, true);
}
}
public class LineGripOverruleClass : GripOverrule
{
static LineGripOverruleClass _instance = new LineGripOverruleClass();
static Dictionary<ObjectId, bool> afterGripMoveProcessedDict = new Dictionary<ObjectId, bool>();
public static LineGripOverruleClass Instance => _instance;
LineGripOverruleClass() { }
public override void GetGripPoints
(Entity entity, GripDataCollection grips,
double curViewUnitSize, int gripSize,
Vector3d curViewDir, GetGripPointsFlags bitFlags)
{
if (entity is Line line)
{
if (afterGripMoveProcessedDict.ContainsKey(line.Id))
{
if (afterGripMoveProcessedDict[line.Id])
{
afterGripMoveProcessedDict.Remove(line.Id);
return;
}
else
{
afterGripMoveProcessedDict[line.Id] = true;
}
}
Point3d pnt1 = line.StartPoint;
MoveGripClass grip1 = new MoveGripClass(pnt1, PointType.Start);
grips.Add(grip1);
Point3d pnt2 = line.EndPoint;
MoveGripClass grip2 = new MoveGripClass(pnt2, PointType.End);
grips.Add(grip2);
}
}
public override void MoveGripPointsAt
(Entity entity, GripDataCollection grips,
Vector3d offset, MoveGripPointsFlags bitFlags)
{
if (entity is Line line)
{
if (SupportClass.IsCivil2018 && line.Id.IsValid)
{
afterGripMoveProcessedDict[line.Id] = false;
}
foreach (GripData grip in grips)
{
if (grip is MoveGripClass 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 MoveGripClass : GripData
{
public PointType Type;
public MoveGripClass(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);
}
}
static class SupportClass
{
public static bool IsCivil2018 =
IsCivilApplication()
&& Application.Version.Major == 22
&& Application.Version.Minor == 0;
/// <summary>
/// Является ли запущенный AutoCAD Civil'ом
/// </summary>
public static bool IsCivilApplication()
{
string rootKeyNumber = null;
Version ver = Application.Version;
bool ret;
if (ver.Major == 19 && ver.Minor == 1)
rootKeyNumber = "D000";
else if (ver.Major == 20 && ver.Minor == 0)
rootKeyNumber = "E000";
else if (ver.Major == 20 && ver.Minor == 1)
rootKeyNumber = "F000";
else if (ver.Major == 21)
rootKeyNumber = "0000";
else if (ver.Major == 22 && ver.Minor == 0)
rootKeyNumber = "1000";
if (string.IsNullOrEmpty(rootKeyNumber))
{
ret = false;
}
else
{
ret = HostApplicationServices.Current
.MachineRegistryProductRootKey.Contains(rootKeyNumber);
}
return ret;
}
}
public enum PointType
{
Start,
End,
}
}