[CommandMethod("CMD2")]
public void Cmd2()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
// select a polyline
PromptEntityOptions peo
= new PromptEntityOptions
("\nSélectionnez une polyligne: "); peo.SetRejectMessage("Objet non valide.");
peo
.AddAllowedClass(typeof(Polyline
),
true); PromptEntityResult per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK) return;
Matrix3d UCS2WCS = ed.CurrentUserCoordinateSystem;
Matrix3d WCS2UCS = UCS2WCS.Inverse();
using (Transaction tr = db.TransactionManager.StartTransaction())
{
// open the polyline
Polyline pline = (Polyline)tr.GetObject(per.ObjectId, OpenMode.ForRead);
// calculate the center of the polyline bounding box
Extents3d extents = pline.GeometricExtents;
Point3d center = extents.MinPoint + (extents.MaxPoint - extents.MinPoint) / 2.0;
// convert the point coordinates into UCS
center = center.TransformBy(WCS2UCS);
// add the mtext
using (MText mtext
= new MText
()) {
mtext.Location = center;
mtext.Contents = "Ceci est un test";
BlockTableRecord curSpace =
(BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
curSpace.AppendEntity(mtext);
tr.AddNewlyCreatedDBObject(mtext, true);
// apply a transformation matrix form UCS to WCS the mtext object
mtext.TransformBy(UCS2WCS);
}
tr.Commit();
}
}