From the class hierarchy we realize that a Group is not derived from Entity class. It is a DBObject and resides in a dictionary. So, to overrule the grips of a Group, we need to overrule the grips of entities that belong to the group. When a group is selected in AutoCAD, it displays the grips of the entities that belong to the group or a single grip, depending on the value set for the "GroupDisplayMode" system variable. The overruling demonstrated in this sample code requires the "GroupDisplayMode" system variable to be set to "0". In that case, AutoCAD attempts to display the grips of the all entities that are part of the group and our overruling then takes effect.
Here is a sample code and a screenshot of the result
static GripPointOverrule m_gripOverrule
= new GripPointOverrule();
static Overrule[] m_overrules = new Overrule[]
{
m_gripOverrule,
};
static bool m_overruleAdded = false;
[CommandMethod("GGO")]
static public void GroupGripOverrule()
{
if (! m_overruleAdded)
{
m_gripOverrule = new GripPointOverrule();
Overrule.AddOverrule
(
RXObject.GetClass(
typeof(Autodesk.AutoCAD.DatabaseServices.Entity)),
m_gripOverrule,
true
);
m_gripOverrule.SetCustomFilter();
Overrule.Overruling = true;
m_overruleAdded = true;
}
else
{
Overrule.RemoveOverrule
(
RXObject.GetClass(
typeof(Autodesk.AutoCAD.DatabaseServices.Entity)),
m_gripOverrule
);
Overrule.Overruling = false;
m_gripOverrule.Dispose();
m_gripOverrule = null;
m_overruleAdded = false;
}
Application.DocumentManager.MdiActiveDocument.Editor.Regen();
}
class GripPointOverrule : GripOverrule
{
internal GripPointOverrule() { }
internal class MyGrip : GripData
{
public MyGrip() { }
public override bool ViewportDraw
(
ViewportDraw vd,
ObjectId entityId,
GripData.DrawType type,
Point3d? imageGripPoint,
int gripSize
)
{
Point2d unit
= vd.Viewport.GetNumPixelsInUnitSquare(GripPoint);
vd.SubEntityTraits.Color = 2;
vd.SubEntityTraits.FillType = FillType.FillAlways;
double radius = gripSize / unit.X;
vd.Geometry.Circle
(GripPoint, radius, vd.Viewport.ViewDirection);
return true;
}
}
List<GripData> m_grips = new List<GripData>();
public override void GetGripPoints
(
Entity entity,
GripDataCollection grips,
double curViewUnitSize,
int gripSize,
Vector3d curViewDir,
GetGripPointsFlags bitFlags
)
{
// We do not want the default entity grips
//base.GetGripPoints(entity, grips, curViewUnitSize, gripSize, curViewDir, bitFlags);
// Find the extents of the entities in the group
Extents3d groupExts = new Extents3d();
using (Transaction tr
= entity.Database.TransactionManager.StartTransaction())
{
ObjectIdCollection ids = entity.GetPersistentReactorIds();
foreach (ObjectId id in ids)
{
DBObject obj = tr.GetObject(id, OpenMode.ForRead);
if (obj is Group)
{
Group group = obj as Group;
ObjectId[] entIds = group.GetAllEntityIds();
foreach (ObjectId entId in entIds)
{
Entity cogroupEnt =
tr.GetObject
(
entId,
OpenMode.ForRead
) as Entity;
if (cogroupEnt != null)
{
Extents3d exts
= cogroupEnt.GeometricExtents;
groupExts.AddExtents(exts);
}
}
}
}
tr.Commit();
}
// Add a single grip at the min point of the
// group extents
MyGrip myGripData = new MyGrip();
myGripData.GripPoint = groupExts.MinPoint;
m_grips.Add(myGripData);
grips.Add(myGripData);
}
public override void MoveGripPointsAt
(
Entity entity,
GripDataCollection grips,
Vector3d offset,
MoveGripPointsFlags bitFlags
)
{
foreach(GripData grip in grips)
{
MyGrip myGrip = grip as MyGrip;
if (myGrip != null)
{
entity.TransformBy(
Matrix3d.Displacement(offset));
}
else
base.MoveGripPointsAt(
entity, grips, offset, bitFlags);
}
}
// Overrule only entities that belong to a group
public override bool IsApplicable(RXObject overruledSubject)
{
bool isApplicable = false;
try
{
Entity ent = overruledSubject as Entity;
if (ent != null && ent.Database != null)
{
// Check if the entity is part of a group
using (Transaction tr
= ent.Database.TransactionManager.StartTransaction())
{
ObjectIdCollection ids
= ent.GetPersistentReactorIds();
foreach (ObjectId id in ids)
{
DBObject obj
= tr.GetObject(id, OpenMode.ForRead);
if (obj is Group)
{
Group group = obj as Group;
if (group != null)
{
// If the entity belongs to
// a group, we will overrule it
isApplicable = true;
}
}
}
tr.Commit();
}
}
}
catch (System.Exception ex)
{
Application.DocumentManager.MdiActiveDocument
.Editor.WriteMessage(ex.Message);
}
return isApplicable;
}
}
Recent Comments