Results 1 to 7 of 7

Thread: Text Replace using CSV

  1. #1
    Member
    Join Date
    2010-11
    Posts
    21
    Login to Give a bone
    0

    Default Text Replace using CSV

    Sorry, me again. Marco Ribar, Opie & Lee Mac have helped me loads recently with a function to export all text,mtext,att-text,etc into a CSV file which worked great.

    Since I have to issue loads of different drawings here in dual language, I'd like to cut out the manual task of exporting (see above) and replacing text values once I get the translations sent back (in CSV format - old column, new column).

    I have written some code so far which I'm generally happy with...I just need help with writing a function to replace old vs new text! Code below, blue is the main function, red is the tricky part!:

    Code:
    (defun ReadFile (fname / fno txt flst e flst1); Function to read files in AutoCAD
    	(if (/= fname NIL)
    		(progn
    			(setq fno (open fname "r"))
    			(setq txt (read-line fno))
    			(while (/= txt nil)
    				(setq flst (cons txt flst))
    				(setq txt (read-line fno))
    			) ;while
    			(close fno)
    			(while (= (nth 0 flst) "")
    				(setq flst (cdr flst))
    			) ;while
    			(setq flst (reverse flst))
    		) ;progn
    	) ;if
    	(setq flst flst)
    ) ;defun
    
    (defun String->List (strtxt delim / tmptxt k tmpchr txtlist)
      (setq tmptxt "")
      (setq k 1)
      (while (<= k (strlen strtxt))
        (setq tmpchr (substr strtxt k 1))
        (cond
          ((/= tmpchr delim) (setq tmptxt (strcat tmptxt tmpchr)))
          ((= tmpchr delim) (setq txtlist (cons tmptxt txtlist)) (setq tmptxt ""))
        )	;cond
        (setq k (1+ k))
      )	;while
      (if (/= tmptxt "")
        (setq txtlist (reverse (cons tmptxt txtlist)))
        (setq txtlist (reverse txtlist))
      )	;if
      (setq txtlist txtlist)
    );defun
    
    (defun replace (/ relist elist olist nlist k alltext)
    	(command "undo" "be")
    	(setq mdatxt (cdr (ReadFile (getfiled "Read CSV" "" "csv" 4))))
    	(foreach e mdatxt
    	(setq elist (string->list e ","))
    		(setq olist (cons (nth 0 elist) olist))
    		(setq nlist (cons (nth 1 elist) nlist))
    	); foreach
    	(setq alltext (ssget "x" '((-4 . "<OR") (0 . "TEXT") (0 . "MTEXT") (-4 . "<AND") (0 . "INSERT") (66 . 1) (-4 . "AND>") (-4 . "OR>"))))
    	(if (/= alltext NIL)
    		(progn
    			(setq k 0)
    			(while (< k (length olist))
    			*****Something to replace olist with nlist?******
    				(setq k (1+ k))
    			); while
    		); progn
    	); if
    (command "undo" "e")
    );defun
    
    Criteria:

    Exact text matches only, I have a feeling that partial text matching could be an issue...!
    Would paperspace/modelspace be an issue? If so, paperspace only is fine.

    Any help would be great, and thanks in advance...

    Nick
    Last edited by mrsusan; 2011-10-21 at 12:16 PM. Reason: typo

  2. #2
    I could stop if I wanted to
    Join Date
    2009-03
    Location
    London, England
    Posts
    304
    Login to Give a bone
    0

    Default Re: Text Replace using CSV

    If you can export your CSV "translation file" to a tab-delimited text file, then my Batch Find & Replace program can read it automatically and perform your replacements.

  3. #3
    Member
    Join Date
    2010-11
    Posts
    21
    Login to Give a bone
    0

    Default Re: Text Replace using CSV

    Hi Lee, thanks for the response. I have loaded BFind, but can't find where to load from a txt file? All I can seem to do is select an individual piece text entity and manually type its replacement.

    Maybe I'm just being blind!

    Cheers,

    Nick

  4. #4
    I could stop if I wanted to
    Join Date
    2009-03
    Location
    London, England
    Posts
    304
    Login to Give a bone
    0

    Default Re: Text Replace using CSV

    Quote Originally Posted by mrsusan View Post
    Hi Lee, thanks for the response. I have loaded BFind, but can't find where to load from a txt file? All I can seem to do is select an individual piece text entity and manually type its replacement.
    Sorry Nick, I didn't explain myself fully -

    When you save the search strings in BFind they are saved to a tab-delimited Text file, which may be manually edited.

    The Text file may be located by first saving a few strings using the program, then exiting the program and typing at the command-line:

    Code:
    (findfile "LMAC_BFind_SavedSearches_V2-0.txt")
    Navigate to that file, and observe how it is formatted (including the 'save reference').

  5. #5
    Member
    Join Date
    2010-11
    Posts
    21
    Login to Give a bone
    0

    Default Re: Text Replace using CSV

    Ah, all clear now! Just tried it and it worked great! Thanks for writing that code in the first place, I'm sure it's helped loads of people. I'll be using that from now on

    I'm still curious as to how I could get my code to work as well - after all I am also here to learn!

    Cheers Lee,

    Nick

  6. #6
    I could stop if I wanted to
    Join Date
    2009-03
    Location
    London, England
    Posts
    304
    Login to Give a bone
    0

    Default Re: Text Replace using CSV

    Quote Originally Posted by mrsusan View Post
    Ah, all clear now! Just tried it and it worked great! Thanks for writing that code in the first place, I'm sure it's helped loads of people. I'll be using that from now on
    Excellent! Glad that the program works as required.

    Quote Originally Posted by mrsusan View Post
    I'm still curious as to how I could get my code to work as well - after all I am also here to learn!
    FWIW, I would approach the task in the following way:


    • Read CSV 'Translation file', create association list of dotted pairs: first element = text to be replaced, second element = replacement text.


    • Iterate through Drawing Block Collection, this means iterating through the ModelSpace and PaperSpace blocks also.


    • For each block, iterate through the objects contained within the block.


    • For each object, if the object is Text, MText, Dimension, MLeader, Leader, Attributed Block, or any other object that can support annotation:


    • For each item in the association list, if the item can be found within the string as a whole word, replace the item with the associated replacement.
    • Move on to next object in the block.
    However, you will have great difficulty when dealing with MText formatting occuring in MText, Dimensions, MLeaders, and Multi-line Attributes since you would need to replace the text without modifying the formatting in any way (else the Text displayed would be scrambled). For this task, I opted to use RegularExpressions, since they are far more powerful for string substitution operations.

    Lee

  7. #7
    Woo! Hoo! my 1st post
    Join Date
    2012-02
    Posts
    1
    Login to Give a bone
    0

    Default Re: Text Replace using CSV

    I saw you can do a multiple find and replace a list of items in text online at http://list-replace.info . Maybe this can help.

Similar Threads

  1. Need help with: Find and replace text
    By drdesignz in forum AutoCAD General
    Replies: 1
    Last Post: 2009-10-19, 01:03 PM
  2. File and Replace text via C#
    By nvphatbk in forum Dot Net API
    Replies: 3
    Last Post: 2008-12-16, 06:23 PM
  3. find and replace text
    By degonia.designs in forum ACA General
    Replies: 1
    Last Post: 2007-06-11, 08:30 PM
  4. Find and Replace Text
    By ccowgill in forum AutoLISP
    Replies: 60
    Last Post: 2006-09-02, 08:12 PM
  5. RS3: Find and Replace Text
    By nemmersbj in forum Revit Structure - Wish List
    Replies: 0
    Last Post: 2006-08-26, 03:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •