Author Topic: Strip Mtext  (Read 723 times)

0 Members and 1 Guest are viewing this topic.

Offline (gile)

  • C#
  • *
  • Posts: 87
  • Karma: +8/-0
  • Gender: Male
    • prefered language: F
    • Prog expertise: Good
    • View Profile
Strip Mtext
« on: November 21, 2010, 09:57:29 PM »
Hi,

This code was inspired by a Tony Tanzillo's one using the Mtext.ExplodeFragments() method to remove all Mtext formatting.

A command: STRIPMT to unformat all selected Mtexts
A LISP function: gc-GetStrippedMtextString to return the unformated text string of the mtext which ename is passed as argument to the function

Code: [Select]
using System.Text;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;

using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace StripMtextSample
{
    public class StripMtext
    {
        private StringBuilder m_sb = null;

        private string GetStrippedMtextContents(MText mt)
        {
            m_sb = new StringBuilder();
            mt.ExplodeFragments(new MTextFragmentCallback(FragmentCallback));
            return m_sb.ToString();
        }

        private MTextFragmentCallbackStatus FragmentCallback(MTextFragment fragment, object obj)
        {
            m_sb.Append(fragment.Text);
            return MTextFragmentCallbackStatus.Continue;
        }

        [CommandMethod("STRIPMT", CommandFlags.Modal | CommandFlags.UsePickSet)]
        public void StripSelSet()
        {
            Document doc = AcAp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            SelectionFilter filter = new SelectionFilter(new TypedValue[] { new TypedValue(0, "MTEXT") });
            ed.WriteMessage("\nSelect mtexts (or type 'Enter' for 'all').");
            PromptSelectionResult psr = ed.GetSelection(filter);
            if (psr.Status == PromptStatus.Error)
                psr = ed.SelectAll(filter);
            if (psr.Status != PromptStatus.OK)
                return;
            try
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    foreach (ObjectId id in psr.Value.GetObjectIds())
                    {
                        MText mt = (MText)tr.GetObject(id, OpenMode.ForWrite);
                        mt.Contents = GetStrippedMtextContents(mt);
                    }
                    tr.Commit();
                }
            }
            catch (System.Exception e)
            {
                ed.WriteMessage(string.Format("\nError: {0}\n", e.Message));
            }
        }

        [LispFunction("gc-GetStrippedMtextString")]
        public string GetStrippedMtextString(ResultBuffer resbuf)
        {
            try
            {
                if (resbuf == null)
                    throw new LispException("to few arguments");
                TypedValue[] arg = resbuf.AsArray();
                if (arg.Length > 1)
                    throw new LispException("to many arguments");
                if (arg[0].TypeCode != (short)LispDataType.ObjectId)
                    throw new LispException(string.Format("incorrect argument type: lentityp: {0}", arg[0].Value));
                Database db = HostApplicationServices.WorkingDatabase;
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    MText mt = tr.GetObject(((ObjectId)arg[0].Value), OpenMode.ForRead) as MText;
                    if (mt == null)
                        throw new LispException("incorrect argument type: not a mtext");
                    return GetStrippedMtextContents(mt);
                }
            }
            catch (LispException e)
            {
                AcAp.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nError: {0}\n", e.Message);
                return null;
            }
        }
    }

    class LispException : System.Exception
    {
        public LispException(string message)
            : base(message)
        {
        }
    }
}

target audience:{intermediate}