lisp code to do what button "OSNAP" does

lisp code to do what button "OSNAP" does

GeryKnee
Advocate Advocate
862 Views
7 Replies
Message 1 of 8

lisp code to do what button "OSNAP" does

GeryKnee
Advocate
Advocate

 

The "osnap" function does not what button "osnap" pressing does.

Pressing the button , there's a translocation between ON OFF mode.

 Trying to do it using the "osnap" commad, the scenario changes.

How two functions

SetOSNAPOn and

SetOSNAPOff , sould do what th button does??

0 Likes
Accepted solutions (1)
863 Views
7 Replies
Replies (7)
Message 2 of 8

Sea-Haven
Mentor
Mentor

Its press F3 there now. 

 

You can make defuns that have preset osnap settings (defun c:47 ()(setvar 'osmode 47)) this will set osnaps to what I like.

 

A c:0 would be osmode 0 no osnaps. In a lot of commands you can change osnap transparently by typing '0 will turn off osnap note the ' single quote indicating use a transparent command.

 

You can set up a defun that toggles if that is really what you want. Change the 47 to your desired osmode.

 

(defun c:0 ()
  (cond 
  ((= (getvar 'osmode) 0)(setvar 'osmode 47))
  ((> (getvar 'osmode) 0)(setvar 'osmode 0))
  )
)

 

Message 3 of 8

GeryKnee
Advocate
Advocate

Hello Sea.heaven.

What I really want is, the following:

--------------------

Think about a user working on a drawing.

This momement wants not OSNAP ON but OFF.

Looks at the current state and sees that the OSNAP button is pressed (is ON).

Independently of what is the current state (maybe snap to line end is ON or else) wants the OFF state.

The useser presses the button "OSNAP" and autocad answers <osnap off> and now, no any osnap on ex.

The above is a code :::

(defun c:SetOsnapOff()

..........................

)

I what the button did

-----------------------------------

after he worked with multiple actions, he needs to set it ON again (to restore the previous state)

He presses the "OSNAP" button and the previous state restores.

Pressing the button autocad says <osnap on>

I suppose the current state was stored somewhere in someone system variable and the pre existant senario restores.

So a next defun is necessary :

(defun c:SetOsnapOn()

..........................

)

not a set of the modes to be one , but to restore the previous wich was on (before pressing button and set it off)

what the button does.

In your code

(defun c:0 ()
(cond
((= (getvar 'osmode) 0)(setvar 'osmode 47))
((> (getvar 'osmode) 0)(setvar 'osmode 0))
)
)

, everything happens during one defun.

That's the problem.

If autocad can do it two defuns can do it.

Thank you,

Gery.

 

0 Likes
Message 4 of 8

CodeDing
Advisor
Advisor
Accepted solution

@GeryKnee ,

 

These functions might interest you. The number 16384 is a flag. You can see it here: https://knowledge.autodesk.com/support/autocad/learn-explore/caas/CloudHelp/cloudhelp/2016/ENU/AutoC...

(defun ToggleOSNAP ( / osm)
  (setq osm (getvar 'OSMODE))
  (cond
    ((or (= 16384 osm) (zerop osm)) (initdia) (command "_.OSNAP") (getvar 'OSMODE))
    ((> osm 16384) (prompt "<Osnap on>") (setvar 'OSMODE (- osm 16384)))
    (t (prompt "<Osnap off>") (setvar 'OSMODE (logior 16384 osm)))
  );cond
);defun

(defun SetOSNAPOn ( / osm)
  (setq osm (getvar 'OSMODE))
  (cond
    ((or (= 16384 osm) (zerop osm)) (initdia) (command "_.OSNAP") (getvar 'OSMODE))
    ((> osm 16384) (prompt "<Osnap on>") (setvar 'OSMODE (- osm 16384)))
  );cond
);defun

(defun SetOSNAPOff ( / osm)
  (setq osm (getvar 'OSMODE))
  (if (< osm 16384)
    (progn (prompt "<Osnap off>") (setvar 'OSMODE (logior 16384 osm)))
  );if
);defun

Best,

~DD

Message 5 of 8

GeryKnee
Advocate
Advocate

Yes,

That's exactly what I want CodeDing.

Thanks and for the lesson ::: (defun c:Mydefan ....

You have humor.

Regards,

Gery.

 

0 Likes
Message 6 of 8

Kent1Cooper
Consultant
Consultant

@CodeDing wrote:

..... The number 16384 is a flag. ....


You can do exactly what the F3 button does much more succinctly:

 

(setvar 'osmode (boole 6 (getvar 'osmode) 16384))

 

That adds the 16384 bit if it is not present [to disable running Osnap but without changing your mode combination setting], and takes it out if it is present [enable Osnap putting your mode combination back in effect].

Kent Cooper, AIA
Message 7 of 8

Sea-Haven
Mentor
Mentor

Like Kent, I said very early in posts use F3 ! Also transparent use '0 can be used in a lot of commands as previously posted.

 

It may be worthwhile checking what all the F function keys do F8 is another very handy.

0 Likes
Message 8 of 8

Kent1Cooper
Consultant
Consultant

@Kent1Cooper wrote:

... exactly what the F3 button does ....


Actually, there is one difference.  If no Osnap modes are set, whether running Osnap is enabled [OSMODE = 0] or disabled [OSMODE = 16384], pressing F3 calls up the Osnap dialog box for the User to set the desired running Osnap mode(s).  @CodeDing covered that possibility, but my earlier suggestion only swaps 0 and 16384, which effectively "toggles" between two kinds of no-Osnap.  Combining a different way of checking for that with the (boole 6) way of toggling when there are modes set:

(if (= (rem (getvar 'osmode) 16384) 0)

  (progn (initdia) (command "_.osnap")); then -- bring up dialog box

  (setvar 'osmode (boole 6 (getvar 'osmode) 16384)); else -- toggle

); if

Kent Cooper, AIA
0 Likes