In a more conventional way (entities are added to the Database only if the Jig result is OK).
[CommandMethod("TEST")]
public static void Test()
{
var doc = AcAp.DocumentManager.MdiActiveDocument;
var db = doc.Database;
var ed = doc.Editor;
var options = new PromptPointOptions("\nFirst point: ");
var result = ed.GetPoint(options);
if (result.Status != PromptStatus.OK) return;
var pt1 = result.Value;
options.Message = "\nSecond point: ";
options.UseBasePoint = true;
options.BasePoint = pt1;
result = ed.GetPoint(options);
if (result.Status != PromptStatus.OK) return;
var pt2 = result.Value;
var fence = new Point3dCollection(new[] { pt1, pt2 });
var filter = new SelectionFilter(new[] { new TypedValue(0, "LINE,LWPOLYLINE") });
var selection = ed.SelectFence(fence, filter);
if (selection.Status != PromptStatus.OK) return;
using (var tr = db.TransactionManager.StartTransaction())
using (var line = new Line(pt1, pt2))
{
line.TransformBy(ed.CurrentUserCoordinateSystem);
var points = new Point3dCollection();
foreach (ObjectId id in selection.Value.GetObjectIds())
{
var curve = (Curve)tr.GetObject(id, OpenMode.ForRead);
curve.IntersectWith(line, Intersect.OnBothOperands, points, IntPtr.Zero, IntPtr.Zero);
}
var dims = new List<AlignedDimension>();
for (int i = 0; i < points.Count - 1; i++)
{
var dim = new AlignedDimension(points[i], points[i + 1], points[i], "", db.Dimstyle);
dim.SetDatabaseDefaults();
dims.Add(dim);
}
var jig = new AlignedDimensionsJig(dims);
var pr = ed.Drag(jig);
if (pr.Status == PromptStatus.OK)
{
var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
foreach (var dim in dims)
{
curSpace.AppendEntity(dim);
tr.AddNewlyCreatedDBObject(dim, true);
}
}
else
{
foreach (var dim in dims)
{
dim.Dispose();
}
}
tr.Commit();
}
}
class AlignedDimensionsJig : DrawJig
{
List<AlignedDimension> dims;
Point3d dragPoint;
public AlignedDimensionsJig(List<AlignedDimension> dims)
{ this.dims = dims; }
protected override SamplerStatus Sampler(JigPrompts prompts)
{
var options = new JigPromptPointOptions("\nSpecify Dimension Line Location: ");
options.UserInputControls =
UserInputControls.Accept3dCoordinates | UserInputControls.GovernedByUCSDetect;
var result = prompts.AcquirePoint(options);
if (result.Value.IsEqualTo(dragPoint)) return SamplerStatus.NoChange;
dragPoint = result.Value;
return SamplerStatus.OK;
}
protected override bool WorldDraw(WorldDraw draw)
{
var geometry = draw.Geometry;
if (geometry != null)
{
foreach (var dim in dims)
{
dim.DimLinePoint = dragPoint;
geometry.Draw(dim);
}
}
return true;
}
}