cadman6735 Posted January 4, 2011 Posted January 4, 2011 I have an error message (Automation Error. Key not found) A bit of research tells me somthing does not exisit that my macro is looking for. I found some sample code using "vl-catch-all-apply" but lost in how to use it in my code. The examples I found seem a bit simple or complex in form and I can't see how to apply it to my code. Can I ask for a little guidence on using vl-catch-all-apply Thanks (defun C:CleanPlot () (vl-load-com) (setq acadObject (vlax-get-acad-object) acadActiveDocument (vla-get-ActiveDocument acadObject) acadActiveLayout (vla-get-ActiveLayout acadActiveDocument) acadLayers (vla-get-Layers acadActiveDocument) ) (vlax-for Layer acadLayers (if (wcmatch (vla-get-PlotStyleName Layer) "Normal,Style_1") (vla-put-PlotStyleName Layer "As Drawn") ) ) (princ) ) Quote
BlackBox Posted January 4, 2011 Posted January 4, 2011 Give this a try: (defun C:CleanPlot (/ layerTable) (vl-load-com) (vla-startundomark (cond (*activeDoc*) ((setq *activeDoc* (vla-get-activedocument (vlax-get-acad-object)))))) (if (setq layerTable (vla-get-layers *activeDoc*)) (vlax-for layer layerTable (if (vl-position (vla-get-PlotStyleName layer) '("Normal" "Style_1")) (vl-catch-all-apply '(lambda (x) (prompt (strcat "\n <!> layer Fixed <!> " (vla-get-name layer))) (vla-put-PlotStyleName x "As Drawn")) (list layer)) (prompt (strcat "\n >> layer Skipped >> " (vla-get-name layer)))))) (vla-endundomark *activeDoc*) (princ)) My two cents... 'catching errors' is not the same as 'mitigating' or 'handling errors'. For example: Preventing this from being displayed at the command line, does not accomplish the task. _$ ; error: Automation Error. The drawing is in color dependent plot style mode Admittedly, the error I encountered above may be due to my office using different standards. Hope this helps! Quote
cadman6735 Posted January 4, 2011 Author Posted January 4, 2011 Thanks Renderman This helps a bunch. Thanks for the heads up on the 'catching errors, 'mitigating and 'handling errors Quote
cadman6735 Posted January 4, 2011 Author Posted January 4, 2011 Does (vl-catch-all-apply) catch errors or error Objects, if an error does not exist (vl-catch-all-apply) will return a nil or in this case layer Fixed or layer Skipped I ran the macro, and I get a list of every layer name layer Fixed but all my layers remain PlotStyleName Style_1 I would of thought I would get a list of every layer name layer Skipped since my PlotStyleName is still Style_1 If the macro would have found an error, would I have gotten an error message with the error object? Quote
Lee Mac Posted January 4, 2011 Posted January 4, 2011 I think: (defun c:test ( / err ) (vl-load-com) (vlax-for layer (vla-get-Layers (vla-get-ActiveDocument (vlax-get-acad-object)) ) (if (member (strcase (vla-get-PlotStylename layer) t) '("normal" "style_1")) (if (vl-catch-all-error-p (setq err (vl-catch-all-apply 'vla-put-PlotStyleName (list layer "As_Drawn")) ) ) (princ (strcat "\n** Error: " (vl-catch-all-error-message err) " **")) (princ (strcat "\n--> Layer: " (vla-get-name layer) " Successful")) ) ) ) (princ) ) Renderman, Regarding your code: (if (setq layerTable (vla-get-layers *activeDoc*)) The Layer table will always exist (and never be empty) so I think this IF statement is irrelevant. (vl-catch-all-apply '(lambda (x) (prompt (strcat "\n <!> layer Fixed <!> " (vla-get-name layer))) (vla-put-PlotStyleName x "As Drawn")) (list layer)) In the above code there is no test for an error, and the "Layer Fixed" message will always print. There is no need for a lambda function in this case - the function vla-put-plotstylename can be applied directly to an argument list. Better to test for the error object, and, if present inform the user. My two cents... 'catching errors' is not the same as 'mitigating' or 'handling errors'. I do however, agree with this - in most cases I view the vl-catch-all-apply function as a 'lazy-man's' approach, as one could always just wrap the whole program inside it and have no error trapping whatsoever... that said, there are times and places for these things: for example, code in which an error-causing condition may not be able to be determined directly by the developer may warrant its use. Sorry for all the criticism Quote
cadman6735 Posted January 4, 2011 Author Posted January 4, 2011 Lee Your code works great, but now my question revolves around the error message I am getting. Everything looks fine but when I run my code I get an Automaiton Error. Key not found. but the funny thing is if I manualy change one layer to "As Drawn" and then run my code it works. I would understand if "As Drawn" was not part of my Active plot style table but it is. Any ideas? Thanks Quote
Lee Mac Posted January 5, 2011 Posted January 5, 2011 Notice that my code uses "As_Drawn", not "As Drawn" Quote
cadman6735 Posted January 5, 2011 Author Posted January 5, 2011 Notice that my code uses "As_Drawn", not "As Drawn" yes, I caught that, I guess I got a grimmlin because it all looks as if it should work. The only thing different about this file I am working on is it is a TurboCAD translation to ACAD and I converted pstyles from ctb to stb, and for some reason it is acting qwerky. In anycase this is a great opertunity for me to learn error trapping, Thanks Quote
BlackBox Posted January 5, 2011 Posted January 5, 2011 Sorry for all the criticism No worries, Lee. Like my signature says - One who is offended by truth, has no place among those who seek wisdom. Quote
Lee Mac Posted January 5, 2011 Posted January 5, 2011 No worries, Lee. Like my signature says - One who is offended by truth, has no place among those who seek wisdom. Nice motto Quote
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.