Simple lisp - can this work better?

Simple lisp - can this work better?

Anonymous
Not applicable
1,260 Views
6 Replies
Message 1 of 7

Simple lisp - can this work better?

Anonymous
Not applicable

I have an autolisp macro for toggling OSMODE and AUTOSNAP programmed into a button on my mouse:

 

(setq om (getvar "osmode"))(setq as (getvar "autosnap"))(if (= om 0)(setvar "osmode" 4101)(setvar "osmode" 0))(if (= as 39)(setvar "autosnap" 63)(setvar "autosnap" 39))(princ)

 

It works a lot of the time but it's not perfect - if I happen to have autosnap or osmode switched off with a status bar button, the macro toggling doesn't work. Also, I'd like to be able to toggle osmode off in the middle of a command - e.g., start a line with osmode 4101 on to pick an endpoint, then toggle it off to 0, but the command stops when I do this.

 

Is there anything I can do with this macro to get it working a little more smoothly?

0 Likes
Accepted solutions (1)
1,261 Views
6 Replies
Replies (6)
Message 2 of 7

john.uhden
Mentor
Mentor
I forget how to program mouse buttons, but isn't there some suggested syntax like ^C^C(myfunction)?

If so, the ^C^C will probably cancel whatever command is running.

John F. Uhden

0 Likes
Message 3 of 7

dbroad
Mentor
Mentor

You might consider using the status bar techniques rather than using 0 and fixed settings for toggles.  Negating the osnap and autosnap modes preserves the settings and toggles them.

 

(setvar "osmode" (- (getvar "osmode"))) or something like that.  You could always keep the logic checking for 0 and use it to lauch the osnap or autosnap settings dialog.

Architect, Registered NC, VA, SC, & GA.
0 Likes
Message 4 of 7

Kent1Cooper
Consultant
Consultant
Accepted solution

@Anonymous wrote:

.... if I happen to have autosnap or osmode switched off with a status bar button, the macro toggling doesn't work. Also, I'd like to be able to toggle osmode off in the middle of a command - e.g., start a line with osmode 4101 on to pick an endpoint, then toggle it off to 0, but the command stops when I do this.  ....


In the case of OSMODE, when you have that 4101 combination set [Endpoint, Center, Extension], if you pick the status-bar icon or hit F3 to turn Osnapping off, it doesn't set OSMODE to 0, but rather adds 16384 to its value, which retains the mode combination you have [which setting it to 0 would not do], but suppresses its application.  That's the only way to go if you want to have the same mode combination going when you turn it back on [in which case it subtracts 16384 from it].  So if you've done it that way, OSMODE will be 20485, not 0, and your macro will therefore set it to 0.  The same would also be true if you have by any other means changed the combination of modes, such as to Endpoint only [OSMODE = 1].  And if you turn it off with your mouse-button macro by changing it to 0, then if you try to turn it back on with F3 or the status-bar icon, instead of with the mouse-button macro, it won't come back to 4101, but will call up the dialog box and request some mode(s) to use.

 

What you want to do is the same thing the icon and F3 do -- add 16384 if it's less than that, or subtract it if it's more.  That will maintain whatever mode combination you have.  You don't even need a variable to check against, but can do it directly.  [It could be that the nil return from the (if) test in some circumstances in your macro is what's stopping commands.]

 

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

 

For some explanation [which some people find helpful, but some do not] of what (boole 6) is doing, look here, and find where it starts "The specific operator 6...."

 

It can also be done in a more long-winded but perhaps more understandable way [this is just one of several ways you could go about it]:

 

(setvar 'osmode (+ (getvar 'osmode) (if (< (getvar 'osmode) 16384) 16384 -16384)))

 

Similarly, with AUTOSNAP, your difference in values is 24, which is toggling Polar Tracking [8] and Object Snap Tracking [16] on and off.  You can perform that toggle in the same way:

 

(setvar 'autosnap (boole 6 24 (getvar 'autosnap)))

 

That will work even if you have some other combination of possibilities for the value, such as if you've turned off Tool Tips [2].  Interestingly, if you should have one of the two [Polar Tracking or Object Snap Tracking] on and the other off, rather than your standard of both either on or off together, it will reverse those -- turn off the one that's on and turn on the one that's off.

Kent Cooper, AIA
Message 5 of 7

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

.... 

(setvar "osmode" (- (getvar "osmode"))) or something like that.  ....


Not that way.  OSMODE can't have a negative value -- it's not like the color number of a Layer in the result from (tblsearch), that's positive if the Layer's on and negative if it's off.

Kent Cooper, AIA
0 Likes
Message 6 of 7

Anonymous
Not applicable

Wow. Incredibly informative and helpful. Thanks, guys!

0 Likes
Message 7 of 7

dbroad
Mentor
Mentor

Oops, thanks Kent.  Should have been adding/subtracting from 16384 instead of negating.  The diesel form of and is bitwise.

 

BTW, this is how the F3 is implemented in the CUI.  It uses Diesel.  This approach might be more suitable for mouse buttons.

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

 

Architect, Registered NC, VA, SC, & GA.
0 Likes