Calculate point for text to insert below existing text

Calculate point for text to insert below existing text

Anonymous
Not applicable
970 Views
3 Replies
Message 1 of 4

Calculate point for text to insert below existing text

Anonymous
Not applicable

I have a TEXT entity that is in the model space already. I would like to add another text entity right below it, having the same alignment, text size, etc. Ho can I determine the insert point for such a text entity? I am trying to emulate the text command that creates multiple text entities when the enter key is pressed. Any help is much appreciated!

0 Likes
971 Views
3 Replies
Replies (3)
Message 2 of 4

Keith.Brown
Advisor
Advisor
Every entity has an extents property from which you can get a bounding box around the entity. From the min and max points of the extents along with the insertion point and/or the text justification you should be able to determine the next insertion point of the next text entity.
Message 3 of 4

Anonymous
Not applicable

Thanks for your suggestion. This would be pretty straight forward to implement if the text is horizontal. However, in my case the text could be rotated at any angle and in different UCS. Although I do handle the UCS transformation for other purposes and is not a concern, I am not sure how to determine the next text insert point if the text is rotated.

0 Likes
Message 4 of 4

SENL1362
Advisor
Advisor

This is a sample to show you the mathematics. It's as simple as it can be, only supporting Single Line Of Text.

Attached are samples before and after executing the sample below.

 

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

            try
            {
                doc = AcadApp.DocumentManager.MdiActiveDocument;
                db = doc.Database;
                ed = doc.Editor;


                var per = ed.GetEntity("Select (Single) Text");
                if (per.Status != PromptStatus.OK)
                {
                    return;
                }
                using (var tr = db.TransactionManager.StartTransaction())
                {
                    var ms = (BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead);
                    var text = tr.GetObject(per.ObjectId, OpenMode.ForRead) as DBText;
                    if (text == null)
                    {
                        throw new System.Exception("No (Single) Text selected");
                    }

                    var textPos = text.Position;
                    var textHeight = text.Height;
                    var textAngle = text.Rotation;
var textAlignPos = text.AlignmentPoint; var textLineSeparation = 2.0; //or use a percentage of textHeight var textDistance = textHeight + textLineSeparation; var dx = textDistance * Math.Sin(textAngle); var dy = textDistance * Math.Cos(textAngle); var textPos2 = new Point3d(textPos.X + dx, textPos.Y - dy, 0); var text2 = (DBText)text.Clone(); ms.UpgradeOpen(); ms.AppendEntity(text2); tr.AddNewlyCreatedDBObject(text2, true);
text2.Position = textPos2;
if (textAlignPos != Point3d.Origin)
                    {
                        text2.AlignmentPoint = new Point3d(textAlignPos.X + dx, textAlignPos.Y - dy, 0);
                    } text2.TextString = "Next Line of Text"; tr.Commit(); } } catch (System.Exception ex) { if (ed != null) { ed.WriteMessage("\n" + ex.Message + "\n"); } } }