1) use ed.GetNestedEntity to pick the start pt of the leader, this also supplies the pickpoint.
Then find the nearest point to the entity you picked. (or set the nearest snap-a bit dodgy).
2)Start the leader jig
public class LeaderJig : EntityJig
{
private Point3d P1,P2,EndP,endPointTemp,Pt1,Pt2;
private PromptPointResult dres;
private Matrix3d ucs;
private Plane pn;
private string jPrompts;
private Leader leader;
public int i=1;
public LeaderJig(Vector3d norm, Point3d BasePt,
Matrix3d Ucs, Plane Pn,string jigPrompts,bool IsSplined,bool HasArrowhead)
: base(new Leader())
{
ucs = Ucs;
jPrompts = jigPrompts;
leader = (Leader)Entity;
pn = Pn;
P1 = BasePt;
P2 = new Point3d(P1.X + 1, P1.Y, P1.Z);
leader.SetDatabaseDefaults();
leader.SetPlane(pn);
leader.AppendVertex(P1);
leader.AppendVertex(P2);
leader.IsSplined = IsSplined;
leader.HasArrowHead = HasArrowhead;
leader =(Leader) Layers.Layers.NotesAndDims(leader);
leader.Dimscale =(double) acadApp.GetSystemVariable("Dimscale");
//addcode to set the dimstyle leader.SetDimstyleData
}
}
public static ObjectId LeaderandMtext(string Msg, Point3d P1)
{
Document doc = acadApp.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
Matrix3d ucs = ed.CurrentUserCoordinateSystem;
CoordinateSystem3d cs = ucs.CoordinateSystem3d;
Vector3d Norm = cs.Zaxis;
Plane pn = new Plane(P1, Norm);
Leader leader = new Leader();
MText mt = new MText();
mt.SetDatabaseDefaults();
double dimgap = leader.Dimgap;
bool bDone = false;
//bool isMM = Util.Is.isMM();
double scale = Util.Get.getscale();
string prompt = "\nSpecify next point:";
ObjectId id=ObjectId.Null;
LeaderJig jig = new LeaderJig(Norm, P1, ucs, pn, prompt,false,true);
for (int i = 0; i < 2; i++)
{
PromptResult res = ed.Drag(jig);
if (res.Status == PromptStatus.Cancel) return id;
if (i == 0) jig.Addvertex();
if (i == 1 && res.Status == PromptStatus.OK) bDone = true;
}
if (bDone)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
leader = jig.GetEntity() as Leader;
Point2d pt1 = leader.GetPointAtParameter(1).Convert2d(pn);
Point2d pt2 = leader.EndPoint.Convert2d(pn);
if (pt2.X < pt1.X) dimgap = -dimgap;
leader.DimensionStyle = Util.Get.CurrentDimstyle(leader.GetType().Name);
id=btr.AppendEntity(leader);
tr.AddNewlyCreatedDBObject(leader, true);
tr.Commit();
}
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
if (Msg == string.Empty)
if (!Util.Get.getstring("\nType the text", "", true, out Msg)) return id;
mt.Contents = Msg;
mt.Layer = Layers.Layers.NNotes();
mt.Normal = Norm;
Point3d leaderEndpt=leader.EndPoint.TransformBy(ucs.Inverse());
Point3d location=new Point3d (leaderEndpt.X+dimgap,leaderEndpt.Y,leaderEndpt.Z).TransformBy(ucs);
mt.Location = location;
leader = tr.GetObject(leader.Id, OpenMode.ForWrite) as Leader;
if (leader.EndPoint.TransformBy(ucs.Inverse()).X >
leader.GetPointAtParameter(1).TransformBy(ucs.Inverse()).X)
mt.Attachment = AttachmentPoint.MiddleLeft;
else
mt.Attachment = AttachmentPoint.MiddleRight;
// mt.TextHeight =Util.Is.Mult()*scale *0.135;
//mt.TextStyleId =
ObjectId mtId=btr.AppendEntity(mt);
tr.AddNewlyCreatedDBObject(mt, true);
leader.Annotation = mtId;// mt.ObjectId;
tr.Commit();
}
}
return id;
} //End LeaderandMtext
Hope this gets you closer