Solutions for dictionaries

Solutions for dictionaries

carlos_m_gil_p
Advocate Advocate
553 Views
3 Replies
Message 1 of 4

Solutions for dictionaries

carlos_m_gil_p
Advocate
Advocate

Hello guys how are you, today I want to ask some questions about dictionaries.

 

1.- Is there a function where I can list the names of all the dictionaries that are in the drawing?

 

2.- How can I eliminate one or all the dictionaries that are in the drawing?

 

3.- How can I prevent any error of a variable in the dictionary?

 

I explain what happened to me.

 

I use this function to create the dictionaries.
And everything works fine.

 

(setq doc (vla-get-activedocument (vlax-get-acad-object)))

(defun dicc (/) 
  (if (vl-catch-all-error-p (vl-catch-all-apply 'vla-item (list (vla-get-dictionaries doc) "CONO-ALTURA"))) 
    (progn (vla-add (vla-get-dictionaries doc) "CONO-ALTURA") 
           (vlax-ldata-put "CONO-ALTURA" "cgp-height" 0.5)
           (vlax-ldata-put "CONO-ALTURA" "cant-line" 10))))

(dicc)

 

But for some reason, the program gave an error and the variable "cant-line" was stored as nil.

And when I tried to use (itoa (vlax-ldata-get "CONO-ALTURA" "cant-line")) it gave me an error.
And I couldn't use the program on that file anymore, until I checked the code and realized it was because it was set to nil.

But I don't know how I can modify the function.

Because it checks if the dictionary exists, if it doesn't it creates it, but if it exists, it doesn't do anything and the "cant-line" variable is still nil.
But I don't want to lose the last value saved in the dictionary, only if it's an error, re-set the initial value of the variable that is wrong.

 

Beforehand thank you very much.
And I hope you can help me.
Greetings.


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

0 Likes
Accepted solutions (1)
554 Views
3 Replies
Replies (3)
Message 2 of 4

john.uhden
Mentor
Mentor

@carlos_m_gil_p 

If I recall correctly, the problem is with vla-item.

I think that instead of returning nil if an item doesn't exist, it errors out or something worthless.

Better to create a list of the dictionaries or contents and use vl-position or member.

John F. Uhden

Message 3 of 4

phanaem
Collaborator
Collaborator
Accepted solution

Hi

 

This will print all the sub-dictionaries in the main dictionary

(foreach x
  (entget (namedobjdict))
  (if
    (= (car x) 3)
    (print (cdr x))
  )
)

To delete a dictionary, use (dictremove (namedobjdict) "CONO-ALTURA"). I wouldn't recommend to delete those starting with ACAD or AEC.

With vlax-ldata functions, you don't need to check the existence of a dictionary.

_$ (vlax-ldata-get "Test" "rec")
nil
_$ (vlax-ldata-put "Test" "rec" 1.0)
1.0
_$ (vlax-ldata-get "Test" "rec")
1.0
_$ 

The data you store in a dictionary is not stored in any variable. I'd like to be wrong, but I don't know any way to define a custom variable and call it anytime, similar to getvar function.

For your situation, try something like this

(defun get_custom_var (dict name default)
  (cond
    ((vlax-ldata-get dict name))
    ((vlax-ldata-put dict name default))
  )
)

(setq cgp-height (get_custom_var "CONO-ALTURA" "cgp-height" 0.5))
(setq cant-line  (get_custom_var "CONO-ALTURA" "cant-line" 10))

Later, you can alter the variables then store them back into the dictionary

(setq cgp-height 12.0)
(setq cant-line  100)

(vlax-ldata-put "CONO-ALTURA" "cgp-height" cgp-height)
(vlax-ldata-put "CONO-ALTURA" "cant-line" cant-line)

 

Next time you call get_custom_var function, the new values are returned

(get_custom_var "CONO-ALTURA" "cgp-height" 0.5) ---> 12.0
(get_custom_var "CONO-ALTURA" "cant-line" 10) ---> 100

 

You see, vlax-ldata-put doesn't create "cgp-height" or "cant-line" variables (which are just the records' names), but you have to specifically set them to the dictionary values.

 

Similar, (getvar "orthomode") or (setvar "orthomode" 1) doesn't create "orthomode" variable. To use the sysvar value, you have to (setq orthomode (getvar "orthomode"))

 

Message 4 of 4

carlos_m_gil_p
Advocate
Advocate

Hello @phanaem how are you.
Thanks for your help, everything worked for me.
Thank you.


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

0 Likes