How to get the MText position of a Dimension, it's not the textposition.

How to get the MText position of a Dimension, it's not the textposition.

swaywood
Collaborator Collaborator
2,490 Views
3 Replies
Message 1 of 4

How to get the MText position of a Dimension, it's not the textposition.

swaywood
Collaborator
Collaborator

hi:

   I want to get the dimension text position of a dimension, it may be a RotatedDimension,AlignedDimension or other type of dimension.

   I tried to get the textposition, but it's not exactly the text postion.

   Could anyone tell me how to get it, or how to get the MText object?

regards

swaywood

0 Likes
Accepted solutions (2)
2,491 Views
3 Replies
Replies (3)
Message 2 of 4

SENL1362
Advisor
Advisor
Accepted solution
               var peo = new PromptEntityOptions("Select Dimension");
                var per = ed.GetEntity(peo);
                if (per.Status != PromptStatus.OK)
                    return;

                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    var dim = tr.GetObject(per.ObjectId, OpenMode.ForRead) as Dimension;
                    if (dim == null)
                        throw new System.Exception("Selected object is not a dimension");
                    var dimBlk = tr.GetObject(dim.DimBlockId, OpenMode.ForRead) as BlockTableRecord;
                    foreach(var id in dimBlk)
                    {
                        if (id.ObjectClass.DxfName=="MTEXT")
                        {
                            var mt = tr.GetObject(id, OpenMode.ForRead) as MText;
                            ed.WriteMessage("\n Dim Text:");
ed.WriteMessage("\n\t Block: {0}", dimBlk.Name); ed.WriteMessage("\n\t Text: {0}", mt.Contents); ed.WriteMessage("\n\t Position: {0}, {1}", mt.Location.X, mt.Location.Y); ed.WriteMessage("\n\t AttachmentPoint: {0}",Enum.GetName(typeof(AttachmentPoint), mt.Attachment)); ed.WriteMessage("\n\t Rotation: {0}", mt.Rotation * 180 / Math.PI); ed.WriteMessage("\n\t Width, Height: {0}, {1}", mt.ActualHeight, mt.ActualWidth); } } tr.Commit(); }
0 Likes
Message 3 of 4

SENL1362
Advisor
Advisor
Accepted solution

When you are interested in the frame around the Dimension Text, add this to the code:

 

[CommandMethod("MtFromDim")]
        public static void MtextFromDimension()
        {
            Document doc = null;
            Database db = null;
            Editor ed = null;


            try
            {
                doc = AcadApp.DocumentManager.MdiActiveDocument;
                if (doc == null)
                    throw new System.Exception("No MdiActiveDocument");
                db = doc.Database;
                ed = doc.Editor;

                var peo = new PromptEntityOptions("Select Dimension");
                var per = ed.GetEntity(peo);
                if (per.Status != PromptStatus.OK)
                    return;

                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    var dim = tr.GetObject(per.ObjectId, OpenMode.ForRead) as Dimension;
                    if (dim == null)
                        throw new System.Exception("Selected object is not a dimension");
                    var dimBlk = tr.GetObject(dim.DimBlockId, OpenMode.ForRead) as BlockTableRecord;
                    foreach(var id in dimBlk)
                    {
                        if (id.ObjectClass.DxfName=="MTEXT")
                        {
                            var mt = tr.GetObject(id, OpenMode.ForRead) as MText;
                            ed.WriteMessage("\n Dim Text:");
                            ed.WriteMessage("\n\t Block: {0}", dimBlk.Name);
                            ed.WriteMessage("\n\t Text: {0}", mt.Contents);
                            ed.WriteMessage("\n\t Position: {0}, {1}", mt.Location.X, mt.Location.Y);
                            ed.WriteMessage("\n\t AttachmentPoint: {0}",Enum.GetName(typeof(AttachmentPoint), mt.Attachment));
                            ed.WriteMessage("\n\t Rotation: {0}", mt.Rotation * 180 / Math.PI);
                            ed.WriteMessage("\n\t Width, Height: {0}, {1}", mt.ActualHeight, mt.ActualWidth);

                            var mtAngle = mt.Rotation;
                            var mtPos = mt.Location;
                            mt.UpgradeOpen();

                            mt.TransformBy(Matrix3d.Rotation(-mtAngle, Vector3d.ZAxis, mtPos));
                            var mtBounds = mt.GetBoundingPoints();
                            mt.TransformBy(Matrix3d.Rotation(mtAngle, Vector3d.ZAxis, mtPos));
                            for (int i = 0; i < mtBounds.Count; i++)
                                mtBounds[i]=mtBounds[i].TransformBy(Matrix3d.Rotation(mtAngle, Vector3d.ZAxis, mtPos));
                            ed.WriteMessage("\n\t Lower Left: ({0}, {1})", mtBounds[2].X, mtBounds[2].Y);
                            ed.WriteMessage("\n\t Lower Right: ({0}, {1})", mtBounds[3].X, mtBounds[3].Y);
                            ed.WriteMessage("\n\t Upper Right: ({0}, {1})", mtBounds[1].X, mtBounds[1].Y);
                            ed.WriteMessage("\n\t Upper Left: ({0}, {1})", mtBounds[0].X, mtBounds[0].Y);
 
var pl = new Polyline(); pl.AddVertexAt(0, new Point2d(mtBounds[2].X, mtBounds[2].Y), 0, 0, 0); pl.AddVertexAt(1, new Point2d(mtBounds[3].X, mtBounds[3].Y), 0, 0, 0); pl.AddVertexAt(2, new Point2d(mtBounds[1].X, mtBounds[1].Y), 0, 0, 0); pl.AddVertexAt(3, new Point2d(mtBounds[0].X, mtBounds[0].Y), 0, 0, 0); pl.Closed = true; pl.LayerId = mt.LayerId; var ms = (BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite); ms.AppendEntity(pl); tr.AddNewlyCreatedDBObject(pl, true); } } tr.Commit(); } } catch (System.Exception ex) { if (ed != null) ed.WriteMessage("\n Error in MtextFromDimension: {0}", ex.Message); } }

 

 

Screenshot_1.png

 

Message 4 of 4

swaywood
Collaborator
Collaborator

Hi senl1362:

   Thank you so much~

   That's really help.

swaywood

0 Likes