[CommandMethod("PRINTCOORDS")]
public static void PrintCoordinates()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
// prompt the user to pick a point
PromptPointResult ppr = ed.GetPoint("\nPick a point: ");
// return if the user cancelled
if (ppr.Status != PromptStatus.OK)
return;
Point3d pickedPoint = ppr.Value;
// start a transaction to add a text to the current space
using (Transaction tr = db.TransactionManager.StartTransaction())
{
// open the current space BlockTableRecord for write
BlockTableRecord curSpace =
(BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
// create a new DBText instance and set its properties
DBText text
= new DBText
() {
Position = pickedPoint,
TextString = $"{pickedPoint.X:0.00}, {pickedPoint.Y:0.00}"
};
// transform the text to UCS
text.TransformBy(ed.CurrentUserCoordinateSystem);
// append the text to current space
curSpace.AppendEntity(text);
// add the text to the transaction
tr.AddNewlyCreatedDBObject(text, true);
// commit the changes
tr.Commit();
}
}