Author Topic: Find & Replace Script - Quick Question  (Read 16980 times)

0 Members and 1 Guest are viewing this topic.

M-dub

  • Guest
Find & Replace Script - Quick Question
« on: January 26, 2007, 03:53:21 PM »
We have about 100 or so drawings that were given temporary drawing numbers like SK-1234-01 while we waited to have proper drawing numbers assigned to us.  We now have those proper drawing numbers and we have to update all of the drawings before they go back to the client.  In order to make sure we cover all of the references to other drawings in addition to what's in the title block, this is what I'm thinking...

Would it be too much to expect from a script to do the following (pseudo script code):

Open SK-1234-01
Do a global find and replace on the text, "SK-1234-01" to "IS-923-01-34" and "SK-1234-02" to "IS-923-01-35" and basically go through the whole list, replacing the sketch number with the proper number (about 100).
SaveAs IS-923-01-34

The bold portion is what I'm wondering about... Although it might take a little while to get through the whole list, would it be too taxing on the PC to do this?  I don't think so, but I thought I'd throw it out there for you folks to see what you think.


(I will also have to do the exact same Find and Replace but removing the hyphens)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Find & Replace Script - Quick Question
« Reply #1 on: January 26, 2007, 04:01:43 PM »
CHText.lsp might do what you want
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Find & Replace Script - Quick Question
« Reply #2 on: January 26, 2007, 04:03:28 PM »
you can also make a rename.bat file to rename all your SK dwgs to IS dwgs.  You can make this halfway in about 3 minutes.  If you know what the numbers are going to be, and they are sequential, you might get the whole batch file in under 5 minutes
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

M-dub

  • Guest
Re: Find & Replace Script - Quick Question
« Reply #3 on: January 26, 2007, 04:05:19 PM »
CHText.lsp might do what you want

Yeppers!  :)

That's what I planned on using IN my script.

I may not have explained my issue very well.

I have a list of 100 drawings.  I have to open each drawing and replace 100 strings of text with 100 OTHER strings of text.

Now that I think of it, it sounds pretty simple... should do just fine.  I guess I might start to worry if it were 10 times that amount.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Find & Replace Script - Quick Question
« Reply #4 on: January 27, 2007, 11:59:21 AM »
I have a lisp from Will Deloach that does that for all files in a directory using ObjectDBX. (Very Fast)
It would have to be modified to accommodate all the text you want to change as it was set up
to change only one text string. I tried to find the old code in the swamp but have had no luck
linking you to it.
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.

M-dub

  • Guest
Re: Find & Replace Script - Quick Question
« Reply #5 on: January 27, 2007, 12:50:35 PM »
That sounds good Alan!

Well, on Monday, I'll be spending an hour or so looking into this to decide whether it's worth automating it or whether we should just do it manually.  We'll catch up with this one then.

Thanks!  :)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Find & Replace Script - Quick Question
« Reply #6 on: January 29, 2007, 08:24:04 AM »
Try this out Mike.
It only works in Modelspace in this first test version & only works with mText & plain Text.
I or someone else can add the layouts & attributes if you need them.
I am short of time this morning.
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 textlist / DIRPATH DWGOBJ FULLPATH OBJ ODBX OLDTXT TEXTLIST TXT TXTLST)
  ;;  Add flag values
  ;;  flag 1 = ModelSpace Only
  ;;       2 = PaperSpace Only
  ;;       4 = Reserved
  ;;       8 = Include Attributed Blocks  * Not Operational
  ;;
  ;;  textlist = text to replace ("OldString" "NewString")

  (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)
)
« Last Edit: February 19, 2007, 03:32:26 PM by CAB »
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.

M-dub

  • Guest
Re: Find & Replace Script - Quick Question
« Reply #7 on: January 29, 2007, 08:30:49 AM »
Awesome!  Thanks, CAB!  I will be on that job in about 2 hours or so.  I'll let you know how it works out for me.

BTW, 98% of all of our work is done in modelspace and this particular client doesn't use attributes too often, but I can't thank you enough for doing that!!!  :-D

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Find & Replace Script - Quick Question
« Reply #8 on: January 29, 2007, 11:37:34 AM »
You might want to test this on one drawing first.  I don't know how well text is handled with ObjectDBX if the text is not left justified.  I know attributes will show up funny, but not sure about text.  Just an FYI...
Tim

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

Please think about donating if this post helped you.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Find & Replace Script - Quick Question
« Reply #9 on: January 29, 2007, 02:49:26 PM »
Tim,I have not tested it but Jeff Said:
Quote
Tim, the alignment problem is only with Attributes and only when you change the alignment. Just changing the value does not cause a problem, IIRC. I'm pretty sure that Autocad already prevents you from creating the Xrefs in a loop. Although a problem is encountered with nested Xrefs, which is why the Reload is wrapped by the (vl-catch-all-apply).....you can't reload a nested xref, only the parent xref.
http://www.theswamp.org/index.php?topic=8246.msg105654#msg105654
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.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Find & Replace Script - Quick Question
« Reply #10 on: January 29, 2007, 03:29:51 PM »
Tim,I have not tested it but Jeff Said:
Quote
Tim, the alignment problem is only with Attributes and only when you change the alignment <snip>
This statement is wrong, if you change the value of the attribute when it is justified to something besides left, then the display is messed up.  I think if you go in and edit it, then it will show correct.  I have tested this and know it to be true.
Tim

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

Please think about donating if this post helped you.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Find & Replace Script - Quick Question
« Reply #11 on: January 29, 2007, 03:34:02 PM »
Thanks for the clarification, good to know.
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.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Find & Replace Script - Quick Question
« Reply #12 on: January 29, 2007, 03:36:30 PM »
I just tried it with just text, and when first opened it shows them in the wrong stop, but then with my company setup (it does somethings that I wish it wouldn't) it regened the drawing, and all the text is shown in the right spot.

FYI..

Thanks for the clarification, good to know.
You're welcome.
« Last Edit: January 29, 2007, 03:37:57 PM by T.Willey »
Tim

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

Please think about donating if this post helped you.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Find & Replace Script - Quick Question
« Reply #13 on: January 29, 2007, 03:43:20 PM »
Ha, you beat me to it. I was typing this. :-)
Did a quick test on plain text center justified. The text appeared off a bit but the grips were in the correct position.
If you regen then the text reappears in the correct location. The grips were in the correct location.
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.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Find & Replace Script - Quick Question
« Reply #14 on: January 29, 2007, 03:49:22 PM »
Ha, you beat me to it. I was typing this. :-)
i ninja!
Did a quick test on plain text center justified. The text appeared off a bit but the grips were in the correct position.
If you regen then the text reappears in the correct location. The grips were in the correct location.
This might be the same issue with attributes then, but to regen them you have to edit the block, or move it.
Tim

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

Please think about donating if this post helped you.