Author Topic: about txt make lisp error  (Read 832 times)

0 Members and 1 Guest are viewing this topic.

dussla

  • Bull Frog
  • Posts: 291
about txt make lisp error
« on: May 13, 2022, 01:50:38 PM »
hi  firends
i modified this lisp .

this lisp is to send  txt coddinate .
sometime is work
but
sometime is error
i can not know problem .

error is  bad arugment type : stringp nil

pls what problem ?




Code - Auto/Visual Lisp: [Select]
  1. (Defun C:smt (/         extractText           makecsvstring        )
  2.  (setvar "osmode" 0)
  3.                    
  4.   (defun extractText
  5.      (filterLIst / outputfile f en i ss name x y z path)
  6.  
  7.                    
  8.     (if (= nil filterLIst)
  9.       (setq filterLIst
  10.          '((-4 . "<or") (0 . "TEXT") (0 . "MTEXT") (-4 . "or>"))
  11.       )
  12.     )
  13.  
  14.  
  15.  
  16.       (setq path  "c:/" )
  17.  
  18.     (setq filename "txt2txt")
  19.  
  20.  
  21.     (setq outputfile
  22.        (strcat path
  23.            "/"
  24.            filename
  25.            ".txt"
  26.        )
  27.     )
  28.  
  29.  
  30.  
  31.                  
  32.     (setq i  -1
  33.       ss (ssget  filterLIst)
  34.     )
  35.                                
  36.  
  37.                
  38.     (if ss
  39.       (progn
  40.     (setq f (open outputfile "w"))
  41.     (if f
  42.       (progn
  43.      
  44.         (while (setq en (ssname ss (setq i (+ 1 i))))
  45.                
  46.                  
  47.                  
  48.                  
  49.                     (vla-GetBoundingBox (vlax-ename->vla-object en) 'Ptmin 'Ptmax)
  50.                     (setq box (cons (vlax-safearray->list PtMin)
  51.                     (vlax-safearray->list PtMax)))
  52.                                 (setq  funx (abs (- (cadr box) (caar box)   ) ) )
  53.                         (setq  funy (abs (- (caddr box) (cadar box))))
  54.                     ;write to csv
  55.        
  56.                  
  57.                    (setq name (makecsvstring (cdr (assoc 1 (entget en)))))smt
  58.            (setq x (rtos (nth 1 (assoc 10 (entget en)))))
  59.            (setq y (rtos (nth 2 (assoc 10 (entget en)))))
  60.            (setq z (rtos (nth 3 (assoc 10 (entget en)))))
  61.                  
  62.                    (setq funxx (rtos (caar box) ))  
  63.                    (setq funyy (rtos (cadar box )))
  64.                    
  65.                    (setq stx (rtos funx ))
  66.                    (setq sty (rtos funy ))  
  67.                    
  68.                  
  69.          (write-line (strcat  X "       " Y "   " Z "   " name "        " funxx "       " funyy "       " stx " " sty    ) f)
  70.        
  71.          
  72.         )
  73.         (close f)
  74.                    
  75.         (Alert "Done!")
  76.       )
  77.                    
  78.       (print "\nWrong input unable to create csv file:")
  79.     )
  80.       )
  81.  
  82.       (print "\nNo text entity found:")
  83.     )
  84.     (princ)
  85.   )
  86.   ;;---------------------------------------------------------------EnD extractText --------------------------------
  87.  
  88.  
  89.  
  90.  
  91.            ;this function add " to start and end of supply string
  92.   (defun makecsvstring (instring)
  93.     (if (= nil instring)
  94.       (setq instring "")
  95.     )
  96.     (strcat instring )
  97.   )
  98.  
  99.  
  100.  
  101.   (extractText filterLIst)
  102.  
  103.  
  104. )



EDIT (John): Added code tags.
« Last Edit: May 13, 2022, 06:59:22 PM by JohnK »

mhupp

  • Bull Frog
  • Posts: 250
Re: about txt make lisp error
« Reply #1 on: May 13, 2022, 08:04:28 PM »
Whats happening is AutoCAD quark  text\mtext that isn't left justified insertion point is stored in dxf code 11. the code doesn't account for this so when it finds text that isn't left justified their isn't any values for x y or z when its trying to write-line so it errors "stringp nil"

updated the code to be a little more efficient and to account for this as well.

Code - Auto/Visual Lisp: [Select]
  1. (defun C:SMT (/ ss f txt LL UR str ent LLx LLy stx sty x y z)
  2.   (setvar "osmode" 0)
  3.   (if (setq ss (ssget '((0 . "*TEXT"))))
  4.     (progn
  5.       (if (setq f (open "C:\\txt2txt.txt" "w"))
  6.         (progn
  7.           (foreach txt (vl-remove-if 'listp (mapcar 'cadr (ssnamex SS)))
  8.             (vla-getboundingbox (vlax-ename->vla-object txt) 'minpt 'maxpt)
  9.             (setq LL (vlax-safearray->list minpt)
  10.                   UR (vlax-safearray->list maxpt)
  11.                   str (cdr (assoc 1 (setq ent (entget txt))))
  12.                   LLx (rtos (car LL) 2)
  13.                   LLy (rtos (cadr LL) 2)
  14.                   stx (rtos (- (car UR) (car LL)) 2)
  15.                   sty (rtos (- (cadr UR) (cadr LL)) 2)
  16.             )
  17.             (if (eq (cdr (assoc 72 ent)) 0)
  18.               (setq x (rtos (cadr (assoc 10 ent)) 2)
  19.                     y (rtos (caddr (assoc 10 ent)) 2)
  20.                     z (rtos (last (assoc 10 ent)) 2)
  21.               )
  22.               (setq x (rtos (cadr (assoc 11 ent)) 2)
  23.                     y (rtos (caddr (assoc 11 ent)) 2)
  24.                     z (rtos (last (assoc 11 ent)) 2)
  25.               )
  26.             )
  27.             (write-line (strcat X "   " Y "   " Z "   " str "   " LLx "   " LLy "   " stx "   " sty) f)
  28.           )
  29.           (close f)
  30.           (Alert "txt2txt Created!")
  31.         )
  32.         (prompt "\nUnable to create CSV file:")
  33.       )
  34.     )
  35.     (prompt "\nNo Text Entity Selected: ")
  36.   )
  37.   (setvar "osmode" 1)
  38.   (princ)
  39. )

Also might want to update the location of where the file is being created.

your documents folder
Code - Auto/Visual Lisp: [Select]
  1. (if (setq F (open (strcat (getenv "userprofile") "\\Documents\\txt2txt.txt") "w"))

or in the same folder as the drawing. (must be saved)
Code - Auto/Visual Lisp: [Select]
  1. (if (setq F (open (strcat (getvar 'dwgprefix) "\\txt2txt.txt") "w"))

dussla

  • Bull Frog
  • Posts: 291
Re: about txt make lisp error
« Reply #2 on: May 14, 2022, 03:18:56 AM »
Whats happening is AutoCAD quark  text\mtext that isn't left justified insertion point is stored in dxf code 11. the code doesn't account for this so when it finds text that isn't left justified their isn't any values for x y or z when its trying to write-line so it errors "stringp nil"

updated the code to be a little more efficient and to account for this as well.

Code - Auto/Visual Lisp: [Select]
  1. (defun C:SMT (/ ss f txt LL UR str ent LLx LLy stx sty x y z)
  2.   (setvar "osmode" 0)
  3.   (if (setq ss (ssget '((0 . "*TEXT"))))
  4.     (progn
  5.       (if (setq f (open "C:\\txt2txt.txt" "w"))
  6.         (progn
  7.           (foreach txt (vl-remove-if 'listp (mapcar 'cadr (ssnamex SS)))
  8.             (vla-getboundingbox (vlax-ename->vla-object txt) 'minpt 'maxpt)
  9.             (setq LL (vlax-safearray->list minpt)
  10.                   UR (vlax-safearray->list maxpt)
  11.                   str (cdr (assoc 1 (setq ent (entget txt))))
  12.                   LLx (rtos (car LL) 2)
  13.                   LLy (rtos (cadr LL) 2)
  14.                   stx (rtos (- (car UR) (car LL)) 2)
  15.                   sty (rtos (- (cadr UR) (cadr LL)) 2)
  16.             )
  17.             (if (eq (cdr (assoc 72 ent)) 0)
  18.               (setq x (rtos (cadr (assoc 10 ent)) 2)
  19.                     y (rtos (caddr (assoc 10 ent)) 2)
  20.                     z (rtos (last (assoc 10 ent)) 2)
  21.               )
  22.               (setq x (rtos (cadr (assoc 11 ent)) 2)
  23.                     y (rtos (caddr (assoc 11 ent)) 2)
  24.                     z (rtos (last (assoc 11 ent)) 2)
  25.               )
  26.             )
  27.             (write-line (strcat X "   " Y "   " Z "   " str "   " LLx "   " LLy "   " stx "   " sty) f)
  28.           )
  29.           (close f)
  30.           (Alert "txt2txt Created!")
  31.         )
  32.         (prompt "\nUnable to create CSV file:")
  33.       )
  34.     )
  35.     (prompt "\nNo Text Entity Selected: ")
  36.   )
  37.   (setvar "osmode" 1)
  38.   (princ)
  39. )

Also might want to update the location of where the file is being created.

your documents folder
Code - Auto/Visual Lisp: [Select]
  1. (if (setq F (open (strcat (getenv "userprofile") "\\Documents\\txt2txt.txt") "w"))

or in the same folder as the drawing. (must be saved)
Code - Auto/Visual Lisp: [Select]
  1. (if (setq F (open (strcat (getvar 'dwgprefix) "\\txt2txt.txt") "w"))
wow
code is very simple
thank you really   rellay  really ~~~

BIGAL

  • Swamp Rat
  • Posts: 1409
  • 40 + years of using Autocad
Re: about txt make lisp error
« Reply #3 on: May 14, 2022, 11:19:41 PM »
Not sure why your using a bounding box, the simplest is get insertion point of text, this varies depending on the text justification, but a entget will reveal dxf code 10 & 11 which can be used.
A man who never made a mistake never made anything