Author Topic: replace character in text file with lisp  (Read 916 times)

0 Members and 1 Guest are viewing this topic.

Topographer

  • Swamp Rat
  • Posts: 639
replace character in text file with lisp
« on: September 28, 2017, 11:22:51 AM »
Hi i am using ths code to export coordinates from my attribiute block  points. I want to do a change in the code. My problem is that in the drawing some points have the letter (for example Q) and when i export the coordinates i need the Q to change with R. Is it possible to do this with lisp? When export the points and before save the file to do this change. The letters  Q and R is for the example
the export file is p,x,y,z

Code - Auto/Visual Lisp: [Select]
  1. (defun c:PXYZ ( / *error* del des ent idx lst obj ord out rtn sel )
  2.  
  3.  
  4.     (defun *error* ( msg )
  5.         (if (= 'file (type des))
  6.             (close des)
  7.         )
  8.         (if (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*"))
  9.             (princ (strcat "\nError: " msg))
  10.         )
  11.         (princ)
  12.     )
  13.  
  14.     (setq ord '("POINT" POINT-X POINT-Y "ELEV" )
  15.           out  (LM:uniquefilename (strcat (getvar 'dwgprefix) (vl-filename-base (getvar 'dwgname))) ".crd")
  16.           del  ","
  17.     )
  18.     (if (setq sel (ssget '((0 . "INSERT") (66 . 1))))
  19.         (if (setq des (open out "w"))
  20.             (progn
  21.                 (repeat (setq idx (sslength sel))
  22.                     (setq ent (ssname sel (setq idx (1- idx)))
  23.                           obj (vlax-ename->vla-object ent)
  24.                     )
  25.                     (setq lst
  26.                         (append
  27.                             (mapcar '(lambda ( a b ) (cons a (rtos b)))
  28.                                '(point-x point-y point-z)
  29.                                 (trans (cdr (assoc 10 (entget ent))) ent 0)
  30.                             )
  31.                             (mapcar '(lambda ( x ) (cons (strcase (vla-get-tagstring x)) (vla-get-textstring x)))
  32.                                 (append
  33.                                     (vlax-invoke obj 'getattributes)
  34.                                     (vlax-invoke obj 'getconstantattributes)
  35.                                 )
  36.                             )
  37.                         )
  38.                     )
  39.                   (setq rtn (cons (vl-remove 'nil (mapcar '(lambda ( x ) (cdr (assoc x lst))) ord)) rtn))
  40.                 )
  41.       (foreach idx (LM:alphanumsort-i (mapcar 'car rtn))
  42.       (write-line (LM:lst->str (nth idx rtn) del) des)
  43.    )
  44.                 (setq des (close des))
  45.             )
  46.             (princ (strcat "\nUnable to open file: \"" out "\" for writing."))
  47.         )
  48.     )
  49.     (princ)
  50. )                
  51.  
  52. ;; List to String  -  Lee Mac
  53. ;; Concatenates each string in a list, separated by a given delimiter
  54.  
  55. (defun LM:lst->str ( lst del )
  56.     (if (cdr lst)
  57.         (strcat (car lst) del (LM:lst->str (cdr lst) del))
  58.         (car lst)
  59.     )
  60. )
  61.  
  62. ;; Unique Filename  -  Lee Mac
  63. ;; Returns a unique filename for a given path & file extension
  64.  
  65. (defun LM:uniquefilename ( pth ext / fnm tmp )
  66.     (if (findfile (setq fnm (strcat pth ext)))
  67.         (progn
  68.             (setq tmp 1)
  69.             (while (findfile (setq fnm (strcat pth "(" (itoa (setq tmp (1+ tmp))) ")" ext))))
  70.         )
  71.     )
  72.     fnm
  73. )
  74.  
  75. ;; Alphanumerical Sort-i  -  Lee Mac
  76. ;; Sorts a list of strings containing a combination of alphabetical & numerical characters and returns the indices.
  77.  
  78. (defun LM:alphanumsort-i ( lst )
  79.     (vl-sort-i (mapcar 'LM:splitstring lst)
  80.         (function
  81.             (lambda ( a b / x y )
  82.                 (while
  83.                     (and
  84.                         (setq x (car a))
  85.                         (setq y (car b))
  86.                         (= x y)
  87.                     )
  88.                     (setq a (cdr a)
  89.                           b (cdr b)
  90.                     )
  91.                 )
  92.                 (cond
  93.                     (   (null x) b)
  94.                     (   (null y) nil)
  95.                     (   (and (numberp x) (numberp y)) (< x y))
  96.                     (   (numberp x))
  97.                     (   (numberp y) nil)
  98.                     (   (< x y))
  99.                 )
  100.             )
  101.         )
  102.     )
  103. )
  104.  
  105. ;; Split String  -  Lee Mac
  106. ;; Splits a string into a list of text and numbers
  107.  
  108. (defun LM:splitstring ( str )
  109.     (
  110.         (lambda ( l )
  111.             (read
  112.                 (strcat "("
  113.                     (vl-list->string
  114.                         (apply 'append
  115.                             (mapcar
  116.                                 (function
  117.                                     (lambda ( a b c )
  118.                                         (cond
  119.                                             (   (or (= 34 b) (= 92 b))
  120.                                                 (list 32 34 92 b 34 32)
  121.                                             )
  122.                                             (   (or (< 47 b 58)
  123.                                                    ;(and (= 45 b) (< 47 c 58) (not (< 47 a 58)))
  124.                                                     (and (= 46 b) (< 47 a 58) (< 47 c 58))
  125.                                                 )
  126.                                                 (list b)
  127.                                             )
  128.                                             (   (list 32 34 b 34 32))
  129.                                         )
  130.                                     )
  131.                                 )
  132.                                 (cons nil l) l (append (cdr l) '(( )))
  133.                             )
  134.                         )
  135.                     )
  136.                     ")"
  137.                 )
  138.             )
  139.         )
  140.         (vl-string->list str)
  141.     )
  142. )
  143.  
  144.  
  145.  

thanks

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9268
Re: replace character in text file with lisp
« Reply #1 on: September 28, 2017, 12:21:11 PM »
I understand your question but my problem is that I cannot read AutoLisp very well anymore (I'm not very good at it anymore because of lack of practice). But from what little I understand of your code I think you're making it too complicated -i.e. why are you sorting a list of chars? Operating on a smallest as possible list (a list of numbers for example) will make your operation faster. I would tackle this in a few steps like so:

1. Read the contents of the file into a list of lists.
2. Iterate through that list and convert to a list of ASCII numbers and search for a value that falls out of range (then replace if necessary).
3. OPTIONAL: do the search and replace here as separate process.
3./4. Convert list back to text and write back to file.

The logic being that searching for number falling outside a specific range is faster then comparing characters or strings.

I have *some* code but you'd have to be the judge of its value (Any autolisp code I have is very old; I wrote it years ago).

I hope that makes sense and what little I offered up helps you in some way. Sorry if it doesn't.

Code - Auto/Visual Lisp: [Select]
  1. (defun file-get-list-from ( name / fp lst read-file-into-list )         ;;/*{{{*/
  2.   ;; file-get-list-from
  3.   ;;
  4.   ;; GENERAL FILE READER
  5.   ;; given a file name this procedure will read the contents
  6.   ;; into a list
  7.   ;;
  8.   ;; EX:
  9.   ;;    (mapcar
  10.   ;;       'read (get-list-from-file
  11.   ;;         (findfile layers-to-load-from-file)))
  12.   ;;
  13.   ;; By: John K
  14.   ;;
  15. (defun read-file-into-list ( str file )         ;;/*{{{*/
  16.   (if str
  17.     (cons
  18.       (if str str)
  19.       (read-file-into-list (read-line file) file))))    ;;/*}}}*/
  20.   (setq fp (open name "R"))
  21.   (setq lst (read-file-into-list (read-line fp) fp))
  22.   (close fp)
  23.  lst )       ;;/*}}}*/
  24.  

Code - Auto/Visual Lisp: [Select]
  1. (defun al-string-to-list (str / alst cntr)      ;;/*{{{*/
  2.   ;; al-string-to-list
  3.   ;;
  4.   ;; Convert a string to a ascii number list of intigers
  5.   ;;
  6.   ;; Ex: (al-string-to-list "test")
  7.   ;;   > (116 101 115 116)
  8.   ;;
  9.   ;; By: John K
  10.   ;;
  11.   (if (= cntr '()) (setq cntr 1))
  12.   (while (<= cntr (strlen str))
  13.     (setq alst (cons (ascii (substr str cntr 1)) alst)
  14.           cntr (1+ cntr)))
  15.   (reverse alst) )       ;;/*}}}*/

Code - Auto/Visual Lisp: [Select]
  1. (defun al-list-to-string (strlist / strlst)     ;;/*{{{*/
  2.   ;; al-list-to-string
  3.   ;;
  4.   ;; Convert a list of ascii numbers to a string
  5.   ;;
  6.   ;; Ex: (al-list-to-string '(116 101 115 116))
  7.   ;;   > ("test")
  8.   ;;
  9.   ;; By: John K
  10.   ;;
  11.   (cond
  12.     ((eq (type StrList) 'LIST)
  13.      (setq strlst "")
  14.       (mapcar
  15.         '(lambda (x) (setq strlst (strcat strlst (chr x))))  
  16.         strlist)
  17.       )
  18.      )
  19.   strlst
  20.   )     ;;/*}}}*/
“Common sense is not so common.” ~Voltaire

--> Donate to TheSwamp.org <--

ribarm

  • Water Moccasin
  • Posts: 2126
  • Marko Ribar, architect
Re: replace character in text file with lisp
« Reply #2 on: September 28, 2017, 01:34:30 PM »
I think OP wants revision of his already posted code... I (perhaps others) don't know though are those letters separate text/mtext entities that are independent and are placed near those blocks or they are bounded to those attributed blocks - perhaps those letters are attributes itself (I mean their textstrings)... It would be nice if OP would upload sample of his testing DWG with some of those block references...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9268
Re: replace character in text file with lisp
« Reply #3 on: September 28, 2017, 01:47:13 PM »
The subject line says: "text file" so I thought it was just that, a text file.

Oh, I don't have the ability to edit anyone's AutoLisp code anymore. Maybe after a weekends hard study I can get back into the swing of things again--enough to scratch through some minor edits based on theory--but I don't really even have the desire to know AutoLisp anymore (I don't even have AutoCAD installed on my work machine).

Sorry, well, I tried.
“Common sense is not so common.” ~Voltaire

--> Donate to TheSwamp.org <--

ronjonp

  • Needs a day job
  • Posts: 7027
Re: replace character in text file with lisp
« Reply #4 on: September 28, 2017, 02:34:20 PM »
Quick glance, replace line 42 with this:
Code - Auto/Visual Lisp: [Select]
  1. (write-line (vl-string-translate "Q" "R" (lm:lst->str (nth idx rtn) del)) des)


Be aware it's case sensitive.

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

Topographer

  • Swamp Rat
  • Posts: 639
Re: replace character in text file with lisp
« Reply #5 on: September 28, 2017, 04:19:21 PM »
Thank you  :-)

ronjonp

  • Needs a day job
  • Posts: 7027
Re: replace character in text file with lisp
« Reply #6 on: September 28, 2017, 04:23:10 PM »

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC