//FH
public static void CurveLabeling()
{
// thanks to Thorsten Kaefer
Document doc = acadApp.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
Matrix3d ucs = ed.CurrentUserCoordinateSystem;
TypedValue
[] filter
= { new TypedValue
((int)DxfCode
.Start,
"LWPOLYLINE") };
SelectionFilter filterSset
= new SelectionFilter
(filter
);
PromptSelectionOptions pso
= new PromptSelectionOptions
();
pso.SingleOnly = true;//comment this if you want to label multiple polylines
pso.MessageForAdding = "Select a single polyline >> ";
PromptSelectionResult psr = ed.GetSelection(pso, filterSset);
if (psr.Status != PromptStatus.OK)
return;
Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
try
{
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
ObjectId[] ids = psr.Value.GetObjectIds();
double theight = db.Textsize;
if (theight == 0)
theight = db.Dimtxt;
for (int i = 0; i < ids.Length; i++)
{
Autodesk.AutoCAD.DatabaseServices.Polyline pline = tr.GetObject(ids[i], OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Polyline;
double step = 50.0;// change step to suit
double leng = pline.Length;
double num = (int)(leng / step);
double ang;
int n = 1;
Plane plan = pline.GetPlane();
Line ln = null;
DBText txt = null;
Vector3d vect
= new Vector3d
();
Vector3d vec
= new Vector3d
();
Point3d ptxt
= new Point3d
();
Point3d ppt
= new Point3d
();
for (n = 0; n < num+1; n++)
{
ptxt = pline.GetPointAtDist(step*n);
vec = pline.GetFirstDerivative(ptxt).GetPerpendicularVector();
ppt = (ptxt + vec * 10.0).TransformBy(ucs);
ln
= new Line
(ptxt, ppt
);
btr.AppendEntity(ln);
tr.AddNewlyCreatedDBObject(ln, true);
vect = ln.GetFirstDerivative(ppt).GetPerpendicularVector();
txt.Position = ppt;
txt.TextString = string.Format("{0:f2}", n*step);
txt.HorizontalMode = TextHorizontalMode.TextCenter;
txt.VerticalMode = TextVerticalMode.TextBottom;
txt.AlignmentPoint = ppt;
ang = ln.Angle + Math.PI;
if ((ang > Math.PI / 2) || (ang < Math.PI * 1.5))
ang = ang + Math.PI;
txt.Rotation = ang;
btr.AppendEntity(txt);
tr.AddNewlyCreatedDBObject(txt, true);
}
ptxt = pline.EndPoint;
vec = pline.GetFirstDerivative(ptxt).GetPerpendicularVector();
ppt = (ptxt + vec * 10.0).TransformBy(ucs);
ln
= new Line
(ptxt, ppt
);
btr.AppendEntity(ln);
tr.AddNewlyCreatedDBObject(ln, true);
vect = ln.GetFirstDerivative(ppt).GetPerpendicularVector();
txt.Position = ppt;
txt.TextString = string.Format("{0:f2}", leng);
txt.HorizontalMode = TextHorizontalMode.TextCenter;
txt.VerticalMode = TextVerticalMode.TextBottom;
txt.AlignmentPoint = ppt;
ang = ln.Angle + Math.PI;
if ((ang > Math.PI / 2) || (ang < Math.PI * 1.5))
ang = ang + Math.PI;
txt.Rotation = ang;
btr.AppendEntity(txt);
tr.AddNewlyCreatedDBObject(txt, true);
db.TransactionManager.QueueForGraphicsFlush();
}
tr.Commit();
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
MessageBox.Show(string.Format(
"Error:\n{0}\nTrace:\n{1}", ex.Message, ex.StackTrace));
}
}
}