public static void polylinenumber()
{
double txth = 1;
int snumber = 101;
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;
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;
List
<Polyline
> lspl
= new List
<Polyline
>(); foreach (SelectedObject sObj in ss)
{
Polyline poly = trans.GetObject(sObj.ObjectId, OpenMode.ForWrite) as Polyline;
lspl.Add(poly);
}
var sortedpolyList = lspl.OrderByDescending(p => p.StartPoint.Y).ThenBy(p => p.StartPoint.X);
//var sortedpolyList = lspl.OrderByDescending(p => p.StartPoint.Y).ThenBy(p => p.StartPoint.X);
foreach (Polyline item in sortedpolyList)
{
DBText txtpl
= new DBText
(); txtpl.SetDatabaseDefaults();
txtpl.TextString = "PH "+ snumber.ToString();
//Point3d p2 = PolylineExtensions.Centroid(item);
txtpl.Position = PolylineExtensions.Centroid(item);
txtpl.Rotation = 0;
txtpl.Height = txth;
btr.AppendEntity(txtpl);
trans.AddNewlyCreatedDBObject(txtpl, true);
snumber++;
}
trans.Commit();
}
}
catch (System.Exception ex)
{
ed.WriteMessage("Error pls chek " + ex.Message);
trans.Abort();
}
}
}