August 2014

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
31            








« ADN DevCast Episode 3 | Main | Changing the case of an MText object using AutoCAD’s in-place editor from .NET »

June 16, 2010

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

blog comments powered by Disqus

Feed/Share

10 Random Posts