Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

excel equivalente to =Iferror() for Visual Lisp

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
jtm2020hyo
320 Views, 4 Replies

excel equivalente to =Iferror() for Visual Lisp

I'm learning Visual Lisp trying to create lines using vlax-curve-getclosestpointto,

 

complete code:

 

(defun j_iferror (x y / j_caa j_cae_p)
  (setq j_caa   (vl-catch-all-apply
                   (list x)
                )
        j_cae_p (cond
                  ((vl-catch-all-error-p j_caa)
                   y
                  )
                  ((not (vl-catch-all-error-p j_caa))
                   x
                  )
                 
                  (T
                   (vl-exit-with-error
                     (strcat "\nError: " (vl-catch-all-error-message j_caa))
                   )
                  )
                )
  )
)

(defun c:JTMgetclosestpointto2 (/ j_acadobj j_actdoc j_ssget j_ssadd j_ctrl item
                                item_dump j_delete pt1 3dpt1
                               )
  (vl-load-com)
  (setq j_acadobj (vlax-get-acad-object)
        j_actdoc  (vla-get-activedocument j_acadobj)
        j_ssget   (vla-get-selectionsets j_actdoc)
        j_delete_p (JTMextract-j-delete-p j_ssget)
        j_ctrl    0
  )

  (eval (j_iferror '(vla-add j_ssget "j_ss02") '(vla-delete (vla-item j_ssget "j_ss02"))))

  (setq j_ssadd (vla-add j_ssget "j_ss02"))

  (vla-selectonscreen j_ssadd)

(while (setq pt1 (getpoint "\n pick point:"))
  (setq item      (vla-item j_ssadd j_ctrl)
        3dpt1     (vlax-3d-point pt1)
        item_dump (vlax-curve-getclosestpointto item pt1)
        j_addline (vla-addline j_mspace
                               (vlax-3d-point item_dump)
                               (vlax-3d-point pt1)
                  ) ;j_ctrl    (1+ j_ctrl)
  )
)
(setq j_delete (vl-catch-all-apply '(vla-delete (vla-item j_ssget "j_ss02"))))
)

... but when I try manage the error with an external defun is impossible, the problem is in:

 

(eval (j_iferror '(vla-add j_ssget "j_ss02") '(vla-delete (vla-item j_ssget "j_ss02"))))

 

 

... this should work like excel iferror function, I mean, if the first expression fails, should return the 2nd expression being evaluated, if there are no errors, the 1st expression should be evaluated.

 

... until I understand, the problem is the variable's scope.

 

Then, how to solve? (or how to create an excel iferror equivalent in visual lisp?)

4 REPLIES 4
Message 2 of 5
komondormrex
in reply to: jtm2020hyo

somewhat shortened and corrected

 

(defun j_iferror (x y / selection)
  (if (vl-catch-all-error-p (setq selection (vl-catch-all-apply 'vla-add (list x y))))
    (progn
     (vla-delete (vla-item x y))
     (setq selection (vla-add x y))
    ) 
  )
  selection
)

(defun c:JTMgetclosestpointto2 (/ j_acadobj j_actdoc j_ssget j_ssadd j_ctrl item
                                item_dump j_delete pt1 3dpt1
                               )
  (vl-load-com)
  (setq j_acadobj (vlax-get-acad-object)
        j_actdoc  (vla-get-activedocument j_acadobj)
        j_mspace (vla-get-modelspace j_actdoc) 
        j_ssget   (vla-get-selectionsets j_actdoc)
;;;     j_delete_p (JTMextract-j-delete-p j_ssget)
        j_ctrl    0
  )

  (setq j_ssadd (j_iferror j_ssget "j_ss02"))
  (vla-selectonscreen j_ssadd)

(while (setq pt1 (getpoint "\n pick point:"))
  (setq item      (vla-item j_ssadd j_ctrl)
        item_dump (vlax-curve-getclosestpointto item pt1)
        j_addline (vla-addline j_mspace
                               (vlax-3d-point item_dump)
                               (vlax-3d-point pt1)
                  ) ;j_ctrl    (1+ j_ctrl)
  )
)
)

 

 

Message 3 of 5
jtm2020hyo
in reply to: jtm2020hyo

Thanks for reply. Is there any possibility to keep the input for j_error?

j_iferror '(vla-add j_ssget "j_ss02") '(vla-delete (vla-item j_ssget "j_ss02"))))

This for recicle the code and used not only for selection.

Message 4 of 5
komondormrex
in reply to: jtm2020hyo

sure

 

(defun j_iferror (x y / j_caa)
  (setq j_caa (vl-catch-all-apply (car x) (list (eval (cadr x)) (caddr x))))
  (cond
  	((vl-catch-all-error-p j_caa)
   		(vl-catch-all-apply (car y) (list (eval (cadr y))))
  	)
  	(t j_caa)
  )
)

(defun c:JTMgetclosestpointto2 (/ j_acadobj j_actdoc j_ssget j_ssadd j_ctrl item
                                  item_dump j_delete pt1 3dpt1
                               )
  (vl-load-com)
  (setq j_acadobj (vlax-get-acad-object)
        j_actdoc  (vla-get-activedocument j_acadobj)
	j_mspace (vla-get-modelspace j_actdoc)
        j_ssget   (vla-get-selectionsets j_actdoc)
        j_ctrl    0
  )

  (if (null (setq j_ssadd (j_iferror '(vla-add j_ssget "j_ss02") '(vla-delete (vla-item j_ssget "j_ss02")))))
  	(setq j_ssadd (vla-add j_ssget "j_ss02"))
  )

  (vla-selectonscreen j_ssadd)

  (while (setq pt1  (getpoint "\n pick point:"))
    (setq item      (vla-item j_ssadd j_ctrl)
          3dpt1     (vlax-3d-point pt1)
          item_dump (vlax-curve-getclosestpointto item pt1)
          j_addline (vla-addline j_mspace
                               (vlax-3d-point item_dump)
                               (vlax-3d-point pt1)
                    ) ;j_ctrl    (1+ j_ctrl)
    )
  )
  (vl-catch-all-apply 'vla-delete (list (vla-item j_ssget "j_ss02")))
)

 

Message 5 of 5
jtm2020hyo
in reply to: komondormrex

thanks lot

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report