Another big thank you to Jeremy Tammik, from our DevTech team in Europe, for providing this elegant sample. This is another one Jeremy presented at the recent advanced custom entity workshop in Prague. I have added some initial commentary as well as some steps to see the code working. Jeremy also provided the code for the last post.
We sometimes want to stop entities from being modified in certain ways, and there are a few different approaches possible, for instance: at the simplest - and least granular - level, we can place entities on locked layers or veto certain commands using an editor reactor. Or we can go all-out and implement custom objects that have complete control over their behaviour. The below technique provides a nice balance between control and simplicity: it makes use of a Document event to check when a particular command is being called, a Database event to cache the information we wish to restore and finally another Document event to restore it. In this case it's all about location (or should I say "location, location, location" ? :-). We're caching an object's state before the MOVE command (which changes an object's position in the model), but if we wanted to roll back the effect of other commands, we would probably want to cache other properties.
Here's the C# code:
/// Reactor command.
/// Demonstrate a simple object reactor, as well as
/// cascaded event handling.
/// In this sample, the MOVE command is cancelled for
/// all red circles. This is achieved by attaching an
/// editor reactor and watching for the MOVE command begin.
/// When triggered, the reactor attaches an object reactor
/// to the database and watches for red circles. If any are
/// detected, their object id and original position are
/// stored. When the command ends, the positions are
/// restored and the object reactor removed again.
/// Reactors create overhead, so we should add them only
/// when needed and remove them as soon as possible
public class CmdReactor
private static Document _doc;
private static ObjectIdCollection _ids =
private static Point3dCollection _pts =
static public void Reactor()
static void doc_CommandWillStart(
if (e.GlobalCommandName == "MOVE")
static void removeEventHandlers()
static void _doc_CommandEnded(
// Remove database reactor before restoring positions
static void _db_ObjectOpenedForModify(
Circle circle = e.DBObject as Circle;
if (null != circle && 1 == circle.ColorIndex)
// In AutoCAD 2007, OpenedForModify is called only
// once by MOVE.
// In 2008, OpenedForModify is called multiple
// times by the MOVE command ... we are only
// interested in the first call, because
// in the second one, the object location
// has already been changed:
static void rollbackLocations()
_ids.Count == _pts.Count,
"Expected same number of ids and locations"
Transaction t =
int i = 0;
foreach (ObjectId id in _ids)
Circle circle =
t.GetObject(id, OpenMode.ForWrite) as Circle;
circle.Center = _pts[i++];
To see the code at work, draw some circles and make some of them red:
Now run the REACTOR command and try to MOVE all the circles:
Although all the circles are dragged during the move, once we complete the command we can see that the red circles have remained in the same location (or have, in fact, had their location rolled back). The other circles have been moved, as expected.