Toggle "Nearest" OSNAP Option

Toggle "Nearest" OSNAP Option

Temssi.d
Enthusiast Enthusiast
989 Views
9 Replies
Message 1 of 10

Toggle "Nearest" OSNAP Option

Temssi.d
Enthusiast
Enthusiast

Hi, I need a script that toggles the nearest option in the OSNAP settings using the shortcut "nn."

 

Thank you in advance for your help!

 

 

0 Likes
Accepted solutions (3)
990 Views
9 Replies
Replies (9)
Message 2 of 10

komondormrex
Mentor
Mentor

hey,

(defun c:nn ()
	(setvar 'osmode 512)
)
0 Likes
Message 3 of 10

Temssi.d
Enthusiast
Enthusiast

Almost there;

my explanation might not have been clear. What I'm looking for is that, upon clicking, the entire configuration remains unchanged, except for toggling the "nearest" option – turning it on if it's off and vice versa.

 

 

0 Likes
Message 4 of 10

hmsilva
Mentor
Mentor
Accepted solution

Try

(defun c:nn nil
  (if (= 512 (logand 512 (getvar 'OSMODE)))
    (setvar 'OSMODE (- (getvar 'OSMODE) 512))
    (setvar 'OSMODE (+ (getvar 'OSMODE) 512))
  )
  (princ)
)

 

Hope this helps,
Henrique

EESignature

Message 5 of 10

komondormrex
Mentor
Mentor
Accepted solution

one more

 

 

(defun c:nn (/ _osmode)
	(if (zerop (logand (lsh (setq _osmode (getvar 'osmode)) -9) 1))
		(setvar 'osmode (logior _osmode 512))
		(setvar 'osmode (logand _osmode 261631))
	)
)

 

 

Message 6 of 10

_gile
Consultant
Consultant
Accepted solution

Hi,

You can do a bitwise 'XOR'.

(defun c:nn ()
  (setvar 'osmode (boole 6 512 (getvar 'osmode)))
  (princ)
)

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 7 of 10

ВeekeeCZ
Consultant
Consultant

Using this sort of toggle for years... and ended up with this macro assigned to the Fx key. A macro, because it simply behaves better than lisp.

 

'_.osmode $M=$(+,$(getvar,osmode),$(if,$(and,$(getvar,osmode),512),$(if,$(and,$(getvar,osmode),16384),-16384,-512),$(if,$(and,$(getvar,osmode),16384),-15872,512)))

0 Likes
Message 8 of 10

Temssi.d
Enthusiast
Enthusiast
huge thanks!
Message 9 of 10

john.kaulB9QW2
Advocate
Advocate

Some of those code offerings are a bit confusing so, in addition (and to get a chance to play around a bit too) I thought I'd offer up some other solutions that do not bother checking if this/that is on/off (you can pretty much just rely on AND, OR, or XOR to do that for you:

 

To make sure snaps are on, you can just do a logand on the 14th bit.
(setvar 'OSMODE (logand (getvar 'OSMODE) -16385))
; -16385 is the 14th bit cleared (aka: (~ 16384))

 

To toggle snaps on/off, you can just do an XOR on the 14th bit.
(setvar 'OSMODE (boole 6 (getvar 'OSMODE) 16384))

 

To turn on the osnaps and turn off the nearest snap, you can clear both bits with logand.
(setvar 'OSMODE (logand (getvar 'OSMODE) (+ (1+ (~ 16384)) (~ 512))))
; (1+ (~ 16384)) is -16484

 

To turn off the osnaps and turn on the nearest snap, you can clear one bit and flip the other with OR.
(setvar 'OSMODE (logior (getvar 'OSMODE) (+ 16384 512)))

 

To just toggle both bits and hope for the best (use XOR).
(setvar 'OSMODE (boole 6 (getvar 'OSMODE) (+ 16384 512)))

 

Finaly, to turn snaps on and toggle the nearest bit on/off (use AND & XOR).
(setvar 'OSMODE (logand (boole 6 512 (getvar 'OSMODE)) -16385))

another swamper
0 Likes
Message 10 of 10

Sea-Haven
Mentor
Mentor

In some commands you can do transparent osnap resets, Line is an example if you have made defuns say for various osnap settings these just (setvar 'osmode 47) in this case I made a (defun c:47 ()(setvar 'osmode 47))

 

In say "LINE" then type '47 the apostrophe is call transparent command '00 would be say (setvar 'osmode 0) for all off, same as F3.

0 Likes