Author Topic: Removing StrikeThrough text  (Read 77 times)

0 Members and 1 Guest are viewing this topic.

Jeff H

  • Needs a day job
  • Posts: 5808
Removing StrikeThrough text
« on: August 01, 2017, 06:51:59 pm »
Any ideas on the best approach for removing text with strikethrough formatting, but does not affect any other formatting ?

The example below removes all text with strikethrough formatting, and text that matches without strikethrough formatting because it uses String.Replace which replaces all occurrences in string.
I tried using MTextFragmentCallback so I could use MTextFragment.Strikethrough to avoid parsing each character but without somehow indexing where the fragment is I see no other choice.

Looking at MText.Contents looks like strikethrough text is wrapped in{\K.......} or {....\K....\k....} if nested inside other formatting. I'm guessing I will need to find the position of strikethrough format codes and use String.Remove to remove them?

Code - C#: [Select]
  1.        [CommandMethod("RemoveStrikethroughText")]
  2.        public void RemoveStrikethroughText()
  3.        {
  4.            using (Transaction trx = Doc.TransactionManager.StartTransaction())
  5.            {
  6.                PromptEntityOptions peo = new PromptEntityOptions("\nSelect Mtext or table");
  7.                peo.SetRejectMessage("\nNot Mtext or table");
  8.                peo.AddAllowedClass(typeof(MText), true);
  9.                peo.AllowObjectOnLockedLayer = true;
  10.  
  11.                PromptEntityResult per = Ed.GetEntity(peo);
  12.                if (per.Status != PromptStatus.OK)
  13.                {
  14.                    return;
  15.                }
  16.                List<string> frags = new List<string>();
  17.                MTextFragmentCallback cb = (frag, obj) =>
  18.                {
  19.                    if (frag.Strikethrough)
  20.                    {
  21.                        frags.Add(frag.Text);
  22.                        Ed.WriteLine($"Removing: {frag.Text}");
  23.                    }
  24.  
  25.                    return MTextFragmentCallbackStatus.Continue;
  26.                };
  27.                MText mt = per.ObjectId.GetDBObject<MText>(OpenMode.ForWrite);
  28.                mt.ExplodeFragments(cb);
  29.                foreach (var frag in frags)
  30.                {
  31.                    mt.Contents = mt.Contents.Replace(frag, String.Empty);
  32.                }
  33.  
  34.                trx.Commit();
  35.            }
  36.        }
  37.