creating text using .net

creating text using .net

Anonymous
Not applicable
857 Views
1 Reply
Message 1 of 2

creating text using .net

Anonymous
Not applicable

there are a lot of examples how to create text in autocad but this text already in code. question is how to INPUT text and it's properties such as point where text should be and style(for example "times new roman, size 12"). should i use PromptStringResult for transfering it to my string variable?

0 Likes
Accepted solutions (1)
858 Views
1 Reply
Reply (1)
Message 2 of 2

_gile
Consultant
Consultant
Accepted solution

Hi,

 

You can use Editor.GetString() to prompt the user for a text style but you'll also have to check if this text style already exists in the drawing text style table.

 

        [CommandMethod("Test")]
        public void Test()
        {
            var doc = AcAp.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;

            // prompt for insertion point
            var ptResult = ed.GetPoint("\nInsertion point: ");
            if (ptResult.Status != PromptStatus.OK)
                return;
            var point = ptResult.Value;

            using (var tr = db.TransactionManager.StartTransaction())
            {
                var styles = (TextStyleTable)tr.GetObject(db.TextStyleTableId, OpenMode.ForRead);

                // prompt for text style
                var strOptions = new PromptStringOptions("\nEnter the text style name: ");
                PromptResult strResult;
                while (true)
                {
                    strResult = ed.GetString(strOptions);
                    if (strResult.Status != PromptStatus.OK)
                        return;
                    if (styles.Has(strResult.StringResult))
                        break;
                    ed.WriteMessage("\nText style not found: {0}", strResult.StringResult);
                }
                var styleId = styles[strResult.StringResult];

                // prompt for text contents
                strOptions.Message = "\nEnter the text: ";
                strOptions.AllowSpaces = true;
                strResult = ed.GetString(strOptions);
                if (strResult.Status != PromptStatus.OK)
                    return;
                var textString = strResult.StringResult;

                // add the text to the current space
                var text = new DBText();
                text.Position = point;
                text.TextStyleId = styleId;
                text.TextString = textString;
                var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                curSpace.AppendEntity(text);
                tr.AddNewlyCreatedDBObject(text, true);

                tr.Commit();
            }
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub