PDA

View Full Version : wordwrap problems in AutoCAD



thamest
03-22-2007, 07:23 AM
I have a textbox that an operator types in, and I need to format the string according to a max column witdh(55 -60 characters). I tried to tokenize the string, InStr, InStrRev, but I still accasionally get a line that run's over the frames border. Is there anyway to get the string wordwrapped to a max column width of 60 characters?
Here's the code I've written that works best so far:
But still doesn't work everytime.

Public Sub BuildDescrArray()
Dim sTmp, sTmp2 As String
Dim i As Integer, iLen As Integer
If DescCnt = "" Then
msg = MsgBox("There must be at least one line item entered in the Description Column", vbCritical, Missing Information")
Exit Sub
StartForm.Show
End If
ReDim Preserve DescStrArray(0 To DescCnt)
sTmp = Array(Split(StartForm.TextBox3.Text, (vbCrLf & vbCrLf))) '''split first time according to double carriage
For b = 0 To UBound(sTmp(0)) '''returns in the textbox
sTmp2 = Split(sTmp(0)(b), vbCrLf)
For c = 0 To UBound(sTmp2)
If Len(sTmp2(c)) > 54 Then
If Len(sTmp2(c)) > 108 Then
Do
position = InStr((position + 1), sTmp2(c), " ")
spacecnt = spacecnt + 1
Loop Until position = 0
position = InStr(1, sTmp2(c), " ")
Do
If spacecnt = 0 Then
Exit Do
End If
position = InStr((position + 1), sTmp2(c), " ")
If position >= 54 Then
sTmp2(c) = Left(sTmp2(c), position) & vbCr & Right(sTmp2(c), Len(sTmp2(c)) - position)
tmppos = position
tmppos1 = position
Do
If spacecnt = 0 Then
Exit Do
End If
position = InStr((tmppos1 + 1), sTmp2(c), " ")
tmppos1 = position
spacecnt = spacecnt - 1
Loop Until position >= (tmppos + 54) Or position = 0
End If '''position >= 54
spacecnt = spacecnt - 1
Loop Until position = 0
Else
position = InStrRev(sTmp2(c), " ", Len(sTmp2(c)))
Do
position = InStrRev(sTmp2(c), " ", (position - 1))
If position <= 54 Then
sTmp2(c) = Left(sTmp2(c), position) & vbCr & Right(sTmp2(c), Len(sTmp2(c)) - position)
Exit Do
End If '''position <= 54
Loop Until position = 0
End If
Else
DescStrArray(b) = sTmp2(c)
'Exit For

End If
tempstr = tempstr & sTmp2(c) & vbCr
Next c
DescStrArray(b) = Split(tempstr, vbCr) '''split second time according to carriage returns in the lineNext b
tempstr = ""
Next b
End Sub

EDIT: Added VBA Code tags Tommy

Tommy
03-22-2007, 04:36 PM
Hi thamest,

Welcome to VBAX! :hi:

First:

If DescCnt = "" Then
msg = MsgBox("There must be at least one line item entered in the Description Column", vbCritical, Missing Information")
Exit Sub
StartForm.Show '<-- this never happens
End If


As a suggestion look into replace. I think a replace on the vbCrLf would simplify you code a lot. then you could check the length for the number of vbCr to insert in the string.

Are you going to have to deal with mtext formatting?
The width facter?
ttf and/or shx?
BigFonts?

I use a facter of 8 when the text height is 12 and the width facter is 0.75. Since I ussually write dxf files I have to figure the actual text insertion point, no acad to figure it for me :(. But this number seems to work the best over all. Very few overwrites on the center, left, and right justifications. The ones that show up are - well now whats. LOL

thamest
03-23-2007, 05:34 AM
There "should be" no mtext. we xplode everything(VBA) with every save. The font is always the same, Romans. How would I measure according to width factor? The width factor we use is 0.900. The available width space is approx. 5.25in. What this is for is a revision write-up. We are still using Lisp for this and management want's everything done in vba. The top line:
if desccnt = "", is just to make sure that the operator has entered data in before continuing. To make everything "easy" for the operator, (press enter twice for a new line item and press enter once for a new line). I split first according to vbcrlf & vbcrlf, then split again according to vbcr.
Then go through and format each line to the width. I am still basically a newbie in Acad vba, written several apps in vb6; but this the first cad-vba app.

Thanks for your reply, any help at this point is useful. And maybe I learn something new......

Tommy
03-23-2007, 02:21 PM
I butchered your code. I had to dim some of the vars so you may need to undim them. I think this is what you are looking for. I basically split the string the same way you have, I just look for a " " at the end to the front for a place to break the line and add a vbcr between the lines.

Public Sub BuildDescrArray()
Dim sTmp, sTmp2() As String', DescStrArray() As Variant, DescCnt As String
Dim i As Integer, iLen As Integer, tempstr As String
'DescCnt = 30
If DescCnt = "" Then
msg = MsgBox("There must be at least one line item entered in the Description Column", _
vbCritical, "Missing Information")
Exit Sub
StartForm.Show
End If
ReDim Preserve DescStrArray(0 To DescCnt)
sTmp = Array(Split(StartForm.TextBox3.Text, (vbCrLf & vbCrLf)))
'''split first time according to double carriage
For b = 0 To UBound(sTmp(0)) '''returns in the textbox
sTmp2 = Split(sTmp(0)(b), vbCrLf)
For c = 0 To UBound(sTmp2)
While Len(sTmp2(c)) > 54
tempstr = tempstr & Left(sTmp2(c), InStrRev(sTmp2(c), " ", 54)) & vbCr
sTmp2(c) = Mid(sTmp2(c), InStrRev(sTmp2(c), " ", IIf(Len(sTmp2(c)) >= 54, 54, Len(sTmp2(c)))))
Wend
tempstr = tempstr & sTmp2(c) & vbCr
Next c
DescStrArray(b) = Split(tempstr, vbCr)
'''split second time according to carriage returns in the lineNext b
tempstr = ""
Next b
End Sub


HTH
Let me know if you need more help. But I would like a sample next time so I don't have to spend a lot of time setting up to debug/test.

I grew up in Starkville and West Point. :)

thamest
03-24-2007, 07:56 PM
Thanks Tommy, I would have never thought about doing it that way. Now I can scrap the idea of trying to use a bounding box, which wreaks havock when placing the text. You are the first person to give me a real idea of how to go about doing this.:bow: