REPLACE

REPLACE

Anonymous
Not applicable
954 Views
15 Replies
Message 1 of 16

REPLACE

Anonymous
Not applicable
I am trying to make an autocad routine that allows the user to select a text or mtext object one at a time using the mouse and look for "S" within that text and replace it with "N" Note: The "S" will only appear once within the object. Any ideas?
0 Likes
955 Views
15 Replies
Replies (15)
Message 2 of 16

Anonymous
Not applicable
VBA has a Replace function for strings.
0 Likes
Message 3 of 16

Anonymous
Not applicable
Hi,

Try the following

Sub ReplaceSwithN()
On Error Resume Next
Dim oEnt As AcadEntity
Dim Pt
Dim sTmp As String
ThisDrawing.Utility.GetEntity oEnt, Pt
Do While Err = 0
ReplaceString oEnt, "S", "N"
ThisDrawing.Utility.GetEntity oEnt, Pt
Loop
Err.Clear
End Sub ' ReplaceSwithN

Function ReplaceString(oEnt As AcadEntity, sCurrent As String, sNew As
String) As Boolean
Dim oText As AcadText
Dim oMText As AcadMText
ReplaceString = True
If TypeOf oEnt Is AcadText Then
Set oText = oEnt
sTmp = oText.TextString
sTmp = Replace(sTmp, sCurrent, sNew)
oText.TextString = sTmp
oText.Update
ElseIf TypeOf oEnt Is AcadMText Then
Set oMText = oEnt
sTmp = oMText.TextString
sTmp = Replace(sTmp, sCurrent, sNew)
oMText.TextString = sTmp
oMText.Update
Else
MsgBox "The selected object was neither Text or MText", vbInformation
ReplaceString = False
End If

End Function ' ReplaceString


--


Regards

Laurie Comerford
wrote in message
news:5864036@discussion.autodesk.com...
I am trying to make an autocad routine that allows the user to select a text
or mtext object one at a time using the mouse and look for "S" within that
text and replace it with "N" Note: The "S" will only appear once within the
object. Any ideas?
0 Likes
Message 4 of 16

Anonymous
Not applicable
As was mentioned in other posts, you can use the VBA Replace() function to replace parts of a string.

But, you should not consider the code Laurie posted a good example of how to do that, because it calls Replace() on every selected text/mtext object and reassigns the result, even when it's unnecessary (e.g., when the string to replace does not even exist in the text).

Instead, use the InStr() function to see if the string you want to replace exists in the text string first, and only proceed to use Replace() if it does.
That also allows you to report back to the user, how many text objects were actually modified.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

wrote in message news:5864036@discussion.autodesk.com...
I am trying to make an autocad routine that allows the user to select a text or mtext object one at a time using the mouse and look for "S" within that text and replace it with "N" Note: The "S" will only appear once within the object. Any ideas?
0 Likes
Message 5 of 16

Anonymous
Not applicable
Laurie,

Thanks for the quick reply. I inserted the code as shown but it doesn't seem to replace anything?
0 Likes
Message 6 of 16

Anonymous
Not applicable
Hi Tony,

Did you notice that the request from the user was for code when the text is
hand selected?

Did you see any requirement to count the results?

Obviously, if the whole process was to be fully automated, you would not use
the "replace" command. As as you pointed out a few months ago, it is
quicker to use a combination of instr and re-assembly of the string. But
the speed issue harfly arises in this case and I opted for simplicity of the
code..


--


Regards

Laurie Comerford

"Tony Tanzillo" wrote in message
news:5864284@discussion.autodesk.com...
As was mentioned in other posts, you can use the VBA Replace() function to
replace parts of a string.

But, you should not consider the code Laurie posted a good example of how to
do that, because it calls Replace() on every selected text/mtext object and
reassigns the result, even when it's unnecessary (e.g., when the string to
replace does not even exist in the text).

Instead, use the InStr() function to see if the string you want to replace
exists in the text string first, and only proceed to use Replace() if it
does.
That also allows you to report back to the user, how many text objects were
actually modified.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

wrote in message
news:5864036@discussion.autodesk.com...
I am trying to make an autocad routine that allows the user to select a text
or mtext object one at a time using the mouse and look for "S" within that
text and replace it with "N" Note: The "S" will only appear once within the
object. Any ideas?
0 Likes
Message 7 of 16

Anonymous
Not applicable
Hi,

Why not step through the code and see where it it not doing as you expect?

It certainly worked for me before I posted it.

--


Regards

Laurie Comerford
wrote in message
news:5864355@discussion.autodesk.com...
Laurie,

Thanks for the quick reply. I inserted the code as shown but it doesn't
seem to replace anything?
0 Likes
Message 8 of 16

Anonymous
Not applicable
Thanks Laurie. Can you do the same thing but like this?

Change all "N" to "S"
all "S" to "N"
all "E" to "W"
and all "W" to "E"
?
I thought I could figure it out with a little help of example usage but it is harder than I thought.
0 Likes
Message 9 of 16

Anonymous
Not applicable
Cycle through each character using the Mid function rebuilding the string with the changes as necessary.
0 Likes
Message 10 of 16

Anonymous
Not applicable
I AM NEW TO VB. CAN YOU PROVIDE EXAMPLE. LAURIES CODE WITH MY MESSED UP ATTEMPT IS AS FOLLOWS...

Do While Err = 0

On Error Resume Next
Dim oEnt As Object 'AcadEntity


Dim Pt

Dim sTmp As String
Thisdrawing.Utility.GetEntity oEnt, Pt
'Do While Err = 0
DoEvents
ReplaceString oEnt, "E", "W"
DoEvents
ReplaceString oEnt, "S", "N"

DoEvents
ReplaceString oEnt, "W", "E"


ReplaceString oEnt, "N", "S"


DoEvents

Thisdrawing.Utility.GetEntity oEnt, Pt

Loop
Err.Clear
0 Likes
Message 11 of 16

Anonymous
Not applicable
The problem is you have already replaced one string with another and then change it back.

Look in the help files for info on sting handling functions such as Mid & Len.
0 Likes
Message 12 of 16

Anonymous
Not applicable
Think about what you're asking it to do...

wrote in message
news:5864868@discussion.autodesk.com...
Thanks Laurie. Can you do the same thing but like this?

Change all "N" to "S"
ok so now all N are S

all "S" to "N"
now you change all S back to N

same with e and w

all "E" to "W"

and all "W" to "E"
?
I thought I could figure it out with a little help of example usage but it
is harder than I thought.

so if you want to do that kind of thing you'll need interim place holders

Change all "N" to "wasN" 'or other temp placeholder certain to *not exist
currently*
Change all "S" to "N"
Change all "wasN" to "S"
or something like that...
hth
mark
0 Likes
Message 13 of 16

Anonymous
Not applicable
Hi,

If you look at the line:

ReplaceString oEnt, "S", "N"

You can change that line to be:

ReplaceString oEnt, "N", "S"

or

ReplaceString oEnt, "E", "W"

In each case the first string is replaced by the second one

So now all you need is a means of distinguishing which repalcement you wish
to make which can be done like this:

If TypeOf oEnt Is AcadText Then
Set oText = oEnt
sTmp = oText.TextString
WhichReplacement oEnt, sTmp
ElseIf TypeOf oEnt Is AcadMText Then
Set oMText = oEnt
sTmp = oMText.TextString
WhichReplacement oEnt, sTmp
Else
MsgBox "The selected object was neither Text or MText", vbInformation
End If

Function WhichReplacement (oEnt as AcadEntity, sTmp as String)
If Instr(sTmp, "N" > 0 Then
ReplaceString oEnt, "N", "S"
ElseIf Instr(sTmp, "S" > 0 Then
ReplaceString oEnt, "S", "N"
ElseIf Instr(sTmp, "W" > 0 Then
ReplaceString oEnt, "W", "E"
ElseIf Instr(sTmp, "E" > 0 Then
ReplaceString oEnt, "E", "W"
End If

End Function


I'm unsure if you expect combinations of N and W or N and E etc. If you do
then the code statements need to be changed to:

Function WhichReplacement (oEnt as AcadEntity, sTmp as String)
If Instr(sTmp, "N" > 0 Then
ReplaceString oEnt, "N", "S"
ElseIf Instr(sTmp, "S" > 0 Then
ReplaceString oEnt, "S", "N"
End If
If Instr(sTmp, "W" > 0 Then
ReplaceString oEnt, "W", "E"
ElseIf Instr(sTmp, "E" > 0 Then
ReplaceString oEnt, "E", "W"
End If
End Function

--
Also, you may care to remove the messagebox from the ReplaceString Function,
as with these changes, you don't need it there

I haven't tested this, and may have missed a couple of Dim statements, but
you can add those as needed
Regards

Laurie Comerford
wrote in message
news:5864868@discussion.autodesk.com...
Thanks Laurie. Can you do the same thing but like this?

Change all "N" to "S"
all "S" to "N"
all "E" to "W"
and all "W" to "E"
?
I thought I could figure it out with a little help of example usage but it
is harder than I thought.
0 Likes
Message 14 of 16

Anonymous
Not applicable
I love this discussion group. It works great! But, It works great in AutoCad 2002 but it doesn't work at all in AutoCad 2007?????????????????????
0 Likes
Message 15 of 16

Anonymous
Not applicable
The above listed code that is.
0 Likes
Message 16 of 16

Anonymous
Not applicable
Why doesn't it work in Acad 2007?
0 Likes