Author Topic: Force osmode  (Read 3208 times)

0 Members and 1 Guest are viewing this topic.

Zykl0

  • Guest
Force osmode
« on: March 14, 2016, 11:56:34 AM »
Hello all,

We are working with a software for AutoCAD that always mess our osnap.
So I found this piece of code that I use for a while, and I can't remember where I found it.
How ever I think the syntax is not good because It force the osmode all the time and not only when we exit a lisp or a command.

Code: [Select]
(defun resetosmode (v1 v2 /)
(if (/= (getvar "osmode") 255)
(setvar "osmode" 255)
)
(princ)
)
(vlr-editor-reactor
nil
'((:vlr-lispEnded . ResetOsmode)
(:vlr-lispCancelled . ResetOsmode)
(:vlr-commandEnded . ResetOsmode)
(:vlr-commandCancelled . ResetOsmode)
(:vlr-commandFailed . ResetOsmode)
)
)

I don't really care because I can deal with osmode : 255 at all time but I have a user who doesn't work like me and he would like the osmode to be forced only when exiting a lisp or a command (Cancel included) and let the lisp routine work with the osmode until he leave the command.

Thank you,

ChrisCarlson

  • Guest
Re: Force osmode
« Reply #1 on: March 14, 2016, 12:02:45 PM »
Might be better suited to address the faulty program from modifying your o-snap settings without returning them.

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: Force osmode
« Reply #2 on: March 14, 2016, 12:28:10 PM »
Turning off osnaps is old school methods (very old!).

I think it was AutoCAD 2000 that allowed you to toggle them on an off via Autolisp.
Code - Auto/Visual Lisp: [Select]
  1. (defun toggleOsnap (value)
  2.   ;; toggleOsnaps
  3.   ;;
  4.   ;; This function accepts a integer of 1 or 0 (on or off) to set the
  5.   ;; end users osnap value to enabled or disabled.
  6.   ;;
  7.   ;; Argument: 1 = Enable the users osnaps
  8.   ;;           0 = Disable the users osnaps
  9.   ;;
  10.   ;; Usage: (toggleOsnap 1) or (toggleOsnap 0)
  11.   ;;
  12.   ;; Inspired by SMadsen
  13.   (setvar "OSMODE" (boole (- 7 value) (getvar "OSMODE") 16384))
  14.   (princ)
  15. )

example usage:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:myline
  2.   ;; Delair variables
  3.   ( / toggleOsnap)
  4.  
  5.   ;;
  6.   ;; Define support procedures.
  7.      (defun toggleOsnap (value)
  8.        ;; toggleOsnaps
  9.        ;;
  10.        ;; This function accepts a integer of 1 or 0 (on or off) to set the
  11.        ;; end users osnap value to enabled or disabled.
  12.        ;;
  13.        ;; Argument: 1 = Enable the users osnaps
  14.        ;;           0 = Disable the users osnaps
  15.        ;;
  16.        ;; Usage: (toggleOsnap 1) or (toggleOsnap 0)
  17.        ;;
  18.        ;; Inspired by SMadsen
  19.        (setvar "OSMODE" (boole (- 7 value) (getvar "OSMODE") 16384))
  20.        (princ)
  21.        )
  22.   ;; End support procedures.
  23.  
  24.   (toggleOsnap 0)
  25.   ;; Turn the osnaps off.
  26.  
  27.   ;; Lets draw aline...
  28.   (command "_line")
  29.   ;; Now that the line command has begun,
  30.   ;; be quite and wait till the user finishes.
  31.   (while (eq (getvar 'cmdactive) 1)
  32.          (command PAUSE))
  33.   ;; now that we are done with the line command,
  34.   ;; let's turn the osnaps back on.
  35.   (toggleOsnap 1)
  36. )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Zykl0

  • Guest
Re: Force osmode
« Reply #3 on: March 14, 2016, 12:29:22 PM »
Might be better suited to address the faulty program from modifying your o-snap settings without returning them.

Sadly this behavior is "intended" from according to their support and we have to deal with it.

Zykl0

  • Guest
Re: Force osmode
« Reply #4 on: March 14, 2016, 12:33:09 PM »
Turning off osnaps is old school methods (very old!).

I think it was AutoCAD 2000 that allowed you to toggle them on an off via Autolisp.
Code - Auto/Visual Lisp: [Select]
  1. (defun toggleOsnap (value)
  2.   ;; toggleOsnaps
  3.   ;;
  4.   ;; This function accepts a integer of 1 or 0 (on or off) to set the
  5.   ;; end users osnap value to enabled or disabled.
  6.   ;;
  7.   ;; Argument: 1 = Enable the users osnaps
  8.   ;;           0 = Disable the users osnaps
  9.   ;;
  10.   ;; Usage: (toggleOsnap 1) or (toggleOsnap 0)
  11.   ;;
  12.   ;; Inspired by SMadsen
  13.   (setvar "OSMODE" (boole (- 7 value) (getvar "OSMODE") 16384))
  14.   (princ)
  15. )

example usage:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:myline
  2.   ;; Delair variables
  3.   ( / toggleOsnap)
  4.  
  5.   ;;
  6.   ;; Define support procedures.
  7.      (defun toggleOsnap (value)
  8.        ;; toggleOsnaps
  9.        ;;
  10.        ;; This function accepts a integer of 1 or 0 (on or off) to set the
  11.        ;; end users osnap value to enabled or disabled.
  12.        ;;
  13.        ;; Argument: 1 = Enable the users osnaps
  14.        ;;           0 = Disable the users osnaps
  15.        ;;
  16.        ;; Usage: (toggleOsnap 1) or (toggleOsnap 0)
  17.        ;;
  18.        ;; Inspired by SMadsen
  19.        (setvar "OSMODE" (boole (- 7 value) (getvar "OSMODE") 16384))
  20.        (princ)
  21.        )
  22.   ;; End support procedures.
  23.  
  24.   (toggleOsnap 0)
  25.   ;; Turn the osnaps off.
  26.  
  27.   ;; Lets draw aline...
  28.   (command "_line")
  29.   ;; Now that the line command has begun,
  30.   ;; be quite and wait till the user finishes.
  31.   (while (eq (getvar 'cmdactive) 1)
  32.          (command PAUSE))
  33.   ;; now that we are done with the line command,
  34.   ;; let's turn the osnaps back on.
  35.   (toggleOsnap 1)
  36. )

I can't edit their lisp files or they will cut the support. We have to work with reactor in order to reset the snap every time a lisp or a command exit or canceled.

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: Force osmode
« Reply #5 on: March 14, 2016, 12:39:06 PM »
Then create a wrapper to call their routine/procedure.

support stuff.
Code - Auto/Visual Lisp: [Select]
  1. (
  2.  ;; These procedures wrapped up in this lambda statement are
  3.  ;; part of an error trapping process.
  4.  ;;
  5.  ;; Although these procedures were assembled and typed up by me,
  6.  ;; the knowledge and understanding came from two very good sources
  7.  ;; I would like to give credit to  ElpanovEvgeniy and
  8.  ;; Vladimir Nesterovsky for their unbelievable talent.
  9.  ;;
  10.  ;; Thank you both for giving me so much knowledge.
  11.  (lambda ()
  12.  
  13.     (defun #Error ( msg )
  14.      ;; General error trap
  15.      ;;
  16.      ;; By: John Kaul
  17.      ;;     11.28.06
  18.      ;;
  19.      ;; Note: This procedure is part of the
  20.      ;;       an ERROR trap set of procedures.
  21.      ;;
  22.       (command) (command)
  23.       (if (or (= msg "Function cancelled") (= msg "quit / exit abort"))
  24.          (princ msg)
  25.          (princ (strcat "\nError: " msg )))
  26.       (if #Error_Lst# (#Error_End)) )
  27.    
  28.     (defun #Error_Start ( / )
  29.      ;; this procedure will set up the error trap
  30.      ;; and set an undo mark.
  31.      ;;
  32.      ;; By: John Kaul
  33.      ;;     11.28.06
  34.      ;;
  35.      ;; Note: This procedure is part of the
  36.      ;;       an ERROR trap set of procedures.
  37.      ;;
  38.        (if (null #Old_Error)
  39.          (setq #Old_Error *error*)
  40.          (setq *error* #Error))
  41.        (vla-StartUndoMark
  42.          (vla-Get-ActiveDocument
  43.            (vlax-Get-Acad-Object)))
  44.       (princ) )
  45.    
  46.     (defun #Error_End ( / )
  47.      ;; This procedure will set the users
  48.      ;; variables back to their orig. settings
  49.      ;; and reset the error trap var.
  50.      ;;
  51.      ;; By: John Kaul
  52.      ;;     11.28.06
  53.      ;;
  54.      ;; Note: This procedure is part of the
  55.      ;;       an ERROR trap set of procedures.
  56.      ;;
  57.       (mapcar 'eval #Error_Lst#)
  58.       (setq *error* #Old_Error)
  59.       (vla-EndUndoMark
  60.            (vlax-Get-Acad-Object)))
  61.       (setq #Error_Lst# nil)
  62.       (princ) )
  63.    
  64.     (defun #Error_Push_Vars ( lst / push->lst)
  65.      ;; This procedure will generate a list of
  66.      ;; variables and their orig. value before
  67.      ;; changing them to a desired value
  68.      ;;
  69.      ;; By: John Kaul
  70.      ;;     11.28.06
  71.      ;;
  72.      ;; Example Use: (#Error_Push_Vars
  73.      ;;                  '(("CMDECHO" 0) ("BLIPMODE" 1)
  74.      ;;                    ("SNAPANG" 45) ("OSMODE" 0)))
  75.      ;;
  76.      ;; The syntax of the list members is as follows:
  77.      ;;  ( <Variable name to chage/save>  <setting desired> )
  78.      ;;
  79.      ;; The variables orig value will be saved for later
  80.      ;; restoration.
  81.      ;;
  82.      ;;
  83.      ;; Note: This procedure is part of the
  84.      ;;       an ERROR trap set of procedures.
  85.      ;;
  86.        (defun push->lst (sym lst)
  87.          (set lst (cons sym (eval lst))) )
  88.      (mapcar
  89.       '(lambda (x)
  90.            (push->lst (list 'setvar (car x) (getvar (car x))) '#Error_Lst#)
  91.            (if (cadr x)
  92.               (setvar (car x) (cadr x)))
  93.          )
  94.         lst
  95.        )
  96.      )
  97.   )
  98.  ;;
  99.  ;; End Error Trap Procedures.
  100.  ;;
  101. )

wrapper example.
Code - Auto/Visual Lisp: [Select]
  1. ;; Example on how to create a wrapper.
  2. (defun wrapper_foo ( / )
  3.   (#Error_Start)
  4.  
  5.   (#Error_Push_Vars '(("CMDECHO" 0) ("BLIPMODE" 1) ("SNAPANG" 45) ("OSMODE" 0)))
  6.  
  7.   ;; call procedure
  8.  
  9.   (#Error_End)
  10.  )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

squirreldip

  • Newt
  • Posts: 114
Re: Force osmode
« Reply #6 on: March 14, 2016, 07:06:20 PM »
I agree with Se7en...

I also have created a simple routine for setting my 'usual' snap:

Code - Auto/Visual Lisp: [Select]
  1. (defun C:41 () (setvar "OSMODE" 41))