.NET do have a PromptStatus.Cancel but most of the time checking for PromptStatus.OK is enough:
result != PromptStatus.OK means result == PromptStatus.Cancel
In the upper code the jigged block reference is not added to the database before the Drag ends. If the Drag status is PromptStatus.OK, the block is added to the database, else (i.e. the user cancelled the drag operation) the block is disposed (using statement).
Going a little further with an attributed block we want to drag the attributes too (inspired by
this Kean's thread).
The BlockAttribJig class inherits from the upper BlockJig class it uses a little helper to store some text geometry infos.
public class BlockAttribJig : BlockJig
{
private Dictionary<AttributeReference, TextInfo> attRefInfos;
private Transaction tr;
public BlockAttribJig(BlockReference br, string message, Dictionary<AttributeReference, TextInfo> attInfos)
: base(br, message)
{
attRefInfos = attInfos;
tr = br.Database.TransactionManager.TopTransaction;
}
protected override bool Update()
{
base.Update();
foreach (var entry in attRefInfos)
{
var att = entry.Key;
var info = entry.Value;
att.Position = info.Position.TransformBy(blockReference.BlockTransform);
if (info.IsAligned)
{
att.AlignmentPoint = info.Alignment.TransformBy(blockReference.BlockTransform);
att.AdjustAlignment(blockReference.Database);
}
if (att.IsMTextAttribute)
{
att.UpdateMTextAttribute();
}
}
return true;
}
}
public class TextInfo
{
public Point3d Position { get; private set; }
public Point3d Alignment { get; private set; }
public bool IsAligned { get; private set; }
public double Rotation { get; private set; }
public TextInfo(DBText text)
{
Position = text.Position;
IsAligned = text.Justify != AttachmentPoint.BaseLeft;
Alignment = text.AlignmentPoint;
Rotation = text.Rotation;
}
}
In this case, the block and its attributes have to be added to the database before the drag operation, so, if the drag result is PromptStatus.Cancel, we need to erase the block.
[CommandMethod("Cmd2")]
public void Cmd2()
{
var doc = AcAp.DocumentManager.MdiActiveDocument;
var db = doc.Database;
var ed = doc.Editor;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
if (!bt.Has("bloc-att"))
{
AcAp.ShowAlertDialog("Block 'bloc-att' not found");
return;
}
var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
// create a block reference and add it to the current space
var br
= new BlockReference
(Point3d
.Origin, bt
["bloc-att"]); br.TransformBy(ed.CurrentUserCoordinateSystem);
curSpace.AppendEntity(br);
tr.AddNewlyCreatedDBObject(br, true);
// add the attribute references to the block reference and fill a dictionary with attributes text infos
var btr = (BlockTableRecord)tr.GetObject(bt["bloc-att"], OpenMode.ForRead);
var attInfos
= new Dictionary
<AttributeReference, TextInfo
>(); foreach (ObjectId id in btr)
{
if (id.ObjectClass.Name == "AcDbAttributeDefinition")
{
var attDef = (AttributeDefinition)tr.GetObject(id, OpenMode.ForRead);
AttributeReference attRef
= new AttributeReference
(); attRef.SetAttributeFromBlock(attDef, br.BlockTransform);
br.AttributeCollection.AppendAttribute(attRef);
tr.AddNewlyCreatedDBObject(attRef, true);
attInfos
.Add(attRef,
new TextInfo
(attDef
)); }
}
var jig
= new BlockAttribJig
(br,
"\nInsertion point: ", attInfos
); var result = ed.Drag(jig);
// erase the block reference if the user cancelled
if (result.Status == PromptStatus.Cancel)
{
br.Erase();
}
tr.Commit();
}
}