Author Topic: To check one of osmode is set to on  (Read 7618 times)

0 Members and 1 Guest are viewing this topic.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: To check one of osmode is set to on
« Reply #15 on: June 10, 2013, 05:19:35 AM »
Anyhow, here's a full to-and-from conversion based on the challenge in that other thread:
Code - Auto/Visual Lisp: [Select]
  1. (defun OSMode->SnapList  (osmode / n)
  2.   (setq n -1)
  3.   (if (< 0 osmode 16383)
  4.     (vl-remove-if (function (lambda (snap) (zerop (logand osmode (expt 2 (setq n (1+ n)))))))
  5.                   '("END" "MID" "CEN" "NOD" "QUA" "INT" "INS" "PER" "TAN" "NEA" "APP" "EXT" "PAR"))))
  6.  
  7. (defun get-SnapList () (OSMode->SnapList (getvar 'OSMode)))
  8.  
  9. (defun set-SnapList  (Snaps / AllSnaps)
  10.   (setq AllSnaps '("END" "MID" "CEN" "NOD" "QUA" "INT" "INS" "PER" "TAN" "NEA" "APP" "EXT" "PAR"))
  11.   (setvar 'OSMode (apply 'logior
  12.                          (mapcar (function (lambda (Snap) (expt 2 (vl-position (strcase (substr Snap 1 3)) AllSnaps))))
  13.                                  Snaps))))

And my version to list which bits are turned on:
Code - Auto/Visual Lisp: [Select]
  1. (defun list-bits (num / pow val res)
  2.   (setq pow 31)
  3.   (while (>= (setq pow (1- pow)) 0)
  4.     (if (> (logand num (setq val (expt 2 pow))) 0)
  5.       (setq res (cons (cons pow val) res))))
  6.   res)
Gives a list stating the 0 based indexes of each bit set followed by its actual value. E.g.
Code: [Select]
_$ (list-bits 123456)
((6 . 64) (9 . 512) (13 . 8192) (14 . 16384) (15 . 32768) (16 . 65536))
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Coder

  • Swamp Rat
  • Posts: 827
Re: To check one of osmode is set to on
« Reply #16 on: June 10, 2013, 06:30:16 AM »
See this thread also:

http://www.theswamp.org/index.php?topic=12786

In the same vein, this digression may also be of interest:

http://www.cadtutor.net/forum/showthread.php?61237-I-want-just-Blocks-and-not-Xrefs&p=416106&viewfull=1#post416106

Thank you for time and for these nice links  :-)

And thank you all guys for your relies , now I have lots of examples to learn from .

Many thanks guys