abra-CAD-abra Posted September 4, 2019 Share Posted September 4, 2019 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.. Quote Link to comment Share on other sites More sharing options...
dlanorh Posted September 4, 2019 Share Posted September 4, 2019 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. Quote Link to comment Share on other sites More sharing options...
BIGAL Posted September 4, 2019 Share Posted September 4, 2019 Maybe (cond ( (not (tblsearch "ltype" l_typ)) (alert (strcat "Linetype " l_typ " has been skipped as missing")))) Quote Link to comment Share on other sites More sharing options...
Grrr Posted September 4, 2019 Share Posted September 4, 2019 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) Quote Link to comment Share on other sites More sharing options...
abra-CAD-abra Posted September 5, 2019 Author Share Posted September 5, 2019 Guys, Thank you all for your responses. I will study the options presented. Grrr, thank you for your detailed explanations and options Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.