Hi guys,
I have seen several lisp routines to find and replace just text, sometimes text and mtext, but not text, mtext and block attributes.
Here is what I have. I need to search 200 drawings for three values (330, 331, 332) and change them to (336, 337, 338). The problem is that these strings may be text, mtext, or block attributes (attribute example: Unit 330. I just want to replace the 330)
I would like to run script pro to use this since I do not have access to superfind (unfortunately). Can anyone help me?
Thanks so much for any help you can give.
Donnie
Also, the drawing may have one of these numbers, two of them, or all three. Is there a way to program it so that it does not crash if it doesnt find one of the numbers?
Thanks as always,
Donnie
Ok, one more thing I just found out. Some of the drawings do not have any of the numbers listed above. But since we do not know which ones, we need to run the lisp on them all.
Sorry for the disjointed messages.
Donnie
Try this:
(defun c:ReplaceThis (/ SearchStr aDoc SelSet FoundString) ;;l Replace Text pBe May 2011 ;;; (defun SearchStr (lst str / a b done) (while (and (setq a (car lst)) (not done)) (setq b (cdr lst)) (if (vl-string-search a str) (progn (setq Done T) (vl-string-subst (itoa (+ (atoi a) 6)) a str)) (setq lst b)) ) ) (setq aDoc (vla-get-activedocument (vlax-get-acad-object))) (ssget "_X" '((0 . "TEXT,MTEXT,INSERT"))) (vlax-for obj (setq SelSet (vla-get-activeselectionset aDoc)) (if (wcmatch (vla-get-Objectname obj) "*Text") (if (setq FoundString (SearchStr '("330" "331" "332") (vla-get-textstring obj))) (vla-put-textstring obj FoundString)) (if (eq (vla-get-HasAttributes obj) :vlax-true) (foreach attStr (vlax-invoke obj 'Getattributes) (if (setq FoundString (SearchStr '("330" "331" "332") (vla-get-textstring attStr))) (vla-put-textstring attStr FoundString)) ) ) ) ) (vla-delete SelSet) (princ) )
hope this helps
Try this
(vl-load-com) (defun newstring (old new str) (while (vl-string-search old str) (setq str (vl-string-subst new old str)) ) str ) (defun replacenum (obj assoc_lst) (cond ((or (eq (strcase (vla-get-objectname obj)) "ACDBTEXT") (eq (strcase (vla-get-objectname obj)) "ACDBMTEXT")) (progn (setq old_text (vla-get-textstring obj)) (foreach pair assoc_lst (if (vl-string-search (car pair) old_text) (setq old_text (newstring (car pair)(cdr pair)old_text)))) (vla-put-textstring obj old_text)) ) ((and (eq (strcase (vla-get-objectname obj))"ACDBBLOCKREFERENCE") (eq :vlax-true (vla-get-hasattributes obj))) (progn (foreach att (vlax-invoke obj 'Getattributes) (setq old_text (vla-get-textstring att)) (foreach pair assoc_lst (if (vl-string-search (car pair) old_text) (setq old_text (newstring (car pair)(cdr pair)old_text)))) (vla-put-textstring att old_text) ))) (T nil))) (defun supfinder (adoc / new_list old_list assoc_lst) (setq old_list (mapcar 'vl-princ-to-string '(330 331 332));<--change old numbers to suit new_list (mapcar 'vl-princ-to-string '(336 337 338));<--change new number to suit ) (if (/= (length old_list)(length new_list)) (progn (alert "Number of items in both lists not equal. Exit program...") (exit)(princ)) ) (setq assoc_lst (mapcar 'cons old_list new_list)) (vlax-for layout (vla-get-layouts adoc) (vlax-for obj (vla-get-block layout) (replacenum obj assoc_lst) ) ) ) (defun C:REPS (/ acapp adoc file_obj fn fold full-names-list full_name_list ) (vl-load-com) (alert "\nPlease wait a minute \nto ending of batch file operation" ) (setvar "SDI" 0) (setq fn (getfiled "Select *ANY .DWG FILE* in a desired folder : " "" "dwg" 4 ) ) (if fn (progn (setq fold (vl-filename-directory fn) full_name_list (vl-directory-files fold "*.dwg" 1) full_name_list (mapcar (function (lambda (x) (strcat fold "\\" x) ) ) full_name_list ) ) (setq acapp (vlax-get-acad-object) adoc (vla-get-activedocument acapp) ) (if full_name_list (progn (vla-save adoc);optional (mapcar (function (lambda (dwgnmame) (progn (setq file_obj (vla-open (vla-get-documents acapp) dwgnmame)) ;;here is your batch function: (supfinder file_obj) (vla-saveas file_obj dwgnmame) (vla-close file_obj) (vl-cmdf "_delay" 200) ) ) ) full_name_list ) ) (princ "\nNo .DWG files in selected directory\n") ) ) ) (alert "Done") (princ) ) (princ "\n\tType REPS to execute...") (prin1)
Nice Hailex
You may want to use this to browse for Folder
(defun BrowseFolderFiles (/ Shell fold) (setq Shell (vla-getInterfaceObject (vlax-get-acad-object) "Shell.Application") fold (vlax-invoke-method Shell 'BrowseForFolder 0 "Select Folder for Drawing Files" 0)) (vlax-release-object Shell) (setq fold (vlax-get-property (vlax-get-property fold 'Self) 'Path)) (mapcar (function (lambda (x) (strcat fold "\\" x))) (vl-directory-files fold "*.dwg" 1)) )
(setq full_name_list (BrowseFolderFiles))
I am still trying to become proficient in Visual Lisp, but the following is a string search and repalce I have been using for years which includes, TEXT, MTEXT, ATTRIBUTES and DIMENSIONS.
(defun c:superstr()
(setq olsosmode (getvar "OSMODE"))
(setvar "OSMODE" 0)
(setq p (ssget))
(if p
(progn
(setq osl (strlen (setq os (getstring "\nOld string: " t))))
(setq nsl (strlen (setq ns (getstring "\nNew string: " t))))
(setq l 0 chm 0 n (sslength p))
(setq adj
(cond
((/= osl nsl) (- nsl osl))
(T nsl)
)
)
(while (< l n)
(setq d (entget (setq e (ssname p l))))
(if (and (= (atext 0) "INSERT")(= (atext 66) 1))
(progn
(setq e (entnext e))
(while e
(setq d (entget e))
(cond
((= (atext 0) "ATTRIB")
(setq chf nil si 1)
(setq s (cdr (setq as (assoc 1 d))))
(while (= osl (setq sl (strlen
(setq st (substr s si osl)))))
(cond
((= st os)
(setq s (strcat (substr s 1 (1- si)) ns
(substr s (+ si osl))))
(setq chf t)
(setq si (+ si adj))
)
)
(setq si (1+ si))
)
(if chf
(progn
(setq d (subst (cons 1 s) as d))
(entmod d)
(entupd e)
(setq chm (1+ chm))
)
)
(setq e (entnext e))
)
((= (atext 0) "SEQEND")
(setq e nil))
(T (setq e (entnext e)))
)
)
)
)
(if (= "MTEXT" ; Look for MTEXT entity type (group 0)
(cdr (assoc 0 (setq e (entget (ssname p l))))))
(progn
(setq chf nil si 1)
(setq s (cdr (setq as (assoc 1 e))))
(while (= osl (setq sl (strlen
(setq st (substr s si osl)))))
(if (= st os)
(progn
(setq s (strcat (substr s 1 (1- si)) ns
(substr s (+ si osl))))
(setq chf t) ; Found old string
(setq si (+ si nsl))
)
(setq si (1+ si))
)
)
(if chf (progn ; Substitute new string for old
(setq e (subst (cons 1 s) as e))
(entmod e) ; Modify the TEXT entity
(setq chm (1+ chm))
))
)
)
(if (= "DIMENSION" ; Look for DIMENSION entity type (group 0)
(cdr (assoc 0 (setq e (entget (ssname p l))))))
(progn
(setq chf nil si 1)
(setq s (cdr (setq as (assoc 1 e))))
(while (= osl (setq sl (strlen
(setq st (substr s si osl)))))
(if (= st os)
(progn
(setq s (strcat (substr s 1 (1- si)) ns
(substr s (+ si osl))))
(setq chf t) ; Found old string
(setq si (+ si nsl))
)
(setq si (1+ si))
)
)
(if chf (progn ; Substitute new string for old
(setq e (subst (cons 1 s) as e))
(entmod e) ; Modify the TEXT entity
(setq chm (1+ chm))
))
)
)
(if (= "TEXT" ; Look for TEXT entity type (group 0)
(cdr (assoc 0 (setq e (entget (ssname p l))))))
(progn
(setq chf nil si 1)
(setq s (cdr (setq as (assoc 1 e))))
(while (= osl (setq sl (strlen
(setq st (substr s si osl)))))
(if (= st os)
(progn
(setq s (strcat (substr s 1 (1- si)) ns
(substr s (+ si osl))))
(setq chf t) ; Found old string
(setq si (+ si nsl))
)
(setq si (1+ si))
)
)
(if chf (progn ; Substitute new string for old
(setq e (subst (cons 1 s) as e))
(entmod e) ; Modify the TEXT entity
(setq chm (1+ chm))
))
)
)
(setq l (1+ l))
)
)
)
(if (> chm 1)
(princ (strcat "\nUpdated " (itoa chm) " text strings"))
(princ (strcat "\nUpdated " (itoa chm) " text string"))
)
(setvar "OSMODE" oldosmode)
(terpri)
)
;
(defun atext (num)
(cdr (assoc num d))
)
Nice code, thanks for refreshing the old schools way
But OP was asked about:
*I need to search 200 drawings for three values*
Regards,
~'J'~
Hello,
I was trying to use this code for my applications but it ran into an error. I changed what it was trying to find "330 331 332" to K12G72346. Then changed the replacement numbers to K12G12345. All I did was drag and drop the code in the drawing I wanted to search. It loaded fine. then when I executed the command of REPS it asked for a file location then I clicked the file I was already in--> then it opened that file and timed out. What should I do differently?
Can't find what you're looking for? Ask the community or share your knowledge.