TheSwamp

Code Red => .NET => Topic started by: slappy on August 25, 2010, 03:43:37 PM

Title: Purging empty text object in VB.NET
Post by: slappy 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
Title: Re: Purging empty text object in VB.NET
Post by: Bryco on August 25, 2010, 03:56:59 PM
if (string.IsNullOrEmpty(text.TextString.Trim()))   is this available in vb
Title: Re: Purging empty text object in VB.NET
Post by: Jeff H on August 25, 2010, 04:21:33 PM
Is there ever an instance or will AutoCad allow you to have an empty string
Title: Re: Purging empty text object in VB.NET
Post by: slappy 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.
Title: Re: Purging empty text object in VB.NET
Post by: slappy 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:
Title: Re: Purging empty text object in VB.NET
Post by: David Hall 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.
Title: Re: Purging empty text object in VB.NET
Post by: Kerry 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]
Title: Re: Purging empty text object in VB.NET
Post by: Jeff H on August 25, 2010, 05:21:20 PM
I just did the empty space thing and it was flagged to be erased
Title: Re: Purging empty text object in VB.NET
Post by: kaefer 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()
Title: Re: Purging empty text object in VB.NET
Post by: gile 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 (http://www.theswamp.org/index.php?topic=33516.0).

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();
        }
Title: Re: Purging empty text object in VB.NET
Post by: Kerry 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.
Title: Re: Purging empty text object in VB.NET
Post by: Jeff H 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
Title: Re: Purging empty text object in VB.NET
Post by: Jeff H 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

Title: Re: Purging empty text object in VB.NET
Post by: gile 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;
}