LISP, find and replace (Specia
Hi,i'm having a little poblem. I want to find and replace some "special characters" in allot of drawings, without a dialog box. i'm having problems with the square bracktes.
for example:
Before, After
============
"]", "A"
"[", "B"
"/", "C"
"{", "D"
i tried to modify this script...
http://www.cadtutor.net/forum/showthread.php?19333-Find-and-replace-text-without-a-dialog-box
(defun C:RT (/ acapp acsp adoc adocs fn fold full_name_list st);; local defun;; by Michael Puckett(defun Replace ( oldText newText text / i )(if (/= oldText newText)(while (setq i (vl-string-search oldText text))(setq text(vl-string-substnewTextoldTexttexti))))text) (or (vl-load-com)) (setq fn (getfiled "Select *ANY .DWG FILE* in a desired folder : " "" "dwg" 4 ) 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) adocs (vla-get-documents acapp) ) (foreach fl full_name_list (setq adoc (vla-open adocs fl :vlax-false)) (setq acsp (vla-get-modelSpace adoc)) (vlax-for lt (vla-get-layouts adoc) (vlax-for obj (vla-get-block lt) (if (eq "AcDbText" (vla-get-objectname obj)) (if (wcmatch (setq st (vla-get-textstring obj)) "*D.F.*") (vla-put-textstring obj (Replace "DF" "D.F." st)) ) ) ) ) (vla-save adoc) (vla-close adoc) ))(princ "\n\t***\tStart command with RT\t***")(princ) You may need to use the (chr and (ascii functions you can find stuff like "/" basicly any key on the keyboard
(ascii "{") = 123(chr 123) = "{"(Replace (chr 123) "A" st)
thx, i'll try that How to modify script to be able to replace multiple characters...
Script:
(defun C:RT (/ acapp acsp adoc adocs fn fold full_name_list st) (defun Replace ( oldText newText text / i ) (if (/= oldText newText) (while (setq i (vl-string-search oldText text)) (setq text (vl-string-subst newText oldText text i ) ) ) ) text ) (or (vl-load-com)) (setq fn (getfiled "Select *ANY .DWG FILE* in a desired folder : " "" "dwg" 4 ) 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) adocs (vla-get-documents acapp) ) (foreach fl full_name_list (setq adoc (vla-open adocs fl :vlax-false)) (setq acsp (vla-get-modelSpace adoc)) (vlax-for lt (vla-get-layouts adoc) (vlax-for obj (vla-get-block lt)(if (eq "AcDbText" (vla-get-objectname obj))(if (wcmatch (setq st (vla-get-textstring obj)) "*") (vla-put-textstring obj (Replace (chr 91) "Å" st)) ;(vla-put-textstring obj (Replace (chr 93) "Ä" st)) Try this
(if (wcmatch (setq st (vla-get-textstring obj)) "*")(progn (vla-put-textstring obj (Replace (chr 91) "Å" st)) (vla-put-textstring obj (Replace (chr 93) "Ä" st)) Thx bigal,
but still doesn't work.. (no errors but only replaces the last letter "(Replace (chr 92) "Ö" st)"
i think the problem is in the first part.. if i understand the code correctly (i'm more used to C# programming)
... (defun Replace ( oldText newText text / i )...
complete code:
(defun C:RT (/ acapp acsp adoc adocs fn fold full_name_list st) (defun Replace ( oldText newText text / i ) (if (/= oldText newText) (while (setq i (vl-string-search oldText text)) (setq text (vl-string-subst newText oldText text i ) ) ) ) text ) (or (vl-load-com)) (setq fn (getfiled "Select *ANY .DWG FILE* in a desired folder : " "" "dwg" 4 ) 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) adocs (vla-get-documents acapp) ) (foreach fl full_name_list (setq adoc (vla-open adocs fl :vlax-false)) (setq acsp (vla-get-modelSpace adoc)) (vlax-for lt (vla-get-layouts adoc) (vlax-for obj (vla-get-block lt) (if (eq "AcDbText" (vla-get-objectname obj)) (if (wcmatch (setq st (vla-get-textstring obj)) "*") (progn (vla-put-textstring obj (Replace (chr 91) "Å" st)) (vla-put-textstring obj (Replace (chr 93) "Ä" st)) (vla-put-textstring obj (Replace (chr 92) "Ö" st)) ) ) ) ) ) (vla-save adoc) (vla-close adoc) ))(princ "\n\t***\tStart command with RT\t***")(princ) Trying to understand how the script works... how to modify it to do someting simple like Zoom Extents, dont get it to work, just opens all the drawings and saves them... so i must have missed something
(defun C:RT (/ acapp acsp adoc adocs fn fold full_name_list)(vl-load-com) (setq fn (getfiled "Select *ANY .DWG FILE* in a desired folder : " "" "dwg" 4) ;Location fold (vl-filename-directory fn) ;Filename full_name_list (vl-directory-files fold "*.dwg" 1) ;changeto Location + Filename full_name_list (mapcar (function (lambda (x) (strcat fold "\\" x))) full_name_list) ) (setq acapp (vlax-get-acad-object) adocs (vla-get-documents acapp) ) (foreach fl full_name_list (setq adoc (vla-open adocs fl :vlax-false)) (setq acsp (vla-get-modelSpace adoc)) (command ".ZOOM" "Extents") (command ".ZOOM" "0.95x") (vla-save adoc) (vla-close adoc) ))(princ "\n\t***\tStart command with RT\t***")(princ) Script is as below
open dwg1 (load "MYRT")(c:RT) CLOSE y
Open dwg2 (load "MYRT")(c:RT) CLOSE y
as you will run multitimes you can add the (Load "MYrt.lsp) to your acaddoc.lsp this way its ready to be used any time
the its just
open dwg1 (c:RT) CLOSE y
thx for the tip,
now is the problem i want to open every drawing in a specific folder.. when i rewrite the code like this than it opens every drawing in the folder, but i also want to run a lisp from the acaddoc.lsp, and i dont know how to do that in this cript.
(defun C:RT (/ acapp acsp adoc adocs fn fold full_name_list) (or (vl-load-com)) ;ensure that AutoLISP ActiveX support is loaded (setq fn (getfiled "Select *ANY .DWG FILE* in a desired folder : " "" "dwg" 4) fold (vl-filename-directory fn) ;Location full_name_list (vl-directory-files fold "*.dwg" 1) ;Filename full_name_list (mapcar (function (lambda (x) (strcat fold "\\" x))) full_name_list) ;changeto Location + Filename ) (setq acapp (vlax-get-acad-object) ;Retrieves the top level AutoCAD application object for the current AutoCAD session adocs (vla-get-documents acapp) ) (foreach drawing full_name_list (setq vgao (vlax-get-acad-object)) (setq vgad (vla-get-activedocument vgao)) (setq vgd (vla-get-documents vgao)) (setq cd drawing) (if (= 0 (getvar "SDI")) (vla-activate (vla-open vgd cd)) (vla-sendcommand vgad (strcat "(command \"_open\")\n" cd "\n")) ) ))(princ "\n\t***\tStart command with RT\t***")(princ) A simple test is add (C:RT) as the last item in your foreach not sure if it will work you may need also a extra bit to make the last opened dwg the current one. I just use the old fashioned way with a script, You can get your file list and get the lisp to write a scriptlike above example.
页:
[1]
2