Macekkrecek 发表于 2022-7-5 23:25:56

Lisp with error-handling

Dear Lisp-Proffessionals
 
i have this following little Code with which i try to convert a text's entity style into "Simplex" (just an example). The text is on the layer "Test". Now, when there is a textfield on the layer "Test", the lisp works perfectly fine, but as soon as there is no textfield it doesn't. this wouldn't be a problem, but since there are some other conversion-codes after this particular one the whole code isn't working..
here is the code:
 
(defun c:Convert(/ entities len count ent ent_data ent_name new_style_name )
 
(setq entities (ssget "X" '(
      (-4 . "")
      (-4 . "")))
      len      (sslength entities)
      count 0
)
   (while (
    (setqent      (ssname entities count)
         ent_data (entget ent)
         ent_name (cdr (assoc 7 ent_data))
   )
      (setq new_style_name (cons 7 "SIMPLEX"))      
    (setq ent_data (subst new_style_name (assoc 7 ent_data) ent_data))
    (entmod ent_data)
   (setq count (+ count 1))
    ) ; while
); defun
 
I guess what i need is some error-handling? So i thought to put in the two lines to stop the error:
; if
; progn entities
 
(defun c:Convert(/ entities len count ent ent_data ent_name new_style_name )
 
(setq entities (ssget "X" '(
      (-4 . "")
      (-4 . "")))
      len      (sslength entities)
      count 0
)
(if entities               
(progn               
   (while (
    (setqent      (ssname entities count)
         ent_data (entget ent)
         ent_name (cdr (assoc 7 ent_data))
   )
      (setq new_style_name (cons 7 "SIMPLEX"))      
    (setq ent_data (subst new_style_name (assoc 7 ent_data) ent_data))
    (entmod ent_data)
   (setq count (+ count 1))
    ) ;while               
   ) ;progn               
) ; if               
); defun
 
Even with that it wouldn't do the following conversions. I don't know much of errror-handling that is maybe why... Can please somebody tell me what i'm doing wrong?
 
Thank you!

Tharwat 发表于 2022-7-5 23:54:56

Welcome to CADTutor .
 
Read this about code posting guidlines .
 
http://www.cadtutor.net/forum/showthread.php?9184-Code-posting-guidelines&p=51051&viewfull=1#post51051

David Bethel 发表于 2022-7-5 23:56:29

Maybe:
 

(defun c:convert (/ ss i en ed) (if (setq ss (ssget "X" '((0 . "*TEXT")(8 . "TEST"))))   (progn       (setq i 0)       (while (setq en (ssname ss i))            (setq ed (entget en))            (entmod (subst (cons 7 "SIMPLEX") (assoc 7 ed) ed))            (setq i (1+ i))))) (prin1))
 
 
-David

Macekkrecek 发表于 2022-7-6 00:10:57

Thank you David for you quick adviceworks perfect now!
...and sorry for not posting correctly, will do different next time...
Have a good day!

David Bethel 发表于 2022-7-6 00:37:54

Glad it worked,
 
Welcome to CADTutor
页: [1]
查看完整版本: Lisp with error-handling