Jump to content

store multiple custom variables


Aftertouch

Recommended Posts

Hello all,

 

Is there a way to store a bunch of custom variables? I know about the 'UserI1' etc. But i got like 20 variables... is it possible to store a whole list or something?

 

Suomething like '("1")("2")("2")("1") ??

i have been searching for a while but cant find anything usefull. :-(

Link to comment
Share on other sites

... Or use the registry.

 

...Sparingly.

 

I would hope that the OP would use a single key to store multiple values if pursuing this route, rather than bloat the registry with 20 separate keys per app...

Link to comment
Share on other sites

Here is another dictionary example, you can make as many variables as you want I would use something like a Var1 Var2 etc.

 

; xdata sample code
; By Alan H
; sample (setq varx "item1")(setq valx 23)(get-or-make-Xrecord)

(defun get-or-create-Dict (/ adict)

 ;;test if "BIGAL" is already present in the main dictionary
 (if (not (setq adict (dictsearch (namedobjdict) "BIGAL")))

   ;;if not present then create a new one and set the main
   ;; dictionary as owner
   (progn

     (setq adict (entmakex '((0 . "DICTIONARY")(100 . "AcDbDictionary"))))

     ;;if succesfully created, add it to the main dictionary
     (if adict (setq adict (dictadd (namedobjdict) "BIGAL" adict)))
   )

   ;;if present then just return its entity name
   (setq adict (cdr (assoc -1 adict)))
 )
)

(defun get-or-make-Xrecord ( / adict anXrec)

 (cond
    ;;first get our dictionary. Notice that "BIGAL" will be
   ;;created here in case it doesn't exist
   ((setq adict (get-or-create-Dict))
     (cond
       ;;if "BIGAL" is now valid then look for "BIGALVARS" Xrecord 
      ((not (setq anXrec (dictsearch adict varx))) 
;the variable BIGALvars is name of xrecord so need to be a variable to add lots
       ;;if "BIGALVARS" was not found then create it
       ;(setq anXrec (entmakex '((0 . "XRECORD")
       (setq anXrec (entmakex (list (cons 0  "XRECORD")
                    (cons 100  "AcDbXrecord")
                    (cons 1 varx)
                    (cons 40 valx)
                              )
                    )
       )
       ;;if creation succeeded then add it to our dictionary
       (if anXrec (setq anXrec (dictadd adict varx anXrec)))
      )
      ;;if it's already present then just return its entity name
      (setq anXrec
       (cdr (assoc -1 (dictsearch adict varx)))
      )
    )
   )
 )
)

; sample (setq varx "item1")(setq valx 0)(getBIGALvars)
(defun getBIGALvars ( / vars varlist)
 ;;retrieve XRecord "varx" from dictionary "BIGAL"
 ;;which in turn calls both functions above
 (setq vars (get-or-make-Xrecord))

 ;;if our Xrecord is found, then get values in group code 
 (cond (vars
        (setq varlist  (entget vars))
        (setq Itemx (cdr (assoc 1 varlist)))
        (setq Itemans (cdr (assoc 40 varlist)))
       )

       ;;otherwise return nil
       (T nil)
 )
(alert (strcat "Var name " itemx "\nItem value " (rtos itemans 2 0)))
)

Edited by BIGAL
Link to comment
Share on other sites

Thanks for all your replies.

I think a dictionary will work best for me. :-)

@BIGAL: i cannot seen to get your code to work?

Link to comment
Share on other sites

...Sparingly.

 

I would hope that the OP would use a single key to store multiple values if pursuing this route, rather than bloat the registry with 20 separate keys per app...

I have a BricsCAD .arg file here with 1725 lines. Adding a mere 20 keys to the registry hardly constitutes bloat.
Link to comment
Share on other sites

Sorted it out needed an example how to use code, I wrote it as part of a bigger program. Re did code to make it a bit more generic next rev would be dictionary name.

 

 

 

(setq itemx "val1")
(setq valx 20) 
(get-or-make-Xrecord)

(setq itemx "val2")
(setq valx 40) 
(get-or-make-Xrecord)

; ok to retrieve a value
(setq varx "val2")
(setq va1 0) ; dummy value required so use 0
(getBIGALvars)
;this returns answer  which is 40

(setq varx "val1")
(setq valx 0)
(getBIGALvars)
; this returns answer 20

 

 ; not tested
;For your example '("1")("2")("2")("1") need a repeat loop of the 3 lines use 
(setq lst (list 1 2 2 1))
(repeat (setq x (length lst))
(setq varx (strcat "Val" (rtos x 2 0))) 
(setq valx (nth (setq x (- x 1)) lst))
(get-or-make-Xrecord)
)

; to retrieve do repeat again or single 
(setq varx "val1")
(setq valx 0)
(getBIGALvars)  returns value for variable val1

Edited by BIGAL
Link to comment
Share on other sites

Thanks BIGAL,

 

Ill see if i can get this to work.

I hope i got some time this week to work with it.

Thanks for the code!

Link to comment
Share on other sites

I am no expert when it comes to dictionaries or Xdata / Xrecords, but aren't they document dependent? If I wanted to store variables I would have previously used setcfg and getcfg, but being that those functions are going to the wayside even Autodesk recommends using the registry.

Link to comment
Share on other sites

Hippe013 your correct and as Lee answered in a very early post write an external file is simplest way, if you want a variable to be same across all drawings. So the choice is with Aftertouch.

 

Having been involved in something like this with house walls the widths change depending on materials so I am half way through go home and continue next day with correct wall sizes set, open another dwg again correct wall sizes set for that wall material type in that dwg.

Link to comment
Share on other sites

In my case, i want all my 'custom' variables to be drawing dependent.

So the dictionary is a perfect solution for me. :-)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...