Jump to content

vl-catch-all-apply | vl-catch-all-error-p


abra-CAD-abra

Recommended Posts

All,

 

If I am iterating through the layer collection using vlax-for, how do I report an error only once?

 

For example, if using the vla-put-linetype function with vl-catch-all-apply and vl-catch-all-error-p. If a particular linetype is not found for any number of layers, how would I report this once?

 

Example below would return the variable em for every catch:

(vlax-for l (vla-get-layers
	      (vla-get-activedocument (vlax-get-acad-object))
	    )
  (setq	em (vl-catch-all-apply
	     'vla-put-linetype
	     (list l "My Linetype")
	   )
  )
  (if (vl-catch-all-error-p em)
    (princ (strcat "\nLinetype " "My Linetype" " Not Found. "))
  )
)

Thanks in advance..

Link to comment
Share on other sites

I'm not a big fan of (vl-catch-all-apply) and (vl-catch-all-errors-p). IMHO it is the lazy way of programming. At the start of your lisp check if the linetype is present in the drawing.

 

Since "continuous" is always present in a new drawing

 

(tblsearch "ltype" "continuous")

should return the linetype definition.

 

Thus, if you send a list of required linetypes (lt_lst) to the below

 

(foreach l_typ lt_lst
  (cond ( (not (tblsearch "ltype" l_typ)) (setq missing (cons l_type missing))))
);end foreach

 

it should return a list (missing) of the line types not in the drawing, or you could alter the routine to load the linetype.

 

The same should apply to layers, text, *leader... styles. I they are not present create or load them.

Link to comment
Share on other sites

3 hours ago, abra-CAD-abra said:

If I am iterating through the layer collection using vlax-for, how do I report an error only once?

 

You could break the whole iteration process upon the first failed evaluation (so the behaviour would be like vl-some for collections) -

(defun test ( / lt c )
  (setq lt "My Linetype")
  (if 
    (vl-catch-all-error-p
      (vl-catch-all-apply
        (function 
          (lambda nil 
            (vlax-for l (vla-get-Layers (vla-get-ActiveDocument (vlax-get-acad-object)))
              (setq c (vla-get-Name l))
              (vla-put-LineType l lt)
            )
          )
        )
      )
    )
    (print (strcat "Unable to put \"" lt "\" on layer \"" c "\""))
  )
  (princ)
)
(test)

 

3 hours ago, abra-CAD-abra said:

 If a particular linetype is not found for any number of layers, how would I report this once?

 

If you want to check if the linetype property of a layer object matches "My Linetype", then use vla-get-Linetype instead of vla-put-Linetype,

And again you can print the first layer that doesn't match the criteria and break the iteration -

(defun test ( / err lt c )
  (setq lt "Continuous")
  (and 
    (vl-catch-all-error-p
      (setq err
        (vl-catch-all-apply
          (function 
            (lambda nil 
              (vlax-for l (vla-get-Layers (vla-get-ActiveDocument (vlax-get-acad-object)))
                (and 
                  (/= (vla-get-LineType l) lt)
                  (setq c (vla-get-Name l))
                  (exit)
                )
              )
            )
          )
        )
      )
    )
    (= (vl-catch-all-error-message err) "quit / exit abort")
    (print (strcat "Layer \"" c "\" doens't contain linetype \"" lt "\""))
  )
  (princ)
)
(test)

Or if you want to print all the layers that don't match the criteria, then you'll have to iterate through all of them - meaning that you won't need the vl-catch-* functions -

(defun test ( / lt c )
  (setq lt "Continuous")
  
  (vlax-for l (vla-get-Layers (vla-get-ActiveDocument (vlax-get-acad-object)))
    (and 
      (/= (vla-get-LineType l) lt)
      (setq c (cons (vla-get-Name l) c))
    )
  )
  
  (if c 
    (print 
      (strcat
        "Layers \"" 
        (substr 
          (apply 'strcat 
            (mapcar '(lambda (x) (strcat "," x )) c)
          )
          2
        )
        "\" do not contain linetype \"" lt "\""
      )
    )
  )
  (princ)
)
(test)

 

Link to comment
Share on other sites

Guys,

 

Thank you all for your responses. I will study the options presented.

 

Grrr, thank you for your detailed explanations and options 👍

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...