Author Topic: Command line Find  (Read 13692 times)

0 Members and 1 Guest are viewing this topic.

SPDCad

  • Bull Frog
  • Posts: 453
Command line Find
« on: March 30, 2005, 12:35:34 PM »
Does the 'Find' command have a command line prompt or is it dialog box controlled only?
I am writting a lisp and I would like to use find and replace in the lisp, but I can't call it up without bringing up the dialog box.
If it is not possible to control the find and replace command at the command prompt, does any one have code that will find and replace a string item in mtext, text and attribute format?  I know I have a few long retired programme that do, but I am at work and I don't have access to the code.
:O(

Any help would be greatly appreciated.

Damn! I need to carry my all my lisp programmes (rerired or not) around with me on a DVD!  :?
AutoCAD 2010, w/ OpenDCL

visit: http://reachme.at/spd_designs

CADaver

  • Guest
Command line Find
« Reply #1 on: March 30, 2005, 12:49:50 PM »
Older than dirt, but still works on TEXT MTEXT and DIMS

Code: [Select]
;;; a rudimentary text editor Frank Emerick, RBCulp
(defun c:cx () (c:chgtext))
(defun C:CHGTEXT () (cht_Edit nil))
(defun cht_Edit (objs / last_o tot_o ent o_str n_str st s_temp
                       n_slen o_slen si chf chm cont ans class)
(command "undo" "BEGIN")
  (if (null objs)
    (setq objs (ssget))
  )
  (setq chm 0)
  (if objs
    (progn                   ;; If any objects selected
      (if (= (type objs) 'ENAME)
        (progn
          (setq ent (entget objs))
          (princ (strcat "\nExisting string: " (cdr (assoc 1 ent))))
        )
        (if (= (sslength objs) 1)
          (progn
            (setq ent (entget (ssname objs 0)))
            (princ (strcat "\nExisting string: " (cdr (assoc 1 ent))))
          )
        )
      )
      (setq o_str (getstring "\nMatch string   : " t))
      (setq o_slen (strlen o_str))
      (if (/= o_slen 0)
        (progn
          (setq n_str (getstring "\nNew string     : " t))
          (setq n_slen (strlen n_str))
          (setq last_o 0
                tot_o  (if (= (type objs) 'ENAME)
                         1
                         (sslength objs)
                       )
          )
          ;; For each selected object...
          (while (< last_o tot_o)
            (setq class (cdr (assoc 0 (setq ent (entget (ssname objs last_o))))))
            (if (cond (= "TEXT" class)
                      (= "mtext" class)
                      (= "DIMENSION" class) )
              (progn
                (setq chf nil si 1)
                (setq s_temp (cdr (assoc 1 ent)))
                (while (= o_slen (strlen (setq st (substr s_temp si o_slen))))
                  (if (= st o_str)
                    (progn
                      (setq s_temp (strcat
                                     (if (> si 1)
                                       (substr s_temp 1 (1- si))
                                       ""
                                     )
                                     n_str
                                     (substr s_temp (+ si o_slen))
                                   )
                      )
                      (setq chf t)    ;; Found old string
                      (setq si (+ si n_slen))
                    )
                    (setq si (1+ si))
                  )
                )
                (if chf
                  (progn              ;; Substitute new string for old
                    ;; Modify the TEXT entity
                    (entmod (subst (cons 1 s_temp) (assoc 1 ent) ent))
                    (setq chm (1+ chm))
                  )
                )
              )
            )
            (setq last_o (1+ last_o))
          )
        )
        ;; else go on to the next line...
      )
    )
  )
  (if (/= (type objs) 'ENAME)
    ;; Print total lines changed
    (if (/= (sslength objs) 1)
      (princ (strcat (rtos chm 2 0) " text lines changed."))
  (terpri)
    )
  )
(command "undo" "END")
  (terpri)
)

SPDCad

  • Bull Frog
  • Posts: 453
Command line Find
« Reply #2 on: March 31, 2005, 10:12:44 AM »
Thanks CADaver! :)
AutoCAD 2010, w/ OpenDCL

visit: http://reachme.at/spd_designs

CADaver

  • Guest
Command line Find
« Reply #3 on: March 31, 2005, 11:11:46 AM »
Quote from: SPDCad
Thanks CADaver! :)
You're welcome.  Bear in mind that it was originally written nearly 18 years ago and has been "tweaked" a bit since.  (I think there may be newer/netter versions floating around somewhere.)

I am quite sure the "real" gurus here can come up with something considerably more elegant.

Sitra

  • Guest
Command line Find
« Reply #4 on: April 04, 2005, 03:25:49 PM »
What about something where you can open up a drawing and it will do a find and replace with preset words to look for and replace. Is this possible.

Mark

  • Custom Title
  • Seagull
  • Posts: 28708
Command line Find
« Reply #5 on: April 04, 2005, 03:53:57 PM »
Yep, with autolisp or vba it is.

You should be about ready to handle that one now, right? You are in Stig's class aren't you?
:)
TheSwamp.org  (serving the CAD community since 2003)

Sitra

  • Guest
Command line Find
« Reply #6 on: April 04, 2005, 03:58:19 PM »
Quote from: Mark Thomas

You should be about ready to handle that one now, right?


Unfortunately no...:cry:

Quote from: Mark Thomas
You are in Stig's class aren't you?


Yes, sir. But, I am so far behind it is not even funny...
 :roll:

Jeff_M

  • King Gator
  • Posts: 4012
  • C3D user & customizer
Command line Find
« Reply #7 on: April 04, 2005, 07:04:37 PM »
Hi Conrad,
Here's a function I put together a while back that you could easily use in a new lisp to do what you want.
Code: [Select]

;| 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

   |;


(defun txtfind (patt newpatt / count ss ent str txthgt match?)
  (vl-load-com)
  (vla-startundomark (vla-Get-ActiveDocument (vlax-Get-Acad-Object)))
  (setq ss (ssget "X" '((0 . "TEXT,MTEXT,DIMENSION,INSERT"))))
  (if (not ss)
    (princ "\nNo Text entities found!")
    (progn
      (setq count -1)
      (while (< (setq count (1+ count))(sslength ss))
(setq ent (entget (ssname ss count))
     obj (vlax-ename->vla-object (cdr (car ent))))
(cond
 ((= (cdr (assoc 0 ent)) "TEXT")
  (progn
    (setq str (cdr (assoc 1 ent)))
    (while (setq match? (vl-string-search patt str))
      (setq str (vl-string-subst newpatt patt str))
      (vla-put-textstring obj str)
      );while
    );progn
  );first condition
 ((= (cdr (assoc 0 ent)) "DIMENSION")
  (progn
    (setq str (cdr (assoc 1 ent)))
    (while (setq match? (vl-string-search patt str))
      (setq str (vl-string-subst newpatt patt str))
      (vla-put-textoverride obj str)
      );while
    );progn
  );second condition
 ((= (cdr (assoc 0 ent)) "MTEXT")
  (progn
    (setq str (vla-get-textstring obj))
    (while (setq match? (vl-string-search patt str))
      (setq str (vl-string-subst newpatt patt str))
      (vla-put-textstring obj str)
      );while
    );progn
  );third condition
 (t
  (progn
    (if (= (vla-get-hasattributes obj) :vlax-true)
      (progn
(setq atts (vla-getattributes obj))
(foreach x (vlax-safearray->list (vlax-variant-value atts))
  (setq str (vla-get-textstring x))
  (while (setq match? (vl-string-search patt str))
    (setq str (vl-string-subst newpatt patt str))
    (vla-put-textstring x str)
    );while
  );for
);progn
      );if
    );progn
  );last condition
 );cond
);while
      );progn
    );if
  (vla-endundomark (vla-Get-ActiveDocument (vlax-Get-Acad-Object)))
  (princ)
  );defun

Make sure this loads first in acaddoc.lsp, then add your lisp after it. Something like this:
Code: [Select]

(txtfind "badtext" "goodtext")

Sitra

  • Guest
Command line Find
« Reply #8 on: April 05, 2005, 09:24:28 AM »
Thanks Jeff, I will give it a try... :D

M-dub

  • Guest
Re: Command line Find
« Reply #9 on: December 13, 2007, 01:17:30 PM »
Here's a function I put together a while back...

Love this one.  Comes in handy, it does!  :-D

M-dub

  • Guest
Re: Command line Find
« Reply #10 on: April 18, 2008, 08:27:11 AM »
8< Snip >8

Any idea as to why I would get this message on some drawings, but not all?

Quote
Command: (txtfind "ABE-4172-AA" "ABE-4172")
; error: ActiveX Server returned an error: Invalid index

One Shot

  • Guest
Re: Command line Find
« Reply #11 on: April 18, 2008, 09:25:24 AM »
Is there a way to us this command to highlight where text was changed?  Like make it change colors when it was changed. 

ronjonp

  • Needs a day job
  • Posts: 7305
Re: Command line Find
« Reply #12 on: April 18, 2008, 10:05:36 AM »
You could add (vla-put-color x 1) to this portion of Jeff's code:

Code: [Select]
   (while (setq match? (vl-string-search patt str))
     (setq str (vl-string-subst newpatt patt str))
     (vla-put-textstring x str)
     );while
   );for

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

T.Willey

  • Needs a day job
  • Posts: 5247
Re: Command line Find
« Reply #13 on: April 18, 2008, 11:02:48 AM »
8< Snip >8

Any idea as to why I would get this message on some drawings, but not all?

Quote
Command: (txtfind "ABE-4172-AA" "ABE-4172")
; error: ActiveX Server returned an error: Invalid index
Does it happen in the same drawing always?  If so maybe you can post a small portion of the drawing.  The only reason I can think of (it's early and I'm tried) is that it's trying to replace the items in a spot that is outside the range of letters.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

M-dub

  • Guest
Re: Command line Find
« Reply #14 on: April 18, 2008, 11:22:56 AM »
8< Snip >8

Any idea as to why I would get this message on some drawings, but not all?

Quote
Command: (txtfind "ABE-4172-AA" "ABE-4172")
; error: ActiveX Server returned an error: Invalid index
Does it happen in the same drawing always?  If so maybe you can post a small portion of the drawing.  The only reason I can think of (it's early and I'm tried) is that it's trying to replace the items in a spot that is outside the range of letters.
Can't really post the drawing, but I could email it to you...


I just figured out that even if I get the error message, it will sometimes change the text anyway.
It should also be noted that this text is in an attribute.