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.
Solved! Go to Solution.
Solved by Hallex. Go to Solution.
@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].
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'~
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.
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.