public class ConnectionGripOverrule : GripOverrule
{
static public ConnectionGripOverrule theOverrule;
static HashSet<ObjectId> mCurves = new HashSet<ObjectId>();
static HashSet<ObjectId> mBlkRefs = new HashSet<ObjectId>();
static ObjectId CurrentId;
ConnectionGripOverrule() { }
public static void StartIfNotStarted()
{
if (theOverrule == null)
{
theOverrule = new ConnectionGripOverrule();
ObjectOverrule.AddOverrule(RXClass.GetClass(typeof(BlockReference)), ConnectionGripOverrule.theOverrule, true);
GripOverrule.Overruling = true;
}
}
public static void AddTrackGeometry(ObjectId curveId)
{
if (!mCurves.Contains(curveId) && curveId != ObjectId.Null)
{
mCurves.Add(curveId);
}
}
public static void AddTrackingBlkRec(ObjectId brId)
{
if (!mBlkRefs.Contains(brId) && brId != ObjectId.Null)
{
mBlkRefs.Add(brId);
ObjectId[] idArray = new ObjectId[mBlkRefs.Count];
mBlkRefs.CopyTo(idArray);
theOverrule.SetIdFilter(idArray);
}
}
public override void GetGripPoints(Entity entity, Point3dCollection gripPoints, IntegerCollection snapModes, IntegerCollection geometryIds)
{
CurrentId = entity.ObjectId;
base.GetGripPoints(entity, gripPoints, snapModes, geometryIds);
}
public override void MoveGripPointsAt(Entity entity, GripDataCollection grips, Vector3d offset, MoveGripPointsFlags bitFlags)
{
base.MoveGripPointsAt(entity, grips, offset, bitFlags);
}
public override void MoveGripPointsAt(Entity entity, IntegerCollection indices, Vector3d offset)
{
if (indices.Count > 0 && indices[0] == 0)
{
BlockReference pt = entity as BlockReference;
if (pt != null && mBlkRefs.Contains(CurrentId))
{
Database db = HostApplicationServices.WorkingDatabase;
bool found = false;
OpenCloseTransaction tr = db.TransactionManager.StartOpenCloseTransaction();
using (tr)
{
foreach (ObjectId curId in mCurves)
{
DBObject obj = tr.GetObject(curId, OpenMode.ForRead);
Curve cur = obj as Curve;
if (cur != null)
{
Point3d ptOffset = pt.Position + offset;
Point3d ptOnCurve = cur.GetClosestPointTo(ptOffset, false);
Vector3d dist = ptOnCurve - ptOffset;
if (!dist.IsZeroLength(Tolerance.Global) && dist.Length - offset.Length <2000)
{
Matrix3d mat = Matrix3d.Displacement(-dist);
Point3d pos = cur.GetClosestPointTo(ptOffset.TransformBy(mat), false);
pt.Position = pos;
found = true;
break;
}
}
}
if (!found)
{
base.MoveGripPointsAt(entity, indices, offset);
}
}
}
else
{
base.MoveGripPointsAt(entity, indices, offset);
}
}
else
{
base.MoveGripPointsAt(entity, indices, offset);
}
}
}