Author Topic: SETVAR tools  (Read 2736 times)

0 Members and 1 Guest are viewing this topic.

Jeremy

  • Guest
SETVAR tools
« on: February 17, 2012, 03:57:31 PM »
Thought I would share some simple tools that I use in dealing with AutoCAD's set variables, especially the boolean ones that take 0 or 1 as values. The first two simply set the value to 0 or 1:

Code: [Select]
(defun setvar0 (setv)(setvar setv 0))
(defun setvar1 (setv)(setvar setv 1))

Now we have a function that toggles the given setvar to its opposite value:

Code: [Select]
(defun setvar-not (setv)(setvar setv (if (zerop (getvar setv)) 1 0)))

And now we make a handy new if-then conditional that works with set variables:

Code: [Select]
(defun setvar-if (setv a b)(eval (if (zerop (getvar setv)) a b)))

So if the setvar is 0 then expression A is evaluated else B is. These might seem trivial but are actually quite handy. Have fun!!

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: SETVAR tools
« Reply #1 on: February 17, 2012, 04:07:42 PM »
Thanks Jeremy.

This one has been around for some time.
Code: [Select]
(defun setvar-toggle (setv)(setvar setv (- 1 (getvar setv))))
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

danallen

  • Guest
Re: SETVAR tools
« Reply #2 on: February 17, 2012, 04:36:07 PM »
I like boolean logic, even though I have to wrap my brain about it
Code - Auto/Visual Lisp: [Select]
  1. ;;;==========================================================
  2. ;;;  Boolian code - taken from www.acadx.com
  3. ;;;
  4. ;;;  (xyz_set_toggle_bit "osmode" 16384)
  5. ;;;
  6. ;;;   That will disable if below 16384 and enable if above 16384.
  7. ;;;   This routine can be used to toggle any system variable that uses bitcodes.
  8. ;;;
  9. ;;;   (xyz_set_toggle_bit "autosnap" 8); toggles Polar Tracking
  10. ;;;   (xyz_set_toggle_bit "autosnap" 16); toggles Object Snap Tracking
  11. ;;;
  12. ;;;   (xyz_set_on_bit  "osmode" 512) ; will turn on nearest (512) snap - not toggle
  13. ;;;   (xyz_set_off_bit "osmode" 1  ) ; will turn off end     (1)   snap - not toggle
  14. ;;;
  15. ;;; 0 None
  16. ;;; 1 Endpoint
  17. ;;; 2 Midpoint
  18. ;;; 4 Center
  19. ;;; 8 Node
  20. ;;; 16 Quadrant
  21. ;;; 32 Intersection
  22. ;;; 64 Insertion
  23. ;;; 128 Perpendicular
  24. ;;; 256 Tangent
  25. ;;; 512 Nearest
  26. ;;; 1024 Quick
  27. ;;; 2048 Apparent Intersection
  28. ;;; 4096 Extension
  29. ;;; 8192 Parallel
  30. ;;;==========================================================
  31. (defun-q xyz_set_toggle_bit (varname bitcode)
  32.   (setvar varname (boole 6 (getvar varname) bitcode))
  33. )
  34. (defun-q xyz_set_on_bit (varname bitcode)
  35.   (setvar varname (boole 7 (getvar varname) bitcode))
  36. )
  37.  
  38. (defun-q xyz_set_off_bit (varname bitcode)
  39.   (setvar varname (boole 2 (getvar varname) bitcode))
  40. )
  41.  

hermanm

  • Guest
Re: SETVAR tools
« Reply #3 on: February 17, 2012, 09:27:14 PM »
Code - Auto/Visual Lisp: [Select]
  1. (defun var-toggle (var) (setvar var (boole 6 1 (getvar var))))
  2.  

Example:

Code - Auto/Visual Lisp: [Select]
  1. (defun C:TMTOGL () (var-toggle "TILEMODE"))
  2.  



Jeremy

  • Guest
Re: SETVAR tools
« Reply #4 on: February 17, 2012, 09:29:59 PM »
In concert with the xyz functions I like to use the following

Code: [Select]
;; Different form of the COND function that is simpler for certain cases.
(defun choose (x lst1 lst2)
  (eval (nth (vl-position (eval x) lst1) lst2))
)

;; Takes a bit code for OSMODE and returns the 3-letter abbreviation associated with it
;; and vice-versa.
;; Example: (osmode->bit "NEA") -> 512
;;          (osmode->bit 512) -> "NEA"
(defun osmode->bit (x / bits strings)
 (setq bits    (list 0 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192)
       strings (list "NON" "END" "MID" "CEN" "NOD" "QUA" "INT" "INS" "PER" "TAN" "NEA" "QUI" "APP" "EXT" "PAR")
 )
 (cond
   ((eq 'INT (type x))
    (choose x bits strings))
   ((eq 'STR (type x))
    (choose x strings bits))
   (t nil)))

Now you can write (xyz_set_on_bit  "OSMODE" (osmode->bit "NEA")) instead of (xyz_set_on_bit  "OSMODE" 512) . It may be longer but you don't have to keep looking up the bit codes for the snap values.

danallen

  • Guest
Re: SETVAR tools
« Reply #5 on: February 20, 2012, 07:25:59 PM »
It may be longer but you don't have to keep looking up the bit codes for the snap values.

Nice idea!