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

  {

    [CommandMethod("CTU")]

    public void ChangeToUppercase()

    {

      ChangeCase(true);

    }

 

    [CommandMethod("CTL")]

    public void ChangeToLowercase()

    {

      ChangeCase(false);

    }

 

    private void ChangeCase(bool upper)

    {

      Document doc =

        Application.DocumentManager.MdiActiveDocument;

      Database db = doc.Database;

      Editor ed = doc.Editor;

 

      // Specifically select an MText object

 

      PromptEntityOptions peo =

        new PromptEntityOptions(

          string.Format(

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

            upper ? "upper" : "lower"

          )

        );

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

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

 

      PromptEntityResult per = ed.Get
Entity(peo);

      if (per.Status != PromptStatus.OK)

        return;

 

      Transaction tr =

        doc.TransactionManager.StartTransaction();

      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)

          return;

 

        // Create a text editor object for the MText

 

        TextEditor te = TextEditor.CreateTextEditor(mt);

        if (te == null)

          return;

 

        // Select the entire contents of the MText

 

        te.SelectAll();

        TextEditorSelection sel = te.Selection;

        if (sel == null)

          return;

 

        // Check whether we can change the selection's

        // case, and then do so

 

        if (sel.CanChangeCase)

        {

          if (upper)

            sel.ChangeToUppercase();

          else

            sel.ChangeToLowercase();

        }

 

        // Be sure to save the results from the editor

 

        te.Close(TextEditor.ExitStatus.ExitSave);

        tr.Commit();

      }

    }

  }

}

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.

Before…

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

6 responses to “Changing the case of an MText object using AutoCAD’s in-place editor from .NET”

  1. Gustavo Barreto Avatar

    Hi Kean,

    I would like to know if there is any way to know if an mtext object is being edited via mtext editor. I don´t know if there is some overrule or something to do it. The idea is to read the entered string, and if it is numeric, to use its value to modify another object's property.

    Thanks for your help.

    Gustavo

    1. Hi Gustavo,

      You could use Database.ObjectModified() to check for changes to MText objects.

      Regards,

      Kean

  2. Hello Kean, thank you for this post. i'm confused and i think i'm missing something here: if i wanted to edit the mtext i could click on it and edit it? i'm not clear on the benefit of the in-place MText editor?

    1. Kean Walmsley Avatar

      Hi Ben,

      Of course you could. The point is that you might want to automate certain repetitive tasks...

      Kean

  3. Heelo Kean,
    Can you write a post about checking characters (including alphabet characters, numbers and special characters) in a selection set of Mtext and replace it with another characters, please ?

    1. Hello Quân,

      I'm not currently blogging about AutoCAD-related topics. I suggest researching RegExp (regular expressions) or posting a request via the AutoCAD .NET forum or via Stack Overflow.

      Best regards,

      Kean

Leave a Reply to Quân Trần Cancel reply

Your email address will not be published. Required fields are marked *