Jump to content

The Best Text Find And Replace LISP Ever...


Freerefill

Recommended Posts

How difficult would it be to make it search/replace one specific text string?

 

I want to change VLV. to VALVE on a bunch of drawings.

Link to comment
Share on other sites

  • Replies 46
  • Created
  • Last Reply

Top Posters In This Topic

  • Lee Mac

    17

  • The Buzzard

    6

  • Freerefill

    6

  • Voaraghamanthar

    6

Top Posters In This Topic

Posted Images

How difficult would it be to make it search/replace one specific text string?

 

I want to change VLV. to VALVE on a bunch of drawings.

 

The way the script is set up, a command "tfind" calls the function "tfindfun" with specified input. Now, you can call the function without the command and specify your own input quite easily. For example, if you wanted to replace "hello" with "goodbye", you could do it by running the script and selecting the appropriate options, or, you could put those options directly into the function call and skip the steps.

 

For example, if you wanted to replace "hello" with "goodbye", you could simply load the script and type at the command line:

 

(tfindfun "hello" "goodbye" 1)

That will run the function which will automatically see that you're passing arguments, and it will use those arguments instead of asking for input.

 

I wrote it this way for a singular purpose: automatically replacing a vast array of text strings. For example, if I wanted to remove abbreviations from a landbase:

 

(defun c:newfun()
(tfindfun "ST" "STREET" 1)
(tfindfun "RD" "ROAD" 1)
(tfindfun "BLVD" "BOULEVARD" 1)
(tfindfun "LN" "LANE" 1)
)

I could just run that one command and poof, all done. No having to do each one individually.

 

I hope that answers your question. ^.^

Link to comment
Share on other sites

Nice.....thank you.

 

Whoa....wait a minute.....when I run this it edits an Mtext in the drawing, and it doesn't even contain the phrase I'm replacing???

 

I'm running it from a Toolbar button with this:

^C^C(load "tfind") (tfind2fun "VLV." "VALVE" 1 )

Link to comment
Share on other sites

  • 2 years later...

I've been looking for this for a while, most programs out there cater for very narrow parameters, this one has lots of flexibility, thankyou.

Link to comment
Share on other sites

  • 3 years later...

Found this during the course of my work. Thank for the lisp.

 

I was wondering if TS or someone can help to adjust the lisp such that :-

1) Find can be the deafault selection.

2) It can search only whole words

 

 

Thanks

Link to comment
Share on other sites

  • 2 months later...

On this subject, how hard would it be, or could someone help me, if I had this Condition. In my "exploded" titleblock....The titleblock has been exploded yes, no mtext, just text strings, but I have the word REV and below it, of course is the Letter Revision the drawing would currently be on. Could you search for text at a specific location or based on the word REV and make a selection and then code that selection to change up to the next REV letter? So, as a breakdown, the code searches for the word REV, finds it, selects whatever text is below that word, and changes it from A to B, or B to C, or C to D.....so on and so forth? And then of course, we want to do this on a whole directory. The only two variables that are consistent in the directory for each drawing is the insertion point.....I.E. X and Y, or the word REV.

Link to comment
Share on other sites

On this subject, how hard would it be, or could someone help me, if I had this Condition. In my "exploded" titleblock....The titleblock has been exploded yes, no mtext, just text strings, but I have the word REV and below it, of course is the Letter Revision the drawing would currently be on. Could you search for text at a specific location or based on the word REV and make a selection and then code that selection to change up to the next REV letter? So, as a breakdown, the code searches for the word REV, finds it, selects whatever text is below that word, and changes it from A to B, or B to C, or C to D.....so on and so forth? And then of course, we want to do this on a whole directory. The only two variables that are consistent in the directory for each drawing is the insertion point.....I.E. X and Y, or the word REV.

 

Here's a start:

(defun c:uprev ( / enx idx itm lay lst rev rvl sel )
   (if (setq sel (ssget "_X" '((0 . "TEXT") (1 . "[Rr][Ee][Vv],@"))))
       (progn
           (repeat (setq idx (sslength sel))
               (setq enx (entget (ssname sel (setq idx (1- idx))))
                     lay (cdr (assoc 410 enx))
               )
               (if (= "REV" (strcase (cdr (assoc 1 enx))))
                   (if (not (assoc lay rvl))
                       (setq rev (cons (cons lay (cdr (assoc 10 enx))) rev))
                   )
                   (if (setq itm (assoc lay lst))
                       (setq lst (subst (vl-list* lay enx (cdr itm)) itm lst))
                       (setq lst (cons  (list lay enx) lst))
                   )
               )
           )
           (foreach x rev
               (if
                   (setq enx
                       (car
                           (vl-sort (cdr (assoc (car x) lst))
                              '(lambda ( a b )
                                   (<  (distance (cdr x) (cdr (assoc 10 a)))
                                       (distance (cdr x) (cdr (assoc 10 b)))
                                   )
                               )
                           )
                       )
                   )
                   (entmod (subst (cons 1 (LM:alpha++ (cdr (assoc 1 enx)))) (assoc 1 enx) enx))
               )
           )       
       )
   )
   (princ)
)

(defun LM:alpha++ ( s )
   (cond
       (   (= s "") "A")
       (   (wcmatch s "*[zZ]")
           (strcat (LM:alpha++ (substr s 1 (1- (strlen s)))) (if (wcmatch s "*z") "a" "A"))
       )
       (   (wcmatch s "*[A-Za-z]")
           (strcat (substr s 1 (1- (strlen s))) (chr (1+ (ascii (substr s (strlen s))))))
       )
       (   (strcat (LM:alpha++ (substr s 1 (1- (strlen s)))) (substr s (strlen s))))
   )
)

(princ)

 

For each layout, the above code will increment the single-line text object containing a single alphabetical character found nearest to a text object containing the word 'REV' (case-insensitive) if found in the layout.

Link to comment
Share on other sites

Lee....your just awesome man. Thank you for getting me started. Where would I start to make a version like this where it searches model space only? Both versions would be handy, as I do get drawings from vendors that use paper space, but we don't. We use model space only. I do have my reservations about that, but that is the general rule as of now.

 

Nevermind, I see why I had to ask that question. It works with Model Space...but my drawing has period at the end of REV and it threw it off. I believe I can tackle this a couple ways.

Edited by Voaraghamanthar
Link to comment
Share on other sites

Lee....your just awesome man. Thank you for getting me started. Where would I start to make a version like this where it searches model space only? Both versions would be handy, as I do get drawings from vendors that use paper space, but we don't. We use model space only. I do have my reservations about that, but that is the general rule as of now.

 

Nevermind, I see why I had to ask that question. It works with Model Space...but my drawing has period at the end of REV and it threw it off. I believe I can tackle this a couple ways.

 

Thanks for the compliments Voaraghamanthar, you're welcome :thumbsup:

 

As you may have gathered, the above code was designed based on the assumption that your drawings would use a separate paperspace layout for each drawing sheet. The current code will work in modelspace, but will only modify the text associated with the first REV text encountered.

 

The following modified version should work with drawings which use only modelspace and with drawings which use paperspace layouts; I have also changed the selection criteria to process any text matching the case-insensitive pattern "REV*", so keep this in mind.

([color=BLUE]defun[/color] c:uprev ( [color=BLUE]/[/color] enx idx itm lay lst rev rvl sel )
   ([color=BLUE]if[/color] ([color=BLUE]setq[/color] sel ([color=BLUE]ssget[/color] [color=MAROON]"_X"[/color] '((0 . [color=MAROON]"TEXT"[/color]) (1 . [color=MAROON]"[Rr][Ee][Vv]*,@"[/color]))))
       ([color=BLUE]progn[/color]
           ([color=BLUE]repeat[/color] ([color=BLUE]setq[/color] idx ([color=BLUE]sslength[/color] sel))
               ([color=BLUE]setq[/color] enx ([color=BLUE]entget[/color] ([color=BLUE]ssname[/color] sel ([color=BLUE]setq[/color] idx ([color=BLUE]1-[/color] idx))))
                     lay ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 410 enx))
               )
               ([color=BLUE]if[/color] ([color=BLUE]wcmatch[/color] ([color=BLUE]strcase[/color] ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 1 enx))) [color=MAROON]"REV*"[/color])
                   ([color=BLUE]setq[/color] rev ([color=BLUE]cons[/color] ([color=BLUE]cons[/color] lay ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 10 enx))) rev))
                   ([color=BLUE]if[/color] ([color=BLUE]setq[/color] itm ([color=BLUE]assoc[/color] lay lst))
                       ([color=BLUE]setq[/color] lst ([color=BLUE]subst[/color] ([color=BLUE]vl-list*[/color] lay enx ([color=BLUE]cdr[/color] itm)) itm lst))
                       ([color=BLUE]setq[/color] lst ([color=BLUE]cons[/color]  ([color=BLUE]list[/color] lay enx) lst))
                   )
               )
           )
           ([color=BLUE]foreach[/color] x rev
               ([color=BLUE]if[/color]
                   ([color=BLUE]setq[/color] enx
                       ([color=BLUE]car[/color]
                           ([color=BLUE]vl-sort[/color] ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] ([color=BLUE]car[/color] x) lst))
                              '([color=BLUE]lambda[/color] ( a b )
                                   ([color=BLUE]<[/color]  ([color=BLUE]distance[/color] ([color=BLUE]cdr[/color] x) ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 10 a)))
                                       ([color=BLUE]distance[/color] ([color=BLUE]cdr[/color] x) ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 10 b)))
                                   )
                               )
                           )
                       )
                   )
                   ([color=BLUE]entmod[/color] ([color=BLUE]subst[/color] ([color=BLUE]cons[/color] 1 (LM:alpha++ ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 1 enx)))) ([color=BLUE]assoc[/color] 1 enx) enx))
               )
           )       
       )
   )
   ([color=BLUE]princ[/color])
)

([color=BLUE]defun[/color] LM:alpha++ ( s )
   ([color=BLUE]cond[/color]
       (   ([color=BLUE]=[/color] s [color=MAROON]""[/color]) [color=MAROON]"A"[/color])
       (   ([color=BLUE]wcmatch[/color] s [color=MAROON]"*[zZ]"[/color])
           ([color=BLUE]strcat[/color] (LM:alpha++ ([color=BLUE]substr[/color] s 1 ([color=BLUE]1-[/color] ([color=BLUE]strlen[/color] s)))) ([color=BLUE]if[/color] ([color=BLUE]wcmatch[/color] s [color=MAROON]"*z"[/color]) [color=MAROON]"a"[/color] [color=MAROON]"A"[/color]))
       )
       (   ([color=BLUE]wcmatch[/color] s [color=MAROON]"*[A-Za-z]"[/color])
           ([color=BLUE]strcat[/color] ([color=BLUE]substr[/color] s 1 ([color=BLUE]1-[/color] ([color=BLUE]strlen[/color] s))) ([color=BLUE]chr[/color] ([color=BLUE]1+[/color] ([color=BLUE]ascii[/color] ([color=BLUE]substr[/color] s ([color=BLUE]strlen[/color] s))))))
       )
       (   ([color=BLUE]strcat[/color] (LM:alpha++ ([color=BLUE]substr[/color] s 1 ([color=BLUE]1-[/color] ([color=BLUE]strlen[/color] s)))) ([color=BLUE]substr[/color] s ([color=BLUE]strlen[/color] s))))
   )
)

([color=BLUE]princ[/color])

Here's a quick demo:

 

uprevexample.gif

Link to comment
Share on other sites

Un-stinkin'-real Lee. Can I be your best friend? LOL. Thank you so much. I'm just starting out with Lisp and I need to move to .NET, this will really help me get in the right direction.

Link to comment
Share on other sites

Hey Lee, what part of that code is selecting whatever text is below the word REV. ? I would like to dive right in and play with some small lisp programs to do other things based on that concept.

Link to comment
Share on other sites

Hey Lee, what part of that code is selecting whatever text is below the word REV. ? I would like to dive right in and play with some small lisp programs to do other things based on that concept.

 

The code doesn't actually select the text found below the word REV, but rather makes an initial selection of all text objects which contain a single alphabetical character, groups these objects by the layout in which they reside, and then for each instance of the word REV, the code will modify the single-character text object found nearest to REV by comparing the insertion points of the text objects.

 

The part which determines which text object is nearest is the vl-sort expression (which sorts the text objects by their distance to the REV text) - this could be further optimised by instead iterating over the set of text objects and using a simple conditional expression to retrieve the closest object (since sorting the objects is not actually required when only the extreme case of the sort is required), but for a small number of objects the performance difference is negligible and the vl-sort expression is intuitive and concise.

Link to comment
Share on other sites

  • 4 months later...

Trying to analyze this and dig a little deeper. Was wondering where I need to look to change it from Alpha to Numeric...or even make it a little smarter to handle both alpha and numeric revision numbers/letters.

Link to comment
Share on other sites

Trying to analyze this and dig a little deeper. Was wondering where I need to look to change it from Alpha to Numeric...or even make it a little smarter to handle both alpha and numeric revision numbers/letters.

 

Try something like this:

([color=BLUE]defun[/color] c:uprev ( [color=BLUE]/[/color] enx idx itm lay lst rev rvl sel )
   ([color=BLUE]if[/color] ([color=BLUE]setq[/color] sel ([color=BLUE]ssget[/color] [color=MAROON]"_X"[/color] '((0 . [color=MAROON]"TEXT"[/color]) (1 . [color=MAROON]"[Rr][Ee][Vv]*,@,[0-9]"[/color]))))
       ([color=BLUE]progn[/color]
           ([color=BLUE]repeat[/color] ([color=BLUE]setq[/color] idx ([color=BLUE]sslength[/color] sel))
               ([color=BLUE]setq[/color] enx ([color=BLUE]entget[/color] ([color=BLUE]ssname[/color] sel ([color=BLUE]setq[/color] idx ([color=BLUE]1-[/color] idx))))
                     lay ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 410 enx))
               )
               ([color=BLUE]if[/color] ([color=BLUE]wcmatch[/color] ([color=BLUE]strcase[/color] ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 1 enx))) [color=MAROON]"REV*"[/color])
                   ([color=BLUE]setq[/color] rev ([color=BLUE]cons[/color] ([color=BLUE]cons[/color] lay ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 10 enx))) rev))
                   ([color=BLUE]if[/color] ([color=BLUE]setq[/color] itm ([color=BLUE]assoc[/color] lay lst))
                       ([color=BLUE]setq[/color] lst ([color=BLUE]subst[/color] ([color=BLUE]vl-list*[/color] lay enx ([color=BLUE]cdr[/color] itm)) itm lst))
                       ([color=BLUE]setq[/color] lst ([color=BLUE]cons[/color]  ([color=BLUE]list[/color] lay enx) lst))
                   )
               )
           )
           ([color=BLUE]foreach[/color] x rev
               ([color=BLUE]if[/color]
                   ([color=BLUE]setq[/color] enx
                       ([color=BLUE]car[/color]
                           ([color=BLUE]vl-sort[/color] ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] ([color=BLUE]car[/color] x) lst))
                              '([color=BLUE]lambda[/color] ( a b )
                                   ([color=BLUE]<[/color]  ([color=BLUE]distance[/color] ([color=BLUE]cdr[/color] x) ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 10 a)))
                                       ([color=BLUE]distance[/color] ([color=BLUE]cdr[/color] x) ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 10 b)))
                                   )
                               )
                           )
                       )
                   )
                   ([color=BLUE]entmod[/color] ([color=BLUE]subst[/color] ([color=BLUE]cons[/color] 1 (LM:alpha++ ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 1 enx)))) ([color=BLUE]assoc[/color] 1 enx) enx))
               )
           )       
       )
   )
   ([color=BLUE]princ[/color])
)

([color=BLUE]defun[/color] LM:alpha++ ( s [color=BLUE]/[/color] n )
   ([color=BLUE]setq[/color] n ([color=BLUE]strlen[/color] s))
   ([color=BLUE]cond[/color]
       (   ([color=BLUE]=[/color] 0 n) [color=MAROON]"A"[/color])
       (   ([color=BLUE]and[/color] ([color=BLUE]=[/color] 1 n) ([color=BLUE]wcmatch[/color] s [color=MAROON]"[zZ9]"[/color]))
           ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] s '(([color=MAROON]"z"[/color] . [color=MAROON]"aa"[/color]) ([color=MAROON]"Z"[/color] . [color=MAROON]"AA"[/color]) ([color=MAROON]"9"[/color] . [color=MAROON]"10"[/color]))))
       )
       (   ([color=BLUE]wcmatch[/color] s [color=MAROON]"*[zZ9]"[/color])
           ([color=BLUE]strcat[/color]
               (LM:alpha++ ([color=BLUE]substr[/color] s 1 ([color=BLUE]1-[/color] n)))
               ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] ([color=BLUE]substr[/color] s n) '(([color=MAROON]"z"[/color] . [color=MAROON]"a"[/color]) ([color=MAROON]"Z"[/color] . [color=MAROON]"A"[/color]) ([color=MAROON]"9"[/color] . [color=MAROON]"0"[/color]))))
           )
       )
       (   ([color=BLUE]wcmatch[/color] s [color=MAROON]"*[A-Za-z0-9]"[/color])
           ([color=BLUE]strcat[/color]
               ([color=BLUE]substr[/color] s 1 ([color=BLUE]1-[/color] n))
               ([color=BLUE]chr[/color] ([color=BLUE]1+[/color] ([color=BLUE]ascii[/color] ([color=BLUE]substr[/color] s n))))
           )
       )
       (   ([color=BLUE]strcat[/color]
               (LM:alpha++ ([color=BLUE]substr[/color] s 1 ([color=BLUE]1-[/color] n)))
               ([color=BLUE]substr[/color] s n)
           )
       )
   )
)

([color=BLUE]princ[/color])

@Moderator: suggest moving all posts from Reply#26 onwards to a new thread entitled: "Up-Issuing an Exploded Titleblock"

Link to comment
Share on other sites

I thought you would come back and say:

 

"Ok just a quick one untested"

 

Haha you know me only too well :P

 

Now a days you just pull them from your endless pile of code.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...