Stripping MText formatting using AutoCAD’s in-place editor from .NET

Last week we saw a series of simple posts about creating, placing and editing MText. Barry Ralphs asked about the ability to fire off editing commands to the in-place MText editor, which – interestingly – was a new feature in AutoCAD 2011, implemented primarily to enable the control of the MText IPE via AutoCAD's ribbon.

While I'm not yet covering Barry's specific question, here's the first of (hopefully) a series of posts which looks into the API now exposed for the MText IPE. There appear to be some quirks related to selection (and especially searching) of an MText's contents using the IPE, so I'm taking baby steps on this one.

[The material I'm using as a starting point is available along with the AutoCAD 2011 New APIs webcast, also linked to from the ADN webcast archive.]

To get the ball rolling, let's use the IPE to strip away all that nice formatting we added in last week's first post. Here's some C# code to do so:

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.Runtime;

 

namespace MTextEditing

{

  public class Commands

  {

    [CommandMethod("SMF")]

    public void StripMTextFormatting()

    {

      Document doc =

        Application.DocumentManager.MdiActiveDocument;

      Database db = doc.Database;

      Editor ed = doc.Editor;

 

      // Specifically select an MText object

 

      PromptEntityOptions peo =

        new PromptEntityOptions(

          "\nSelect MText to strip of formatting: "

        );

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

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

 

      PromptEntityResult per = ed.GetEntity(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;

 

        // Simply select the entire contents and strip

        // all formatting from the selection

 

        te.SelectAll();

        te.Selection.RemoveAllFormatting();

 

        // Be sure to save the results from the editor

 

        te.Close(TextEditor.ExitStatus.ExitSave);

        tr.Commit();

      }

    }

  }

}

We can use our new SMF command to strip the formatting from a particular MText object:

Before…

Our multi-coloured mtext

After…

Our newly monochrome mtext

6 responses to “Stripping MText formatting using AutoCAD’s in-place editor from .NET”

  1. MText.ExplodeFragments() seems to work well for me, and has no dependence on the AutoCAD drawing editor.

  2. Kean,

    I am wondering how to do this on table objects. When importing a table from excel as pasting special ACAD objects, you can reset the table overrides, but this does not reset the cell content. You manually have to go through each cell to reset the formatting of the content.

    I have written an app that iterates through the selected table's rows/columns and gets the content in an attempt to use the text editor to reset the content of each cell. However, the texteditor.create only allows for MTEXT in which the table content is not MTEXT.

    I've looked on your other blog posts on tables and don't see a way to do this. Please help if you can.

    Thanks.

    Steve

  3. Steve,

    That sounds like an interesting blog topic. I'll take a look when I get some time.

    Regards,

    Kean

  4. Hi Kean (and readers) - q: why use the IPE when we can get the .Text property of the mtext objects - if we substitute the contents there for the .Contents property won't it come out unformatted? i hope im making sense?

    1. MText.ExplodeFragments is probably the best way to get at unformatted content. This post was exploring a new (at the time) API that was available.

      Kean

Leave a Reply to Ben Cancel reply

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