1
Dimensions / Create leader with mtext
« on: January 28, 2014, 12:29:10 PM »
C#
Code: [Select]
[CommandMethod("LDM")]
public void CreateLeaderWithMText()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
PromptPointOptions prOpt = new PromptPointOptions("\nSelect first point ");
PromptPointResult es = ed.GetPoint(prOpt);
if (es.Status != PromptStatus.OK)
{
if (es.Status == PromptStatus.Cancel)
{
ed.WriteMessage("\nInterrupted by user"); return;
}
else
{
ed.WriteMessage("\nError on specifying a point"); return;
}
}
Point3d p1 = es.Value;
prOpt = new PromptPointOptions("\nSelect Second point ");
prOpt.UseBasePoint = true;
prOpt.UseDashedLine = true;
prOpt.BasePoint = p1;
es = ed.GetPoint(prOpt);
if (es.Status != PromptStatus.OK)
{
if (es.Status == PromptStatus.Cancel)
{
ed.WriteMessage("\nInterrupted by user"); return;
}
else
{
ed.WriteMessage("\nError on specifying a point"); return;
}
}
Point3d p2 = es.Value;
Transaction tr = doc.TransactionManager.StartTransaction();
using (tr)
{
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
ObjectId mtextId = ObjectId.Null;
MText mtx = new MText();
mtx.SetDatabaseDefaults();
mtx.Contents = "TEXT STRING\\PNEW STRING";
mtx.Location = p2;
mtextId = btr.AppendEntity(mtx);
tr.AddNewlyCreatedDBObject(mtx, true);
// mtx.DowngradeOpen();
ObjectId leaderId = ObjectId.Null;
Leader ld = new Leader();
ld.AppendVertex(p1);
ld.AppendVertex(p2);
ld.SetDatabaseDefaults();
leaderId = btr.AppendEntity(ld);
ld.Annotation = mtextId;
if (p2.X < p1.X)
{
mtx.Attachment = AttachmentPoint.MiddleRight;
mtx.Location = p2;
}
ld.EvaluateLeader();
tr.AddNewlyCreatedDBObject(ld, true);
tr.Commit();
}
}
VB.NETCode: [Select]
<CommandMethod("LDM")> _
Public Sub CreateLeaderWithMText()
Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
Dim ed As Editor = doc.Editor
Dim db As Database = doc.Database
Dim prOpt As New PromptPointOptions(vbLf & "Select first point ")
Dim es As PromptPointResult = ed.GetPoint(prOpt)
If es.Status <> PromptStatus.OK Then
If es.Status = PromptStatus.Cancel Then
ed.WriteMessage(vbLf & "Interrupted by user")
Return
Else
ed.WriteMessage(vbLf & "Error on specifying a point")
Return
End If
End If
Dim p1 As Point3d = es.Value
prOpt = New PromptPointOptions(vbLf & "Select Second point ")
prOpt.UseBasePoint = True
prOpt.UseDashedLine = True
prOpt.BasePoint = p1
es = ed.GetPoint(prOpt)
If es.Status <> PromptStatus.OK Then
If es.Status = PromptStatus.Cancel Then
ed.WriteMessage(vbLf & "Interrupted by user")
Return
Else
ed.WriteMessage(vbLf & "Error on specifying a point")
Return
End If
End If
Dim p2 As Point3d = es.Value
Dim tr As Transaction = doc.TransactionManager.StartTransaction()
Using tr
Dim btr As BlockTableRecord = DirectCast(tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)
Dim mtextId As ObjectId = ObjectId.Null
Dim mtx As New MText()
mtx.SetDatabaseDefaults()
mtx.Contents = "TEXT STRING\PNEW STRING"
mtx.Location = p2
mtextId = btr.AppendEntity(mtx)
tr.AddNewlyCreatedDBObject(mtx, True)
' mtx.DowngradeOpen();
Dim leaderId As ObjectId = ObjectId.Null
Dim ld As New Leader()
ld.AppendVertex(p1)
ld.AppendVertex(p2)
ld.SetDatabaseDefaults()
leaderId = btr.AppendEntity(ld)
ld.Annotation = mtextId
If p2.X < p1.X Then
mtx.Attachment = AttachmentPoint.MiddleRight
mtx.Location = p2
End If
ld.EvaluateLeader()
tr.AddNewlyCreatedDBObject(ld, True)
tr.Commit()
End Using
End Sub