Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Find and Replace all text afterwards especific word or symbol

15 REPLIES 15
SOLVED
Reply
Message 1 of 16
Anonymous
1155 Views, 15 Replies

Find and Replace all text afterwards especific word or symbol

Hello, 

 

I made a search here on forum and I've seen Autodesk "About Findind and Replacing Text" help from Autodesk website, mainly using wild-card characters, but I can't get this done: My wish is to replace all text that comes afterwards the first "comma (,)" in the texts bellow:

 
 
 

Global Adress SchemeGlobal Adress Scheme

From the image above, I want to keep only the Street/Avenue name and replace with blank/empty text everything that comes aftwards the first comma, leaving intact all the other texts in my drawing that have "comma (,) space" sequence inside them. As this adress is imported from Google Earth, using this plugin called GeoCode and Google API, it always comes with the following pattern:

 

"Avenue/Street Name" "comma" "space" "Number(#)" "space" "-(trace/subtraction symbol)" "space" "Neighborhood Name" "comma" "space" "City" "space" "-(trace/subtraction symbol)" "space" "State Abbreviation (2 letters)" "comma" "space" "ZIP Code" "comma" "space" "Country".

 

Any help with that? Thank you very much!

 

 

 

15 REPLIES 15
Message 2 of 16
ВeekeeCZ
in reply to: Anonymous

Start with posting this image as dwg twice - once with an initial state, the second as a wanted outcome. thx

 

btw it looks trivial - you should be able to manage it by your own means.

Message 3 of 16
Kent1Cooper
in reply to: Anonymous

(defun C:TCS (/ txtent txt); = Truncate from Comma-&-Space
  (setq
    txtent (car (entsel "\nSelect Text to truncate from comma-&-space: "))
    txt (getpropertyvalue txtent "TextString")
  )
  (setpropertyvalue txtent "TextString" (substr txt 1 (vl-string-search ", " txt)))
)

Kent Cooper, AIA
Message 4 of 16
Anonymous
in reply to: ВeekeeCZ

Here is the .dwg as it comes from the GoeCode plugin and the expected result. The problem is: in the same drawing it can exist many texts with the "comma space" sequence, so they could be replaced aswell.

Message 5 of 16
Anonymous
in reply to: Kent1Cooper

Thank you sir! That's almost what I wanted to do. Can this LISP to be able to select lastest created object (using the "last (l")) or previous selected object when running?
Message 6 of 16
Kent1Cooper
in reply to: Anonymous

Here's the version that finds all of them for you:

(defun C:TCS (/ ss n txtent txt); = Truncate from Comma-&-Space
  (if (setq ss (ssget "_X" '((1 . "*`, *"))))
    (repeat (setq n (sslength ss)); then
      (setq
        txtent (ssname ss (setq n (1- n)))
        txt (getpropertyvalue txtent "TextString")
      ); setq
      (setpropertyvalue txtent "TextString" (substr txt 1 (vl-string-search ", " txt)))
    ); repeat
  ); if
  (princ)
); defun

Kent Cooper, AIA
Message 7 of 16
Kent1Cooper
in reply to: Anonymous


@Anonymous wrote:
.... Can this LISP to be able to select lastest created object (using the "last (l")) or previous selected object when running?

(defun C:TLCS (/ txtent txt); = Truncate Last from Comma-&-Space
  (setq
    txtent (entlast)
    txt (getpropertyvalue txtent "TextString")
  )
  (setpropertyvalue txtent "TextString" (substr txt 1 (vl-string-search ", " txt)))
)

Kent Cooper, AIA
Message 8 of 16
Anonymous
in reply to: Kent1Cooper

Thanks for this one. I tried to execute it. It works perfetcly but aftects other texts in drawings that contains "comma space". Maybe it will be useful for some situations! Thanks a zilino!

Message 9 of 16
Anonymous
in reply to: Kent1Cooper

Could you make this one to work with DText and MText aswell? This one it's just what I wanted. Thank you so much for this!
Message 10 of 16
Kent1Cooper
in reply to: Anonymous


@Anonymous wrote:

Thanks for this one. I tried to execute it, but I'm getting some error on command line: error: bad argument type: numberp: nil


They both [the find-them-all and the last-entity versions -- I'm not sure to which you are referring] work for me, in your sample drawing and in my own test drawing.  I wondered whether it could be that [in the find-them-all version] there aren't any  Text objects with the comma-&-space combination, or [in the last-entity version] that last one doesn't contain it, but in neither case do I get that error message.  And in the last-entity version, if the last thing isn't Text at all, I get an error message, but not that one.

Kent Cooper, AIA
Message 11 of 16
Kent1Cooper
in reply to: Anonymous


@Anonymous wrote:
Could you make this one to work with DText and MText aswell? This one it's just what I wanted. Thank you so much for this!

Oddly, I find that using the (setpropertyvalue) function doesn't do for Mtext.  Its text-content property is named differently ["Text"] from that for regular Text ["TextString"], but in addition, for some reason it's Read-Only!

 

So I moved to the (vla-put-TextString) approach, because it can work on Mtext, and besides, the property in that context is named the same for both.

 

(vl-load-com); just in case
(defun C:TLCS (/ txtobj txt); = Truncate Last from Comma-&-Space
  (setq
    txtobj (vlax-ename->vla-object (entlast))
    txt (vla-get-TextString txtobj)
  )
  (vla-put-TextString txtobj (substr txt 1 (vl-string-search ", " txt)))
)

 

Find-them-all version:

 

(defun C:TCS (/ ss n txtobj txt); = Truncate from Comma-&-Space
  (if (setq ss (ssget "_X" '((1 . "*`, *"))))
    (repeat (setq n (sslength ss)); then
      (setq
        txtobj (vlax-ename->vla-object (ssname ss (setq n (1- n))))
        txt (vla-get-TextString txtobj)
      ); setq
      (vla-put-TextString txtobj (substr txt 1 (vl-string-search ", " txt)))
    ); repeat
  ); if
  (princ)
); defun

Kent Cooper, AIA
Message 12 of 16
Anonymous
in reply to: Kent1Cooper

Just worked perfectly sir. Thank you so much! Just asking you one last thing, how do I modify this LISP to replace the text "R." (abbreviation) by "RUA" (Street in English) and "AV." (abbreviation) by "AVENIDA" (Avenue in English). Is it possible with this LISP?
Message 13 of 16
Kent1Cooper
in reply to: Anonymous


@Anonymous wrote:
.... how do I modify this LISP to replace the text "R." (abbreviation) by "RUA" ... and "AV." (abbreviation) by "AVENIDA" ....

In either of them, replace this line:

 

      (vla-put-TextString txtobj (substr txt 1 (vl-string-search ", " txt)))

 

with this:

 

      (vla-put-TextString txtobj
        (vl-string-subst "RUA" "R."
          (vl-string-subst "AVENIDA" "AV."
            (substr txt 1 (vl-string-search ", " txt))
          )
        )
      )

 

It's case-sensitive, but could be made to not be, if needed.  And they would make that substitution of full words for abbreviations only in objects that contain the comma-&-space combination that are being truncated, but could be made to do the same in others that are not being truncated, if needed.

Kent Cooper, AIA
Message 14 of 16
Anonymous
in reply to: Kent1Cooper


@Kent1Cooper wrote:

@Anonymous wrote:
.... how do I modify this LISP to replace the text "R." (abbreviation) by "RUA" ... and "AV." (abbreviation) by "AVENIDA" ....

In either of them, replace this line:

 

      (vla-put-TextString txtobj (substr txt 1 (vl-string-search ", " txt)))

 

with this:

 

      (vla-put-TextString txtobj
        (vl-string-subst "RUA" "R."
          (vl-string-subst "AVENIDA" "AV."
            (substr txt 1 (vl-string-search ", " txt))
          )
        )
      )

 

It's case-sensitive, but could be made to not be, if needed.  And they would make that substitution of full words for abbreviations only in objects that contain the comma-&-space combination that are being truncated, but could be made to do the same in others that are not being truncated, if needed.


Sorry for being dumb, but I wasn't able to succed in modifying your code to make those replacements:

Would be somehting like this:


(defun C:TCRA (/ ss n txtobj txt); = Truncate from Comma-&-Space
(if (setq ss (ssget "_X" '((1 . "*"))))
(repeat (setq n (sslength ss)); then
(setq
txtobj (vlax-ename->vla-object (ssname ss (setq n (1- n))))
txt (vla-get-TextString txtobj)
); setq
(vla-put-TextString txtobj
(vl-string-subst "RUA" "R."
(vl-string-subst "AVENIDA" "AV."
(substr txt 1 (vl-string-search ", " txt))
)
)
)
); repeat
); if
(princ)
); defun
(c:TCRA)

Message 15 of 16
Kent1Cooper
in reply to: Anonymous


@Anonymous wrote:
.... I wasn't able to succed .... Would be somehting like this: ....

That looks right to me, without testing it, but you can test it easily enough....  But what does "I wasn't able to succeed" mean?  Doesn't load?  Loads but doesn't recognize the command name?  Any error message(s)?  Etc.

 

I see you've got it revised to select all  objects with text content, not just those containing the comma-&-space combination.  That also looks like it should work, since when (vl-string-search) doesn't find that combination, the entire string would be returned.

 

[I don't suppose it's likely that you'll have things like Dimensions  with override text including those abbreviations, but those would also be found.  In fact, all Dimensions  would be found, since they all have a 1-code entry, even if there's no override.  But they wouldn't be altered, since their text-content property is named differently.  But it might give you an error for not finding the specified property.]

Kent Cooper, AIA
Message 16 of 16
Kent1Cooper
in reply to: Kent1Cooper


@Kent1Cooper wrote:
....... all Dimensions  would be found, since they all have a 1-code entry, even if there's no override.  ....  But it might give you an error for not finding the specified property.]

If that's the problem, you can restrict it to finding only Text and/or Mtext, not Dimensions:

 

  (setq ss (ssget "_X" '((0 . "*TEXT") (1 . "*"))))

Kent Cooper, AIA

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report