Author Topic: Change the background mask property of AcDbMText object programmatically  (Read 3589 times)

0 Members and 1 Guest are viewing this topic.

Offline fixo

  • Full Member
  • ***
  • Posts: 135
  • Karma: +4/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Good
    • View Profile

See for more an article written  by Fenton Webb here
Code: [Select]
        Public Shared Function fixoGetEntity(tr As Transaction, ed As Editor, rx As RXClass, msg As String, ByRef id As ObjectId) As ErrorStatus
            Dim es As ErrorStatus
            Dim ent As Entity
            id = ObjectId.Null
            Dim peo As New PromptEntityOptions(msg)
            peo.SetRejectMessage(vbLf & "You're missing, try again >>")
            peo.AddAllowedClass(GetType(Entity), False)
            Dim res As PromptEntityResult
            res = ed.GetEntity(peo)
            If res.Status <> PromptStatus.OK Then
                es = ErrorStatus.PointNotOnEntity
            End If
            id = res.ObjectId
            If id = ObjectId.Null Then
                es = ErrorStatus.NullObjectId
            End If
            ent = TryCast(tr.GetObject(id, OpenMode.ForRead, False), Entity)
            If ent.GetRXClass() <> rx Then
                ed.WriteMessage(vbLf & "{0}Must be a type of ""{0}"" only!", rx.DxfName)
                es = ErrorStatus.NotThatKindOfClass
            End If
            If ent Is Nothing Then
                es = ErrorStatus.NotAnEntity
                es = ErrorStatus.OK
            End If
            Return es
        End Function
        <CommandMethod("BM")> _
        Public Shared Sub test_setBackgroundFill_MText()
            Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument

            Dim ed As Editor = doc.Editor

            Dim db As Database = HostApplicationServices.WorkingDatabase

            Dim id As ObjectId = ObjectId.Null

            Using tr As Transaction = db.TransactionManager.StartTransaction()
                If fixoGetEntity(tr, ed, RXClass.GetClass(GetType(MText)), vbLf & "Please pick an MText entity: ", id) = ErrorStatus.OK Then

                    Dim btr As BlockTableRecord = TryCast(tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)
                    ' get entity by direct cast
                    Dim ent As Entity = DirectCast(tr.GetObject(id, OpenMode.ForRead), Entity)

                    If id.ObjectClass.DxfName = "MTEXT" Then
                        Dim mtx As MText = TryCast(ent, MText)


                        Dim color As Autodesk.AutoCAD.Colors.Color

                        If mtx.BackgroundFill Then

                            mtx.UseBackgroundColor = False

                            mtx.BackgroundFill = False

                            mtx.BackgroundFill = True

                            color = color.FromColorIndex(ColorMethod.ByAci, CInt(1))

                            mtx.BackgroundFillColor = color

                            mtx.UseBackgroundColor = False
                        End If

                    End If
                End If
            End Using
        End Sub

Code: [Select]
        public static ErrorStatus fixoGetEntity(Transaction tr, Editor ed, RXClass rx, string msg, out ObjectId id)
            ErrorStatus es;
            Entity ent;
            id = ObjectId.Null;
            PromptEntityOptions peo = new PromptEntityOptions(msg);
            peo.SetRejectMessage("\nYou're missing, try again >>");
            peo.AddAllowedClass(typeof(Entity), false);
            PromptEntityResult res;
            res = ed.GetEntity(peo);
            if (res.Status != PromptStatus.OK)
                es = ErrorStatus.PointNotOnEntity;
            id = res.ObjectId;
            if (id == ObjectId.Null)
                es = ErrorStatus.NullObjectId;
            ent = tr.GetObject(id, OpenMode.ForRead, false) as Entity;
            if (ent.GetRXClass() != rx)
                ed.WriteMessage("\n{0}Must be a type of \"{0}\" only!", rx.DxfName);
                es = ErrorStatus.NotThatKindOfClass;
            if (ent == null)
                es = ErrorStatus.NotAnEntity;
            else es = ErrorStatus.OK;
            return es;
        static public void test_setBackgroundFill_MText()
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            Editor ed = doc.Editor;

            Database db = HostApplicationServices.WorkingDatabase;

            ObjectId id = ObjectId.Null;

            using (Transaction tr = db.TransactionManager.StartTransaction())
                if (fixoGetEntity(tr, ed, RXClass.GetClass(typeof(MText)), "\nPlease pick an MText entity: ", out id) == ErrorStatus.OK)

                    BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
                   // get entity by direct cast
                    Entity ent = (Entity)tr.GetObject(id, OpenMode.ForRead);

                    if (id.ObjectClass.DxfName == "MTEXT")
                        MText mtx = ent as MText;


                        Autodesk.AutoCAD.Colors.Color color;

                        if (mtx.BackgroundFill )

                            mtx.UseBackgroundColor= false;





                            color = Color.FromColorIndex(ColorMethod.ByAci, (int)1);

                            mtx.BackgroundFillColor = color;