Hugh,
I think I've failed you. I'm so close but just can't close it. The code I have below gives you a dialog box to select a file (single selection only). Once you select the file and click open it reloads the dialog box to select another file and continues this process until you click cancel. Then it continues with the rest of the routine. This process can be changed to select all the dwg's in a folder if you need that instead.
I tried to emulate the ACAD find & replace, but did a bad job. I put in 3 options to search by:
1. Matching the whole word only.
2. Matching case only.
3. Matching both whole word and case.
To say the least this was not easy. I quickly determined that I didn't have the time nor the skills to effectively construct this for you. So I am putting my work here in hopes that some other brave souls will take pity on me and help you out and instruct me on how to do this (if even possible). My code currently only works for option #2 (case). I am too busy right now to work on option #1. The only way I could think of to implement option #1 was to look at the ascii character values of the text string and compare those to the space value, I just don't have that much time right now. Sorry.
The other problem that I had wasn't so big, kind of little. I can't get the program to save the changes back to the file. I'm doing something wrong ang\d its not updating the textstring properly. Once again I'm am truely sorry for getting your hopes up and then failing you miserably.
Hope this helps in some small way,
Code:
(vl-load-com)
(defun *error* (msg)
(princ "\nError: ")
(princ msg)
(princ)
(if (and dbxdoc (not (vlax-object-released-p dbxdoc)))
(vlax-release-object dbxdoc)
)
(gc)
(princ)
)
(defun fandr (txtobj)
(vl-string-subst news olds (vla-get-textstring txtobj))
)
(defun findandreplacetext (document)
(vlax-for item (vla-get-modelspace document)
(cond ((not (= (vla-get-ObjectName item) "AcDbText")))
;((and whole case)()) This matches whole word and case
;(whole ()) This matches whole word only
(case (fandr item)); This matches case only
(T (fandr item)); This matches neither whole word nor case
)
)
)
(defun c:tfar (/ file files dcl_id dbxdoc of olds news whole case)
(setq file "")
(while (setq file
(getfiled "Select a file to replace text in" file "dwg" 128)
)
(setq files (cons file files))
)
(cond ((not files) (princ "No files were selected."))
((not (setq dcl_id (load_dialog "textreplace.dcl")))
(princ "Unable to load dialog box.")
)
((not (new_dialog "textreplace" dcl_id))
(princ "Could not load (textreplace.dcl) dialog box.")
)
((not
(and (not (mode_tile "eb1" 2))
(action_tile "eb1" "(setq olds (get_tile \"eb1\"))")
(action_tile "eb2" "(setq news (get_tile \"eb2\"))")
(action_tile "tog1" "(setq whole $value)")
(action_tile "tog2" "(setq case $value)")
(action_tile "cancel" "(done_dialog)")
(action_tile "ok" "(done_dialog)")
(if (and (= (start_dialog) 1)
(> (strlen olds) 0)
(> (strlen news) 0)
)
(not (unload_dialog dcl_id))
(unload_dialog dcl_id)
)
)
)
(princ "Function Cancelled by User.")
)
((not (setq dbxdoc (vla-GetInterfaceObject
(vlax-get-acad-object)
"ObjectDBX.AxDbDocument.16"
)
)
)
(princ "Unable to load ObjectDBX.")
)
(T
(foreach f (reverse files)
(setq of (vl-catch-all-apply
'(lambda ()
(vlax-invoke-method dbxdoc 'open f)
)
)
)
(if (vl-catch-all-error-p of)
(*error* (vl-catch-all-error-message of))
(progn
(findandreplacetext dbxdoc)
(vla-saveas dbxdoc f)
)
)
)
)
)
(if (and dbxdoc (not (vlax-object-released-p dbxdoc)))
(vlax-release-object dbxdoc)
)
(gc)
(princ)
)
Code:
//dcl_settings : default_dcl_settings {audit_level = 3; }
textreplace : dialog {
label = "Replace Text" ;
: row {
: column {
: edit_box {
key = "eb1";
label = "Find text string:";
edit_width = 20;
is_default = true;
}
: edit_box {
key = "eb2";
label = "Replace with:";
edit_width = 20;
}
}
: boxed_column {
: toggle {
key = "tog1";
label = "Find whole words only";
}
: toggle {
key = "tog2";
label = "Match case";
}
}
}
ok_cancel ;
}