Multiple Background Mtext

Multiple Background Mtext

traiduong014969
Collaborator Collaborator
556 Views
5 Replies
Message 1 of 6

Multiple Background Mtext

traiduong014969
Collaborator
Collaborator

Hi everyone,

I have an issue that I need help with. Here are the details:

I created a command to make the background of multiple MText objects. However, after running this command, all the text merges into a single line. I would like to keep the original positions of the text. How can I achieve that?

 

 [CommandMethod("QM")]
        public void ApplyBackgroundMaskToMTexts()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            PromptSelectionOptions pso = new PromptSelectionOptions();
            pso.MessageForAdding = "Select MText:";
            SelectionFilter filter = new SelectionFilter(new TypedValue[] { new TypedValue((int)DxfCode.Start, "MTEXT") });
            PromptSelectionResult psr = ed.GetSelection(pso, filter);

            if (psr.Status != PromptStatus.OK)
            {
                ed.WriteMessage("\nNone Object selected.");
                return;
            }

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                SelectionSet sset = psr.Value;
                foreach (SelectedObject selObj in sset)
                {
                    if (selObj != null)
                    {
                        MText mtext = tr.GetObject(selObj.ObjectId, OpenMode.ForWrite) as MText;
                        if (mtext != null)
                        {
                           
                            mtext.BackgroundFill = true;
                            mtext.BackgroundScaleFactor = 1;
                            mtext.BackgroundFillColor = Color.FromColorIndex(ColorMethod.ByAci, 1); 
                            mtext.ColumnType = ColumnType.NoColumns;
                            mtext.Width = 0;
                            mtext.Height = 0;
                        }
                    }
                }

                tr.Commit();
            }

        }
    }

 

traiduong014969_0-1724981786195.png

 

 
0 Likes
557 Views
5 Replies
Replies (5)
Message 2 of 6

cuongtk2
Advocate
Advocate

Not set mtext.width, height

0 Likes
Message 3 of 6

traiduong014969
Collaborator
Collaborator

Yes, if I don’t set the maximum width and height, some background areas and Mtext might be redundant

traiduong014969_0-1724984470052.png

 

 
0 Likes
Message 4 of 6

cuongtk2
Advocate
Advocate

Bạn hãy lưu giá trị width trước khi thay đổi mtext

0 Likes
Message 5 of 6

traiduong014969
Collaborator
Collaborator

Yes, I repaired it like this, but it still combines into a single line

traiduong014969_0-1724987731494.png

 

0 Likes
Message 6 of 6

GiaBach
Contributor
Contributor

Add the UseBackgroundColor=false.

                        if (mtext != null)
                        {
                            mtext.BackgroundFill = true;
                            mtext.BackgroundScaleFactor = 1;
                            mtext.BackgroundFillColor = Color.FromColorIndex(ColorMethod.ByAci, 1);
                            //mtext.ColumnType = ColumnType.NoColumns;
                            //mtext.Width = 0;
                            //mtext.Height = 0;

                            mtext.UseBackgroundColor = false;
                        }
0 Likes