Visual LISP, AutoLISP and General Customization

Visual LISP, AutoLISP and General Customization

Reply
Valued Mentor
Shneuph
Posts: 356
Registered: ‎11-26-2007
Message 1 of 5 (239 Views)
Accepted Solution

Avoid VLA-ITEM Error in Conditional

239 Views, 4 Replies
02-20-2012 12:09 PM

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)

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'~

*Expert Elite*
Kent1Cooper
Posts: 5,872
Registered: ‎09-13-2004
Message 2 of 5 (229 Views)

Re: Avoid VLA-ITEM Error in Conditional

02-20-2012 01:25 PM 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
*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 3 of 5 (225 Views)

Re: Avoid VLA-ITEM Error in Conditional

02-20-2012 01:48 PM 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
Valued Mentor
Shneuph
Posts: 356
Registered: ‎11-26-2007
Message 4 of 5 (223 Views)

Re: Avoid VLA-ITEM Error in Conditional

02-20-2012 01:55 PM 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)
*Expert Elite*
dgorsman
Posts: 5,698
Registered: ‎10-12-2006
Message 5 of 5 (192 Views)

Re: Avoid VLA-ITEM Error in Conditional

02-21-2012 08:43 AM 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.
Adopt. Adapt. Overcome. Or be overcome.
A good question will be halfway to a good answer.


Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.