Jump to content

Not difficult (if you know how to...) Set multiple variables...


MarcoW

Recommended Posts

Just for the learningprocess, not for the worthy of the routine...

How can I make the repetitive "setvar" more appropriate?

Like (setvar '("cursorsize" 1 "xdwgfadectl" 20 ... etc...

 

I have no clue allthough I remember seeing it somewhere..

Any help is appreciated, again, this is to learn only.

 

Here my code:

 

(defun c:hsh ( / CmdEchoOld)
 (setq CmdEchoOld
 (getvar "cmdecho")
 )
 (setvar "cmdecho" 0)
 (setvar "cursorsize" 100)
 (setvar "xdwgfadectl" 20)
 (setvar "savetime" 3)
 (setvar "dimassoc" 1)
 (setvar "cmdecho" CmdEchoOld)
 (princ)
)

Link to comment
Share on other sites

In SETVAR or SET you cannot use multiple assignments like in SETQ statement. You may use a list of pairs variable - value:

 

(foreach VariableSet '(("cmdecho" . 0) ("cursorsize" . 100) ("xdwgfadectl" . 20) ("savetime" . 3) ("dimassoc" . 1))
(setvar (car VariableSet)
        (cdr VariableSet))
)

 

 

Regards,

Link to comment
Share on other sites

I prefer making a list of dotted pairs

 

 (setq var '(("CMDECHO"   . 0) ("MENUECHO"   . 0)
               ("MENUCTL"   . 0) ("MACROTRACE" . 0)
               ("OSMODE"    . 0) ("SORTENTS"   . 119)
               ("LUPREC"    . 2) ("MODEMACRO" . ".")
               ("BLIPMODE"  . 0) ("EXPERT"     . 5)
               ("SNAPMODE"  . 1) ("PLINEWID"   . 0)
               ("ORTHOMODE" . 1) ("GRIDMODE"   . 0)
               ("ELEVATION" . 0) ("THICKNESS"  . 0)
               ("FILEDIA"   . 0) ("FILLMODE"   . 0)
               ("SPLFRAME"  . 0) ("UNITMODE"   . 0)
               ("TEXTEVAL"  . 0) ("ATTDIA"     . 0)
               ("AFLAGS"    . 0) ("ATTREQ"     . 1)
               ("ATTMODE"   . 1) ("UCSICON"    . 1)
               ("HIGHLIGHT" . 1) ("REGENMODE"  . 1)
               ("COORDS"    . 2) ("DRAGMODE"   . 2)
               ("DIMZIN"    . 1) ("PDMODE"     . 0)
               ("CECOLOR"   . "BYLAYER")
               ("CELTYPE"   . "BYLAYER")))
(foreach v var
  (and (getvar (car v))
       (setq rst (cons (cons (car v) (getvar (car v))) nw_rst))
       (setvar (car v) (cdr v))))

 

The list rst contains the prior settings so that you can easily rest the variables back to their original values. -David

Link to comment
Share on other sites

Similar to others : I had a phone call in the middle of putting it together :)

 


(setq CmdEchoOld (getvar "cmdecho")
     varsList   (list '("cursorsize" . 1)
                  '("xdwgfadectl"  . 20)
                  '("cmdecho" . 0)
                  '("cursorsize" . 100)
                  '("xdwgfadectl" . 20)
                  '("savetime" . 3)
                  '("dimassoc" . 1)
                  (cons "cmdecho" CmdEchoOld)
                 )
)

(foreach item varsList (setvar (car item) (cdr item)))

 

 

or perhaps

 


(setq sysvarlist  nil
      generalVars '(("CMDECHO" 0)           ; save current and Turns off echoing
                    ("expert" 5)            ; save current value and set
                    ("ORTHOMODE")           ; save current value
                    ("SNAPANG")             ; save current value
                    ("UCSICON")             ; save current value
                    ("SNAPMODE")            ; save current value
                    ("OSMODE")              ; save current value
                    ("CLAYER")
                    ("PICKADD" 2)           ; save current and  Turns on PICKADD. Shift-Pick to remove
                    ("PICKAUTO" 1)          ; save current and  Draws a selection window (for either a window or a crossing selection) automatically 
                    ("PICKBOX" 5)           ; save current and  initial is 3. my default is 6
                    ("INSUNITS" 0)          ; save current and  Unspecified (No units)
                    ("SORTENTS" 1)          ; save current and  use selection Order to control
                   )
)
(vars_list '(("cursorsize" . 1) ''("xdwgfadectl" . 20)))

 (foreach item (append vars_list generalVars)
   (setq sysvarlist (cons (list (car item) (getvar (car item)))
                                  sysvarlist
                            )
   )
   (if (cadr item)
     (setvar (car item) (eval (cadr item)))
   )
 )
  ;;---------

  ;; do code mojo

  ;;---------

  ;; clean up
  (foreach item sysvarlist (setvar (car item) (cadr item))) 

Link to comment
Share on other sites

oh!? Kerry you're a genius.

You had:

(if (cadr item)
     (setvar (car item) (eval (cadr item)))
   )

I had:

(if (cadr x)
              (if (not (eq (type (cadr x)) 'LIST))
                (setvar (car x) (cadr x))
                (setvar (car x) (eval (cadr x)))
              )
            )

 

So much nicer. What was i thinking?

 

thx

Link to comment
Share on other sites

Thanks everybody!!

 

That shure is a big help. I will explore the differences and what may suit me best.

Tharwats reply whas exactly what I meant but maybe the other options are more convenient..?

I will try some coding. WHat I want to keep is an easy view.... if you get what I mean.

 

In such case the other options seem better "readable".

 

Thanks again for the good examples!

Link to comment
Share on other sites

*blink*

 

before last comment:

much respect for Kerry | ---x---------------------- | *meow*

 

after last comment:

much respect for Kerry | -------------------------x | *meow*

 

 

 

 

*lol*

Link to comment
Share on other sites

How about evaluation of quoted expressions:

 

(setq StoredList
 (mapcar
   (function
     (lambda ( SystemVariable )
       (list (quote setvar) SystemVariable (getvar SystemVariable))
     )
   )
   (setq SystemVariables
    '( "CMDECHO" "OSMODE" "BLIPMODE" "FILLMODE" )
   )
 )
)

(mapcar (function setvar) SystemVariables '( 0 256 0 1 ))

(mapcar (function eval) StoredList)

Link to comment
Share on other sites

How about evaluation of quoted expressions:

 

Personally I don't like seperated key and value lists .. too error prone and problematic to debug

Link to comment
Share on other sites

My five cents worth all the above are good ideas but I would make what your trying to do 2 defuns (newvars) & (oldvars) you will probably find that you want to do this same thing in lots of your code. Just put the defuns into a master lisp that loads at startup then each new program you only need to (newvars) at end (oldvars)

 

I have a particular suite of 92 lisps and use this method all the time and then there are no mistakes and scratching of head as to why something doesn't work or went on the wrong layer (have a external customisable layer system) a particular area is "units" there are multiple setvars sometimes to be changed.

Link to comment
Share on other sites

Yep, that works fine untill you need to address a couple or 6 variables that you don't usually worry about ... and to handle their restoration at completion/error.

 

I don't see anything wrong or incorrect with the OP's intention.

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...