Argo Posted October 13, 2015 Share Posted October 13, 2015 Hi All, I'm currently trying to get AutoCAD to replace all instances of a string of text with another. Basically the reason for this is because I have a .las file which is designed to apply layer states to all layers including XREF's but the name and thus the prefix before each XREF will change depending on the project I am working on. Before you mention I know I could simply do a Find and Replace in notepad but I want the user to be able to do it all through CAD. I managed to find a code which replaces text within a file on a certain line to something new but I require the code to do a search and replace through the whole file and replace a certain portion of text (with wildcards) to text which the user will specify. I.E. The .las file specifies the layer name within an XREF in a format of XREF_NAME|Layer_NAME. So an Example string would be 'Project 12345-Architectual Plan|Walls' where 'Project 12345-Architectural' Is the XREF name and 'Walls' is the layer name. I'd like to change the '12345' part as each project will have a different code. I'd need the lisp to prompt the user to input the replacement text. (defun c:test (/ $TEXT $FILENAME lineList newList LineNumToChange) (setq LineNumToChange 124) ; set filename to change (setq $FILENAME (findfile "SDSK.DFM")) ; new text to swap out with old (setq $TEXT "PROT=(G:\\cadd standards\\Surveying\\Prototypes\\,Project Prototypes)") ;;;--- Open the text file (if (findfile $FILENAME) (progn (if (setq fil(open (findfile $FILENAME) "r")) (progn ;;;--- Read the lines of data in the text file (while (setq a (read-line fil)) ;;;--- Add the data to the list (setq lineList (append lineList (list a) ) ) ) ;;;--- Close the file. (close fil) ) ) ) ) (setq count 0) (foreach item lineList (progn (if (= count (- LineNumToChange 1)) (setq newList(append newList(list $TEXT))); if true do this (setq newList(append newList(list item))); if false do this ) (setq count (+ count 1)) ) ) ;;;--- write changes back to file (setq f (open $FILENAME "w")) ; loop to write list to file (foreach item newlist (write-line item f) ) ;;;--- Close the file. (close f) (princ) ) Quote Link to comment Share on other sites More sharing options...
BlackBox Posted October 14, 2015 Share Posted October 14, 2015 While external reference file names may be unique, regardless of relative path or not, they can be named anything you want within the host drawing... Proposed, Existing, etc... Which makes your Layer State 'relative' to any, and all projects thereafter. Consider instead streamlining the renaming of these references at attachment, so you mitigate the need to secondarily modify an .LAS, and subsequently re-import + restore. Cheers Quote Link to comment Share on other sites More sharing options...
Argo Posted October 14, 2015 Author Share Posted October 14, 2015 Hi There, Well that may indeed make life easier. I'll see how it all goes and report back Quote Link to comment Share on other sites More sharing options...
BlackBox Posted October 14, 2015 Share Posted October 14, 2015 Good luck! Should you be interested to know more about Layer States, and how AutoCAD handles them under the hood, this may be of some help: http://forums.augi.com/showthread.php?126337-Restore-a-Layer-State-when-opening-closing&p=1289465&viewfull=1#post1289465 Cheers Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.