How to prevent if a system variable does not exist, Activex

How to prevent if a system variable does not exist, Activex

carlos_m_gil_p
Advocate Advocate
1,359 Views
8 Replies
Message 1 of 9

How to prevent if a system variable does not exist, Activex

carlos_m_gil_p
Advocate
Advocate

Hello boys how are you.
With the changes of versions of autocad, they are eliminating system variables.
How do I avoid the error, if a variable does not exist, with activex functions.

I tried as follows, but it didn't work.

(vl-catch-all-error-p 
  (setq v (vlax-variant-value (vla-GetVariable (vla-get-activedocument (vlax-get-acad-object)) "casa"))))

 Thanks.


AutoCAD 2026
Visual Studio Code 1.99.3
AutoCAD AutoLISP Extension 1.6.3
Windows 10 (64 bits)

0 Likes
Accepted solutions (1)
1,360 Views
8 Replies
Replies (8)
Message 2 of 9

_gile
Consultant
Consultant
Accepted solution

Hi,

Try this way:

(vl-catch-all-error-p
  (vl-catch-all-apply
    '(lambda ()
       (setq v (vlax-variant-value
		 (vla-GetVariable (vla-get-activedocument (vlax-get-acad-object)) "casa")
	       )
       )
     )
  )
)

Why not simply use the getvar function?

(vl-catch-all-error-p
  (vl-catch-all-apply
    '(lambda () (setq v (getvar "casa")))
  )
)


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 9

carlos_m_gil_p
Advocate
Advocate

Hi @_gile , thank you for your answer.
I always apply it with getvar.
The point is that I wanted to learn how to do it with vl ...
Thank you.
Greetings.


AutoCAD 2026
Visual Studio Code 1.99.3
AutoCAD AutoLISP Extension 1.6.3
Windows 10 (64 bits)

0 Likes
Message 4 of 9

john.uhden
Mentor
Mentor

Often times the vl way is the long way.

Another thing I was reminded of last week...

The vla-item method errors out if the named item doesn't exist.  You would think it would return nil, but it doesn't.  So if you are looking for the existence of a table entry, use the old tblsearch function instead.

John F. Uhden

0 Likes
Message 5 of 9

Sea-Haven
Mentor
Mentor

Why not

 

(if (setq v (getvar 'casa))
(progn do something now
0 Likes
Message 6 of 9

carlos_m_gil_p
Advocate
Advocate

Hi @john.uhden , how are you?

I understand that the vl are more powerful.

Yes, it is a longer way, but I think it can be solved by creating a general function for each case.

I am starting to use VSCode and with one click I can insert these functions.
I improve my code and my workflow.

All this based on my opinion and in the same way learn more about the vl functions.

Thanks for the information about the vla-item method, I will keep it in mind for future use.

Greetings.

 

Hi @Sea-Haven 

I am not saying that it is wrong or that I do not use them that way, as I mentioned above, it is a matter of learning more about these functions.

Thanks.


AutoCAD 2026
Visual Studio Code 1.99.3
AutoCAD AutoLISP Extension 1.6.3
Windows 10 (64 bits)

0 Likes
Message 7 of 9

_gile
Consultant
Consultant

@john.uhden  a écrit :

The vla-item method errors out if the named item doesn't exist.  You would think it would return nil, but it doesn't.  So if you are looking for the existence of a table entry, use the old tblsearch function instead.


;; gc:GetItem (gile)
;; Returns the vla-object of item if it exists in the collection (or nil)
;;
;; Arguments
;; col : collection (vla-object)
;; name : object name (string) or index (integer)

(defun gc:GetItem (col name / obj)
  (vl-catch-all-apply
    (function (lambda () (setq obj (vla-Item col name))))
  )
  obj
)


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 8 of 9

Sea-Haven
Mentor
Mentor

Have a look at attached

 

 

0 Likes
Message 9 of 9

carlos_m_gil_p
Advocate
Advocate

Hello @Sea-Haven , thank you very much for your information and help.
Greetings.


AutoCAD 2026
Visual Studio Code 1.99.3
AutoCAD AutoLISP Extension 1.6.3
Windows 10 (64 bits)

0 Likes