TheSwamp

Code Red => .NET => Topic started by: Jeff H on August 01, 2017, 06:51:59 PM

Title: Removing StrikeThrough text
Post by: Jeff H 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 (https://msdn.microsoft.com/en-us/library/fk49wtc1(v=vs.110).aspx) which replaces all occurrences in string.
I tried using MTextFragmentCallback (http://help.autodesk.com/view/OARX/2018/ENU/?guid=OREFNET-Autodesk_AutoCAD_DatabaseServices_MText_ExplodeFragments_MTextFragmentCallback_object) so I could use MTextFragment.Strikethrough (http://help.autodesk.com/view/OARX/2018/ENU/?guid=OREFNET-Autodesk_AutoCAD_DatabaseServices_MTextFragment_Strikethrough) to avoid parsing each character but without somehow indexing where the fragment is I see no other choice.

Looking at MText.Contents (http://help.autodesk.com/view/OARX/2018/ENU/?guid=OREFNET-Autodesk_AutoCAD_DatabaseServices_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 (https://msdn.microsoft.com/en-us/library/d8d7z2kk(v=vs.110).aspx) 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.