Author Topic: Command line Find  (Read 13691 times)

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12499
  • London, England
Re: Command line Find
« Reply #30 on: October 15, 2012, 07:24:09 PM »
Would this help at all Mike?

http://lee-mac.com/bfind.html

M-dub

  • Guest
Re: Command line Find
« Reply #31 on: October 17, 2012, 12:28:21 PM »
Big thanks to Tim and Ron for helping me out with this.

Ron got me squared away with this update and all is well again.  :)

So, what we used to do with this lisp was to write a script that just ran this routine over and over again for each string we wanted to change.  With Ron's update, we just throw what used to be in the script down at the bottom of the code and run the lisp.

Again, many thanks to you both!  :)

Code: [Select]
(defun txtfind (patt newpatt / _replacetext e el n obj ss str)
        ;| Routine to find specified text and replace with new text. Works on Text,
    Mtext, Attributes and Dimension text overrides.
    WARNING: it will change all occurances of a pattern with the new text.
    Such as: if "test" "contest" "testing" are all valid text entries in the
    drawing, running this: (txtfind "test" "newtest") will change
    the original text to "newtest" "connewtest" "newtesting", but for the
    original intent of this routine that was not a problem. Modifications
    may be made to force matching of whole word only.
   
    by: Jeff Mishler Sept. 2003
    10.16.2012 Modified by RJP to prevent infinite loop when source search includes all or part of replace pattern
    Created more refined filter
    ie.
    (txtfind "AI1" "FAI1")
    (txtfind "AI2" "FAI2")
    |;
  (defun _replacetext (new old textstring / i out tmp)
    (cond ((vl-string-search old textstring)
    (setq tmp textstring)
    (setq out "")
    (while (setq i (vl-string-search old tmp))
      (setq out (strcat out (vl-string-subst new old (substr tmp 1 (+ i (strlen old))))))
      (setq tmp (substr tmp (1+ (+ i (strlen old)))))
    )
    (if (zerop (strlen tmp))
      out
      (strcat out tmp)
    )
   )
   (textstring)
    )
  )
  (vl-load-com)
  ;; More refined filter
  (if (setq ss (ssget "_X"
       (list '(-4 . "<OR")
     '(-4 . "<AND")
     ;; *text that only has part of the search string
     '
      (0 . "MTEXT,TEXT")
     (cons 1 (strcat "*" patt "*"))
     '(-4 . "AND>")
     '(-4 . "<AND")
     ;; Attributed blocks
     '
      (0 . "INSERT")
     '(66 . 1)
     '(-4 . "AND>")
     ;; Dimension DUH :)
     '
      (-4 . "<AND")
     '(0 . "DIMENSION")
     '(-4 . "AND>")
     '(-4 . "OR>")
       )
        )
      )
    (progn
      ;;(vla-startundomark (setq adoc (vla-get-activedocument (vlax-get-acad-object))))
      (setq n -1)
      (while (setq e (ssname ss (setq n (1+ n))))
(setq el (entget e))
(setq obj (vlax-ename->vla-object e))
(cond ((wcmatch (cdr (assoc 0 el)) "TEXT,MTEXT")
        (setq str (cdr (assoc 1 el)))
        (vla-put-textstring obj (_replacetext newpatt patt str))
       )
       ((= (cdr (assoc 0 el)) "DIMENSION")
        (and (wcmatch (strcase (setq str (cdr (assoc 1 el)))) (strcat "*" patt "*"))
     (vla-put-textoverride obj (_replacetext newpatt patt str))
        )
       )
       (t
        (foreach x (vlax-invoke obj 'getattributes)
(and (wcmatch (strcase (setq str (vla-get-textstring x))) (strcat "*" patt "*"))
       (vla-put-textstring x (_replacetext newpatt patt str))
)
        )
       )
)
      )
      ;;(vla-endundomark adoc)
    )
    (princ "\nNo Text entities found!")
  )
  (princ)
)

(txtfind "AI1" "FAI1")
(txtfind "AI2" "FAI2")
(txtfind "AI3" "FAI3")
(txtfind "AI4" "FAI4")
(txtfind "AO1" "FAO1")
(txtfind "AO2" "FAO2")
(txtfind "AO3" "FAO3")
(txtfind "AO4" "FAO4")
(txtfind "DI1" "FDI1")
(txtfind "DI2" "FDI2")
(txtfind "DI3" "FDI3")
(txtfind "DI4" "FDI4")
(txtfind "DO1" "FDO1")
(txtfind "DO2" "FDO2")
(txtfind "DO3" "FDO3")
(txtfind "DO4" "FDO4")

M-dub

  • Guest
Re: Command line Find
« Reply #32 on: October 17, 2012, 12:30:33 PM »
Would this help at all Mike?

http://lee-mac.com/bfind.html
Funny story...

The short version:  Yes, it helps!  :lol:

Lee Mac

  • Seagull
  • Posts: 12499
  • London, England
Re: Command line Find
« Reply #33 on: October 17, 2012, 02:53:02 PM »
Would this help at all Mike?

http://lee-mac.com/bfind.html
Funny story...

The short version:  Yes, it helps!  :lol:

Excellent  :-)

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9667
Re: Command line Find
« Reply #34 on: June 16, 2021, 04:05:10 PM »
I needed a text "find and replace" routine I could use with accoreconsole and I found this thread which saved me a lot of time. I have modified the above routine (txtfind) to use regular AutoLisp (no VisualLisp).

Thank you to those of you that helped create/modify this routine. Here is my contribution.
Code - Auto/Visual Lisp: [Select]
  1. (defun txtfind (patt newpatt / _replacetext e el n obj ss str)
  2.     ;; Routine to find specified text and replace with new text. Works on Text,
  3.     ;; Mtext, Attributes and Dimension text overrides.
  4.     ;;
  5.     ;; WARNING : it will change all occurances of a pattern with the new text.
  6.     ;;           Such as: if "test" "contest" "testing" are all valid text
  7.     ;;           entries in the drawing, running this: (txtfind "test"
  8.     ;;           "newtest") will change the original text to "newtest"
  9.     ;;           "connewtest" "newtesting", but for the original intent of this
  10.     ;;           routine that was not a problem. Modifications may be made to
  11.     ;;           force matching of whole word only.
  12.     ;; by: Jeff Mishler Sept. 2003
  13.     ;;
  14.     ;; 10.16.2012 - Modified by RJP to prevent infinite loop when source search includes
  15.     ;;              all or part of replace pattern.
  16.     ;;            - Created more refined filter
  17.     ;;                  ie.
  18.     ;;                  (txtfind "AI1" "FAI1")
  19.     ;;                  (txtfind "AI2" "FAI2")
  20.     ;; 06.16.2021 - Modified by John Kaul to use only AutoLisp functions (no visual lisp),
  21.     ;;              so this can be used with accoreconsole.
  22.  
  23.    (defun str_subst ( new old txtstr / i char)                  ; /*{{{*/
  24.        ;;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  25.        ;; STR_SUBST
  26.        ;;  replace text in a string with new text
  27.        ;;
  28.        ;;  by: Iqbal Lotey 09/10/1999
  29.        ;;
  30.        ;;  Takes  new    string to replace with
  31.        ;;         old    string to be replaced
  32.        ;;         txtstr string to search in
  33.        ;;
  34.        ;;  Returns modified string, or unmodified string if old not found
  35.        ;;  WARNING : no error checking! function WILL hang or crash if it doesn't
  36.        ;;            recieve sensible values (i.e. no emtpy strings!)
  37.      (setq i 1)
  38.      (while ( <= i (strlen txtstr))
  39.        (setq char (substr txtstr i (strlen old)))
  40.        (if (equal char old)
  41.          (setq txtstr (strcat (substr txtstr 1 (- i 1))
  42.                               new (substr txtstr
  43.                                           (+ i (strlen old))))
  44.                i (+ i (- (strlen new) 1))
  45.          )
  46.          (setq i (1+ i))
  47.        )
  48.      )
  49.      txtstr
  50.    )                    ; /*}}}*/
  51.   ;; More refined filter
  52.   (if (setq ss (ssget "_X"
  53.                        (list '(-4 . "<OR")
  54.                              '(-4 . "<AND")
  55.                              ;; *text that only has part of the search string
  56.                              '
  57.                               (0 . "MTEXT,TEXT")
  58.                              (cons 1 (strcat "*" patt "*"))
  59.                              '(-4 . "AND>")
  60.                              '(-4 . "<AND")
  61.                              ;; Attributed blocks
  62.                              '
  63.                               (0 . "INSERT")
  64.                              '(66 . 1)
  65.                              '(-4 . "AND>")
  66.                              ;; Dimension DUH
  67.                              '
  68.                               (-4 . "<AND")
  69.                              '(0 . "DIMENSION")
  70.                              '(-4 . "AND>")
  71.                              '(-4 . "OR>")
  72.                        )
  73.                 )
  74.       )
  75.     (progn
  76.       (setq n -1)
  77.       (while (setq e (ssname ss (setq n (1+ n))))
  78.          (setq el (entget e))
  79.          (cond ((wcmatch (cdr (assoc 0 el)) "TEXT,MTEXT")
  80.                 (setq str (cdr (assoc 1 el)))
  81.                 (entmod (subst (cons 1 (str_subst newpatt patt str)) (assoc 1 el) el)))
  82.                ((= (cdr (assoc 0 el)) "DIMENSION")
  83.                 (and (wcmatch (setq str (cdr (assoc 1 el))) (strcat "*" patt "*"))
  84.                      (entmod (subst (cons 1 (str_subst newpatt patt str)) (assoc 1 el) el))))
  85.                (t
  86.                  (setq el (entget (entnext (cdr (assoc -1 el)))))
  87.                  (while (and (= (cdr (assoc 0 el)) "ATTRIB") (not (= (cdr (assoc 0 el)) "SEQEND")))
  88.                    (if (wcmatch (setq str (cdr (assoc 1 el))) patt)
  89.                      (entmod (subst (cons 1 (str_subst newpatt patt str)) (assoc 1 el) el))
  90.                    )
  91.                    (setq el (entget (entnext (cdr (assoc -1 el)))))))
  92.          )
  93.       )
  94.     )
  95.     (princ "\nNo Text entities found!")
  96.   )
  97.   (princ)
  98. )


EDIT:
2021-06-16: Fixed minor code error.
2021-07-20: Adjusted attribute code section slightly.
« Last Edit: July 20, 2021, 11:54:10 AM by John Kaul (Se7en) »
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9667
Re: Command line Find
« Reply #35 on: July 20, 2021, 11:55:09 AM »
Update: Adjusted attribute code section slightly (slightly more efficient).
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org