Jump to content

vl-catch-all-apply (catching errors)


Recommended Posts

Posted

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)
)

Posted

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!

Posted

Thanks Renderman

 

This helps a bunch.

 

Thanks for the heads up on the 'catching errors, 'mitigating and 'handling errors

Posted

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?

Posted

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 :unsure:

Posted

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

Posted

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

Posted
Sorry for all the criticism :unsure:

 

No worries, Lee.

 

Like my signature says - One who is offended by truth, has no place among those who seek wisdom.

Posted
No worries, Lee.

 

Like my signature says - One who is offended by truth, has no place among those who seek wisdom.

 

Nice motto :thumbsup:

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...