Author Topic: Purging empty text object in VB.NET  (Read 4416 times)

0 Members and 1 Guest are viewing this topic.

slappy

  • Guest
Purging empty text object in VB.NET
« on: August 25, 2010, 03:43:37 PM »
Any thoughts?

Doing a symble check of:
Code: [Select]
String.Emptyon the contents doesn't seem to work.

Here is what I've tried
Code: [Select]
Dim txt As DBText = TryCast(tr.GetObject(id, OpenMode.ForRead), DBText)
If txt IsNot Nothing Then
   If txt.TextString.Trim() = String.Empty Then
      txt.UpgradeOpen()
      txt.[Erase]()
   End If
   Continue For
End If
Dim mTxt As MText = TryCast(tr.GetObject(id, OpenMode.ForRead), MText)
If mTxt IsNot Nothing Then
   Dim result As String = mTxt.Contents
   If result.Trim = String.Empty Then
      mTxt.UpgradeOpen()
      mTxt.[Erase]()
    End If
End If

Thanks

Chris

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Purging empty text object in VB.NET
« Reply #1 on: August 25, 2010, 03:56:59 PM »
if (string.IsNullOrEmpty(text.TextString.Trim()))   is this available in vb

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Purging empty text object in VB.NET
« Reply #2 on: August 25, 2010, 04:21:33 PM »
Is there ever an instance or will AutoCad allow you to have an empty string

slappy

  • Guest
Re: Purging empty text object in VB.NET
« Reply #3 on: August 25, 2010, 04:24:00 PM »
if (string.IsNullOrEmpty(text.TextString.Trim()))   is this available in vb

It is!  I'll see if that works.

slappy

  • Guest
Re: Purging empty text object in VB.NET
« Reply #4 on: August 25, 2010, 04:27:58 PM »
Is there ever an instance or will AutoCad allow you to have an empty string

You would think... :lol:

However, there is the option to purge it and it tells me there is one and that it is removed during a manual "-purge".  Draw your own conclusions.   :evil:

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Purging empty text object in VB.NET
« Reply #5 on: August 25, 2010, 04:44:15 PM »
Is there ever an instance or will AutoCad allow you to have an empty string
If you have one of those "power" users that erase text by replacing the contents with a single space, you can get these.
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Purging empty text object in VB.NET
« Reply #6 on: August 25, 2010, 04:53:34 PM »

[offTopic]

I'll have to stop looking at VB.net code ;
a language that has the same operator for Assignment and Logical Comparison gives me a headache.

[/backToIt]
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Purging empty text object in VB.NET
« Reply #7 on: August 25, 2010, 05:21:20 PM »
I just did the empty space thing and it was flagged to be erased

kaefer

  • Guest
Re: Purging empty text object in VB.NET
« Reply #8 on: August 25, 2010, 05:24:29 PM »
Is there ever an instance or will AutoCad allow you to have an empty string
If you have one of those "power" users that erase text by replacing the contents with a single space, you can get these.
Why, can't you screw up automagically all by yourself?

Code: [Select]
    let db = acApp.DocumentManager.MdiActiveDocument.Database
    use tr = db.TransactionManager.StartTransaction()
    let btr = db.CurrentSpaceId.GetObject OpenMode.ForWrite :?> BlockTableRecord
    // Empty text
    let tx = new DBText()
    btr.AppendEntity tx |> ignore
    tr.AddNewlyCreatedDBObject(tx, true)
    // Zero length geometry
    let pl = new Polyline()
    btr.AppendEntity pl |> ignore
    tr.AddNewlyCreatedDBObject(pl, true)
    tr.Commit()

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Purging empty text object in VB.NET
« Reply #9 on: August 26, 2010, 12:39:28 AM »
Hi,

With mtexts, you have to consider formated strings: "{\fArial|b0|i0|c0|p34; }" is an empty mtext content.

The way I deal with this here.

Code: [Select]
// ...
DBText txt = tr.GetObject(id, OpenMode.ForRead) as DBText;
                                if (txt != null)
                                {
                                    if (txt.TextString.Trim() == string.Empty)
                                    {
                                        txt.UpgradeOpen();
                                        txt.Erase();
                                    }
                                    continue;
                                }
                                MText mTxt = tr.GetObject(id, OpenMode.ForRead) as MText;
                                if (mTxt != null && UnFormatMtext(mTxt.Contents).Trim() == string.Empty)
                                {
                                    mTxt.UpgradeOpen();
                                    mTxt.Erase();
                                }
//...

 private string UnFormatMtext(string str)
        {
            StringBuilder sb = new StringBuilder(str);
            string[] patterns = new string[14] {
                "\\A", "\\T", "\\C", "\\Q", "\\W", "\\H", "\\F",
                "\\a", "\\t", "\\c", "\\q", "\\w", "\\h", "\\f"};
            while (sb.ToString().Contains(MText.BlockBegin))
            {
                int begin = sb.ToString().IndexOf(MText.BlockBegin);
                int end = sb.ToString().IndexOf(MText.BlockEnd, begin);
                StringBuilder sub =
                    new StringBuilder(sb.ToString().Substring(begin + 1, end - begin - 1));
                foreach (string pat in patterns)
                {
                    while (sub.ToString().Contains(pat))
                    {
                        int n = sub.ToString().IndexOf(pat);
                        sub.Remove(n, sub.ToString().IndexOf(';', n) - n + 1);
                    }
                }
                sb.Remove(begin, end - begin + 1);
                sb.Insert(begin, sub.ToString());
            }
            int i = 0;
            while ((i = sb.ToString().IndexOf("\\S", i)) != -1)
            {
                int j = sb.ToString().IndexOf(';', i);
                if (j != -1)
                {
                    int k = sb.ToString().IndexOf('/', i);
                    if (k != -1 && k < j)
                    {
                        sb.Remove(j, 1); ;
                        sb.Remove(i, 2);
                        continue;
                    }
                    k = sb.ToString().IndexOf('#', i);
                    if (k != -1 && k < j)
                    {
                        sb.Remove(j, 1);
                        sb.Remove(k, 1);
                        sb.Insert(k, '/');
                        sb.Remove(i, 2);
                        continue;
                    }
                    k = sb.ToString().IndexOf('^', i);
                    if (k != -1 && k < j)
                    {
                        sb.Remove(j, 1);
                        sb.Remove(k, 1);
                        sb.Insert(k, '/');
                        sb.Remove(i, 2);
                        continue;
                    }
                    i++;
                }
            }
            patterns = new string[7] { "\\P", "\\p", "\\L", "\\p", "\\O", "\\o", "\\~" };
            foreach (string pat in patterns)
            {
                sb.Replace(pat, string.Empty);
            }
            return sb.ToString();
        }
Speaking English as a French Frog

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Purging empty text object in VB.NET
« Reply #10 on: August 26, 2010, 01:01:08 AM »

gile,

Have you played with

MtextFragment class
and
ExplodeFragements() method in the MText Class  to strip the formatting ??

I recall Tony posted a link(on the newsgroup) to a sample on his site.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Purging empty text object in VB.NET
« Reply #11 on: August 27, 2010, 09:09:02 AM »
This erased them

Code: [Select]
Dim pad As String = ""
    If txt.TextString = pad.PadRight(txt.TextString.Count) Then
        txt.UpgradeOpen()
        txt.Erase()
    End If

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Purging empty text object in VB.NET
« Reply #12 on: August 27, 2010, 09:18:56 AM »
It did not work for Mtext you could use what Kerry what mentioned
Or maybe do a string.split and use "\" as the dilimter and check for <=1


gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Purging empty text object in VB.NET
« Reply #13 on: August 28, 2010, 04:54:50 AM »
Thanks Kerry.

I didn't play with ExplodeFragments() before, I'm glad having learned something new this morning.

Here's a new example, inspired by Tony's sample.

Code: [Select]
// ...
DBText txt = tr.GetObject(id, OpenMode.ForRead) as DBText;
if (txt != null)
{
    if (txt.TextString.Trim() == string.Empty)
    {
        txt.UpgradeOpen();
        txt.Erase();
    }
    continue;
}
MText mTxt = tr.GetObject(id, OpenMode.ForRead) as MText;
if (mTxt != null && GetStripedMtextContents(mTxt).Trim() == string.Empty)
{
     mTxt.UpgradeOpen();
     mTxt.Erase();
}
//...

private string m_str;

private string GetStripedMtextContents(MText mt)
{
     m_str = string.Empty;
     mt.ExplodeFragments(new MTextFragmentCallback(FragmentCallback));
     return m_str;
 }

private MTextFragmentCallbackStatus FragmentCallback(MTextFragment fragment, object obj)
{
     m_str += fragment.Text;
     return MTextFragmentCallbackStatus.Continue;
}
Speaking English as a French Frog