more activex layer testing!

Discussion in 'AutoCAD' started by kemp, May 15, 2004.

  1. kemp

    kemp Guest

    Can someone please tell me what I am doing wrong with this statement. If the
    value is true then it works, but if it is false I get the error: "Automation
    Error. Key not found" and it bombs out instead of returning "False."

    (if
    (vla-item
    (vla-get-layers
    (vla-get-activedocument
    (vlax-get-acad-object)
    )
    )
    "foo")
    (princ "True")
    (princ "False")
    )

    Thanks a bunch!

    kemp
     
    kemp, May 15, 2004
    #1
  2. kemp

    Jeff Mishler Guest

    This is due to the activex method returning an error. In vlisp you must
    'catch' the error, if any, and then act on it if it doesn't error.

    This is how you would revise your code to do this:

    (if
    (not (vl-catch-all-error-p
    (vl-catch-all-apply
    '(lambda ()
    (vla-item
    (vla-get-layers
    (vla-get-activedocument
    (vlax-get-acad-object)
    )
    )
    "foo")
    )
    nil)
    )
    )
    (princ "True")
    (princ "False")
    )

    Good Luck,
    Jeff
     
    Jeff Mishler, May 15, 2004
    #2
  3. kemp

    Doug Broad Guest

    Kemp,
    The item method should only be used if you are
    sure that the key exists. One way to guard against
    errors is to wrap the item call inside a
    vl-catch-all-apply statement.

    If your target is always the activedocument,
    then tblsearch is sufficient and IMO is easier to read
    for your application.

    Code:
    (if (tblsearch "layer" "foo") <then> <else>)
    
    or
    
    (if (tblobjname "layer" "foo") <then> <else>)
    
    One way to do it in activeX:
    
    (vl-catch-all-apply
    '(lambda ()
    (setq lay (vla-item
    (vla-get-layers
    (vla-get-activedocument
    (vlax-get-acad-object)))
    "foo"))))
    (if lay "true" "false")
    
    One problem with vl-catch-all-apply is that
    it can hide logic errors if used indiscriminately.
    
    Jeff's example is also feasible.
    
    
    
     
    Doug Broad, May 15, 2004
    #3
  4. kemp

    Jeff Mishler Guest

    And I was going to add what Doug replied with, regarding the tblsearch, as
    well as when I'm dealing with layers I will just do this:

    (setq oLay (vla-add (vla-get-layers *doc*) "foo"))

    And oLay will always be my 'foo' layer since this doesn't return an error.

    HTH,
    Jeff
     
    Jeff Mishler, May 15, 2004
    #4
  5. kemp

    kemp Guest

    Thanks Doug and Jeff. Your posts are really helpful in my understanding of
    how this all works.

    kemp
     
    kemp, May 17, 2004
    #5
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.