View Full Version : Script needed to use find and replace in scriptpro utility
senecal2001
2005-10-13, 07:23 PM
I need a program, a script specifically, so that i can find and replace multiple items (strings of text) in large quantities of drawings without doing them individually with the find and replace command. I will be using this to quickly make text changes in red-lined drawings. Any help would be greatly appreciated.
I don't think you're going to be able to use a script to do a find and replace. Find and replace is not a command line routine that I know of.
senecal2001
2005-10-13, 08:02 PM
I have a macro already written that can find and replace a text string with another text string, but i don't know how to manipulate it. If this macro would be of any help to anyone then I would be glad to email it. It finds the words tape down and replaces them with the drawing name.
it is a macro that runs inside of a script written to create a dxf from a dwg and find the words tape down and replace them with the drawing name.
You can post the macro here if you like. Here (http://forums.augi.com/misc.php?do=bbcode#code) is how to post code.
senecal2001
2005-10-13, 08:54 PM
;; Tape_down_to_dwg.lsp
;; For Batching .dwgs in script.
;; Finds string "TAPE DOWN"..
;; Replaces it with (drawingname) - the .dwg
(setvar "cmdecho" 0)
;; Place Search String in next line..
(setq CHKSTR "TAPE DOWN")
;;
(setq dwg_name (getvar "DWGNAME"))
(setq dwg_name (substr dwg_name 1 (- (strlen dwg_name) 4)))
; strip the .dwg
(setq new_txt_val (strcase dwg_name))
;;
(setq A (ssget "X")) ; get all elements of drawing.
(if (/= A nil) ; Else if A is nil, do nothing.
(setq B (sslength A)) ; check number of members in 'A' selection set.
(setq B 0) ; --- else, set length to zip.
) ; end if
;;
(if (> B 0) ; must be some entities ?
(progn
(setq CTR 0)
; Note: CTR starts SSNAME function at 0, counts 1,2,3 ..
(repeat B
(setq C (ssname A CTR)) ; get item 0, item 1, etc.
(setq ent (entget C)) ; put assoc list into ent
(setq P (cdr (assoc 0 ent))) ; get entity name
(setq val (cdr (assoc 1 ent))) ; get old value
(if (= P "TEXT")
(progn
(if (= CHKSTR val)
(progn
(setq ent (subst (cons 1 new_txt_val) (assoc 1 ent) ent))
(entmod ent)
(entupd ent)
) ; end progn
) ; end if
) ; end progn
) ; end if
(setq CTR (+ CTR 1)) ; ratchet counter, for next one
) ; end repeat
) ; end progn
) ; end if
(setq CTR nil
P nil
B nil
A nil
val nil
ent nil
C nil
new_txt_val nil
CHKSTR nil
) ; cleanup
(princ)
;; Tape_down_to_dwg.lsp
;; For Batching .dwgs in script.
;; Finds string "TAPE DOWN"..
;; Replaces it with (drawingname) - the .dwg
(setvar "cmdecho" 0)
;; Place Search String in next line..
(setq CHKSTR "TAPE DOWN")
;;
(setq dwg_name (getvar "DWGNAME"))
(setq dwg_name (substr dwg_name 1 (- (strlen dwg_name) 4))); strip the .dwg
(setq new_txt_val (strcase dwg_name))
;;
(setq A (ssget "X")); get all elements of drawing.
(if (/= A nil); Else if A is nil, do nothing.
(setq B (sslength A)); check number of members in 'A' selection set.
(setq B 0); --- else, set length to zip.
); end if
;;
(if (> B 0); must be some entities ?
(progn
(setq CTR 0); Note: CTR starts SSNAME function at 0, counts 1,2,3 ..
(repeat B
(setq C (ssname A CTR)); get item 0, item 1, etc.
(setq ent (entget C)); put assoc list into ent
(setq P (cdr (assoc 0 ent))); get entity name
(setq val (cdr (assoc 1 ent))); get old value
(if (= P "TEXT")
(progn
(if (= CHKSTR val)
(progn
(setq ent (subst (cons 1 new_txt_val)(assoc 1 ent) ent))
(entmod ent)
(entupd ent)
); end progn
); end if
); end progn
); end if
(setq CTR (+ CTR 1)); ratchet counter, for next one
); end repeat
); end progn
); end if
(setq CTR nil P nil B nil A nil val nil ent nil C nil new_txt_val nil CHKSTR nil); cleanup
(princ)
Place the following in your script .scr file
(load "Tape_down_to_dwg.lsp")
If your filename to the above lsp file is different, replace with the correct name. The file must also be in the AutoCAD support paths or place the full directory/filename within the parens. Remember that \ for directories need to either be doubled or switched with a / instead.
senecal2001
2005-10-14, 04:05 PM
I no longer need to replace the words tape down with the drawing name, now i need to replace multiple words with other words within a set of drawings. and the words and replacement words will change with the job. so i need a generic script that i can manipulate to do this.
I no longer need to replace the words tape down with the drawing name, now i need to replace multiple words with other words within a set of drawings. and the words and replacement words will change with the job. so i need a generic script that i can manipulate to do this.
You have the beginning workings of such a script with your original lisp file.
;; Switch_Text.lsp
;; For Batching .dwgs in script.
;; Finds string CHKSTR
;; Replaces it with NEW_TXT_VAL
;; Usage (SWITCHTEXT CHKSTR NEW_TXT_VAL)
(defun SWITCHTEXT (CHKSTR NEW_TXT_VAL / ENTTYPE SSET SSLEN CTR ENT VAL)
(setvar "cmdecho" 0)
(setq SSET (ssget "X" '((0 . "TEXT")))) ; get all elements of drawing.
(if SSET ; Else if A is nil, do nothing.
(progn
(setq SSLEN (sslength SSET)) ; check number of members in 'A' selection set.
(setq CTR 0) ; Note: CTR starts SSNAME function at 0, counts 1,2,3 ..
(repeat SSLEN
(setq ENT (entget (ssname SSET CTR))) ; put assoc list into ent
(setq ENTTYPE (cdr (assoc 0 ENT))) ; get entity name
(setq VAL (cdr (assoc 1 ENT))) ; get old value
(if (= CHKSTR VAL) ; Need to add in case sensitivity This will only be correct
;if the two values are identically cased.
(progn
(setq ENT (subst (cons 1 NEW_TXT_VAL) (assoc 1 ENT) ENT))
(entmod ENT)
(entupd ENT)
) ; end progn
) ; end if
(setq CTR (+ CTR 1)) ; ratchet counter, for next one
) ; end repeat
) ; end progn
) ; end if
(princ)
)
vBulletin® v3.6.7, Copyright ©2000-2010, Jelsoft Enterprises Ltd.