Author Topic: Lock a variable  (Read 2655 times)

0 Members and 1 Guest are viewing this topic.

Zykl0

  • Guest
Lock a variable
« on: July 06, 2015, 02:53:39 PM »
Hello Guys,

We are working with a software at job that like to play with our variable but forget to restore it (Autosnap, Osmode etc..) Is there a way to freeze or lock a variable in order to prevent any change to it? I have AutoCAD 2016, I tried to work with SYSVARMONITOR but this don't do the job I need.

Thanks

tombu

  • Bull Frog
  • Posts: 289
  • ByLayer=>Not0
Re: Lock a variable
« Reply #1 on: July 06, 2015, 02:59:54 PM »
Is it editable?  Have you tried complaining to get them to fix it?  You can add a reactor to reset the system variable, but it may cause problems with their poorly written code.
Tom Beauford P.S.M.
Leon County FL Public Works - Windows 7 64 bit AutoCAD Civil 3D

ChrisCarlson

  • Guest
Re: Lock a variable
« Reply #2 on: July 06, 2015, 03:07:21 PM »
"locking" in a traditional sense, no.

Code - Auto/Visual Lisp: [Select]
  1.  ;Reactor by: Peter Jamtgaard
  2. (defun c:savetimeforce (/ ReacChange)
  3.         (setq
  4.                 ReacChange (vlr-editor-reactor nil '((:VLR-sysVarChanged . ReacAlert)))
  5.         )      
  6. )
  7. (defun ReacAlert (CALL CALLBACK / Savetime)
  8.         (if (and
  9.                 (= (strcase (car CALLBACK)) (setq Savetime "SAVETIME"))
  10.                 (not (eq (getvar Savetime) 10))
  11.                 )
  12.         (progn
  13.                 (setvar "SAVETIME" 10)
  14.         )
  15.         (princ)
  16.         )
  17. )
  18.  
  19. (setvar "SAVETIME" 10)
  20. (c:savetimeforce)
  21.  

Zykl0

  • Guest
Re: Lock a variable
« Reply #3 on: July 06, 2015, 03:16:07 PM »
Is it editable?  Have you tried complaining to get them to fix it?  You can add a reactor to reset the system variable, but it may cause problems with their poorly written code.

Sadly not this is intented on their side but I don't like this practice, everything is hardcoded so no way for me to modify it.

"locking" in a traditional sense, no.

Code - Auto/Visual Lisp: [Select]
  1.  ;Reactor by: Peter Jamtgaard
  2. (defun c:savetimeforce (/ ReacChange)
  3.         (setq
  4.                 ReacChange (vlr-editor-reactor nil '((:VLR-sysVarChanged . ReacAlert)))
  5.         )      
  6. )
  7. (defun ReacAlert (CALL CALLBACK / Savetime)
  8.         (if (and
  9.                 (= (strcase (car CALLBACK)) (setq Savetime "SAVETIME"))
  10.                 (not (eq (getvar Savetime) 10))
  11.                 )
  12.         (progn
  13.                 (setvar "SAVETIME" 10)
  14.         )
  15.         (princ)
  16.         )
  17. )
  18.  
  19. (setvar "SAVETIME" 10)
  20. (c:savetimeforce)
  21.  

Thanks ChrisCarlston But my skills are not high enough to understand your code.
On the other hand is there a way to execute a command, let's say (setvar "autosnap" 63) and
(setvar "osmode" 255) everytime a PAN occur in the Model?

kpblc

  • Bull Frog
  • Posts: 396
Re: Lock a variable
« Reply #4 on: July 06, 2015, 04:03:05 PM »
You can write command reactor and change system variables inside it. Something like this:
Code: [Select]
(if *vlr-cmd*
  (progn
    (setq *vlr-cmd* nil)
    (vlr-remove-all :vlr-command-reactor)
    ) ;_ end of progn
  ) ;_ end of if

(if (not *vlr-cmd*)
  (setq *vlr-cmd*
         (vlr-command-reactor
           "kpblc-command-reactor"
           '((:vlr-commandwillstart . _kpblc-vlr-command-start)
             (:vlr-commandended . _kpblc-vlr-command-end)
             (:vlr-commandcancelled . _kpblc-vlr-command-cancel)
             (:vlr-commandfailed . _kpblc-vlr-command-fail)
             )
           ) ;_ end of VLR-Command-Reactor
        ) ;_ end of setq
  ) ;_ end of if

(defun _kpblc-vlr-command-start (react cmd)
  (setq cmd (strcase (car cmd) t))
  (cond
    ((= cmd "pan")
     (setq *sysvar-list* (_kpblc-sysvar-save '(("autosnap" . 63) ("osmode" . 255))))
     )
    ) ;_ end of cond
  ) ;_ end of defun

(defun _kpblc-vlr-command-end (react cmd)
  (setq cmd (strcase (car cmd) t))
  (cond
    ((= cmd "pan") (_kpblc-sysvar-restore *sysvar-list*))
    ) ;_ end of cond
  ) ;_ end of defun

(defun _kpblc-vlr-command-cancel (react cmd)
  (setq cmd (strcase (car cmd) t))
  (cond
    ((= cmd "pan") (_kpblc-sysvar-restore *sysvar-list*))
    ) ;_ end of cond
  ) ;_ end of defun

(defun _kpblc-vlr-command-fail (react cmd)
  (setq cmd (strcase (car cmd) t))
  (cond
    ((= cmd "pan") (_kpblc-sysvar-restore *sysvar-list*))
    ) ;_ end of cond
  ) ;_ end of defun

(defun _kpblc-sysvar-save (lst)
  (mapcar
    (function
      (lambda (x / tmp)
        (if (setq tmp (getvar (car x)))
          (progn
            (setvar (car x) (cdr x))
            (cons (car x) tmp)
            ) ;_ end of progn
          ) ;_ end of if
        ) ;_ end of LAMBDA
      ) ;_ end of function
    lst
    ) ;_ end of mapcar
  ) ;_ end of defun

(defun _kpblc-sysvar-restore (lst)
  (foreach item lst
    (setvar (car item) (cdr item))
    ) ;_ end of foreach
  ) ;_ end of defun

Codes of functions called by cancel command, end command or fail command are the same to educatuional purposes :)

But this code will works only at calling pan command. It can no works by pressing middle mouse button.
« Last Edit: July 06, 2015, 04:12:22 PM by kpblc »
Sorry for my English.

77077

  • Guest
Re: Lock a variable
« Reply #5 on: July 06, 2015, 08:18:18 PM »
I know how to lock a function ,But I don't know how to lock a sys variable

you can use reactor like this

Code: [Select]
(or *osvlr*
    (setq *osvlr*
           (vlr-command-reactor nil '((:vlr-commandEnded . oschange)))
    )
)
(defun oschange        (obj lst)
  (if (or
        (WCMATCH (strcase (car lst)) "*OSMODE")
        (WCMATCH (strcase (car lst)) "*DSETTINGS")
      )
    (progn
      (if *osmode*
        (progn
          (if (/= *osmode* (getvar 'osmode))
            (progn
              (princ "\nOSMODE has changed")
              (setq *osmode* (getvar 'osmode))
            )
          )
        )
        (setq *osmode* (getvar 'osmode))
      )
    )
  )
  (princ)
)