Hi gile
This is the codes that I wrote and the rotation of text is always 0.0 and it is not following each line's angle.
If you have any comment about the codes please let me know.
Thanks.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
namespace SecondExample
{
public class Class1
{
[CommandMethod("LineLengths", CommandFlags.UsePickSet)]
public void GetlineLengths()
{
Editor edit = Application.DocumentManager.MdiActiveDocument.Editor;
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database Dtb = acDoc.Database;
using (Transaction trans = Dtb.TransactionManager.StartTransaction())
{
double len, dis = 0.0 ;
Line ent;
Point3d p1, p2, p3;
DBText txt;
try
{
BlockTable blktbl = trans.GetObject(Dtb.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord blkrec = trans.GetObject(Dtb.CurrentSpaceId, OpenMode.ForWrite)as BlockTableRecord;
TypedValue[] tv = new TypedValue [] { new TypedValue(0, "LINE") };
SelectionFilter fltr = new SelectionFilter(tv);
PromptSelectionResult ss = edit.GetSelection(fltr);
if (ss.Status == PromptStatus.OK)
{
foreach (SelectedObject obj in ss.Value)
{
ent = trans.GetObject(obj.ObjectId, OpenMode.ForRead) as Line;
len = ent.Length;
dis += len;
p1 = ent.StartPoint;
p2 = ent.EndPoint;
p3 = Polar (p1,ent.Angle, (len / 2.0));
txt = new DBText();
txt.SetDatabaseDefaults();
txt.Position = p3;
txt.TextString = Math.Round(len, 4).ToString();
txt.Height = 0.2;
blkrec.AppendEntity(txt);
trans.AddNewlyCreatedDBObject(txt, true);
}
trans.Commit();
}
if (dis > 0.0)
{
edit.WriteMessage("Total lengths : " + dis.ToString());
}
}
catch (System.Exception exp)
{
edit.WriteMessage(exp.Message);
trans.Abort();
}
}
}
/// <summary>
/// Defines a point with polar coordinates from a base point.
/// </summary>
/// <param name="basePt">The base point.</param>
/// <param name="angle">The angle (radians) about the X axis.</param>
/// <param name="distance">The distance from the base point.</param>
/// <returns>The new 3d point.</returns>
public Point3d Polar(Point3d basePt, double angle, double distance)
{
return new Point3d(
basePt.X + (distance * Math.Cos(angle)),
basePt.Y + (distance * Math.Sin(angle)),
basePt.Z);
}
}
}