Try this command, just change layer names on whatever you need
//__________________________C#_________________________//
// Add fields to lines in the model space
// The following layers: Layer1,Layer2,Text_Layer might be exist before
[CommandMethod("adf", CommandFlags.UsePickSet | CommandFlags.Redraw)]
public void testAddFieldsToLines()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
BlockTableRecord btr;
MText txtObj;
bool result = true;
string fieldText = string.Empty;
Point3d spt;
Point3d ept;
double ang = 0;
double x = 0;
double y = 0;
Point3d pt;
ObjectId txtStyleId = ObjectId.Null;
doc.TransactionManager.EnableGraphicsFlush(true);
try
{
Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
TextStyleTable txttb = (TextStyleTable)tr.GetObject(db.TextStyleTableId, OpenMode.ForRead);
if (txttb.Has("ARIAL"))
{
txtStyleId = txttb["ARIAL"];
}
else
{
txtStyleId = txttb["Standard"];
}
TypedValue[] tpv = new TypedValue[] {
new TypedValue(0, "line"),
new TypedValue(8, "Layer1,Layer2"),// the layers, you interesting in, separated by commas
new TypedValue(410, "Model") };
SelectionFilter filt = new SelectionFilter(tpv);
PromptSelectionResult resSel = ed.SelectAll(filt);
if (resSel.Status != PromptStatus.OK)
{
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("Nothing selected");
return;
}
SelectionSet selSet = resSel.Value;
foreach (SelectedObject selObj in selSet)
{
ObjectId id = selObj.ObjectId;
Entity ent = (Entity)tr.GetObject(id, OpenMode.ForRead);
if (ent != null)
{
Line ln = (Line)ent as Line;
string oidStr = id.ToString().Trim(new char[] { '(', ')' });
fieldText = "%<\\AcObjProp Object(%<\\_ObjId " + oidStr + ">%).Length \\f \"%lu2%pr2%ps[,]\">%";// metric, precision 2 decimals
spt = ln.StartPoint;
ept = ln.EndPoint;
x = (spt.X + ept.X) / 2;
y = (spt.Y + ept.Y) / 2;
if (ept.X - spt.X == 0)
{
ang = Math.PI / 2;
}
else
{
ang = Math.Atan((ept.Y - spt.Y) / (ept.X - spt.X));
}
pt = new Point3d(x, y, 0.0);
txtObj = new MText();
txtObj.SetDatabaseDefaults();
txtObj.Location = pt;
txtObj.Height = 3.0;
txtObj.Contents = "Length:";// dummy text
txtObj.Rotation = ang;
txtObj.TextStyleId = txtStyleId;
txtObj.Layer = "Text_Layer";// Text_Layer might be exist
txtObj.Attachment = AttachmentPoint.BottomCenter;
txtObj.SetAttachmentMovingLocation(txtObj.Attachment);
btr.AppendEntity(txtObj);
tr.AddNewlyCreatedDBObject(txtObj, true);
tr.TransactionManager.QueueForGraphicsFlush();
Autodesk.AutoCAD.DatabaseServices.Field fileldObj = new Autodesk.AutoCAD.DatabaseServices.Field();
fileldObj.EvaluationOption = FieldEvaluationOptions.Automatic;
fileldObj.Evaluate((int)(FieldEvaluationOptions.Automatic), db);
fileldObj.SetFieldCode(fieldText);
txtObj.SetField(fileldObj);
tr.AddNewlyCreatedDBObject(fileldObj, true);
ed.WriteMessage("Point\t{0}", txtObj.ObjectId);
tr.TransactionManager.QueueForGraphicsFlush();
txtObj.Width = txtObj.ActualWidth;
}
}
doc.TransactionManager.FlushGraphics();
tr.Commit();
ed.Regen();
}
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
result = false;
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(ex.Message + "\n" + ex.StackTrace);
}
finally
{
if (result)
{
ed.WriteMessage("\nThe command executed successfully\n");
}
}
}
''__________________________VB.NET__________________________''
' Add fields to lines in the model space
' The following layers: Layer1,Layer2,Text_Layer might be exist before
<CommandMethod("adf", CommandFlags.UsePickSet Or CommandFlags.Redraw)> _
Public Sub testAddFieldsToLines()
Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
Dim ed As Editor = doc.Editor
Dim db As Database = doc.Database
Dim btr As BlockTableRecord
Dim txtObj As MText
Dim result As Boolean = True
Dim fieldText As String = String.Empty
Dim spt As Point3d
Dim ept As Point3d
Dim ang As Double = 0
Dim x As Double = 0
Dim y As Double = 0
Dim pt As Point3d
Dim txtStyleId As ObjectId = ObjectId.Null
doc.TransactionManager.EnableGraphicsFlush(True)
Try
Dim tr As Transaction = db.TransactionManager.StartTransaction()
Using tr
Dim bt As BlockTable = DirectCast(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
btr = DirectCast(tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)
Dim txttb As TextStyleTable = DirectCast(tr.GetObject(db.TextStyleTableId, OpenMode.ForRead), TextStyleTable)
If txttb.Has("ARIAL") Then
txtStyleId = txttb("ARIAL")
Else
txtStyleId = txttb("Standard")
End If
' the layers, you interesting in, separated by commas
Dim tpv As TypedValue() = New TypedValue() {New TypedValue(0, "line"), New TypedValue(8, "Layer1,Layer2"), New TypedValue(410, "Model")}
Dim filt As New SelectionFilter(tpv)
Dim resSel As PromptSelectionResult = ed.SelectAll(filt)
If resSel.Status <> PromptStatus.OK Then
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("Nothing selected")
Return
End If
Dim selSet As SelectionSet = resSel.Value
For Each selObj As SelectedObject In selSet
Dim id As ObjectId = selObj.ObjectId
Dim ent As Entity = DirectCast(tr.GetObject(id, OpenMode.ForRead), Entity)
If ent IsNot Nothing Then
Dim ln As Line = TryCast(DirectCast(ent, Line), Line)
Dim oidStr As String = id.ToString().Trim(New Char() {"("c, ")"c})
fieldText = "%<\AcObjProp Object(%<\_ObjId " & oidStr & ">%).Length \f ""%lu2%pr2%ps[,]"">%"
' metric, precision 2 decimals
spt = ln.StartPoint
ept = ln.EndPoint
x = (spt.X + ept.X) / 2
y = (spt.Y + ept.Y) / 2
If ept.X - spt.X = 0 Then
ang = Math.PI / 2
Else
ang = Math.Atan((ept.Y - spt.Y) / (ept.X - spt.X))
End If
pt = New Point3d(x, y, 0.0)
txtObj = New MText()
txtObj.SetDatabaseDefaults()
txtObj.Location = pt
txtObj.Height = 3.0
txtObj.Contents = "Length:"
' dummy text
txtObj.Rotation = ang
txtObj.TextStyleId = txtStyleId
txtObj.Layer = "Text_Layer" ' Text_Layer might be exist
txtObj.Attachment = AttachmentPoint.BottomCenter
txtObj.SetAttachmentMovingLocation(txtObj.Attachment)
btr.AppendEntity(txtObj)
tr.AddNewlyCreatedDBObject(txtObj, True)
tr.TransactionManager.QueueForGraphicsFlush()
Dim fileldObj As New Autodesk.AutoCAD.DatabaseServices.Field()
fileldObj.EvaluationOption = FieldEvaluationOptions.Automatic
fileldObj.Evaluate(CInt(FieldEvaluationOptions.Automatic), db)
fileldObj.SetFieldCode(fieldText)
txtObj.SetField(fileldObj)
tr.AddNewlyCreatedDBObject(fileldObj, True)
ed.WriteMessage("Point" & vbTab & "{0}", txtObj.ObjectId)
tr.TransactionManager.QueueForGraphicsFlush()
txtObj.Width = txtObj.ActualWidth
End If
Next
doc.TransactionManager.FlushGraphics()
tr.Commit()
ed.Regen()
End Using
Catch ex As Autodesk.AutoCAD.Runtime.Exception
result = False
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(ex.Message + vbLf + ex.StackTrace)
Finally
If result Then
ed.WriteMessage(vbLf & "The command executed successfully" & vbLf)
End If
End Try
End Sub
_____________________________________
C6309D9E0751D165D0934D0621DFF27919