Hi,
The goal was to insert a MText object at the center of a polyline bounding box parallely to the current coordinates system X axis.
These two examples helped bathepn to solve its problem.
This first one inserts the mtext at the center of the polyline bbox and rotate it to be parallel too the current UCS Xaxis.
[CommandMethod("CMD1")]
public void Cmd1()
{
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;
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;
// add the mtext
using (MText mtext
= new MText
()) {
mtext.SetDatabaseDefaults();
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);
// rotate the mtext
Vector3d xdir = (Point3d)Application.GetSystemVariable("UCSXDIR") - Point3d.Origin;
double ucsRotation = Vector3d.XAxis.GetAngleTo(xdir, Vector3d.ZAxis);
mtext.TransformBy(Matrix3d.Rotation(ucsRotation, Vector3d.ZAxis, center));
}
tr.Commit();
}
}
This other one illustrates Kerry's suggestion. It uses the Editor.CurrentCoordinatesSystem matrix to transform the mtext form UCS to WCS.
Before applying the transformation matrix, the insertion point have to be converted form WCS to UCS with the inverse matrix.
[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();
}
}