Author Topic: Preform a "Find and Replace on a Layout Tab" ????  (Read 8793 times)

0 Members and 1 Guest are viewing this topic.

Krushert

  • Seagull
  • Posts: 13679
  • FREE BEER Tomorrow!!
Preform a "Find and Replace on a Layout Tab" ????
« on: February 26, 2010, 12:17:07 PM »
Is there a Way to Preform a Find and Replace on Layout Tab?  Has anyone done this before with Lisp?  Can some point me in the right direction.

I got hit with this by the Client Yesterday. Need to change the drawing Number from "ASB-whatever" to ASP-whatever"

Now all our tabs are named with the drawing number and the TB's drawing number is updated via "ctab" field in an the attribute.   Well not all the attributes are linked to the ctab variable.  But I am trying to crack nut as we speak while making changes (manually) to the files that are needed immediately.    However this is nut is above my head me thinks.  Some files have single Layout Tab and some have multiple. 

Thanks in Advanced
I + XI = X is true ...  ... if you change your perspective.

I no longer CAD or Model, I just hang out here picking up the empties beer cans

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Preform a "Find and Replace on a Layout Tab" ????
« Reply #1 on: February 26, 2010, 12:56:23 PM »
Have you looked at this Krushert?

http://www.theswamp.org/index.php?topic=30262.0

It has a Find and Replace option  8-)

Krushert

  • Seagull
  • Posts: 13679
  • FREE BEER Tomorrow!!
Re: Preform a "Find and Replace on a Layout Tab" ????
« Reply #2 on: February 26, 2010, 01:55:14 PM »
Have you looked at this Krushert?

http://www.theswamp.org/index.php?topic=30262.0

It has a Find and Replace option  8-)
No I have not. I probably skipped over it Because of the name when looking at the search results.  Thanks.

You mind if I try (try being keyword) to rip out the "find and replace" part of it?  I have to do this on 150 or so drawings give or take and would like to eventually batch file it the find and replace it.
I + XI = X is true ...  ... if you change your perspective.

I no longer CAD or Model, I just hang out here picking up the empties beer cans

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Preform a "Find and Replace on a Layout Tab" ????
« Reply #3 on: February 26, 2010, 01:57:20 PM »
Have you looked at this Krushert?

http://www.theswamp.org/index.php?topic=30262.0

It has a Find and Replace option  8-)
No I have not. I probably skipped over it Because of the name when looking at the search results.  Thanks.

You mind if I try (try being keyword) to rip out the "find and replace" part of it?  I have to do this on 150 or so drawings give or take and would like to eventually batch file it the find and replace it.

Well - to be honest, I'm not sure it would help much - the Find and Replace function in that code is heavily engineered to work with the dialog  :|

But at least it may give you some idea of how to approach it  :-)

Krushert

  • Seagull
  • Posts: 13679
  • FREE BEER Tomorrow!!
Re: Preform a "Find and Replace on a Layout Tab" ????
« Reply #4 on: February 26, 2010, 02:05:25 PM »
Well - to be honest, I'm not sure it would help much - the Find and Replace function in that code is heavily engineered to work with the dialog  :|

But at least it may give you some idea of how to approach it  :-)
First glance leads me to think so too.  :-o
I + XI = X is true ...  ... if you change your perspective.

I no longer CAD or Model, I just hang out here picking up the empties beer cans

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Preform a "Find and Replace on a Layout Tab" ????
« Reply #6 on: February 26, 2010, 02:20:40 PM »
Code: [Select]
"This routine replace text in dwg files from the selected folder."
"Options:"
"-match case: allow for uppercase and lowercase alphabetic characters"
"-find whole words only: if active the routine analyse only whole words"
"-find in subfolder: if active the routine find dwg files also in subfolders of selected folder"
"-model space: if active the routine analize text entities only in model space"
"-paper space: if active the routine analize text entities only in paper space (layouts)"
"-both space: if active the routine analize text entities only in model and paper spaces"
|;

;;;CADALYST 04/05 Tip2028:  Replace Text In Dwgs.lsp      Adjust Text for All Drawings in a Directory (c) 2005 Andrzej Gumula
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Preform a "Find and Replace on a Layout Tab" ????
« Reply #7 on: February 26, 2010, 02:24:42 PM »
You might be able to use some of this. I haven't worked on it in some time.
Code: [Select]
;;  TextReplace.lsp
;;  CAB 02/19/07 version 3

;;  Warning: There is no Undo, so back up your work.

;;  ObjectDBX Routine to replace text strings in all drawings in a
;;  selected folder

;;  This is a "Work In Progress"
;;  This is a collection of subroutines from several contributers
;;  at the Swamp & my own brand of tweaking. :) CAB

(defun TextReplaceDBX
       (flag
  ;;  Add flag values
  ;;  flag 1 = Search ModelSpace
  ;;       2 = Search PaperSpace
  ;;       4 = Reserved
  ;;       8 = Include Attributed Blocks  * Not Operational

       textlist ; list of pairs, text to replace (("OldString" "NewString")..)
        ;;  if NewString is nil then Find Only Occures
        ;;  if NewString = "" the Old text is removed
        / DIRPATH DWGOBJ FULLPATH OBJ ODBX OLDTXT TEXTLIST TXT TXTLST)
 
  (vl-load-com)
  (setq *acad (vlax-get-acad-object))


;;; -=<*>=- -=<*>=- -=<*>=- -=<*>=- -=<*>=- -=<*>=- -=<*>=- -=<*>=-
;;;                      Local Functions                           
;;; -=<*>=- -=<*>=- -=<*>=- -=<*>=- -=<*>=- -=<*>=- -=<*>=- -=<*>=-
  (defun *error* (msg)
    (if
      (not
        (member
          msg
          '("console break" "Function cancelled" "quit / exit abort" "")
        )
      )
       (princ (strcat "\nError: " msg))
    )
    (if (and odbx (not (vlax-object-released-p odbx)))
      (vlax-release-object odbx)
    )
    (gc)
    (princ)
  )
  ;;end error function

  (defun get_atts (obj / val)
    (if (vl-catch-all-error-p
          (vl-catch-all-apply
            '(lambda ()
               (setq val (vlax-safearray->list
                           (vlax-variant-value (vla-getattributes obj))
                         )
               )
             )
          )
        )
      nil
      val
    )
  )


  (defun ReplaceText (phraselst document / txt oName swaptext ck_for_text)
    (defun swaptext (obj txtlst)
      (setq txt    (vla-get-textstring obj)
            Oldtxt txt
      )
      (mapcar '(lambda (x / tmp)
                 (if (setq tmp (vl-string-subst (cadr x) (car x) txt))
                   (setq txt tmp)
                 )
               )
              phraselst
      )
      (if (/= OldTxt Txt)
        (vla-put-textstring obj txt)
      )
    )

    (defun ck_for_text (obj oType lst)
          (cond ((vl-position oType '("AcDbText" "AcDbMText"))
             (swaptext obj lst)
            )
               
             ;|((and (> (logand flag 8) 0)
                  (eq oType "AcDbBlockReference")
                  (eq (vla-get-hasattributes item) :vlax-true)
             )
             (foreach for-item (get_atts item)
               (swaptext obj lst)
             )
            )|;
      )
    )

    (if (> (logand flag 1) 0)
    ;;  Model Space Only
    (vlax-for item (vla-get-modelspace document)
      (setq oName (vla-get-objectname item))
      (ck_for_text item oName textlist)
    )
    )
    (if (> (logand flag 2) 0)
    ;;  Paper Space Only
    (vlax-for item (vla-get-paperspace document)
      (setq oName (vla-get-objectname item))
      (ck_for_text item oName textlist)
    )
  )
  )



  ;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  ;;        S T A R T   O F   M A I N   R O U T I N E       
  ;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  (if (setq DirPath (getfiled "Browse for folder"
                              "Open a folder and click on SAVE"
                              " "
                              1
                    )
      )
    (setq DirPath (substr DirPath 1 (- (strlen DirPath) 31)))
  )

  (if DirPath
    (progn

     (setq odbx (if (< (setq Vers (substr (getvar "acadver") 1 2)) "16")
                       (vla-GetInterfaceObject (vlax-get-acad-object) "ObjectDBX.AxDbDocument")
                       (vla-GetInterfaceObject (vlax-get-acad-object) (strcat "ObjectDBX.AxDbDocument." Vers))
                     ))     
      (foreach Dwg (vl-directory-files DirPath "*.dwg" 1)
        (setq FullPath (strcat DirPath Dwg))
        (if (vl-catch-all-error-p
                  (vl-catch-all-apply
                                 '(lambda ()
                                    (vlax-invoke-method odbx 'open FullPath)
                                  )
                  )
            )
          (prompt (strcat "\n ++ Couldn't open drawing \"" FullPath "\""))
          (progn
            (prompt (strcat "\n  - Report for - " FullPath))
            ;;  Replace Strings
            (ReplaceText TextList odbx)

            (vla-saveas odbx (vla-get-name odbx))
          )
        )
      )
      (prompt "\n***  REGEN all drawings to display new text properly.  ***")
      (vlax-release-object odbx)
      (setq odbx nil)
    )
  )



  (princ)
)    ;  **  END MAIN PROCEDURE  **
(prompt "\nText Replace Loaded, enter TextReplace to run.")
(princ)


(defun c:TextReplace ()
  ;;  Add flag values
  ;;  flag 1 = ModelSpace Only
  ;;       2 = PaperSpace Only
  ;;       4 = reserved
  ;;       8 = Include Attributed Blocks  * Not Operational
  ;;
  ;;  text to replace ("OldString" "NewString")
  (TextReplaceDBX 3 ; both Model & Paper Space
    '(("OldText" "NewString")
      ("Test 012807.1" "NewString")
     )
  )
  (princ)
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Preform a "Find and Replace on a Layout Tab" ????
« Reply #8 on: February 26, 2010, 02:25:47 PM »
I thought the request was to find and replace text in a Layout Tab name - maybe I misunderstood  :oops:

Krushert

  • Seagull
  • Posts: 13679
  • FREE BEER Tomorrow!!
Re: Preform a "Find and Replace on a Layout Tab" ????
« Reply #9 on: February 26, 2010, 02:35:15 PM »
I thought the request was to find and replace text in a Layout Tab name - maybe I misunderstood  :oops:
That is correct Sir!
The attribute is linked to CTAB variable using a field.  Well some of them are.  :realmad:

Thanks CAB I still might have a need for those gems.

I + XI = X is true ...  ... if you change your perspective.

I no longer CAD or Model, I just hang out here picking up the empties beer cans

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Preform a "Find and Replace on a Layout Tab" ????
« Reply #10 on: February 26, 2010, 02:53:30 PM »
Talking of Finding text strings, I just provided this for a member over at CADTutor, uses my ODBX base program  8-)

Code: [Select]
;;;¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,;;;
;;;ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,¤º°`°º¤;;;
;;                                                                               ;;
;;                                                                               ;;
;;                       --=={  ObjectDBX Base Program  }==--                    ;;
;;                                                                               ;;
;;  Provides a shell through which a LISP may operate on multiple drawings in a  ;;
;;  folder/sub-folders.                                                          ;;
;;                                                                               ;;
;;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=;;
;;                                                                               ;;
;;  AUTHOR:                                                                      ;;
;;                                                                               ;;
;;  Copyright © Lee McDonnell, January 2010. All Rights Reserved.                ;;
;;                                                                               ;;
;;      { Contact: Lee Mac @ TheSwamp.org, CADTutor.net }                        ;;
;;                                                                               ;;
;;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=;;
;;                                                                               ;;
;;  ARGUMENTS:                                                                   ;;
;;                                                                               ;;
;;  foo  ~  a function taking a single argument (the Document Object), and       ;;
;;          following the 'rules' of ObjectDBX:                                  ;;
;;                                                                               ;;
;;            - No SelectionSets               (ssget,ssname etc.)               ;;
;;            - No Command calls                                                 ;;
;;            - No *Ent Methods                (entget,entmod etc.)              ;;
;;            - No Access to System Variables  (vla-Set/GetVariable, etc)        ;;
;;                                                                               ;;
;;  dwgLst  ~  [Optional]  A list of dwg filepaths to process, if nil, program   ;;
;;                         will display BrowseForFolder dialog.                  ;;
;;                                                                               ;;
;;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=;;
;;                                                                               ;;
;;;¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,;;;
;;;ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,¤º°`°º¤;;;


(defun Mac-ODBX (foo dwgLst / *error* ObjRelease Get_Subs DirDialog ObjectDBXDocument

                              DBX DBXDOC DOCLST DWGLST ERR FILE FILEPATH FLAG FOLDER PATH RESULT SUBS)
  (vl-load-com)

  (setq *acad (cond (*acad) ((vlax-get-acad-object)))
        *adoc (cond (*adoc) ((vla-get-ActiveDocument *acad))))
  
  ;;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=;;

  (defun *error* (msg)
    (ObjRelease (list dbx dbxdoc))
    (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
        (princ (strcat "\n** Error: " msg " **")))
    (princ))

  ;;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=;;

  (defun ObjRelease (lst)
    (mapcar
      (function
        (lambda (x)
          (if (and (eq (type x) 'VLA-OBJECT)
                   (not (vlax-object-released-p x)))
            (vl-catch-all-apply
              (function vlax-release-object) (list x))))) lst))

  ;;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=;;

  (defun Get_Subs (folder / file) ;; CAB
    (mapcar
      (function
        (lambda (x) (setq file (strcat folder "\\" x))
                    (cons file (apply (function append) (get_subs file)))))
        (cddr (vl-directory-files folder nil -1))))

  ;;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=;;
  
  (defun DirDialog (msg dir flag / Shell Fold Path)
    ; Lee Mac  ~ 07.06.09

    (setq *acad (cond (*acad) ((vlax-get-acad-object))))

    (setq Shell (vla-getInterfaceObject *acad "Shell.Application")
          Fold  (vlax-invoke-method Shell 'BrowseForFolder
                  (vla-get-HWND *acad) msg flag dir))
    (vlax-release-object Shell)

    (if Fold
      (progn
        (setq Path (vlax-get-property
                     (vlax-get-property Fold 'Self) 'Path))
        (vlax-release-object Fold)

        (and (= "\\" (substr Path (strlen Path)))
             (setq Path (substr Path 1 (1- (strlen Path)))))))
    
    Path)

  ;;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=;;

  (defun ObjectDBXDocument (/ acVer)

    (setq *acad (cond (*acad) ((vlax-get-acad-object))))
    
    (vla-GetInterfaceObject *acad
      (if (< (setq acVer (atoi (getvar "ACADVER"))) 16) "ObjectDBX.AxDbDocument"
        (strcat "ObjectDBX.AxDbDocument." (itoa acVer)))))

  ;;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=;;

  (if (setq dwgLst

        (cond (dwgLst)

              (  (setq Path (DirDialog "Select Directory to Process" nil 0))

                 (initget "Yes No")
                 (setq subs (cond ((getkword "\nProcess SubDirectories? <Yes> : ")) ("Yes")))

                 (apply (function append)
                        (vl-remove 'nil
                          (mapcar
                            (function
                              (lambda (Filepath)
                                (mapcar
                                  (function
                                    (lambda (Filename)
                                      (strcat Filepath "\\" Filename)))
                                  (vl-directory-files Filepath "*.dwg" 1))))
                            (append (list Path)
                                    (apply (function append)
                                           (if (= "Yes" subs) (Get_Subs Path))))))))))
    (progn

      (vlax-for doc (vla-get-Documents *acad)
        (setq DocLst
          (cons
            (cons
              (strcase
                (vla-get-fullname doc)) doc) DocLst)))

      (setq dbxdoc (ObjectDBXDocument))

      (foreach dwg dwgLst        

        (setq flag (and (setq dbx (cdr (assoc (strcase dwg) DocLst)))))

        (and (not flag)
             (setq Err (vl-catch-all-apply
                         (function vla-open) (list dbxdoc dwg)) dbx dbxdoc))

        (if (or flag (not (vl-catch-all-error-p Err)))
          (setq Result (cons (cons dwg ((eval foo) dbx)) Result))

          (princ (strcat "\n** Error Opening File: " (vl-filename-base dwg)  ".dwg **"))))))

  (ObjRelease (list dbx dbxdoc))

Result)

Code: [Select]
;; Test Function

(defun c:test (/ searchterm)

  (setq searchterm "M7 LUBE PUMP")

  (print
    (Mac-ODBX
      '(lambda (document / i) (setq i 0)
        
         (vlax-for lay (vla-get-layouts document)
          
           (vlax-for obj (vla-get-block lay)

             (if (and (eq "AcDbBlockReference"
                          (vla-get-Objectname obj))
                      (eq :vlax-true (vla-get-hasAttributes obj)))

               (foreach att (append (vlax-invoke obj 'GetAttributes)
                                    (vlax-invoke obj 'GetConstantAttributes))
                
                 (if (eq searchterm (vla-get-TextString att))
                  
                   (setq i (1+ i)))))))

         i)

      nil))

  (princ))

The search term is specified at the top (case-sensitive in this case), and the function returns the number of occurences in each drawing,

Lee

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Preform a "Find and Replace on a Layout Tab" ????
« Reply #11 on: February 26, 2010, 03:02:13 PM »
Case sensitive.

Code: [Select]
(defun c:TEst (/ #Find #Replace)
  (vl-load-com)
  (and
    (setq #Find (getstring T "\nFind what: "))
    (setq #Replace (getstring T "\nReplace with: "))
    (not (vl-position "" (list #Find #Replace)))
    (vlax-for x (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
      (vl-catch-all-apply 'vla-put-name (list x (vl-string-subst #Replace #Find (vla-get-name x))))
    ) ;_ vlax-for
  ) ;_ and
  (princ)
) ;_ defun
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

StykFacE

  • Guest
Re: Preform a "Find and Replace on a Layout Tab" ????
« Reply #12 on: February 08, 2012, 12:12:39 PM »
Case sensitive.

Code: [Select]
(defun c:TEst (/ #Find #Replace)
  (vl-load-com)
  (and
    (setq #Find (getstring T "\nFind what: "))
    (setq #Replace (getstring T "\nReplace with: "))
    (not (vl-position "" (list #Find #Replace)))
    (vlax-for x (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
      (vl-catch-all-apply 'vla-put-name (list x (vl-string-subst #Replace #Find (vla-get-name x))))
    ) ;_ vlax-for
  ) ;_ and
  (princ)
) ;_ defun
Alan, I found this while searching Google. I changed the getstring functions to the text I needed so that it didn't pause for user input and it worked beautifully. I have 261 drawings to convert. I already used Lee's "Batch Find & Replace Text" tool to change text in all 261 drawings which saved us a TON of manual editing time (Lee, thank you so much for that program) but I'd like to change the layout tab to reflect the sheet number change as well. We're simply changing the suffix of each sheet number from DD to CD (Design Documents to Construction Documents).

This isn't really all that important technically because the Layout Tab name doesn't show anything on the sheet so it's really just a preference. I tried a batch lisp from Jeff P Sanders' site but it hangs when there's a proxy graphics notification (FILEDIA and SDI doesn't help). Either way, I was just going to open up about 10 drawings at a time and invoke the raw program that you posted just to speed up the process.

Didn't know if this was ever resolved to run a "batch" text replace for the Layout Tab text. TabSort does the trick but not for multiple DWG files. Thanks in advance guys, and thanks again for these nifty programs. Lee's DEFINITELY saved the day today, holy cow.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Preform a "Find and Replace on a Layout Tab" ????
« Reply #13 on: February 08, 2012, 01:14:24 PM »
You might want to give this one a try.
http://www.theswamp.org/index.php?topic=31827.0
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.