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?
[CommandMethod("RemoveStrikethroughText")]
public void RemoveStrikethroughText()
{
using (Transaction trx = Doc.TransactionManager.StartTransaction())
{
PromptEntityOptions peo
= new PromptEntityOptions
("\nSelect Mtext or table"); peo.SetRejectMessage("\nNot Mtext or table");
peo
.AddAllowedClass(typeof(MText
),
true); peo.AllowObjectOnLockedLayer = true;
PromptEntityResult per = Ed.GetEntity(peo);
if (per.Status != PromptStatus.OK)
{
return;
}
List
<string> frags
= new List
<string>(); MTextFragmentCallback cb = (frag, obj) =>
{
if (frag.Strikethrough)
{
frags.Add(frag.Text);
Ed.WriteLine($"Removing: {frag.Text}");
}
return MTextFragmentCallbackStatus.Continue;
};
MText mt = per.ObjectId.GetDBObject<MText>(OpenMode.ForWrite);
mt.ExplodeFragments(cb);
foreach (var frag in frags)
{
mt.Contents = mt.Contents.Replace(frag, String.Empty);
}
trx.Commit();
}
}