Author Topic: Change alignment of MText but retain actual text position  (Read 1649 times)

0 Members and 1 Guest are viewing this topic.

Hackysack

  • Guest
Change alignment of MText but retain actual text position
« on: August 27, 2015, 10:21:10 AM »
Hi!

I've built an AutoCAD Plugin that replaces some DBText and MText Objects with an attributed block. My problem: Some of the MText have their alignment set to "Top left" and some have it set to "Bottom left".
The block that replaces the MText is designed so that it perfectly matches the text position if it was set to "Bottom Left" - since the majority of MText objects is drawn this way; I'd like to change all the MText alignments to "Bottom Left", so I can use the same block for all MText objects.

When I change the alignment in AutoCAD, the actual text position stays the same - the alignment property is set to a new value and the text is moved to the new position, so that the position of the letters stays the same <- That's what I'm trying to accomplish :)

Can someone get me started on how to do this? I'm not sure how to tackle this one... Below I have attached a small test command to change the .Attachment of the MText's.

Code: [Select]
        [CommandMethod("MTextJustificationTest")]
        public void MTextJustificationTest()
        {
            Database db = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument.Database;
            Editor ed = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument.Editor;

            Point3d startPos = new Point3d();

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TypedValue[] filterDxf = new TypedValue[]
                {
                    new TypedValue((int)DxfCode.Start,"MTEXT")
                };
                SelectionFilter selectionFilter = new SelectionFilter(filterDxf);
                PromptSelectionResult psr = ed.SelectAll(selectionFilter);
                SelectionSet selectionSet = psr.Value;

                foreach (var objId in psr.Value.GetObjectIds())
                {
                    MText mText = tr.GetObject(objId, OpenMode.ForWrite) as MText;
                    if (mText.Attachment != AttachmentPoint.BottomLeft)
                    {
                        startPos = mText.Location;
                        // How to get the _real_ text position?
                        mText.Attachment = AttachmentPoint.BottomLeft;
                    }

                }
                tr.Commit();
            }
        }

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Re: Change alignment of MText but retain actual text position
« Reply #1 on: August 30, 2015, 01:38:01 AM »
Can't help but will tell you I've tried a hundred times to deal with text styles and properties with .Net and there's always some little bug, something in the drawing, or the code, or whatever that makes it a pain in the booty.   I decided to stick with autolisp functions for them.

Hi!

I've built an AutoCAD Plugin that replaces some DBText and MText Objects with an attributed block. My problem: Some of the MText have their alignment set to "Top left" and some have it set to "Bottom left".
The block that replaces the MText is designed so that it perfectly matches the text position if it was set to "Bottom Left" - since the majority of MText objects is drawn this way; I'd like to change all the MText alignments to "Bottom Left", so I can use the same block for all MText objects.

When I change the alignment in AutoCAD, the actual text position stays the same - the alignment property is set to a new value and the text is moved to the new position, so that the position of the letters stays the same <- That's what I'm trying to accomplish :)

Can someone get me started on how to do this? I'm not sure how to tackle this one... Below I have attached a small test command to change the .Attachment of the MText's.

Code: [Select]
        [CommandMethod("MTextJustificationTest")]
        public void MTextJustificationTest()
        {
            Database db = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument.Database;
            Editor ed = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument.Editor;

            Point3d startPos = new Point3d();

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TypedValue[] filterDxf = new TypedValue[]
                {
                    new TypedValue((int)DxfCode.Start,"MTEXT")
                };
                SelectionFilter selectionFilter = new SelectionFilter(filterDxf);
                PromptSelectionResult psr = ed.SelectAll(selectionFilter);
                SelectionSet selectionSet = psr.Value;

                foreach (var objId in psr.Value.GetObjectIds())
                {
                    MText mText = tr.GetObject(objId, OpenMode.ForWrite) as MText;
                    if (mText.Attachment != AttachmentPoint.BottomLeft)
                    {
                        startPos = mText.Location;
                        // How to get the _real_ text position?
                        mText.Attachment = AttachmentPoint.BottomLeft;
                    }

                }
                tr.Commit();
            }
        }