Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Avoid VLA-ITEM Error in Conditional

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
Shneuph
508 Views, 4 Replies

Avoid VLA-ITEM Error in Conditional

I just realized I should just be using the tblsearch function but just for curiosity...

 

I'm trying to see if a text style exists in a drawing using Vlisp.  Thought it would be pretty easy like:

 

(setq tlv-text_Styles (vla-get-textstyles (vla-get-activedocument (vlax-get-acad-object))))
  (if (not (vla-item
	     tlv-text_Styles
	     "TL-Standard")
	   );not
      ...

 

but if the style name doens't exist it gives an error instead of just nil.  Anyone know a trick to do this sort of conditional statement w/o causing errors?

 

Thanks.  For now I'll use tblsearch.

---sig---------------------------------------
'(83 104 110 101 117 112 104 64 71 109 97 105 108 46 99 111 109)
4 REPLIES 4
Message 2 of 5
Kent1Cooper
in reply to: Shneuph


@Shneuph wrote:

.... 

I'm trying to see if a text style exists in a drawing using Vlisp.  Thought it would be pretty easy like:

 

(setq tlv-text_Styles (vla-get-textstyles (vla-get-activedocument (vlax-get-acad-object))))
  (if (not (vla-item
	     tlv-text_Styles
	     "TL-Standard")
	   );not
      ...

 

but if the style name doens't exist it gives an error instead of just nil.  Anyone know a trick to do this sort of conditional statement w/o causing errors?

....


I suspect it's just the way the (vla-item) function is built.  That's where the error occurs:

 

Command: (vla-item tlv-text_Styles "JONES")
; error: Automation Error. Key not found

 

Some other functions are similarly picky, and others are not, though I don't know whether there's any identifiable reason for the distinction.  For example, there's that kind of difference between selection sets and lists.  If you ask for an entity name out of a selection set that doesn't exist, you get an error:

 

Command: (ssname NoSuchSelectionSet 0)
; error: bad argument type: lselsetp nil

 

whereas you can ask for an item out of a list that doesn't exist, and whether the item or the list doesn't exist, or even if both don't exist, you just get nil, but not an error:

 

Command: (member NoSuchItem NoSuchList)

nil

 

or

 

Command: (car NoSuchList)
nil
 

That's why if you want to build a selection set by adding things to one with (ssadd), you must start with an empty set first, whereas if you want to build a list, you can use (cons) or (append) to add something to a non-existent list variable name, as a way of starting the list.

 
You may need to use use some variety of vl-catch-all-... approach, but I'm not up on those [search the Discussion Group and you'll find a bunch of threads about them].

Kent Cooper, AIA
Message 3 of 5
Hallex
in reply to: Shneuph

I would use something similar on this snippet:

{code}

(setq tlv-text_Styles (vla-get-textstyles
   (vla-get-activedocument
     (vlax-get-acad-object))))
(if (not (vl-catch-all-error-p
    (vl-catch-all-apply 'vla-item (list tlv-text_Styles "TL-Standard")
      )
    )
  )
  (alert "Style \"TL-Standard\" exist")
  (alert "Style \"TL-Standard\" does not exist")
  )
)

{code}

 

See if this helps

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 4 of 5
Shneuph
in reply to: Hallex

Thank you both for the replies.  I'll try your snippet when I have a chance, Hallex.  It looks simpler than I was expecting a solution to be.

---sig---------------------------------------
'(83 104 110 101 117 112 104 64 71 109 97 105 108 46 99 111 109)
Message 5 of 5
dgorsman
in reply to: Shneuph

For VLISP, something returned (even if it is nil) is considered a valid value e.g. found in the collection.  To handle this different way of thinking, it throws an error instead of just returning nil.  In order to deal with that you will probably want to wrap the call in a (vl-catch-all-apply...) function call, which will "catch" the thrown error.  This has an additional benefit, in that (vl-catch-all-error-message...) can tell you exactly *what* the problem was - in this case, whats being looked for is not a member of the collection, in which case you could follow on with adding it.  If the error was more serious (e.g. passing an index value of -1 because of a non-localized variable reset your loop counter) then you could handle that differently.

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost