Through the Interface: Changing the case of an MText object using AutoCADs in-place editor from .NET

April 2015

Sun Mon Tue Wed Thu Fri Sat
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    


« Stripping MText formatting using AutoCAD’s in-place editor from .NET | Main | Tracing a boundary defined by AutoCAD geometry using .NET »

June 18, 2010

Changing the case of an MText object using AutoCAD’s in-place editor from .NET

In the last post we saw a very simple, preliminary exploration of some of the new programmatic capabilities of the in-place MText editor in AutoCAD 2011. In that basic case we just used it to strip off all formatting from an MText object. Now we’re going to implement a couple of commands to toggle the case of the contents of an MText object between lower- and uppercase.

Here’s the C# code:

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.Runtime;


namespace MTextEditing


  public class Commands



    public void ChangeToUppercase()






    public void ChangeToLowercase()





    private void ChangeCase(bool upper)


      Document doc =


      Database db = doc.Database;

      Editor ed = doc.Editor;


      // Specifically select an MText object


      PromptEntityOptions peo =

        new PromptEntityOptions(


            "\nSelect MText to change to {0}case: ",

            upper ? "upper" : "lower"



      peo.SetRejectMessage("\nObject must be MText.");

      peo.AddAllowedClass(typeof(MText), false);


      PromptEntityResult per = ed.GetEntity(peo);

      if (per.Status != PromptStatus.OK)



      Transaction tr =


      using (tr)


        // We only need our MText open for read


        DBObject obj =

          tr.GetObject(per.ObjectId, OpenMode.ForRead, false);

        MText mt = obj as MText;


        if (mt == null)



        // Create a text editor object for the MText


        TextEditor te = TextEditor.CreateTextEditor(mt);

        if (te == null)



        // Select the entire contents of the MText



        TextEditorSelection sel = te.Selection;

        if (sel == null)



        // Check whether we can change the selection's

        // case, and then do so


        if (sel.CanChangeCase)


          if (upper)






        // Be sure to save the results from the editor








After building the code into a DLL and NETLOADing it, we can use our new custom commands to change the case of the entire contents of an MText object.


Our multi-coloured mtext with different cases

After using the CTU command to change it to all caps…

Our multi-coloured mtext in uppercase

After using the CTL command to change it to all lowercase…

Our multi-coloured mtext in lowercase

blog comments powered by Disqus


10 Random Posts