[CommandMethod("PLDIM")]
public void polylinewritedim()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
try
{
BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
PromptDoubleOptions pdouble
= new PromptDoubleOptions
("\nEnter Text Highte : "); pdouble.AllowNegative = false;
pdouble.AllowZero = false;
pdouble.AllowNone = false;
PromptDoubleResult pdoubelresulr = ed.GetDouble(pdouble);
if (pdoubelresulr.Status != PromptStatus.OK)
{
return;
}
if (pdoubelresulr.Status == PromptStatus.OK)
{
double txth = pdoubelresulr.Value;
TypedValue
[] tv
= new TypedValue
[1]; tv
.SetValue(new TypedValue
((int)DxfCode
.Start,
"LWPOLYLINE"),
0); SelectionFilter filter
= new SelectionFilter
(tv
); PromptSelectionResult ssPrompt;
ssPrompt = ed.GetSelection(filter);
if (ssPrompt.Status == PromptStatus.OK)
{
SelectionSet ss = ssPrompt.Value;
foreach (SelectedObject sObj in ss)
{
Autodesk.AutoCAD.DatabaseServices. Polyline poly = trans.GetObject(sObj.ObjectId, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices. Polyline;
for (int i = 0; i < poly.NumberOfVertices; i++)
{
if (poly.GetSegmentType(i) == SegmentType.Line)
{
LineSegment3d segmline = poly.GetLineSegmentAt(i);
double angle = AngleFromXAxis(segmline.StartPoint, segmline.EndPoint);
MText dimtxtline
= new MText
(); dimtxtline.Attachment = AttachmentPoint.MiddleCenter;
dimtxtline.TextHeight = txth;
dimtxtline.Rotation = angle;
dimtxtline.Contents = $@"{segmline.Length:0.00}";
dimtxtline.Location = segmline.MidPoint;
btr.AppendEntity(dimtxtline);
trans.AddNewlyCreatedDBObject(dimtxtline, true);
}
else if (poly.GetSegmentType(i) == SegmentType.Arc)
{
CircularArc3d segmArc = poly.GetArcSegmentAt(i);
Curve curve = Curve.CreateFromGeCurve(segmArc);
double arcLen = CurveLength(curve);
double dist2Middle = arcLen / 2;
Point3d midPt = GetMidpoint(curve);
mt.Contents = $@"R{segmArc.Radius:0.00}";
mt.TextHeight = txth;
mt.Height = txth;
Point3d location = PolarPoint(segmArc.Center, AngleFromXAxis(segmArc.Center, midPt), segmArc.Radius);
mt.Location = location;
mt.Attachment = AttachmentPoint.MiddleCenter;
double dist1 = mt.ActualWidth / 2; double dist2 = mt.ActualWidth / 2; //helper to find points for text rotation near mid point of arc
if (dist2Middle - dist1 < 0) dist1 = dist2Middle;
if (dist2Middle + dist2 > arcLen) dist2 = arcLen - dist2Middle;
Point3d p1 = curve.GetPointAtDist(dist2Middle - dist1);
Point3d p2 = curve.GetPointAtDist(dist2Middle + dist2);
double angle = AngleFromXAxis(p1, p2);
//adjust angle for text to be "most readable"
if (angle > Math.PI && angle < 3 * Math.PI / 2) angle -= Math.PI;
else if (angle > Math.PI / 2 && angle <= Math.PI) angle += Math.PI;
mt.Rotation = angle;
btr.AppendEntity(mt);
trans.AddNewlyCreatedDBObject(mt, true);
}
}
}
}
}
trans.Commit();
}
catch (System.Exception ex)
{
ed.WriteMessage("Error pls chek " + ex.Message);
trans.Abort();
}
}
}
Point3d GetMidpoint(Curve curve) //https://forums.autodesk.com/t5/net/mid-point-on-arc/td-p/2720851
{
double d1 = curve.GetDistanceAtParameter(curve.StartParam);
double d2 = curve.GetDistanceAtParameter(curve.EndParam);
return curve.GetPointAtDist(d1 + ((d2 - d1) / 2.0));
}
public double AngleFromXAxis(Point3d p1, Point3d p2) //TT
{
return new Vector2d
(p2
.X - p1
.X, p2
.Y - p1
.Y).Angle; }
public double CurveLength(Curve curve)
{
double d1 = curve.GetDistanceAtParameter(curve.StartParam);
double d2 = curve.GetDistanceAtParameter(curve.EndParam);
return d2 - d1;
}
public Point3d PolarPoint(Point3d basepoint, double angle, double distance) //TT
{
return new Point3d
(basepoint
.X + (distance
* Math
.Cos(angle
)), basepoint
.Y + (distance
* Math
.Sin(angle
)), basepoint
.Z); }